VB6.0封装MSComm控件以采集Alicat流量计数据( 二 )

<= 256 ThencomNum = comIDElseopenDevice = FalseExit FunctionEnd If'波特率If baudRate = 300 Or baudRate = 600 Or baudRate = 1200 Or baudRate = 2400 Or baudRate = 4800 Or baudRate = 9600 _Or baudRate = 19200 Or baudRate = 38400 Or baudRate = 56000 Or baudRate = 57600 Or baudRate = 115200 ThenElsebaudRate = 19200End If'校验码If check <> "N" And check <> "O" And check <> "E" Thencheck = "N"End If'数据位If dataBits <> 8 And dataBits <> 7 And dataBits <> 6 ThendataBits = 8End If'停止位If stopBits <> 2 And stopBits <> 1 ThenstopBits = 1End If'打开串口On Error Resume NextErr.Clear'连接串口If MSCommCls.PortOpen = True ThenMSCommCls.PortOpen = FalseEnd IfMSCommCls.CommPort = comNumMSCommCls.Settings = CStr(baudRate) + "," + check + "," + CStr(dataBits) + "," + CStr(stopBits) '设置波特率及数据帧格式MSCommCls.InputLen = 0'读取接收缓冲区的所有字符MSCommCls.InBufferSize = 256'数据接受缓冲区大小MSCommCls.OutBufferSize = 256'数据发送缓冲区大小MSCommCls.RThreshold = 1'接受一个字节就产生ON_COMM事件'MSComm1.SThreshold = 1'发送缓冲区空触发发送事件'MSComm1.InputMode = comInputModeText'字节模式'设定 InputMode 以读取二进位资料MSCommCls.InputMode = comInputModeBinaryMSCommCls.PortOpen = TrueIf Err.Number ThenIf Err.Number = 8002 Then'MsgBox "串口" + CStr(comNum) + "不存在!", vbOKOnly, "警告"openDevice = FalseExit FunctionElseIf Err.Number = 8005 Then'MsgBox "串口" + CStr(comNum) + "已打开!", vbOKOnly, "警告"openDevice = TrueElse'其他错误'Text1.Text = "收到错误码:" + CStr(Err.Number)Err.ClearopenDevice = FalseExit FunctionEnd IfElseopenDevice = TrueEnd If'Text1.Text = CStr(MSComm1.Settings)openDevice = TrueEnd FunctionPublic Function changeDevID(deviceID1 As String, deviceID2 As String) As BooleanIf deviceID2 >= "A" And deviceID2 <= "Z" And deviceID1 >= "A" And deviceID1 <= "Z" ThendevID = deviceID2changeDevID = TrueElsechangeDevID = FalseExit FunctionEnd IfDim orders As Stringorders = CStr(Hex(Asc(deviceID1))) & "403D" & CStr(Hex(Asc(deviceID2))) & "0D0D"Call sendOrders(orders)changeDevID = TrueEnd FunctionPublic Function initDevID(deviceID As String)If deviceID >= "A" And deviceID <= "Z" ThendevID = deviceIDElseExit FunctionEnd IfEnd FunctionPrivate Function getVolumetricFlowRate()Dim orders As StringDim deviceID As String'初始化数据dataStr = ""revStr = ""cntForRev = 0'确定设备地址If devID = "" ThendeviceID = "41"ElsedeviceID = CStr(Hex(Asc(devID)))End Iforders = deviceID & "560D0D"Call sendOrders(orders)End FunctionPublic Sub sendOrders(ordersHex As String)Dim s As StringDim sz As Integer '记录输入字符串个数Dim i As Integer, cnt As IntegerDim a As IntegerDim temStr As StringDim deviceID As StringDim send_buf() As ByteReDim send_buf(1024)dataStr = ""revStr = ""cntForRev = 0cnt = 0s = ordersHexsz = Len(s)temStr = s'判断所有字符是否为16进制For i = 0 To sz - 1If IsHex(Left(temStr, 1)) = 16 Then'MsgBox "发送内容必须为16进制形式", vbOKOnly, "警告"Exit SubEnd IftemStr = Right(temStr, sz - 1 - i)Next iDo While (sz)a = IsHex(Left(s, 1))sz = sz - 1s = Right(s, sz)If sz <> 0 Thena = a * 16 + IsHex(Left(s, 1))sz = sz - 1s = Right(s, sz)End Ifcnt = cnt + 1send_buf(cnt) = aLoopIf cnt > 128 Then cnt = 128ReDim Preserve send_buf(cnt) '保留原始数据并改变字符串长度'Text1.Text = CStr(send_buf())MSCommCls.Output = send_buf() '发送16进制ASCII码字节流'curT = timeGetTime '记录发送时间End SubPublic Sub initDevice(rates As Long, checkCode As String, dataBit As Integer, stopBit As Integer)'初始化基本配置baudRate = rates'comNum = comIDcheck = checkCodedataBits = dataBitstopBits = stopBitEnd SubPublic Sub closeDevice()If MSCommCls.PortOpen = True ThenMSCommCls.PortOpen = FalseEnd IfEnd Sub
三、控件使用案例 1.窗口界面
控件注册后,添加控件,左上角空白区域 。
2.代码页代码
代码页的代码如下: