LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Write to Serial Port without splitting string data

Thanks a lot to all the replies! It helps.

One more question though, I can change the property of a string constant or a string input into hex display and send one byte hex string. But I have a number feedback that I want to use to send to the serial port. So I go from double precision to "number to hex string conversion". In normal display, it is the string that I want, but in hex display (added a string indicator to check), it converts according to the ascii table, which are the strings sent to the serial port, such as: 64 (DBL, number from feedback) => 40 (Hex in normal display) => "3430" (actually sent to the serial port). Any suggestions?

0 Kudos
Message 11 of 14
(1,142 Views)
I don't think you are using the number to hex to string conversion functions right.  Please post what you have and what the result should look like.  34 30 are tehe ASCII values for the characters 4 and 0.  Not the value 40 in hex, or the value 64 in decimal.
0 Kudos
Message 12 of 14
(1,124 Views)

There seems to be a confusion of terms here, so how about a bit of clarification? (Hopefully)


To begin with I'll refer to U8 unsigned integers, then expand to other types later.

Assume that you have the decimal number 42 stored in a U8 integer, but you want to send that as a string in ONE byte, You would CAST (not convert) the U8 to a string. If you look at the string by a string indicator after the cast, it will be the one character string "*". If you set the string indicator (or a similar constant) to display hex codes, it will show ON THE SCREN as 2A. This is an important point so I'll emphasise it. Changing the property of a string constant to display hex codes DOES NOT CHANGE THE CONTENTS OF THE STRING. All it does it to change the way it looks on the screen. It may appear now to be two characters long, but it isn't. It is still a string one character long. Try wiring a "string length" to the constant if you don't believe me!

If you connect the string to a VISA output vi, it will output ONE CHARACTER. That character will be the ASCII "*" character. In terms of the bit pattern that goes to the serial line it is 0010 1010 (I've left a space in the visual representation so that you can see how it splits into the hexadecimal 2A, and ignored parity). But remember that these representations are just for human convenience. The string is still one byte in length.

If you do a number to hex string CONVERSION (ie NOT a cast) you end up with a string that is two characters (bytes) long, "2" followed by "A". (Assuming that the length input is unwired) If you display THIS string as hex display, you get the hex display for "2", followed by the hex dislplay for "A" (3241), but the string remains the two characters (bytes) long string "2A".

The same number, but stored in a 32 bit integer would cast to a 4 byte string. This wouldn't display properly as a normal string as it is three nuls, followed by a "*". If you put the string as hex display it will show on screen as 0000 002A. A number to hex string conversion would again give the string "2A" if you leave the length input unwired

This same number stored as a DOUBLE, when CAST to a string is 8 character (bytes) long, being the bit representation of the 64 bit double precision number, and almost certainly NOT what is wanted. Again if CONVERTED to a a hex string you could end up with the two character string "2A"

So check what your device needs to be sent. Is it a single character wich can represent a small range of values? For simplicity, I'll restrict the range of values to be 0 to 127. If this is what you require, CAST the U8 number to a string. If the number you want to send is stored in another format, convert to U8 before casting. However if the device requires the number in ASCII hexadecimal, (so you send TWO characters to the device) do the number to hex string conversion to send the character "2" followed by the character "A"

It can be very confusing when you talk about sending hex to the device if you really need to send a binary representation of the integer.

Rod.

(Hoping that this hasn't added to the confusion)

0 Kudos
Message 13 of 14
(1,119 Views)
Rod, thank you! Problem solved. I didn't know what I was talking about...
 
I attached the screen snap of the vi.
0 Kudos
Message 14 of 14
(1,106 Views)