Link to home
Start Free TrialLog in
Avatar of dys
dys

asked on

Tell me how to read serial port!

            When moden is communicating with other hosts,how can I read data from serial port? best to give me  source code for example,thank you!
Avatar of khaledc
khaledc

dys,
You have to use a MSCOMM control (or MSCOMM AcriveX control), the following will answer your question:
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 = "AT" + Chr$(13)
      ' Wait for data to come back to the serial port.

Do
            DoEvents
      Loop Until MSComm1.InBufferCount >= 2
      ' Read the "OK" response data in the serial port.
      Instring = MSComm1.Input
      ' Close the serial port.
      MSComm1.PortOpen = False
End Sub

Note   The MSComm control can use polling or an event-driven method to retrieve data from the port. This simple example uses the polling method. For an example of the event-driven method, see help for the OnComm event.

This is how to read data from a serial port.

You can also check the VB help under "MSComm Control"

Best of luck.

On more thing I forgot to mention
Do not forget that you have to insert a MSComm Control from VB Prefrences (position your mouth over the toolbox and click on the right hand button of the mouse, from the pull down menu click on "Components" , find and tick "Microsoft Comm Control").
Then insert an Comm control on your form.
Have fun
ASKER CERTIFIED SOLUTION
Avatar of khaledc
khaledc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
dys
The above example works for sure, because I simply tried it myself

I think he is looking for a way to peek at a connection that is already open.