From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

Serial Communication

Hi:

I try to use the RS232C port to remotely to initializing and download data from a meter. I have no problem to send in command for initialize and the meter sent back the correct response in ASCII. However when I try to send in command to read the clock, the meter return back with some strange character instead of the BCD format as it should be. I wonder is there anyway I can translated ther result into proper format.

The method I use to receive the response from the meter is the same as the sample program
"serial.prj":
bytes_read = ComRdTerm (comport, read_data, read_cnt,read_term)

when I use the stepinto function to debug, the "read_data" that supposed content the data ( in BCD format) give response as "44||||49".

Q1. Can you give me some hint/example to solve the above problem?

Q2. Accorinding to the paramater for the COmRdTerm, read_data should be declare as char, How should I declare the read_data if I want to receive response in some other format e.g. Hex or integer?

Looking forward to your prompt reply. Thank You

Vicky Lai
0 Kudos
Message 1 of 2
(2,772 Views)
Hello,

The interface for writing and reading the serial port is string, or ASCII, however, you can receive arbitrary data. If you are certain that you are sending the appropriate command, and you think you are receiving the correct response and only need to interpret it, really the only consideration you have to make involves type casting. That is, you will receive the data back as a string, which is really just a sequence of bytes. You can type cast the result, or part of the result, in order to have your computer interpret the string, or part of the string, as an integer for example. I think this is the heart of both of your questions below. Here is a specific example of type casting part of a string to type unsigned integer:

Suppose you received the string "abcdef" back. Well, the characters a, b, c, d, e, and f have ASCII representations of Hex 62, 63, 64, 65, 66, and 67 respectively (each of these is represented by 8 bits, or 1 byte). Thus, the corresponsing binary representations are: 0110 0010, 0110 0011, 0110 0100, and 0110 0101 respectivley. Now, suppose we extracted the substring "bcdf"; this is 4 bytes or 32 bits. Well, we know that there is a fundamental primitive data type for 32 unsigned integers, so we should be able to type cast the string "bcde" to type unsigned 32-bit integer and produce the result which is the integer defined by the 32 bits which define the ascii characters bcde in that order. Well, the underlying bits in this case will be: 0110 0010 0110 0011 0110 0100 0110 0101, which when "pushed together" will be 01100010011000110110010001100101, which is the binary representation for the number 1650680933 (decimal).

That is an example of how you can take a string result and type cast to the data type which you (perhaps) know the instrument sent back to you.

Repost if you any other questions!

Thank you,

JLS
Best,
JLS
Sixclear
0 Kudos
Message 2 of 2
(2,747 Views)