自己程序谢了 读仪表地址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