Academic Company Events Community Support Solutions Products & Services Contact NI MyNI
6 ratings:
 3.66 out of 5     Rate this Document

How Do I Send Hexadecimal Data via Serial RS-232 Using LabWindows/CVI?

Primary Software: LabWindows/CVI Development Systems>>Full Development System
Primary Software Version: 8.0
Primary Software Fixed Version: N/A
Secondary Software: LabWindows/CVI Development Systems>>Base Package

Problem:
I want to send data in hexadecimal format to my device through an RS-232 serial interface in CVI, but the ComWrt command is sending the ASCII code rather than the raw hex data. How do I send it this information in hex?

Solution:
There are multiple solutions to this problem.

  • The simplest solution is to use the ComWrtByte function to send hex data one byte at a time. For instance, you can use the following commands to send the data:
    ComWrtByte(1, 0x02);
    ComWrtByte(1, 0x22);
    ComWrtByte(1, 0x08);
    The ComWrtByte function will accept integers as its data parameter as well.
  • Another solution is to use the ComWrt function and send all of the data using one call by creating a buffer (an array can also be used):
    unsigned char dataBuffer[10]={0x02, 0x22, 0x08};
    ComWrt(1, dataBuffer, 3);

    The data can also be sent in octal format directly from a string:
     
    ComWrt(1, "\002\042\010", 3);

    Where \042 = 0x22. The calc program in windows can do hex to octal conversion, which may be helpful if you decide to use this solution.

  • One final solution to this problem is to collect the data in hex as a string, convert it to an integer using the Scan function, and then send it with the ComWrtByte function:
    char send_byte[100];
    int send_byte_converted_to_int;
    Scan(send_byte,"%s>%x",&send_byte_converted_to_int);
    ComWrtByte (comport, send_byte_converted_to_int);


  • Related Links:
    Products and Services: NI LabWindows™/CVI
    Developer Zone Example: Instrument Control Made Easier with NI LabWindows™/CVI Plug and Play Instrument Drivers

    Attachments:





    Report Date: 12/02/2005
    Last Updated: 11/14/2012
    Document ID: 3S1F23Q4

    Your Feedback! poor Poor  |  Excellent excellent   Yes No
     Document Quality? 
     Answered Your Question? 
      1 2 3 4 5
    Please Contact NI for all product and support inquiries.submit