read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · web viewadditionally,...

25
先先 ,。 http://zone.ni.com/devzone/cda/tut/p/id/2897 Serial Instrument Control Tutorial 先先 1. Overview of Serial Bus 2. Connecting and Setting up a Serial Instrument Control System 3. How to Create a VISA Resource for the Instrument in MAX 4. Instrument Control Software Articles and Related Links Overview of Serial Bus Serial is a common device communication protocol for instrument control because most computers and many remote instruments include at least one serial port. Single applications can be used across a variety of instruments with few modifications, reducing programming and test time. For more information on the serial protocol, including information on transfer speeds and important parameters, please refer to Serial Communication Overview. Connecting and Setting up a Serial Instrument Control System

Upload: others

Post on 11-Mar-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

先是英文资料,后面有详细的中文资料。

http://zone.ni.com/devzone/cda/tut/p/id/2897

Serial Instrument Control Tutorial

目录

1. Overview of Serial Bus 2. Connecting and Setting up a Serial Instrument Control System

3. How to Create a VISA Resource for the Instrument in MAX

4. Instrument Control Software Articles and Related Links

Overview of Serial Bus

Serial is a common device communication protocol for instrument control because most computers and many remote instruments include at least one serial port. Single applications can be used across a variety of instruments with few modifications,

reducing programming and test time.

For more information on the serial protocol, including information on transfer speeds and important parameters, please refer to Serial Communication Overview.

Connecting and Setting up a Serial Instrument Control System

Page 2: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

Engineers typically use serial to transmit American Standard Code for Information

Interchange (ASCII) data. All data types are converted to strings of ASCII characters and

then transmitted bit by bit across the serial bus. For two ports to communicate, both

the instrument and the controlling computer must have the same baud rate, data bit

size, stop bits, and parity.

A basic serial instrument control system consists of four parts:

1. PC

2. Serial cable

3. Serial instrument

As mentioned, most computers include a serial port. The only hardware setup required

is connecting the serial cable to the serial port on the PC and the serial port on the

instrument. The serial instrument may include some hardware drivers or software

utilities for communication, and should include documentation on the baud rate, packet

size, stop bits, and parity bits that the instrument will use. Additionally, check the

National Instruments Instrument Driver Network to see if LabVIEW drivers already exist

for yout instrument, as well has tutorials on how to use them.

Page 3: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

To confirm that the serial port on your computer is working properly, refer to Serial

Communication Starting Point, to see how to perform a serial loop back test.

How to Create a VISA Resource for the Instrument in MAX

Page 4: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

With NI-VISA, communicating with your serial instrument is easy. To confirm that you are

able to communicate with your instrument, open Measurement & Automation Explorer

(MAX) found at Start>>Programs>>National Instruments>>Measurement &

Automation.

Page 5: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

Figure 1. MAX Main Menu

Page 6: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

Expand the “Devices and Interfaces” subdirectory below “My System,” then expand

“Serial & Parallel.” Below it you should see all the serial and parallel ports on your

computer. In this example the computer has one serial port (“COM1”) and one parallel

port (“LPT1”). Figure 2 shows the directory path to the serial port and the configuration

window when COM1 is selected.

Page 7: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

Figure 2. Serial Port Configuration Window

From this configuration window you can change the VISA Resource Name for easier

Page 8: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

identification and you can also “Open VISA Test Panel” to send and receive

communication from your serial instrument. Your instrument should have specific

commands for collecting and transmitting data, and sending those commands to the

instrument from the VISA Test Panel will confirm that you have a connection.

Using NI-VISA to Communicate with Your Serial Instrument

NI-VISA makes serial instrument programming fast and easy. VISA Open, VISA Read,

VISA Write, and VISA Close are the basic VISA functions in LabVIEW used in the example

VI in Figure 3. This example opens a VISA session to a serial device. A command is

written to the device, and the response is read back. In this example the specific

command being sent is the ID query for the device. Check with your device

manufacturer for your device command set. After all communication is complete, the

VISA session is closed.

Page 9: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

Figure 3. LabVIEW Serial Program Using NI-VISA

 

 http://digital.ni.com/public.nsf/allkb/9F439B3D68D9072F4825703000383197

Page 10: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

串口通信的基本概念

问题: 我从那里能够学到串口通信的基本知识解答: 串口通信的基本概念

1,什么是串口?2,什么是 RS-232?3,什么是 RS-422?4,什么是 RS-485?

5,什么是握手?1,什么是串口?

串口是计算机上一种非常通用设备通信的协议(不要与通用串行总线 Universal Serial Bus 或者 USB 混淆)。大多数计算机包含两个基于 RS232 的串口。串口同时也是仪器仪表设备通用的通信协议;很多 GPIB 兼容的设备也带有 RS-232 口。同时,

串口通信协议也可以用于获取远程采集设备的数据。串口通信的概念非常简单,串口按位(bit)发送和接收字节。尽管比按字节(byte)的并行通信慢,但是串口可以在使用一

根线发送数据的同时用另一根线接收数据。它很简单并且能够实现远距离通信。比如 IEEE488 定义并行通行状态时,规定设备线总常不得超过 20 米,并且任意两个设备间的长度不得超过 2

米;而对于串口而言,长度可达 1200 米。

Page 11: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

典型地,串口用于 ASCII 码字符的传输。通信使用 3 根线完成:(1)地线,(2)发送,(3)接收。由于串口通信是异步的,端口能够在一根线上发送数据同时在另一根线上接收数据。其他线用于握手,但是不是必须的。串口通信最重要的参数是波特率、数据位、停止位和奇偶校验。对于两个进行通行的端口,这些参

数必须匹配:a,波特率:这是一个衡量通信速度的参数。它表示每秒钟传送的 bit 的个数。例如 300 波特表示每秒钟发送 300 个 bit。当我们提到时钟周期时,我们就是指波特率例如如果协议需要 4800波特率,那么时钟是 4800Hz。这意味着串口通信在数据线上的采样率为 4800Hz。通常电话线的波特率为 14400,28800和 36600。波特率可以远远大于这些值,但是波特率和距离成反比。高波特率常常用于放置的很近的仪器间的通信,典型的例

子就是 GPIB 设备的通信。b,数据位:这是衡量通信中实际数据位的参数。当计算机发送一个信息包,实际的数据不会是 8 位的,标准的值是 5、7 和 8位。如何设置取决于你想传送的信息。比如,标准的 ASCII 码是0~127(7 位)。扩展的 ASCII 码是 0~255(8 位)。如果数据使用简单的文本(标准 ASCII 码),那么每个数据包使用7 位数据。每个包是指一个字节,包括开始/停止位,数据位和奇偶校验位。由于实际数据位取决于通信协议的选取,术语“包”

指任何通信的情况。

Page 12: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

c,停止位:用于表示单个包的最后一位。典型的值为 1,1.5和 2 位。由于数据是在传输线上定时的,并且每一个设备有其自己的时钟,很可能在通信中两台设备间出现了小小的不同步。因此停止位不仅仅是表示传输的结束,并且提供计算机校正时钟同步的机会。适用于停止位的位数越多,不同时钟同步的容忍程度

越大,但是数据传输率同时也越慢。d,奇偶校验位:在串口通信中一种简单的检错方式。有四种检错方式:偶、奇、高和低。当然没有校验位也是可以的。对于偶和奇校验的情况,串口会设置校验位(数据位后面的一位),用一个值确保传输的数据有偶个或者奇个逻辑高位。例如,如果数据是 011,那么对于偶校验,校验位为 0,保证逻辑高的位数是

偶数个。如果是奇校验,校验位位 1,这样就有 3 个逻辑高位。高位和低位不真正的检查数据,简单置位逻辑高或者逻辑低校验。这样使得接收设备能够知道一个位的状态,有机会判断是否有噪

声干扰了通信或者是否传输和接收数据是否不同步。2,什么是 RS-232?

RS-232(ANSI/EIA-232 标准)是 IBM-PC及其兼容机上的串行连接标准。可用于许多用途,比如连接鼠标、打印机或者Modem,同时也可以接工业仪器仪表。用于驱动和连线的改进

实际应用中 RS-232 的传输长度或者速度常常超过标准的值 。RS-232只限于 PC 串口和设备间点对点的通信。RS-232 串口

通信最远距离是 50 英尺。

Page 13: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

DB-9针连接头-------------

\ 1 2 3 4 5 / \ 6 7 8 9 /

-------从计算机连出的线的截面。

RS-232针脚的功能:数据:

TXD(pin 3):串口数据输出RXD(pin 2):串口数据输入

握手:RTS(pin 7):发送数据请求

CTS(pin 8):清除发送DSR(pin 6):数据发送就绪DCD(pin 1):数据载波检测DTR(pin 4):数据终端就绪

地线:GND(pin 5):地线

Page 14: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

其他RI(pin 9):铃声指示3,什么是 RS-422?

RS-422(EIA RS-422-A Standard)是 Apple 的Macintosh 计算机的串口连接标准。RS-422 使用差分信号,

RS-232 使用非平衡参考地的信号。差分传输使用两根线发送和接收信号,对比 RS-232,它能更好的抗噪声和有更远的传输

距离。在工业环境中更好的抗噪性和更远的传输距离是一个很大的优点。

4,什么是 RS-485?RS-485(EIA-485 标准)是 RS-422 的改进,因为它增加了设备的个数,从 10 个增加到 32 个,同时定义了在最大设备个数情况下的电气特性,以保证足够的信号电压。有了多个设备的能力,你可以使用一个单个 RS-422 口建立设备网络。出色抗噪和多设备能力,在工业应用中建立连向PC 机的分布式设备网络、其他数据收集控制器、HMI 或者其他操作时,串行连接会选择RS-485。RS-485 是 RS-422 的超集,因此所有的 RS-422设备可以被RS-485控制。RS-485 可以用超过 4000 英尺的

线进行串行通行。

Page 15: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

DB-9 引脚连接-------------

\ 1 2 3 4 5 / \ 6 7 8 9 /

-------

从计算机连出的线的截面。RS-485 和 RS-422 的引脚的功能

数据:TXD+(pin 8),TXD-(pin 9),RXD+(pin 4),RXD-(pin 5)

握手:RTS+(pin 3),RTS-(pin 7),CTS+(pin 2),CTS-(pin 6)

地线:GND (pin 1)5,什么是握手?

RS-232 通行方式允许简单连接三线:Tx、Rx 和地线。但是对于数据传输,双方必须对数据定时采用使用相同的波特率。尽管这种方法对于大多数应用已经足够,但是对于接收方过载的情况这种使用受到限制。这时需要串口的握手功能。在这一部分,我们讨论三种最常用的 RS-232 握手形式:软件握手、硬件握手和

Page 16: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

Xmodem。a,软件握手:我们讨论的第一种握手是软件握手。通常用在实际数据是控制字符的情况,类似于 GPIB 使用命令字符串的方

式。必须的线仍然是三根:Tx,Rx 和地线,因为控制字符在传输线上和普通字符没有区别,函数 SetXModem允许用户使

能或者禁止用户使用两个控制字符 XON 和 OXFF。这些字符在通信中由接收方发送,使发送方暂停。

例如:假设发送方以高波特率发送数据。在传输中,接收方发现由于 CPU忙于其他工作,输入buffer已经满了。为了暂时停止传输,接收方发送 XOFF,典型的值是十进制19,即十六进制13,直到输入buffer空了。一旦接收方准备好接收,它发送XON,典型的值是十进制17,即十六进制11,继续通信。输入buffer半满时,LabWindows 发送 XOFF。此外,如果XOFF 传输被打断,LabWindows 会在 buffer 达到 75%和90%时发送 XOFF。显然,发送方必须遵循此守则以保证传输

继续。b,硬件握手:第二种是使用硬件线握手。和 Tx 和 Rx 线一样,RTS/CTS 和 DTR/DSR 一起工作,一个作为输出,另一个作

为输入。第一组线是 RTS(Request to Send)和CTS(Clear to Send)。当接收方准备好接收数据,它置高RTS 线表示它准备好了,如果发送方也就绪,它置高 CTS,表

Page 17: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

示它即将发送数据。另一组线是 DTR(Data Terminal Ready)和 DSR(Data Set Ready)。这些现主要用于

Modem 通信。使得串口和 Modem 通信他们的状态。例如:当 Modem已经准备好接收来自 PC 的数据,它置高 DTR 线,

表示和电话线的连接已经建立。读取 DSR 线置高,PC 机开始发送数据。一个简单的规则是 DTR/DSR 用于表示系统通信就绪,

而 RTS/CTS 用于单个数据包的传输。在 LabWindows,函数 SetCTSMode 使能或者禁止使用硬件握手。如果 CTS模式使能,LabWindows 使用如下规则:

当 PC 发送数据:RS-232库必须检测CTS 线高后才能发送数据。

当 PC 接收数据:如果端口打开,且输入队列有空接收数据,库函数置高 RTS 和

DTR。如果输入队列90%满,库函数置低 RTS,但使 DTR维持高电

平。如果端口队列近乎空了,哭喊数置高 RTS,但使 DRT维持高电

平。如果端口关闭,库函数置低 RTS 和 DTR。

c,XModem 握手:最后讨论的握手叫做XModem 文件传输

Page 18: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

协议。这个协议在 Modem 通信中非常通用。尽管它通常使用在Modem 通信中,XModem 协议能够直接在其他遵循这个协

议的设备通信中使用。在 LabWindows 中,实际的XModem应用对用户隐藏了。只要 PC 和其他设备使用XModem 协议,在文件传输中就使用 LabWindows 的

XModem函数。函数是XModemConfig,XModemSend 和

XModemReceive。XModem 使用介于如下参数的协议:

start_of_data、end_of_data、neg_ack、wait_delay、start_delay、max_tries、packet_size。这些参数需要通信双方认定,标准的 XModem 有一个标准的定义:然而,可以

通过 XModemConfig函数修改,以满足具体需要。这些参数的使用方法由接收方发送的字符 neg_ack 确定。这通知发送方

其准备接收数据。它开始尝试发送,有一个超时参数start_delay;当超时的尝试超过 max_ties次数,或者收到接收方发送的 start_of_data,发送方停止尝试。如果从发送方收到 start_of_data,接收方将读取后继信息数据包。包中含有包的数目、包数目的补码作为错误校验、packet_size 字节大小的实际数据包,和进一步错误检查的求和校验值。在读取数据后,接收方会调用 wait_delay,然后想发送方发送响应。如果发送方没有收到响应,它会重新发送数据包,直到收到响应或

Page 19: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

者超过重发次数的最大值 max_tries。如果一直没有收到响应,发送方通知用户传输数据失败。

由于数据必须以 pack_size 个字节按包发送,当最后一个数据包发送时,如果数据不够放满一个数据包,后面会填充ASCII

码 NULL(0)字节。这导致接收的数据比原数据多。在XModem 情况下一定不要使用 XON/XOFF,因为

XModem 发送方发出包的数目很可能增加到 XON/OFF控制字符的值,从而导致通信故障。RS-232,RS-422 和 RS-485 串口通讯接口的快速比较

问题: RS-232,RS-422 以及RS-485 串口的基本区别是什么?

解答: 下面的表格比较了:工作方式,驱动器和接收器的总数,电缆的最大长度及最大传输速率。RS-232 是大多数计算

机通用的接口,比如 COM1 和 COM2。注意,大多数计算机的接口 COM1 以及COM2 并不是 RS-422/RS-485。然而,RS-422 是苹果 Macintosh 计算机的标准接口。RS-485 是基于 RS-422 的一种改进,在工业中

更普遍。所有 NI 的 RS-485 板卡都支持RS-422 标准。

问题: 串行通讯的基本架构是什么?解答: 串口通讯架构

每一个异步串行系统的核心都是一个 UART(通用异步接收机/发送机)。UART 不仅控制传输的数据,相应的电平,同时也控制通讯的速度。UART 能够存储足够的信息,所以保证了在电脑忙得时候,数据流也能连续传输。这对于同时处理大量任务的操作系统非常有帮助。下面附图是理想串行通讯的示意。我们大部分的串行卡都有一个输入输出的FIFO(查看目录了解详细信息)。FIFO 的数据可以通过串行驱动获取。串行驱动会自动地把FIFO 的数据传输到

Page 20: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

软件的缓存,这个缓存是可以在应用软件由用户配置的(比如,在 LabVIEW您可以使用 Serial Port Init VI来设置缓存去大小)。读接口的数据实际上包含从软件缓存读的过程。

确认硬件操作的回路测试有三种方式确认串口的操作:LabVIEW,超级终端和 LabWindows/CVI。这三种方式进行回路测试是把串口的发送和接受引脚短接。第一步描述短接引脚的过程。LabVIEW 的处理写在下面,而超级终端和 LabWindows/

CVI 的过程在本文后面的链接里。对于 LabVIEW建议使用如下步骤:a,连接一根电缆到串口。最常用的 RS-232 电缆是 9针或者 25针(DB-9 或者 DB-25)。在电缆端把2、3引脚短接。这会把计算机发送数据的线连接到接收数据的线。一旦短接,BD-9 的上一排变成【1 2=3 4 5】。

对于 RS-485 端口,电压是差分的。因此,你必须短接 TXD+和 RXD+、TXD-和 RXD-(对于 DB-9 电缆,连接pin4 到 pin8 和 pin5 到 pin9;对于模块化接口,连接 pin2 到 pin6 和 pin3 到 pin7;对于 Combicon 接头,

连接 pin1 到 pin5 和 pin2 到 pin4。)确保你的软件配置为 4 线模式。

在 LabVIEW中应用串口第一部分使用 LabVIEW系统 VI

 

一、串口 VI介绍LabVIEW的串口通讯 VI位于 Instrument I/O Platte的 Serial中,包括: 

 VI名称  VI功能 VISA Configure Serial Port  初始化 VISA resource name指定的串口通讯参数 VISA Write  将输出缓冲区中的数据发送到 VISA resource name指定的串口 VISA Read  将 VISA resource name指定的串口接收缓冲区中的数据读取指定字节数的数据到计算机内存中 VISA Serial Break  向 VISA resource name指定的串口发送一个暂停信号 VISA Bytes at Serial Port  查询 VISA resource name指定的串口接收缓冲区中的数据字节数 VISA Close  结束与 VISA resource name指定的串口资源之间的会话 VISA Set I/O Buffer Size  设置 VISA resource name指定的串口的输入输出缓冲区大小 VISA Flush I/O Buffer  清空 VISA resource name指定的串口的输入输出缓冲区

 

二、使用说明在 LabVIEW环境中使用串口与在其它开发环境中开发过程类似,基本的流程框图如下。 

 

Page 21: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

 图 1、串口操作数据流图 

首先需要调用VISA Configure Serial Port完成串口参数的设置,包括串口资源分配、波特率、数据位、停止位、校验位和流控等等。 

 

 图 2、初始化串口 

如果初始化没有问题,就可以使用这个串口进行数据收发。发送数据使用VISA Write,接收数据使用VISA Read。在接收数据之前需要使用 VISA Bytes at Serial Port查询当前串口接收缓冲区中的数据字节数,如果 VISA Read要读取的字节数大于缓冲区中的数据字节数,VISA Read操作将一直等待,直至 Timeout或者缓冲区中的数据字节数达到要求的字节数。当然也可以分批读取接收缓冲区或者只从中读取一定字节的数据。 

   

 图 3、从串口发送数据  图 4、从串口接收数据 

在某些特殊情况下,需要设置串口接收/发送缓冲区的大小,此时可以使用 VISA Set I/O Buffer Size;而使用 VISA Flush I/O Buffer则可以清空接收与发送缓冲区。在串口使用结束后,使用 VISA Close结束与VISA resource name指定的串口之间的会话。 

 

 

Page 22: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

 图 5、设置缓冲区大小 图 6、 清空缓冲区  图 7、结束会话/线程 

具体的例子可以参考:examples\instr\smplserl.llb。 

第二部分使用MSCOMM控件 

在 LabVIEW中使用MSCOMM控件,与在VC、VB中使用一样。同样可以使用中断方式进行接收。具体的例子可以发   索取。 

 图 8、使用控件的串口收发程序 

Page 23: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

 

 图 9、程序数据流图 

首先通过 LabVIEW 的工具菜单“Tools >> Advanced >> Import ActiveX Controls”将 Microsoft 的串口控件 -“Microsoft Communications Control,version 6.0”添加到 LabVIEW环境中,存放在缺省路径即可,这样在 User Controls Palette里面可以找到这个控件。 

Page 24: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

 图 10、添加 ActiveX控件菜单 

 图 11、选择添加用户控件 

Page 25: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

 图 12、用户控件将串口控件   放置在 Front Panel上,在框图程序中用控件属性对其进行编程,实现所需要的功能。注意:一个串口资源要有一个MSCOMM控件与之相对应。 

第三部分 注意事项 

一、串口通讯的波特率设置要精确,比如要求 9600的波特率,则晶振应选择 11.0593MHz或其倍数。 

二、由于通常情况下 LabVIEW串口VI接收或发送的都是字符串(Normal),所以如果需要发送或接收十六进制数值(Hex),请在发送或接收之前进行必要的转换。 

2.1、数值型数据的处理方法: 

2.1.1、1、如果这些数据是静态的,也就说在程序设计阶段要传输的数据就已经确定了,在这种情况下,首先设置 VISA Write的write

buffer的显示属性为Hex Display,然后直接输入要发送的 16进制字符串就可以了。串口设备的控制命令通常是由一个或多个 16进制字

Page 26: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

符组成的,当我们需要对其进行控制时经常会采用这种方法发送控制命令。 

2.1.2、数据是动态的;即要传输的数值型数据是动态产生和变化的,在发送之前首先要将其转换成对应的 16进制字符串,才能赋给 VISA

Write发送。将这些数据构成一个数组,用 Byte Array To String进行转换,转换的结果就是对应数组数值的字符串,可以提交 VISA Write

发送。或者使用 Type Cast也可以实现同样的功能。 

 

 

 图 13、串口数据转换界面 图 14、 串口数据转换数据流图  

 

Page 27: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

 图 15、操作界面 

 

 

图 16、单片机C程序 2.2、字符串型数据的处理方法: 

我们在处理过程当中,传输的数据可能具有一定格式或协议,比如一个完整的数据帧包括起始段、数据段、校验段等等,而每一段往往又有几个部分组成,这几个部分的类型和长度可能又不尽相同,可能是数值型的,可能是字符型的,也可能是布尔型的,单字节或者是多

Page 28: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers

字节。此时我们往往都采用统一的字符形式来处理这些数据,因此有时候我们说,字符是 LabVIEW里最方便的数据类型。 

经过打包的字符数据要经过串口发送需要进行必要的转换,否则传输将出错。比如我们打包过后需要发送的的字符串为:34 12 56 78

94(空格为了区分),如果直接将其赋给 VISA Write发送,串口上的数据将是:33 34 31 32 35 36 37 38 39 34。正确的转换程序如下。 

 

 图 17、字符型数据处理 

数据接收:从计算机串口接收到的数据是 16进制的 ASCII码,要转换成对应的数值型数据,可以使用 String To Byte Array或者 Type Cast。 

三、LabVIEW串口VI不能使用中断方式传输。

Page 29: read.pudn.comread.pudn.com/downloads158/doc/703125/labview serial port... · Web viewAdditionally, check the National Instruments Instrument Driver Network to see if LabVIEW drivers