本人新手请教一个MScomm控件的问题
各位大虾:
本人新手想学习用mscomm做串口通信程序.为便于测试,我将pc上两个com口相连.想开发一小程序,实现功能如下:在文本框中输入文本点发送按钮后利用mscount控件将输入文本发送至com口,当com口接受到数据后弹出对话框现实收到数据并将收到文本显示在另一文本框内.我写了一个程序但没有反应请大家帮忙解决一下(如果我的程序行不通,请给段实例代码)谢谢:
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
期待大家回复谢谢了
[解决办法]
http://community.csdn.net/Expert/topic/5366/5366904.xml?temp=2.079409E-02
上述网址有接收二进制数据并处理为16进制显示的代码.
http://community.csdn.net/Expert/topic/5365/5365521.xml?temp=.3101923
上述网址有发送二进制数据的代码.
请参阅如下网址本人的答复:
http://zhidao.baidu.com/question/18050782.html
http://zhidao.baidu.com/question/12535506.html
http://zhidao.baidu.com/question/12522809.html
http://zhidao.baidu.com/question/11725744.html
http://zhidao.baidu.com/question/11040704.html
http://zhidao.baidu.com/question/10941227.html
[解决办法]
你的代码中的
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