LabVIEW serial port asynchronous query operation

Source: Internet
Author: User

LabVIEW serial port asynchronous query operation

 

Send the command visa write:When sending a command, it opens the serial port according to the initialization format, such as the baud rate, parity, and terminator. Return variable return count size only refers to the length of write buffer, write buffer is "#01", then return count returns 3.

 

Read buffer visa read:First, enter the number of bytes to read, and return count is the actual number of bytes to read. After reading the data, the number of bytes in the serial buffer will decrease return count accordingly. For example, the buffer currently contains 22 bytes,

If the input byte count is equal to 22, all 22 bytes are read, and return count returns 22;

If the input byte count is equal to 20, the first 20 bytes of the buffer will be read, and return count returns 20. At this time, there are 2 bytes in the buffer, then, if visa read calls the serial port attribute number of bytes at serial port, the value is 2. In other cases, the value is 0. However, if you call the visa advanced node visa flush Io buffer after visa read, clear the buffer, and then call the attribute number of bytes at serial port, the value will also return 0;

If the input byte count is greater than 22, all 22 bytes are read, and return count returns 22.

 

The above features of the serial port write and read operations determine some issues that should be considered when writing a serial port program:

Generally, a serial communication program consists of data collection and signal control. Data collection needs to be collected cyclically, while signal control only issues control commands when necessary. After the control command is issued, you generally need to determine whether the device has responded to the command, which requires reading the input buffer. However, when sending the control command, the data collection command may also be sending, because the data collection is continuously collected cyclically, so the input buffer may be the collected data at this time, it may also be the response packet of the control command. The size and format of the collected data and the response packet of the control command are generally different. For example, if the collected data is> + 0022.3, the control command response message is! 01.

If the program reads data first, then the byte count is equal to 8, and then the buffer size is 0, and then the read response is blank, the program will think that the device is not properly responding to the control, this operation will be abandoned and the control command will be ressent;

If the program reads the response first, the byte count is equal to 3. If the corresponding buffer zone is> + 0022.3, the read format is inconsistent with the response message, the program should judge that the device did not respond to the control command normally and should discard this operation. After the response is read, the buffer is changed to 022.3. At this time, the collected data is read-only to 5 bytes, which is inconsistent with the data format. The program should discard the data and resend the data collection command.

This will cause confusion in collection and control when sending control commands. Messages may be lost, and the program needs to send commands multiple times to reduce the running efficiency.

Solution:Set a collection interrupt bit. before sending the signal control command, collect the interrupt position and then send the control command. After the control command returns normally, the collection interrupt bit is reset.

 

Example program:

Data collection:


Signal Control:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.