2014년 10월 8일 수요일

[LabVIEW] About the casting from Byte to String, and vice versa.

When your application make a communication(RS232, TCP, UDP, GPIB, and so on) to other device in LabVIEW, the type of data which is transmitted is STRING. But C and other text language use byte to transmit to LabVIEW.

User can feel confused when they integrated their system with other program which is implemented in C or C#. How does it solve this problem in LabVIEW?

To solve it, you should cast a data-type from byte to string , and vice versa. You can use Type Cast function for it in LabVIEW.

The description of Type Cast is as follow:
  • X : It is a real value to cast
  • type : It is a type of data to cast
  • Result : It is a result which is change


The below code is the example which used this function. As you can see, the first algorithm change from HEX values(1-9) to STRING values(ASCII code) and then string values change from STRING values to INTEGER values. The important point is that the result size is variable depend on the size of INTEGER.


You can check it in the result. As you can see, the value in length(i8) is 1. The means the result which is changed from I8 is 1byte. On the contrary to this, length(I32) is 4.

In other words, the Type Cast function converts each 8-bits of input data into a result. 

This function can generate unexpected data if x and type are not the same size. If x requires more bits of storage than type, this function uses the upper bytes of x and discards the remaining lower bytes. If x is of a smaller data type than type, this function moves the data in x to the upper bytes of type and fills the remaining bytes with zeros. For example, an 8-bit unsigned integer with value 1 type cast to a 16-bit unsigned integer results in a value of 256.


The cast from byte to string in LabVIEW makes many people confused in real world situation. This article explained how to solve this problem. Thanks.

댓글 없음:

댓글 쓰기