MSComm Control Example The following simple example shows basic serial communications using a modem: Private Sub Form_Load () ' Buffer to hold input string Dim Instring As String ' Use COM1. MSComm1.CommPort = 1 ' 9600 baud, no parity, 8 data, and 1 stop bit. MSComm1.Settings = "9600,N,8,1" ' Tell the control to read entire buffer when Input ' is used. MSComm1.InputLen = 0 ' Open the port. MSComm1.PortOpen = True ' Send the attention command to the modem. MSComm1.Output = "ATV1Q0" & Chr$(13) ' Ensure that ' the modem responds with "OK". ' Wait for data to come back to the serial port. Do DoEvents Buffer$ = Buffer$ & MSComm1.Input Loop Until InStr(Buffer$, "OK" & vbCRLF) ' Read the "OK" response data in the serial port. ' Close the serial port. MSComm1.PortOpen = False End Sub
Private Sub Form_Load () ' 保存输入子串的缓冲区 Dim Instring As String ' 使用 COM1。 MSComm1.CommPort = 1 ' 9600 波特,无奇偶校验,8 位数据,一个停止位。 MSComm1.Settings = "9600,N,8,1" ' 当输入占用时, ' 告诉控件读入整个缓冲区。 MSComm1.InputLen = 0 '设定 InputMode 读取二进制数据 MSComm1.InputMode = comInputModeText ' 打开端口。 MSComm1.PortOpen = True End Sub'发送 Private Sub cmdSend_Click() MSComm1.Output = "SendData" End Sub'接收 Private Sub cmdReceive_Click() text1.Text = MSComm1.Input End Sub
The following simple example shows basic serial communications using a modem: Private Sub Form_Load ()
' Buffer to hold input string
Dim Instring As String
' Use COM1.
MSComm1.CommPort = 1
' 9600 baud, no parity, 8 data, and 1 stop bit.
MSComm1.Settings = "9600,N,8,1"
' Tell the control to read entire buffer when Input
' is used.
MSComm1.InputLen = 0
' Open the port.
MSComm1.PortOpen = True
' Send the attention command to the modem.
MSComm1.Output = "ATV1Q0" & Chr$(13) ' Ensure that
' the modem responds with "OK".
' Wait for data to come back to the serial port.
Do
DoEvents
Buffer$ = Buffer$ & MSComm1.Input
Loop Until InStr(Buffer$, "OK" & vbCRLF)
' Read the "OK" response data in the serial port.
' Close the serial port.
MSComm1.PortOpen = False
End Sub
' 保存输入子串的缓冲区
Dim Instring As String
' 使用 COM1。
MSComm1.CommPort = 1
' 9600 波特,无奇偶校验,8 位数据,一个停止位。
MSComm1.Settings = "9600,N,8,1"
' 当输入占用时,
' 告诉控件读入整个缓冲区。
MSComm1.InputLen = 0
'设定 InputMode 读取二进制数据
MSComm1.InputMode = comInputModeText
' 打开端口。
MSComm1.PortOpen = True
End Sub'发送
Private Sub cmdSend_Click()
MSComm1.Output = "SendData"
End Sub'接收
Private Sub cmdReceive_Click()
text1.Text = MSComm1.Input
End Sub