VB6.0封装MSComm控件以采集Alicat流量计数据 - Modbus ASCII协议

本文介绍了如何使用VB6.0的MSComm控件封装,以采集支持Modbus ASCII协议的Alicat流量计数据。详细阐述了Alicat的采集指令,MSComm控件的设计和使用,以及在VB6.0程序中的应用案例,提供源代码供读者参考。
摘要由CSDN通过智能技术生成

封装MSComm控件采集Alicat流量计数据



前言

Alicat流量计支持Modbus ASCII协议,为了方便测量,并将Alicat流量计集成到VB6.0测量程序中,利用VB6.0自带的MSComm控件实现了集成。


一、Alicat采集指令

在测量程序中用到的指令为:
1)A:质量流量数据返回指令;
2)AV:体积流量返回指令;
3)A@=B:设置控件设备号;
上述指令中的A和B均为设备ID号,Alicat支持A-Z的设备号。

二、MSComm控件封装过程

1.程序设计简要说明

硬件逻辑:打开设备openDevice(comID)->采集数据revMassFlowData, revVolFlowData->检测数据状态readyRead->读取数据dataStr->关闭设备closeDevice
质量流量测量:revMassFlowData->发送指令->读取数据并存储onComm
体积流量采集:revVolFlowData->发送指令->读取数据并存储onComm
支持通过sendOrders发送其他指令。

2.程序对象界面如下

添加MSComm控件,MSComm属性窗口可修改为自定义名称为MSCommCls。双击控件会产生MSCommCls_OnComm()控件,这是采集数据事件。
界面图片
在右侧属性里面可以更改名称

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 Integer
Dim devID As String '设备号
Public readyRead As Boolean '判断数据是否存放完成

Private Function IsHex(c As String) As Integer
    If c >= "0" And c <= "9" Then
      IsHex = Val(c) - Val("0")
    ElseIf c >= "a" And c <= "f" Then
      IsHex = Asc(c) - Asc("a") + 10
    ElseIf c >= "A" And c <= "F" Then
      IsHex = Asc(c) - Asc("A") + 10
    Else
      IsHex = 16
    End If
End Function

Public Function revMassFlowData() '读取质量流量
    readyRead = False
    Call collectData
End Function

Public Function revVolFlowData() '读取体积流量
    readyRead = False
    Call getVolumetricFlowRate
End Function

Public Function returnCnt()
    returnCnt = cntForRev
End Function

Private Function analysData(temStr As String) As String
    Dim sz As Long
    Dim i As Long
    Dim str As String
    
    str = ""
    sz = Len(temStr)
    For i = 0 To sz - 2 Step 2
        str = str + Chr(Val("&H" + Left(temStr, 2)))
        temStr = Right(temStr, sz - i - 2)
    Next i
    analysData = str
End Function

Private Sub MSCommCls_OnComm()
    Static cnt As Integer
    Dim receive_cnt As Integer
    Dim i As Integer

    Dim Buffer As Variant
    Dim Arr() As Byte
    Dim s As String
    
    readyRead = False
    cntForRev = cntForRev + 1
    
'    Static num As Integer
'    Text1.Text = Text1.Text + "计数:" + CStr(num
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值