06-17-2020 02:59 PM
Hello, I've got a problem with the "Number to Hexadecimal string" function in LV 2019. I'm controlling a stepper motor by sending it a hex string. The last two characters of the string are twice the PWM command in hex. For example, if I want to send the motor a 50% PWM command, the last two digits of the hex string should be "64" ( dec2hex(2*50)=64 ). As you can see from the screenshot, "64" is what's travelling through the wire after the number to hex function. However, when I concatenate the strings to form the entire string to send to the motor, LV thinks "64" is ASCII so it converts the 6 and the 4 to hex, which are 36 and 34, respectively (as you can see from the front diagram indicator). I've tried a lot of things to fix this, (ASCII to hex converter, scan value, format value, etc) but no luck. It works fine when I replace all that with just a hex string constant where I type in "64", so I am confused why LabVIEW is concatenating it as ASCII normally. Does anyone know what's going on here?
Solved! Go to Solution.
06-17-2020 03:20 PM
Of course. Hex strings (or any string for that matter) are always ASCII (or Unicode).
Do you need the last byte of the string to have a character whose value is 64hex?
If so, convert the double to a U8, then cast it to a string (see attachment).
06-17-2020 03:25 PM
So I'm confused, do you need to send the value "64" as a string to your device, or do you need to send it as hex to your device? Right now, you're sending it as a text string. Whether you consider it to be the string "64" or the hex values 0x36 0x34, the actual information going to the device is the same. Note that the display mode of indicators and constants doesn't change the underlying data, just the display.
If you want to send 0x64 (which is "d" according to http://www.asciitable.com/), just use Type Cast on a U8 (you can't use a Double here, so convert it to a U8 before sending).
06-17-2020 03:40 PM - edited 06-17-2020 03:40 PM
Thank you Paul and Bert. I need to send "64" as a string, so that's an excellent idea that works.
06-17-2020 05:06 PM
@Akaf24 wrote:
Thank you Paul and Bert. I need to send "64" as a string, so that's an excellent idea that works.
If you really needed to send "64" as a string, then your original code would have worked.
By converting 64 to U8 and casting it to a string, you are sending 64 as binary.