品牌:GE | 规格:全新 | 材质:原装 |
产地:国外 |
IC697MDL251RR
设置MSComm控件的接收数据模式采用二进制方式,即 InputMode=comInputModeBinary,但用Input属性读取数据时,不能直接赋值给 Byte 类型变量,只能通过先赋值给一个 Variant 类型变量,返回一个二进制数据的数组,再转换保存到Byte类型数变量中。
3.2.5 VB中有 Byte类型变量,但没有字节的位处理语句。通道状态字节的位处理要通过对该字节的值运算进行判断,符号字节的位处理则要判断符号字节的值是否大于127,大于127则为负数;压缩的BCD码存入Byte类型变量,VB系统只按十进制数处理,这要通过一个简单算法换算,解压BCD码才能还原成十进制表示数值。假如a是Byte类型变量,W是Single类型变量,将一个压缩的BCD码存入 a中,则算法是:
W=(a\16)*10 + a-(a\16)*16
则W=a-(a\16)*6
3.3 程序代码
在(通用)(声明)中定义程序所用变量:
Dim ab(4) As Byte '字节数据类型数组,用来存贮接收到的一组字节数据
Dim av As Variant '用来从接收缓冲区读取数据
Dim i As Integer
Dim j As Integer
Dim w As Integer '接收数据个数计数器
Dim b1 As Single
Dim b2 As Single
Dim WW As Single '十进制检测值
Dim TD(8) as Boolean '通道状态数组
Dim Wmax(8) As Single '大值数组
Dim Wmin(8) As Single '小值数组
在窗体中添加名为Command1的[开始]按钮和名为MSComm1的MSComm控件。
[开始]按钮的Click事件处理程序主要是对MSComm1控制的参数初始化设置,程序中大部分参数在设计时可在MSComm1控制的属性窗口中设置:
Private Sub Command1_Click() '开始按钮
With MSComm1
mPort=2 '使用COM2
.Setting="9600,N,8,1" '设置通信口参数
.InBufferSize=40 '设置MSComm1接收缓冲区为40字节
.OutBufferSize=2 '设置MSComm1发送缓冲区为2字节
.InputMode = comInputModeBinary '设置接收数据模式为二进制形式
.InputLen = 1 '设置Input 一次从接收缓冲读取字节数为1
.SThreshold = 1 '设置Output 一次从发送缓冲读取字节数为1
.InBufferCount = 0 '清除接收缓冲区
.OutBufferCount = 0 '清除发送缓冲区
For i=1 to 8
Wmax(i) = -99 '大值赋初值
Wmin(i) = 99 '小值赋初值
Next i
w = 0 '数据个数计数器清零
.RThreshold = 1 '设置接收一个字节产生OnComm事件
On Error Resume Next '改变错误处理的方式。
Err.Clear
If .PortOpen = False Then '判断通信口是否打开
.PortOpen = True '打开通信口
If Err Then '错误处理
MsgBox "串口通信无效"
Exit Sub
End If
End If
End With
End Sub
为了达到实时数据采集目的,实时数据采集处理程序采用MSComm事件驱动方式。MSComm1_OnComm 的事件处理程序只处理comEvReceive事件,首先判断帧数据的开始字节,关闭OnComm接收事件,然后接收数据字节,将压缩BCD进行还原转换,再接收符号字节,判断数据符号,判断数据大小值,后打开OnComm接收事件,等待下一次OnComm事件产生:
Private Sub MSComm1_OnComm()
With MSComm1
Select Case mEvent '判断MSComm1通讯事件
Case comEvReceive '收到Rthreshold个字节产生的接收事件
av = .Input '读取一个接收字节
ab(1) = av(0) '转换保存到字节数据类型数组
If ab(1) = 170 Then '判断是否为数据开始标志,0FAH=170
.RThreshold = 0 '关闭OnComm事件接收
W=W+1 '计数器加1
av = .Input '读取通道状态字节
ab(0) = av(0) '转换保存到字节数据类型数
For i = 1 To 8 '通道状态数组复位
TD(i) = False
Next i
cn = 2 '帧长度赋初值,一个开始字,一个状态字
If ab(0) >= 128 Then '判断第八通状态
TD(8) = True '第八道是真
ab(0) = ab(0) - 128 '第7位置零
cn = cn + 3 '帧长度加3个字节
End If
If ab(0) >= 64 Then '判断第七通状态
TD(7) = True '第七道是真
ab(0) = ab(0) - 64 '第6位置零
cn = cn + 3 '帧长度加3个字节
End If
If ab(0) >= 32 Then '判断第六通状态
TD(6) = True '第六道是真
ab(0) = ab(0) - 32 '第5位置零
cn = cn + 3 '帧长度加3个字节
End If
If ab(0) >= 16 Then '判断第五通状态
TD(5) = True '第五道是真
ab(0) = ab(0) - 16 '第4位置零
cn = cn + 3 '帧长度加3个字节
End If
If ab(0) >= 8 Then '判断第四通状态
TD(4) = True '第四道是真
ab(0) = ab(0) - 8 '第3位置零
cn = cn + 3 '帧长度加3个字节
End If
If ab(0) >= 4 Then '判断第三通状态
TD(3) = True '第三道是真
ab(0) = ab(0) - 4 '第2位置零
cn = cn + 3 '帧长度加3个字节
End If
If ab(0) >= 2 Then '判断第二通状态
TD(2) = True '第二道是真
ab(0) = ab(0) - 2 '第0位置零
cn = cn + 3 '帧长度加3个字节
End If
If ab(0) >= 1 Then '判断第一通状态
TD(1) = True '第一道是真
cn = cn + 3 '帧长度加3个字节
End If
For i = 1 To 8 '
If TD(i) = True Then
av = .Input '读取第一个数据字节(BCD码高位字节)
ab(2) = av(0) '转换保存到字节数据类型数组
av = .Input '读取第二个数据字节(BCD码低位字节)
ab(3) = av(0) '转换保存到字节数据类型数组
av = .Input '读取第三个接收字节(符号位字节)
ab(4) = av(0) '转换保存到字节数据类型数组
b1 = ab(2) - 6 * (ab(2) \ 16) '高位字节压缩BCD码转换为实数
b2 = ab(3) - 6 * (ab(3) \ 16) '低位字节压缩BCD码转换为实数
WW = b2 + b1 / 100 '数值组合,标定小数点
If ab(4) > 127 Then WW = -WW '判断数据符号位
Label1(i-1) = Format(WW,"0.00") '显示毫米单位数值,2位小数
If WW>Wmax(i) And WW<51 Then '判断大值,仪表在刚开始工作时有干扰,会传导一些乱码,位移传感器有参数偏差,大值一般都略大于50毫米,所以取51为极限大值,取-51为极限小值。
SICK OPTIC ELECTRONIC 1012276 SENDER, 60M (196) RANGE,
SICK OPTIC ELECTRONIC 1012295 2 Beam Sender 500 mm Beam Spacing; Total Height 70
SICK OPTIC ELECTRONIC 1012296 2 Beam Receiver 500 mm Beam Spacing; Total Height
SICK OPTIC ELECTRONIC 1012463 PROXIMITY PHOTOELECTRIC,
SICK OPTIC ELECTRONIC 1012502 SENSOR
SICK OPTIC ELECTRONIC 1012503 SENSOR
SICK OPTIC ELECTRONIC 1012506 LIGHT CURTAIN
SICK OPTIC ELECTRONIC 1012507 LIGHT CURTAIN
SICK OPTIC ELECTRONIC 1012510 CONTROL
SICK OPTIC ELECTRONIC 1012512 LGHT CTN SENDR
SICK OPTIC ELECTRONIC 1012513 LGHT CTN RECVR
SICK OPTIC ELECTRONIC 1012523 CONTROL UNIT FOR LIGHT CURTAIN W/MUTING 24VDC RLY
SICK OPTIC ELECTRONIC 1012569 RS 232 INTERFACE, YELLOW
SICK OPTIC ELECTRONIC 1012570 RS 422 INTERFACE, YELLOW
SICK OPTIC ELECTRONIC 1012571 RS 232 INTERFACE, YELLOW
SICK OPTIC ELECTRONIC 1012572 RS 422 INTERFACE, YELLOW
SICK OPTIC ELECTRONIC 1012576 FGS Sender, 300mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012577 FGS Receiver, 300mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012578 FGS Sender, 450mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012579 FGS Receiver, 450mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012580 FGS Sender, 600mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012581 FGS Receiver, 600mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012582 FGS Sender, 750mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012583 FGS Receiver, 750mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012584 FGS Sender, 900mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012585 FGS Receiver, 900mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012586 FGS Sender, 300mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012587 FGS Receiver, 300mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012588 FGS Sender, 450mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012589 FGS Receiver, 450mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012590 FGS Sender, 600mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012591 FGS Receiver, 600mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012592 FGS Sender, 750mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012593 FGS Receiver, 750mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012594 FGS Sender, 900mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012595 FGS Receiver, 900mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012606 LIGHT CURTAIN
SICK OPTIC ELECTRONIC 1012607 LIGHT CURTAIN
SICK OPTIC ELECTRONIC 1012609 PART
SICK OPTIC ELECTRONIC 1012610 unknown
SICK OPTIC ELECTRONIC 1012611 unknown
SICK OPTIC ELECTRONIC 1012612 LIGHT CURTAIN
SICK OPTIC ELECTRONIC 1012613 LIGHT CURTAIN
SICK OPTIC ELECTRONIC 1012708 SCANNER
SICK OPTIC ELECTRONIC 1012711 Fiber Photoelectric, NAMUR, Red, 4-pin M12 connect
SICK OPTIC ELECTRONIC 1012753 FGS Sender, 1050mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012754 FGS Receiver, 1050mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012756 FGS Receiver, 300mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012757 FGS Sender, 450mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012758 LIGHT CURTAIN RECEIVER 450MM 14MM RESOLUTION
SICK OPTIC ELECTRONIC 1012759 FGS Sender, 600mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012760 FGS Receiver, 600mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012761 FGS Sender, 750mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012762 FGS Receiver, 750mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012763 FGS Sender, 900mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012764 FGS Receiver, 900mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012765 FGS Sender, 1050mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012766 FGS Receiver, 1050mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012767 LIGHT CURTAIN SENDER 1200MM 0-6MRANGE 24VDC
SICK OPTIC ELECTRONIC 1012768 LIGHT CURTAIN RECEIVER 1200MM 0-6MRANGE 24VDC
SICK OPTIC ELECTRONIC 1012769 LIGHT CURTAIN SENDER 1350MM 0-6MRANGE 24VDC
SICK OPTIC ELECTRONIC 1012770 LIGHT CURTAIN RECEIVER 1350MM 0-6MRANGE 24VDC
SICK OPTIC ELECTRONIC 1012771 LIGHT CURTAIN
SICK OPTIC ELECTRONIC 1012772 FGS Receiver, 1500mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012773 FGS Sender, 1650mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012774 FGS Receiver, 1650mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012775 LIGHT CURTAIN
SICK OPTIC ELECTRONIC 1012776 FGS Receiver, 1800mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012777 SAFETY LIGHT CURTAIN SENDER
SICK OPTIC ELECTRONIC 1012778 SAFETY LIGHT CURTAIN RECEIVER
SICK OPTIC ELECTRONIC 1012779 LIGHT CURTAIN SENDER 450MM 0-6MRANGE 24VDC
SICK OPTIC ELECTRONIC 1012780 LIGHT CURTAIN RECEIVER 450MM 0-6MRANGE 24VDC
SICK OPTIC ELECTRONIC 1012781 LGHT CURTAIN SENDER 600MM 30MM RESOLUTION
SICK OPTIC ELECTRONIC 1012782 FGS Receiver, 600mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012783 FGS Sender, 750mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012784 FGS Receiver, 750mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012785 LIGHT CURTAIN TRANSMITTER 932MM
SICK OPTIC ELECTRONIC 1012786 FGS Receiver, 900mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012787 FGS Sender, 1050mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012788 FGS Receiver, 1050mm, 30mm Resolution