VCR(2维码读取器)-PC Data transmission - RS232

时间:2022-04-06 13:53:46

1.Hardware : RS-232 interface         Rs-232 是个人计算机上的通讯接口之一,由电子工业协会 (Electronic Industries Association EIA) 所制定的异步传输标准接口。通常 RS-232 接口以 9 个接脚 (DB-9) 或是 25 个接脚 (DB-25) 的型态出现,一般个人计算机上会有两 RS-232 接口,分别称为 COM1 COM2 VCR(2维码读取器)-PC Data transmission - RS232
2. Software:MSComm control
       在工业控制领域,我们经常需要进行计算机与其他设备之间的通信,而串行通信作为一种灵活、方便、可靠的通信方式被广泛采用。在开发串行通信程序的过程中, 利用微软的 MSComm 通信控件则相对较简单,该控件具有丰富的与串行通信密切相关的属性及事件,提供了对串口的各种操作。 MSComm 控件在串口编程时非常方 便,程序员不必花时间去了解较为复杂的 API 函数,而且在 VB 中容易使用。
VCR(2维码读取器)-PC Data transmission - RS232
VB Source
' /************************************************************/ ' /*                                                                                                      */ ' /* Function    : Form_Load                                                                  */ ' /*                                                                                                       */ ' /* Description : Form Load Event                                                         */ ' /*                                                                                                       */ ' /*************************************************************/ Private Sub Form_Load()        MSComm1.CommPort = 1                                  使用 COM1     MSComm1.Settings = “9600,N,8,1”                   设置参数 : 波特率 9600, 无校验 , 数据位 8, 停止位 1     MSComm1.RThreshold = 1                                    每接收 1 字节就触发一次 OnComm 事件     MSComm1.InputMode = comInputModeBinary 接收数据为 2 进制     MSComm1.InputLen = 0                                        每次读入缓冲区所有字符     MSComm1.InBufferCount = 0                               清除接收缓冲区     If MSComm1.PortOpen = False Then     MSComm1.PortOpen = True                                     打开串口      End If End Sub
' /****************************************************/ ' /*                                                                                                     */ ' /* Function    : Read_Func                                                             */ ' /*                                                                                                     */ ' /* Description : Read Panel ID                                                       */ ' /*                                                                                                     */ ' /****************************************************/ Private Sub Read_Func()        Dim a(0 To 3) As Byte   将读取命令放入一个数组中     a(0) = &H1B     a(1) = &H50     a(2) = &HFF     a(3) = &HD         MSComm1.InputMode = comInputModeBinary     MSComm1.Output = a     End Sub

' /****************************************************/ ' /*                                                                                                     */ ' /* Function    : MSComm1_OnComm                                                        */ ' /*                                                                                                     */ ' /* Description : Read Panel ID                                                       */ ' /*                                                                                                     */ ' /****************************************************/
Private Sub MSComm1_OnComm()     Dim temp$                                              隐式定义     Dim strdata As String     Dim sData() As Byte     Dim timemscomm        Select Case MSComm1.CommEvent    有字符返回 MSComm1.CommEvent 值为 2       Case comEvReceive         MSComm1.InputLen = 0                     读入所有字符         timemscomm = GetTickCount()         获取自 windows 启动以来经历的时间长度 ( 毫秒 ) 返回值 Long         Do             DoEvents                                           放弃执行 , 使操作系统处理其他事件         Loop Until GetTickCount - timemscomm > 100                 temp$ = MSComm1.Input                sData() = temp         For i = 0 To UBound(sData)              strdata = strdata & Chr(sData(i))                 Next                 If temp <> "" Then                 MSComm1.RThreshold = 0                 MSComm1.InBufferCount = 0    清除接收缓冲区                 MSComm1.OutBufferCount = 0 清除发送缓冲区                 txtShtID.Text = Trim(strdata)                 End If                 MSComm1.RThreshold = 1        再次发送读取命令时触发 OnComm 事件     End Select End Sub