How to Create String Message in LabVIEW for TCP Communication

Updated Dec 18, 2023

Environment

Software

  • LabVIEW Full

How can I convert numeric data to string data for TCP communication?

In TCP communication of LabVIEW, all TCP messages are sent and received as string data (ASCII type). You can find a basic example for TCP communication here, ‘LabVIEW Help>>Find Examples..>> Search Simple TCP.lvproj. There are 2 ways of creating string messages for TCP, using Type Cast and Number to Decimal string.
  1. Using Type Cast: Type cast function casts input data to the data type, which should be string type in TCP communication. Data is parsed from binary to ASCII units(8 bits) without changing the original data. Follow the below examples.

If you used type cast the 32-bit integer with a string, integer would change to 4 numbers of 8-bit strings(ASCII)
 

  1. Using Number to Decimal string: These functions are located in the string tab in block diagram function, which converts number type to various string types. The types are decimal, Hexadecimal, Octal and so on which you can choose. Follow the below examples.

If you convert the 32-bit integer with a Number to Decimal String VI, integer would change to a single string of 8bit corresponding to 3 of ASCII code.
Additionally, When you send or receive string via TCP API, you should follow the sequence of each case.
  1. If you used type cast before sending TCP Write VI, you must also read them as type cast after TCP Read VI.
  1. If you used Number to Decimal string before sending TCP Write VI, you must also read them as Number to Decimal string after TCP Read VI.