RS485通讯的求助 请高手指点一下
我需要MSCOMM控件 发送指令到RS485 终端会反馈一组字符 但问题是指令是类似“86 01 05 0F 12”一类的 而我找的代码中是byte 写出的是&HFF &HAA这样的 我不知道指令如何转换为我需要的指令啊 菜鸟求教高手指导一下 多谢了。代码如下:
Private Sub Form_Load()
MSComm1.CommPort = 1 '利用串口COM1进行通讯
MSComm1.InputLen = 0 '每次读取接收缓冲区的1个字节
MSComm1.OutBufferSize = 512 '设置发送缓冲区为512字节
MSComm1.InBufferSize = 512 '设置接收缓冲区为2048字节
MSComm1.OutBufferCount = 0 '清除发送缓冲区
MSComm1.InBufferCount = 0 '清除接收缓冲区
MSComm1.InputMode = comInputModeBinary '数据传输设置为二进制格式
MSComm1.RThreshold = 1 '一次性接收起始码1个字节'可触发On_Comm
MSComm1.Settings = "2400,N,8,1" '设置波特率、校验位(1)、数据位、停止位
MSComm1.PortOpen = True '打开通信口COM1---需要连接RS485
Text1 = ""
End Sub
Private Sub Command1_Click()
'手动发送键
Dim DatTempBuf(30) As Byte '发送字节长31
Dim OutDatV As Variant
MSComm1.OutBufferCount = 0 '清除发送缓冲区
MSComm1.OutBufferCount = 0 '清除发送缓冲区MSComm1.OutBufferCount = 0
If MSComm1.PortOpen = False Then
MsgBox "串口未打开", 52, "提示"
Exit Sub
End If
DatTempBuf(0) = &HFF
DatTempBuf(1) = &H55
DatTempBuf(2) = &HAA
DatTempBuf(3) = &H4
DatTempBuf(4) = &H0
DatTempBuf(5) = &H3
DatTempBuf(6) = &H7
MSComm1.Output = DatTempBuf
MSComm1.InBufferCount = 0 '清除接收缓冲区
MSComm1.RThreshold = 1 '所要接收的数据长度
End Sub
Private Sub MSComm1_OnComm() '接收数据
Dim strBuff As String
Dim i As Integer
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
For i = 0 To UBound(BytReceived) '接收数据处理为16进制
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i))
Else
strData = strData & Hex(BytReceived(i))
End If
Next
If Len(strData) = 60 Then
Text1 = strData '数据处理--处理后进入对应的数据库
strData = ""
End If
End Select
End Sub
[最优解释]
ption Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim strData As String
Private Sub Form_Load()
MSComm1.CommPort = 1 '利用串口COM1进行通讯
MSComm1.InputLen = 1 '每次读取接收缓冲区的1个字节
MSComm1.OutBufferSize = 512 '设置发送缓冲区为512字节
MSComm1.InBufferSize = 512 '设置接收缓冲区为512字节
MSComm1.OutBufferCount = 0 '清除发送缓冲区
MSComm1.InBufferCount = 0 '清除接收缓冲区
MSComm1.InputMode = comInputModeBinary '数据传输设置为二进制格式
MSComm1.RThreshold = 1 '每接收一个字符就触发一次OnComm事件
MSComm1.Settings = "2400,N,8,1" '设置波特率、校验位(1)、数据位、停止位
If Not MSComm1.PortOpen Then MSComm1.PortOpen = True '打开通信口COM1---需要连接RS485
Text1.Text = ""
End Sub
Private Sub Command1_Click()
Dim DatTempBuf(0 To 2) As Byte
Dim OutDatV As Variant
On Error GoTo errSub
If Not MSComm1.PortOpen Then MSComm1.PortOpen = True
' DatTempBuf(0) = &HFF
' DatTempBuf(1) = &H55
' DatTempBuf(2) = &HAA
' DatTempBuf(3) = &H4
' DatTempBuf(4) = &H0
' DatTempBuf(5) = &H3
' DatTempBuf(6) = &H7
DatTempBuf(0) = &H68
DatTempBuf(1) = &HF
DatTempBuf(2) = &H43
MSComm1.Output = DatTempBuf
Exit Sub
errSub:
End Sub
Private Sub MSComm1_OnComm() '接收数据
Dim strBuff As String
Dim i As Integer
Dim varInput As Variant
On Error GoTo errSub
Select Case MSComm1.CommEvent
Case 2
strData = ""
MSComm1.RThreshold = 0 '屏蔽掉OnComm事件的触发
Sleep 20 '等待20ms,以便数据完全上传
Do
If Not IsNull(varInput) Then varInput = Null
varInput = MSComm1.Input '每次读取一个数据
If Not IsNull(varInput) Then
strData = strData & Right("00" & Hex("00" & varInput(0)), 2) & " "
End If
Loop Until MSComm1.InBufferCount = 0
Text1.Text = strData
MSComm1.RThreshold = 1
End Select
Exit Sub
errSub:
End Sub
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim strData As String
Private Sub Form_Load()
MSComm1.CommPort = 1 '利用串口COM1进行通讯
MSComm1.InputLen = 1 '每次读取接收缓冲区的1个字节
MSComm1.OutBufferSize = 512 '设置发送缓冲区为512字节
MSComm1.InBufferSize = 512 '设置接收缓冲区为512字节
MSComm1.OutBufferCount = 0 '清除发送缓冲区
MSComm1.InBufferCount = 0 '清除接收缓冲区
MSComm1.InputMode = comInputModeBinary '数据传输设置为二进制格式
MSComm1.RThreshold = 1 '每接收一个字符就触发一次OnComm事件
MSComm1.Settings = "2400,N,8,1" '设置波特率、校验位(1)、数据位、停止位
If Not MSComm1.PortOpen Then MSComm1.PortOpen = True '打开通信口COM1---需要连接RS485
Text1.Text = ""
End Sub
Private Sub Command1_Click()
Dim DatTempBuf(0 To 6) As Byte
Dim OutDatV As Variant
On Error GoTo errSub
If Not MSComm1.PortOpen Then MSComm1.PortOpen = True
DatTempBuf(0) = &HFF
DatTempBuf(1) = &H55
DatTempBuf(2) = &HAA
DatTempBuf(3) = &H4
DatTempBuf(4) = &H0
DatTempBuf(5) = &H3
DatTempBuf(6) = &H7
MSComm1.Output = DatTempBuf
Exit Sub
errSub:
End Sub
Private Sub MSComm1_OnComm() '接收数据
Dim strBuff As String
Dim i As Integer
Dim varInput As Variant
On Error GoTo errSub
Select Case MSComm1.CommEvent
Case 2
strData = ""
MSComm1.RThreshold = 0 '屏蔽掉OnComm事件的触发
Sleep 20 '等待20ms,以便数据完全上传
Do
If Not IsNull(varInput) Then varInput = Null
varInput = MSComm1.Input '每次读取一个数据
If Not IsNull(varInput) Then
strData = strData & Right("00" & Hex("00" & varInput(0)), 2) & " "
End If
Loop Until MSComm1.InBufferCount = 0
Text1.Text = strData
MSComm1.RThreshold = 1
End Select
Exit Sub
errSub:
End Sub