您好,我想写个VB接收串口的程序,可是出了一些问题,显示不出来接收的数据。您能帮我看看嘛。
PrivateSubCommand1_Click()''DimmsgAsStringCommand1.Caption="串口2已启动"MSComm1.CommPort=3...
Private Sub Command1_Click()
''
Dim msg As String
Command1.Caption = "串口2已启动"
MSComm1.CommPort = 3
MSComm1.Settings = "9600,N,8,1"
MSComm1.InputLen = 0
MSComm1.OutBufferSize = 256
MSComm1.InBufferSize = 512
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
''
End Sub
Private Sub Command3_Click()
''
MSComm1.PortOpen = False
Command1.Caption = "启动串口2"
''
End Sub
Private Sub MSComm1_OnComm()
''
Dim Buffer As Variant
Select Case MSComm1.CommEvent
Case comEvReceive
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeBinary
Buffer = MSComm1.Input
MSComm1.InputMode = comInputModeText
Buffer = MSComm1.Input
Text1.Text = Buffer
Case Else
End Select
''
End Sub
这些是我的代码可以帮我看看嘛 展开
''
Dim msg As String
Command1.Caption = "串口2已启动"
MSComm1.CommPort = 3
MSComm1.Settings = "9600,N,8,1"
MSComm1.InputLen = 0
MSComm1.OutBufferSize = 256
MSComm1.InBufferSize = 512
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
''
End Sub
Private Sub Command3_Click()
''
MSComm1.PortOpen = False
Command1.Caption = "启动串口2"
''
End Sub
Private Sub MSComm1_OnComm()
''
Dim Buffer As Variant
Select Case MSComm1.CommEvent
Case comEvReceive
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeBinary
Buffer = MSComm1.Input
MSComm1.InputMode = comInputModeText
Buffer = MSComm1.Input
Text1.Text = Buffer
Case Else
End Select
''
End Sub
这些是我的代码可以帮我看看嘛 展开
展开全部
问题出在OnComm事件是以二进制模式读取了MsComm控件的输入,那必须用Byte字节流来处理。
或者注释掉二进制接收模式,以文本接收。
Private Sub MSComm1_OnComm()
Dim Buffer As Variant
Select Case MSComm1.CommEvent
Case comEvReceive
'MSComm1.InputMode = comInputModeBinary
'Buffer = MSComm1.Input
MSComm1.InputMode = comInputModeText
Buffer = MSComm1.Input
Text1.Text = Buffer
End Select
End Sub
追问
您好,按照你这样改了之后还是没有数据这是怎么回事。
追答
这是不可能的,代码经测试通过的。
Private Sub Form_Load()
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
Dim Buffer As Variant
Select Case MSComm1.CommEvent
Case comEvReceive
MSComm1.InputMode = comInputModeText
Buffer = MSComm1.Input
Text1.Text = Buffer
End Select
End Sub
注意MsComm控件使用的COM口号是否与你机器实际物理COM口一致。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询