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

封装控件采集流量计数据
文章目录三、控件使用案例
前言
流量计支持 ASCII协议,为了方便测量,并将流量计集成到VB6.0测量程序中,利用VB6.0自带的控件实现了集成 。
一、采集指令
在测量程序中用到的指令为:
1)A:质量流量数据返回指令;
2)AV:体积流量返回指令;
3)A@=B:设置控件设备号;
上述指令中的A和B均为设备ID号,支持A-Z的设备号 。
二、控件封装过程 1.程序设计简要说明

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

文章插图
硬件逻辑:打开设备(comID)->采集数据, ->检测数据状态->读取数据->关闭设备
质量流量测量:->发送指令->读取数据并存储
体积流量采集:->发送指令->读取数据并存储
支持通过发送其他指令 。
2.程序对象界面如下
添加控件,属性窗口可修改为自定义名称为 。双击控件会产生()控件,这是采集数据事件 。
3.程序代码如下
代码页的代码如下:
Option Base 1 '限定动态数组下标从1开始Dim baudRate As Long '波特率Dim comNum As Integer '通道号Dim check As String '校验码Dim dataBits As Integer '数据位数Dim stopBits As Integer '停止位数Dim revStr As String '记录原始接收数据Public dataStr As String '记录字符串数据Dim cntForRev As IntegerDim devID As String '设备号Public readyRead As Boolean '判断数据是否存放完成Private Function IsHex(c As String) As IntegerIf c >= "0" And c <= "9" ThenIsHex = Val(c) - Val("0")ElseIf c >= "a" And c <= "f" ThenIsHex = Asc(c) - Asc("a") + 10ElseIf c >= "A" And c <= "F" ThenIsHex = Asc(c) - Asc("A") + 10ElseIsHex = 16End IfEnd FunctionPublic Function revMassFlowData() '读取质量流量readyRead = FalseCall collectDataEnd FunctionPublic Function revVolFlowData() '读取体积流量readyRead = FalseCall getVolumetricFlowRateEnd FunctionPublic Function returnCnt()returnCnt = cntForRevEnd FunctionPrivate Function analysData(temStr As String) As StringDim sz As LongDim i As LongDim str As Stringstr = ""sz = Len(temStr)For i = 0 To sz - 2 Step 2str = str + Chr(Val("&H" + Left(temStr, 2)))temStr = Right(temStr, sz - i - 2)Next ianalysData = http://www.kingceram.com/post/strEnd FunctionPrivate Sub MSCommCls_OnComm()Static cnt As IntegerDim receive_cnt As IntegerDim i As IntegerDim Buffer As VariantDim Arr() As ByteDim s As StringreadyRead = FalsecntForRev = cntForRev + 1'Static num As Integer'Text1.Text = Text1.Text +"计数:" + CStr(num) + "; "'num = num + 1Select Case MSCommCls.CommEventCase comEvReceivereceive_cnt = MSCommCls.InBufferCount'接收缓冲区的字节数' 往暂存区存二进位资料Buffer = MSCommCls.Input' 指定给位元组阵列以便处理Arr = Buffers = ""For i = 0 To receive_cnt - 1If Arr(i) > 15 Then's = s + Hex(Arr(i)) + " "revStr = revStr + Hex(Arr(i))'Text1.Text = Text1.Text & sElse's = s + "0" + Hex(Arr(i)) + " "revStr = revStr + "0" + Hex(Arr(i))'Text1.Text = Text1.Text & sIf "0" + Hex(Arr(i)) = "0D" Then '判断结尾dataStr = analysData(revStr)readyRead = TrueEnd IfEnd Ifcnt = cnt + 1Next i'Text1.Text = Text1.Text & sIf cnt >= 300 Then'Text1.Text = ""cnt = 0Exit SubEnd IfCase comEvSendCase comEvEOFEnd SelectEnd SubPrivate Function collectData()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 = 0'确定设备地址If devID = "" ThendeviceID = "41" '设备默认为AElsedeviceID = CStr(Hex(Asc(devID)))End Ifs = deviceID & "0D0D"call sendOrders(s)'发送指令End FunctionPublic Function openDevice(comID As Integer) As Boolean'初始化基本配置以便选择Dim i As IntegerDim temStr As String'数据初始化dataStr = ""revStr = ""cntForRev = 0'串口If comID >= 1 And comID