VB与单片机串口通信问题,请高手赐教!
VB程序发送AA给单片机后,单片机响应回传16个字节的十六进制数,VB程序接收并显示在DISPTEXT文本框中,但是VB收不到单片机回传的数...接收代码如下:Priva...
VB程序发送AA给单片机后,单片机响应回传16个字节的十六进制数,VB程序接收并显示在DISPTEXT文本框中,但是VB收不到单片机回传的数...
接收代码如下:
Private Sub MSComm1_OnComm()
Dim BytReceived() As Byte
Dim strBuff As String
Dim i As Integer
Select Case MSComm1.CommEvent '事件发生
Case comEvReceive
Cls
MSComm1.InputLen = 0 '读入缓冲区全部内容
strBuff = MSComm1.Input '读入到缓冲区
Label1.Caption = Label1.Caption + Len(strBuff) '接收计数
If MSComm1.InputMode = comInputModeBinary Then
BytReceived() = strBuff '如果是二进制接收模式则进行数据处理,否则直接显示字符串
For i = 0 To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i)) & " " '如果只有一个字符,则前补0,如F显示0F,最后补空格
Else '方便显示观察如: 00 0F FE
strData = strData & Hex(BytReceived(i)) & " "
End If
Next
DispText.Text = DispText & strData '显示接收的数据
strData = ""
Else
DispText = DispText & strBuff
End If
End Select
End Sub
发送代码好像没错:
Private Sub Send_Data_Click() '手动发送程序
Dim temp(0) As Byte
Dim strBuff As String
On Error GoTo uerror
If MSComm1.PortOpen = False Then
MsgBox "串口未打开!"
End If
If SendText.Text = "" Then
pp = MsgBox("发送的数据不能为空!", 16)
Exit Sub
End If
If DispOpt(0).Value = True Then '如果设置十六进制则进行十六进制处理
temp(0) = "&H" & SendText
MSComm1.Output = temp
Else
strBuff = SendText
End If
MSComm1.Output = strBuff
Label7.Caption = labe7.Caption + Len(strBuff) '发送计数
uerror:
End Sub 展开
接收代码如下:
Private Sub MSComm1_OnComm()
Dim BytReceived() As Byte
Dim strBuff As String
Dim i As Integer
Select Case MSComm1.CommEvent '事件发生
Case comEvReceive
Cls
MSComm1.InputLen = 0 '读入缓冲区全部内容
strBuff = MSComm1.Input '读入到缓冲区
Label1.Caption = Label1.Caption + Len(strBuff) '接收计数
If MSComm1.InputMode = comInputModeBinary Then
BytReceived() = strBuff '如果是二进制接收模式则进行数据处理,否则直接显示字符串
For i = 0 To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i)) & " " '如果只有一个字符,则前补0,如F显示0F,最后补空格
Else '方便显示观察如: 00 0F FE
strData = strData & Hex(BytReceived(i)) & " "
End If
Next
DispText.Text = DispText & strData '显示接收的数据
strData = ""
Else
DispText = DispText & strBuff
End If
End Select
End Sub
发送代码好像没错:
Private Sub Send_Data_Click() '手动发送程序
Dim temp(0) As Byte
Dim strBuff As String
On Error GoTo uerror
If MSComm1.PortOpen = False Then
MsgBox "串口未打开!"
End If
If SendText.Text = "" Then
pp = MsgBox("发送的数据不能为空!", 16)
Exit Sub
End If
If DispOpt(0).Value = True Then '如果设置十六进制则进行十六进制处理
temp(0) = "&H" & SendText
MSComm1.Output = temp
Else
strBuff = SendText
End If
MSComm1.Output = strBuff
Label7.Caption = labe7.Caption + Len(strBuff) '发送计数
uerror:
End Sub 展开
展开全部
Public WithEvents moCom As SCCIATLLib.WinCom
俺一般是用read去读取数据
在 moCom_OnComEvent(ByVal comEvent As Integer, ByVal eError As Integer) 事件中
当 comEvent=1 时;
用循环直到读出所有数据
sA = frmMain.moCom.Read
Do While sA <> ""
mScom = mScom & sA
sA = frmMain.moCom.Read
Loop
俺一般是用read去读取数据
在 moCom_OnComEvent(ByVal comEvent As Integer, ByVal eError As Integer) 事件中
当 comEvent=1 时;
用循环直到读出所有数据
sA = frmMain.moCom.Read
Do While sA <> ""
mScom = mScom & sA
sA = frmMain.moCom.Read
Loop
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询