本人新手请教一个关于vb中MScomm控件的问题
各位高手:本人新手想学习用mscomm做串口通信程序.为便于测试,我将pc上两个com口用串口线相连.想开发一小程序,实现功能如下:用两个mscomm控件在同一pc上实现...
各位高手:
本人新手想学习用mscomm做串口通信程序.为便于测试,我将pc上两个com口用串口线相连.想开发一小程序,实现功能如下:用两个mscomm控件在同一pc上实现通信,一个mscomm负责向com1口发送数据,另一mscomm负责从com2口接收数据,当在文本框中输入文本点发送按钮后利用mscount控件将输入文本发送至com1口,当com2口接受到数据后弹出对话框告诉收到数据,并将收到文本显示在另一文本框内.我写了一个程序但没有反应请大家帮忙解决一下(如果我的程序行不通,请给段实例代码)谢谢:
Private Sub cmdsent_Click()
Dim strsent As String
strsent = Trim(txtsent.Text)
If (strsent = "") Then
MsgBox "发送数据不能为空!", vbOKOnly, "提示"
txtsent.SetFocus
Else
comsent.Output = strsent
MsgBox "已经执行发送!", vbOKOnly, "提示"
End If
End Sub
Private Sub comreceive_OnComm()
MsgBox "收到!"
txtreceive.Text = comreceive.Input
End Sub
Private Sub Form_Activate()
comsent.CommPort = 1
comsent.PortOpen = True
comsent.InputMode = comInputModeText
comsent.InputLen = 20
comreceive.CommPort = 2
comreceive.PortOpen = True
comreceive.InputMode = comInputModeText
comreceive.InputLen = 20
'其余属性均按默认设置
End Sub
期待大家回复谢谢了 展开
本人新手想学习用mscomm做串口通信程序.为便于测试,我将pc上两个com口用串口线相连.想开发一小程序,实现功能如下:用两个mscomm控件在同一pc上实现通信,一个mscomm负责向com1口发送数据,另一mscomm负责从com2口接收数据,当在文本框中输入文本点发送按钮后利用mscount控件将输入文本发送至com1口,当com2口接受到数据后弹出对话框告诉收到数据,并将收到文本显示在另一文本框内.我写了一个程序但没有反应请大家帮忙解决一下(如果我的程序行不通,请给段实例代码)谢谢:
Private Sub cmdsent_Click()
Dim strsent As String
strsent = Trim(txtsent.Text)
If (strsent = "") Then
MsgBox "发送数据不能为空!", vbOKOnly, "提示"
txtsent.SetFocus
Else
comsent.Output = strsent
MsgBox "已经执行发送!", vbOKOnly, "提示"
End If
End Sub
Private Sub comreceive_OnComm()
MsgBox "收到!"
txtreceive.Text = comreceive.Input
End Sub
Private Sub Form_Activate()
comsent.CommPort = 1
comsent.PortOpen = True
comsent.InputMode = comInputModeText
comsent.InputLen = 20
comreceive.CommPort = 2
comreceive.PortOpen = True
comreceive.InputMode = comInputModeText
comreceive.InputLen = 20
'其余属性均按默认设置
End Sub
期待大家回复谢谢了 展开
1个回答
展开全部
你的代码中的
MsgBox "已经执行发送!", vbOKOnly, "提示"
语句中断了接收事件,所以接收文本框未见显示,代码修改如下:
Private Sub cmdsent_Click()
Dim strsent As String
strsent = Trim(txtsent.Text)
If (strsent = "") Then
MsgBox "发送数据不能为空!", vbOKOnly, "提示"
txtsent.SetFocus
Else
comsent.Output = strsent
'MsgBox "已经执行发送!", vbOKOnly, "提示"
End If
End Sub
Private Sub comreceive_OnComm()
Select Case comreceive.CommEvent
Case comEvReceive
comreceive.InputMode = comInputModeText 'ASCII接收
txtreceive.Text = comreceive.Input
MsgBox "收到!"
End Select
End Sub
Private Sub Form_Load()
txtreceive = ""
comsent.CommPort = 1
comsent.PortOpen = True
comreceive.CommPort = 2
comreceive.PortOpen = True
comreceive.RThreshold = 1
'其余属性均按默认设置
End Sub
MsgBox "已经执行发送!", vbOKOnly, "提示"
语句中断了接收事件,所以接收文本框未见显示,代码修改如下:
Private Sub cmdsent_Click()
Dim strsent As String
strsent = Trim(txtsent.Text)
If (strsent = "") Then
MsgBox "发送数据不能为空!", vbOKOnly, "提示"
txtsent.SetFocus
Else
comsent.Output = strsent
'MsgBox "已经执行发送!", vbOKOnly, "提示"
End If
End Sub
Private Sub comreceive_OnComm()
Select Case comreceive.CommEvent
Case comEvReceive
comreceive.InputMode = comInputModeText 'ASCII接收
txtreceive.Text = comreceive.Input
MsgBox "收到!"
End Select
End Sub
Private Sub Form_Load()
txtreceive = ""
comsent.CommPort = 1
comsent.PortOpen = True
comreceive.CommPort = 2
comreceive.PortOpen = True
comreceive.RThreshold = 1
'其余属性均按默认设置
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询