串口接收16进制数据后,再把接到的数据以16进制发送!
本帖最后由 qiuming1 于 2010-06-04 17:10:00 编辑 Dim str As String
Dim BytReceived() As Byte
Dim bytSendByte() As Byte
Private Sub MSComm1_OnComm()
Dim strBuff As String
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
jieshou
End Select
Dim sj() As Byte
Dim sj_Txt As String
Dim q As Integer
sj_Txt = str
If Len(str) Mod 2 = 0 And Len(str) <> 0 Then
ReDim sj(Len(sj_Txt) / 2 - 1)
For q = 0 To Len(sj_Txt) - 1 Step 2
sj(q / 2) = Val("&H" & Mid(sj_Txt, q + 1, 2))
Next
MSComm1.Output = sj
str = ""
End If
end sub
Public Function jieshou()
Dim i As Integer
For i = 0 To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
str = str & "0" & Hex(BytReceived(i))
Else
str = str & Hex(BytReceived(i))
End If
Next
Text1.Text = str
End Function
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeBinary
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
MSComm1.RThreshold = 1
MSComm1.InBufferSize = 1024
MSComm1.PortOpen = True
end sub
接收的数据在text1.text中显示,发送好像错了...错在哪儿了...
[解决办法]
看看你的代码,不知道你来回折腾啥?
---------------------------------------------
你折腾半天与以代码有什么区别?
Private Sub MSComm1_OnComm()
Dim strBuff() As byte
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
jieshou
End Select
MSComm1.Output = strBuff
End If
end sub
Option Explicit
Dim str As String
Dim BytReceived() As Byte
Dim bytSendByte() As Byte
Dim sj_Txt As String
Private Sub MSComm1_OnComm()
Dim strBuff As String
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
Dim i As Integer
For i = 0 To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
str = str & "0" & Hex(BytReceived(i))
Else
str = str & Hex(BytReceived(i))
End If
Next
Text1.Text = str
If Mid(str, 1, 2) = "AA" And Len(str) = 22 Then
sj_Txt = str
Timer1.Enabled = True
str = ""
End If
End Select
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeBinary
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
MSComm1.RThreshold = 1
MSComm1.InBufferSize = 1024
MSComm1.PortOpen = True
Timer1.Enabled = False
Timer1.Interval = 3000
End Sub
Private Sub Timer1_Timer()
Dim sj() As Byte
Dim q As Integer
If Len(sj_Txt) Mod 2 = 0 And Len(sj_Txt) <> 0 Then
ReDim sj(Len(sj_Txt) / 2 - 1)
For q = 0 To Len(sj_Txt) - 1 Step 2
sj(q / 2) = Val("&H" & Mid(sj_Txt, q + 1, 2))
Next
MSComm1.Output = sj
str = ""
End If
Timer1.Enabled = False
End Sub