仪器社区

本人现在在做modbus rtu 协议 上位机与智能仪表通信 希望高手指点一下

邾佑樘 2011-09-22
自己程序谢了 读仪表地址01的值,读到txt2.text中 程序如下: Private Sub Form_Load() '初始化串口 MSComm1.CommPort = 1 '选择串口 MSComm1.Settings = "9600,E,8,1" '设... 自己程序谢了 读仪表地址01的值,读到txt2.text中 程序如下: Private Sub Form_Load() '初始化串口 MSComm1.CommPort = 1 '选择串口 MSComm1.Settings = "9600,E,8,1" '设置通信参数 MSComm1.InputMode = comInputModeBinary '设置接受数据为二进制形式 MSComm1.InBufferSize = 512 MSComm1.OutBufferSize = 512 MSComm1.InputLen = 0 '从接受缓冲区读取全部数据 Timer1.Interval = 100 Timer1.Enabled = True Timer2.Interval = 10 Timer2.Enabled = False 'MSComm1.SThreshold = 1 ' If Not MSComm1.PortOpen Then MSComm1.PortOpen = True ' End If End Sub Function crc16(ByRef cmdstring() As Byte, ByVal j As Integer) Dim data As Integer Dim i As Integer Dim k As Integer Dim Addressreg_crc As Long Addressreg_crc = &HFFFF For i = 0 To j Addressreg_crc = Addressreg_crc Xor cmdstring(i) For k = 0 To 7 data = Addressreg_crc And &H1 If data Then Addressreg_crc = Addressreg_crc \ 2 ' Addressreg_crc = Addressreg_crc And &H7FFF Addressreg_crc = Addressreg_crc Xor &HA001 Else Addressreg_crc = Addressreg_crc \ 2 ' Addressreg_crc = Addressreg_crc And &H7FFF End If Next k Next i Hibyte = Addressreg_crc And &HFF Lobyte = (Addressreg_crc And &HFF00) / &H100 End Function Private Sub timer1_timer() MSComm1.OutBufferCount = 0 '清空接受缓冲区 Dim sendstr(8) As Byte sendstr(0) = &H1 sendstr(1) = &H3 sendstr(2) = &H0 sendstr(3) = &H1 sendstr(4) = &H0 sendstr(5) = &H1 Call crc16(sendstr(), 5) sendstr(5) = Hibyte sendstr(6) = Lobyte MSComm1.Output = sendstr '发送查询命令 Timer2.Enabled = True '启动定时器2 End Sub ' Private Function Hex2(ByRef c As String) As String ' Hex2 = Hex(CInt(c)) 'If Len(Hex2) < 2 Then 'Hex2 = "0" & Hex2 'End If 'End Function Private Sub Timer2_Timer() Dim inputbuf As String '接收数组 Dim inputsafebuf() As Byte '安全数组 Dim index As Integer inputbuf = "" ' MSComm1.RThreshold = 7 inputsafebuf = MSComm1.Input '将输入缓冲区中的数据放入安全数组中 Select Case MSComm1.CommEvent Case comEvReceive For index = 0 To UBound(inputsafebuf) - 1 If Len(Hex(inputsafebuf(index))) = 1 Then inputbuf = inputbuf & "0" & inputsafebuf(index) Else inputbuf = inputbuf & inputsafebuf(index) End If Next index '将安全数组中的数据放入接收数组中 ' n = Val("&H" & CStr(Mid(inputbuf, 5, 2))) '字符数 ' If Mid(inputbuf, 1, 2) = 1 And Mid(inputbuf, 3, 2) = 3 And Mid(inputbuf, 5, 2) = 2 Then txt2.Text = Val("&H" & CStr(Mid(inputbuf, 7, 4))) ' End If Case comEvSend Exit Sub End Select End Sub
评论
全部评论
Sy__永远九姐姐
你程序调通了,就可以了,你是想问什么问题啊
16 0 2011-09-23 0条评论 回复
Sally1976mm
调通即可 modbus 说白了 就是做了个crc16的校验 类似的我以前做过 并不复杂
7 0 2011-09-27 0条评论 回复
chipin98
PLC是继电器电路发展起来的逻辑控制器,DCS是过程控制仪表发展起来的智能仪表,现在两者互相渗透,化工行业还是用DCS,这是习惯问题

3 0 2011-09-23 0条评论 回复
您可能感兴趣的社区主题
加载中...
发布 评论