Sending a NULL Character Using Serial or GPIB Communication

Updated Jul 19, 2024

Reported In

Software

  • LabWindows/CVI Base
  • LabWindows/CVI Full
  • LabVIEW Base
  • LabVIEW Full

Issue Details

I am trying to send a NULL character to my instrument but have been unsuccessful. Can I send a NULL character with LabVIEW, LabWindows™/CVI, Visual C, Visual Basic, and so on?

Solution

You can send and receive NULL characters using serial or GPIB communication in any programming language. All strings are terminated with a NULL character; therefore, string functions handle the NULL character as the end of the string, including an empty string.

Additional Information

The  following code will send only "hello" from the serial port, because StringLength encounters the NULL character in the string and interprets this character as the end of the string.

char send_data[15] = "hello\00world";
stringsize = StringLength (send_data);       // stringsize = 5
ComWrt (comport, send_data, stringsize);     // "hello" is sent over serial

 
If you want to NULL terminate a serial message, you do not need to add a NULL character. The NULL character is already in the string. You just have to increment the number of characters to send.
 
    char send_data[15] = "hello";
    stringsize = StringLength (send_data)++;     // stringsize = 6
    ComWrt (comport, send_data, stringsize);     // "hello/0" is sent over serial​

 
If you are in the LabWindows/CVI environment you can use NULL termination in the example named serial.prj, which is located in the samples\rs232 directory of your LabWindows/CVI installation, by replacing the procedure SendAscii and the callback ReadCallBack with the attached code. When the user selects "None" for the Terminator or Termination Byte, they will get NULL termination instead.