How LabVIEW implements high-performance serial port assistant - with code

LabVIEW is basically developed under the Windows system. Under Windows, a lot of system interfaces are provided, which can be called during application development to realize various functions. In LabVIEW, these system interfaces can also be packaged to form controls in LabVIEW, which can be called during LabVIEW program development. As mentioned in the previous article, in the field of embedded development, serial port tools on the PC side are very important, but many serial port tools often experience stuck problems due to performance reasons when the amount of data is large. In addition, the inherent serial port tool cannot be modified to realize more application functions after serial port data collection. The previous article uses LabVIEW VISA API to quickly develop a serial port assistant, but the serial port data receiving efficiency is relatively low, so this article will introduce in detail how to use LabVIEW to quickly develop a higher-performance serial port program.

1 How to realize high-performance serial port transceiver

We know that the boards of embedded systems such as single-chip microcomputers, ARMs, and FPGAs basically have serial ports, because the protocol is simple and mature, and there are many ready-made things that can be used for reference. To obtain the information or data from the embedded system through the serial port on the PC, LabVIEW generally has several options under Windows:

  1. Use the serial port operation interface function provided by the development environment itself;
  2. Use the ActiveX control MSComm;
  3. Directly call Windows API to realize serial port operation.

It is relatively more difficult to directly call Windows API to realize serial port operation. It is necessary to be familiar with the underlying API of Windows, and the encapsulation of these APIs has been realized by the MSComm control. In addition, the VISA interface of LabVIEW was introduced earlier to operate the serial port, and the sending and receiving efficiency is relatively low, so the MSComm control is selected to realize the high-performance serial port program.

2. What is MSComm

Each MSComm control corresponds to a serial port, and multiple MSComm controls must be used to access multiple serial ports.

MSComm is an ActiveX control, which can control the sending and receiving of serial port data on the PC, and supports query mode and interrupt mode (called event-driven mode under Windows).

  • Interrupt mode: When the serial port has data arriving or data is written into the serial port buffer, an interrupt will be triggered, and OnComm can be used to capture the event and process it. This method responds in a timely manner and is more efficient than the polling method.
  • 查询方式:实际上仍热是事件驱动的,需要检查CommEvent属性值来查询事件。

2.1 MSComm的常用属性

  • CommPort:设置并返回通讯端口号
  • Settings:以字符串形式设置并返回波特率、奇偶校验、数据位、停止位
  • PortOpen:设置并返回通讯端口的状态。也可以打开和关闭端口
  • Input:从接收缓冲区返回和删除字符
  • InputLen:设置并返回input属性从接收缓冲区读取的字符数
  • Output:向传输缓冲区写一个字符串
  • RThreshold:在设置CommEvent属性为comEvReceive并产生OnComm之前,设置并返回要接收的字符数
  • CTSHolding:确定是否可通过查询CTS线的状态发送数据
  • SThreshold:设置CommEvent属性为comEvSend并产生OnComm事件之前,设置并返回传输缓冲区中允许的最小字符数
  • CDHolding:通过查询CD线的状态确定当前是否有传输
  • DSRHolding:确定DSR线的状态
  • EOFEnable:确定在输入过程中MSComm控件是否寻找文件结尾(EOF)

2.2 MSComm控件的事件

MSCOMM控件只使用一个事件OnComm,用属性CommEvent的十七个值来区分不同的触发时机。主要有以下几个:

  • CommEvent=1时:传输缓冲区中的字符个数已少于Sthreshold(可设置的属性值)个
  • CommEvent=2时:接收缓冲区中收到Rthreshold(可设置的属性值)个字符,利用此事件可编写接收数据的过程
  • CommEvent=3时:CTS线发生变化
  • CommEvent=4时:DSR线发生变化
  • CommEvent=5时:CD线发生变化
  • CommEvent=6时:检测到振铃信号

另外十种情况是通信错误时产生,即错误代码。关于MSComm的更详细的资料可参考Microsoft的msdn网站。

三 MSComm控件的注册

When using the MSComm control in LabVIEW, you need to import --ActiveX Control to the palette to see the MSComm control. However, if there is a problem with the registration of the MSComm control of Windows PC, it cannot be seen through importing, so you need to download and register the MSComm control first.

First put the downloaded mscomm32.ocx file under C:\windows\system32, and then enter Regsvr32 C:\windows\system32 during operation, and a prompt box of successful registration will pop up:

Then enter the Regedit command to open the registry during operation, find HKEY_CLASSES_ROOT\Licenses, add the primary key in it, right-click and select "New"--"Item", and then name the item 4250E830-6AC2-11cf-8ADB-00AA00C00905, on the right Right-click "Default" and select "Modify", and then enter the value kjljvjjjoquqmjjjvpqqkqmqykypoqjquoun. Registers the mscomm32.ocx control to prevent "Unauthorized Use of Class" errors when imported in LabVIEW.
After the registration is complete, restart the computer, and then you can see the MSComm control (the third one in the picture):

Four LabVIEW uses MSComm control to implement serial port program

As mentioned above, the general application can be realized directly by using the VISA serial port API function in LabVIEW, which is convenient and fast, but for those who need to use the interrupt method for serial port communication, the MSComm control must be used. To operate the ActiveX control, the most important thing is to make good use of its properties, method functions, and do a good job in callback processing of interrupt events. The control set by LabVIEW for ActiveX provides 8 functions in the "ActiveX" of "Interconnection Interface": Automation Open, Close Reference, To Variant, Variant to Data, Invoke Node, Property Node, Register Event Callback, UnRegister Event Callback .

4.1 Using MSComm control in LabVIEW

First, you need to add MSComm in LabVIEW, add MSComm to the LabVIEW environment in "Tools" - "Import" - "ActiveX to Palette", and select the default path. Then you can find mscomm in ".Net and ActiveX" of the front panel and drag it to the front panel. You can also place the ActiveX container on the front panel first, and then right-click to select the mscomm control:

Call the ActiveX control to create the client application: mainly use the 8 functions in the "interconnection interface" -- "ActiveX":

ActiveX event registration related: that is, use event callbacks to handle interrupt events, etc.,

4.2 Programming Notes

For the MSComm control, the programming uses its automation reference, that is, LabVIEW treats the ActiveX control as an object. Connect the property node to the control, and you can view all properties. When connecting the call node, MSComm does not provide a method, so it will display that there is no method. The following are the properties of MSComm seen in LabVIEW:

In addition, the callback event registration must first determine what kind of data needs to be transmitted. If it contains multiple data types, it can be solved by using a cluster. The callback function will automatically create an event parameter of the same data type and provide a reference to the ActiveX control .

Activities at the end of five articles

For the source code of LabVIEW's serial port implementation, interested friends can reply "LABV_UART" at vx gzh (Technology Shapes the Future) to get it. At the same time, the implementation of the LabVIEW VISA serial port assistant mentioned above is also placed under this LabVIEW project.

Guess you like

Origin blog.csdn.net/suxiang198/article/details/128277414