看看我用VB做的MSCOMM串口通信为什么不好用?
代码如下:
Private Sub Command1_Click()
If ComSelect.Text = "SELECT COM " Then
MsgBox ( "Please Select COM Port ")
Else
MSComm1.OutBufferCount = 0
MSComm1.Output = Text1.Text
Text1.Text = " "
End If
End Sub
-----------------------
Private Sub ComSelect_Click()
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
MSComm1.CommPort = Replace(ComSelect.Text, "COM ", " ")
MSComm1.Settings = "9600,n,8,1 "
MSComm1.PortOpen = True
End Sub
------------------------
Private Sub Mscomm1_Oncomm()
Select Case MSComm1.CommEvent
Case comEvReceive
MSComm1.InputMode = comInputModeText
Text2.Text = MSComm1.Input
MSComm1.InBufferCount = 0
End Select
End Sub
按发送怎么都收不到啊,各位请指点12
[解决办法]
你的代码经检查,存在较多错误:
1)Mscomm控件的Oncomm事件发生的必要条件RThreshold 属性未作设置
解释:RThreshold 属性
在 MSComm 控件设置 CommEvent 属性为 comEvReceive 并产生 OnComm 之前,设置并返回的要接收的字符数。
当接收字符后,若 Rthreshold 属性设置为 0(缺省值)则不产生 OnComm 事件。
2)如果你是试自发自收,则串口的2与3脚应短接。
以下给出能接收收的代码:
Option Explicit
Private Sub Command1_Click()
MSComm1.OutBufferCount = 0
MSComm1.Output = Text1.Text
Text1.Text = ""
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.RThreshold = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
Select Case MSComm2.CommEvent
Case comEvReceive
MSComm1.InputMode = comInputModeText
Text2.Text = MSComm2.Input
MSComm1.InBufferCount = 0
End Select
End Sub