LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to send a Hex format command to RS232 port using LabWindows CVI?

I want to send Hex format command to my device through RS232 serial interface, I have experience sending command in ASCII format but not in HEX format. Please see one command example below:
 
0x02 0x22 0x08 0x01 0x20 0x64 0xD4 0x03
 
This command is asking the device version number, how can I send this HEX format to RS232 port using LabWindows CVi.
 
Please give me some advice.Smiley Sad
0 Kudos
Message 1 of 6
(7,685 Views)

Do you need to format a string exactly as you wrote, including the '0x' prefix? Then you can use sprintf (msg, "%#X\r", value); and output the string with ComWrt (comPort, msg, strlen (msg));

If '0x' prefix is not necessary, simply omit the '#' flag.

I added final '\r' as a termination character: it may be unnecessary or dangerous depending on your device communication protocol.

If you use 'x' operator you will have literal part of the string written in lowercase (e.g. 0x12ab), if you use 'X' it will be written in uppercase (0X12AB).

 

Message Edited by Roberto Bozzolo on 10-18-2005 04:44 PM



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 6
(7,686 Views)
Hi,

This other threads might also me helpful for you:
http://forums.ni.com/ni/board/message?board.id=140&message.id=12855&requireLogin=False
http://forums.ni.com/ni/board/message?board.id=180&message.id=15304&requireLogin=False

Good Luck,

Tica T
Applications Engineering
National Instruments
Message 3 of 6
(7,631 Views)

Some other ways to send binary data are:

Send it byte by byte

 ComWrtByte(1, 0x02);

 ComWrtByte(1, 0x22);

 ComWrtByte(1, 0x08);

or load a buffer and send the buffer, you can load the array up any way you like,

  unsigned char dataBuffer[10]={0x02, 0x22, 0x08, 0x01, 0x20, 0x64, 0xD4, 0x03};

  ComWrt(1, dataBuffer, 8);

 

or this is another way, the data is sent as the octal representation directly from inside a string. 

ComWrt(1, "\002\042\010\001\040\144\324\003", 8);

that is \042 = 0x22.  The calculator in windows can do hex to octal conversion for you if you ever need to do it this way.

 

Reading the data back is also strait forward:

ComRdByte() reads a single byte.

 

ComRd() reads as many bytes as you ask for

I would assume that the instrument sends back something like 0x02, 0x22, then maybe the number of bytes it will send.

You can use ComRdByte() to get all the bytes up to and including the length, then you know how many more bytes to get with ComRd() into an array of type char or unsigned char.   You can do with the results what you wish.

 

Good Luck

 

 

 

Message Edited by mvr on 10-19-2005 03:24 PM

0 Kudos
Message 4 of 6
(7,626 Views)

How to send a Hex format command to TCP port using LabWindows CVI?

i need to send this command : A5 00 00 00 50 5E 00 00 11 D0

i sent   unsigned char transmitBuf[11]={0xA5, 0x00, 0x00, 0x00, 0x50, 0x5E, 0x00, 0x00, 0x11, 0xD0};  

, just send A5 to the server, WHY ?

 

 

0 Kudos
Message 5 of 6
(5,934 Views)

Which functions did you use to send the string? If you happen to use any text-related command (strcpy, strcat, strlen...) they will terminate on the first NUL byte found. That's the most probable cause for your program to send only the first part of the string.

 

 

Having said this, I urge you to open a new discussion thread instead of reviving a 12-years old thread that only partially covers your problem.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 6 of 6
(5,921 Views)