vb串口通信
我希望实现输入单片机地址A1,口令01和校验5E,点击输出,再点击输入,得到的返回数据为A1015E(中间为空格),校验结果为00,如果结果为00,则显示校验成功,如果不...
我希望实现输入单片机地址A1,口令01和校验5E,点击输出,再点击输入,得到的返回数据为A1 01 5E(中间为空格),校验结果为00,如果结果为00,则显示校验成功,如果不是00,则校验失败。代码如下,一直没结果,求大家帮帮忙!谢谢!!
Private Sub Command1_Click()
Dim uout1 As Variant
Dim uout2 As Variant
Dim uout3 As Variant
uout1 = "H" & Val(Text1.Text)
uout2 = "H" & Val(Text2.Text)
uout3 = "H" & Val(Text3.Text)
MSComm1.Output = uout1 & uout2 & uout3
End Sub
Private Sub Command2_Click()
MSComm1.PortOpen = False
Unload Me
End Sub
Private Sub Command3_Click()
Dim inbyte() As Byte
Dim buffer As String
Dim strdata(10) As String
Dim datanum As String
Select Case MSComm1.CommEvent
Case comEvReceive
inbyte = MSComm1.Input
For i = LBound(inbyte) To UBound(inbyte)
buffer = buffer + Hex(inbyte(i)) + Chr(32)
Next i
Case comEvSend
End Select
For jj = 1 To 6
strdata(jj) = Mid(Trim(buffer), jj, 1)
Next jj
If Len(Trim(buffer)) = 3 Then
datanum = "0" & strdata(1) & strdata(2) & "0" & strdata(3)
End If
If Len(Trim(buffer)) = 4 Then
datanum = "0" & strdata(1) & strdata(2) & strdata(3) & strdata(4)
End If
Text4.Text = datanum
Text5.Text = right(Hex(Val("&H" & Text1.Text) + Val("&H" & Text2.Text) + Val("&H" & Text3.Text)), 2)
If Text5.Text = "00" Then
Text6.Text = "校验成功"
Else
Text6.Text = "校验失败"
End If
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 3
MSComm1.InputMode = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.RThreshold = 1
MSComm1.SThreshold = 1
MSComm1.PortOpen = True
End Sub 展开
Private Sub Command1_Click()
Dim uout1 As Variant
Dim uout2 As Variant
Dim uout3 As Variant
uout1 = "H" & Val(Text1.Text)
uout2 = "H" & Val(Text2.Text)
uout3 = "H" & Val(Text3.Text)
MSComm1.Output = uout1 & uout2 & uout3
End Sub
Private Sub Command2_Click()
MSComm1.PortOpen = False
Unload Me
End Sub
Private Sub Command3_Click()
Dim inbyte() As Byte
Dim buffer As String
Dim strdata(10) As String
Dim datanum As String
Select Case MSComm1.CommEvent
Case comEvReceive
inbyte = MSComm1.Input
For i = LBound(inbyte) To UBound(inbyte)
buffer = buffer + Hex(inbyte(i)) + Chr(32)
Next i
Case comEvSend
End Select
For jj = 1 To 6
strdata(jj) = Mid(Trim(buffer), jj, 1)
Next jj
If Len(Trim(buffer)) = 3 Then
datanum = "0" & strdata(1) & strdata(2) & "0" & strdata(3)
End If
If Len(Trim(buffer)) = 4 Then
datanum = "0" & strdata(1) & strdata(2) & strdata(3) & strdata(4)
End If
Text4.Text = datanum
Text5.Text = right(Hex(Val("&H" & Text1.Text) + Val("&H" & Text2.Text) + Val("&H" & Text3.Text)), 2)
If Text5.Text = "00" Then
Text6.Text = "校验成功"
Else
Text6.Text = "校验失败"
End If
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 3
MSComm1.InputMode = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.RThreshold = 1
MSComm1.SThreshold = 1
MSComm1.PortOpen = True
End Sub 展开
2个回答
推荐于2016-01-14 · 知道合伙人软件行家
关注
展开全部
MSComm1.InputMode = 1,这是设置为以二进制形式取回。从你提供代码中并未见到是以二进制形式取回。
从界面看是想发送3个Byte的16进制指令,这实际是以Byte(字节流)发送,代码也有问题。
Private Sub Command1_Click()
Dim uout(2) As Byte
uout(0) = Val("&H" & Val(Text1.Text)) '3个文本框只能输入00到FF之间的16进制字符串
uout(1) = Val("&H" & Val(Text2.Text))
uout(2) = Val("&H" & Val(Text3.Text))
MSComm1.Output = uout
End Sub
建议参考提供的工程修改代码。
更多追问追答
追问
校验出结果了,太谢谢了!但是依旧没有返回数据,其实返回那块的代码我不是很清楚,求大神指点;另外comm调试我也试了,为什么显示是无效的端口呀?
追答
USB转RS232口建立的COM不是COM1,1我那代码可能是固定了COM1,需要修改代码de
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询