The Free Site   |  Hug.me - Get a dinner date this weekend   |  Cheap Web Hosting - starting at $5

Serial Port Communications with VB6

These three code examples show how to send and receive data from the computer's serial port.

Uses com port 2 (The mouse usually uses com port 1, but com port 1 may be free if you use a USB mouse)

To setup, start a new VB .exe project, and go to Project/Components/Controls. Put a check mark in Microsoft Comm Control 6.0 and click Apply

A little telephone will appear in the menu icons on the left of the screen. Click on this, and then draw a box on the form. This is the comm control and defaults to MSComm1 below.

First test is a loopback test. Connect pin 2 and 3 on the serial plug and run the code below. This sends one character to the serial port and reads the same character back in. If this works then you are almost there. If it doesn't work, next step is getting out the test leads and building a led/resistor circuit to see if anything is coming out of the serial port.

Sub EchoRS232()
' use baudrate 1200 commport=2 and timeout=5
Dim InputData As String
Dim ErrorFlag
Dim StartTime
Dim RequestString As String
Dim i As Single
Dim ReturnFlag As String
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
MSComm1.CommPort = "2"
MSComm1.Settings = "1200,n,8,1":' 1200 baud, no parity 8 bits, 1 stop bit
MSComm1.RThreshold = 1
MSComm1.InputLen = 1
MSComm1.PortOpen = True
StartTime = Now
RequestString = "A"
MSComm1.Output = RequestString: ' sends out an A
ReturnFlag = ""
Do
  If MSComm1.InBufferCount >= 1 Then Exit Do: ' got enough bytes
  i = DateDiff("s", StartTime, Now)
  If i > 5 Then ReturnFlag = "Timeout": Exit Do
Loop
' sends back the data
InputData = MSComm1.Input: ' read back the A
If InputData = "A" Then ErrorFlag = "Working"
If InputData = "" And ReturnFlag = "Timeout" Then ErrorFlag = "Timeout"
MsgBox (ErrorFlag)
End Sub

Getting data from the serial port. This code reads 10 bytes. It is best if the computer knows how many bytes to expect. A timeout circuit times out after 5 seconds to stop the computer hanging.



Sub GetTenBytes(SerialInputString As String)
Dim i As Integer
Dim StartTime
SerialInputString = ""
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
MSComm1.CommPort = "2"
MSComm1.Settings = "1200,n,8,1"
MSComm1.RThreshold = 1
MSComm1.InputLen = 1
MSComm1.PortOpen = True
StartTime = Now
ReturnFlag = ""
Do
  If MSComm1.InBufferCount >= 10 Then Exit Do: ' got enough bytes
  i = DateDiff("s", StartTime, Now)
  If i > 5 Then ReturnFlag = "Timeout": Exit Do: ' timeout in 5 seconds
Loop
If ReturnFlag <> "Timeout" Then
For i = 1 To 10: ' read in 10 bytes
  SerialInputString = SerialInputString + MSComm1.Input
Next
Else
SerialInputString = "No data"
End If
MSComm1.PortOpen = False
Exit Sub
GetSerialError: MSComm1.PortOpen = False
End Sub

Send data to the serial port - this data sends 'Hello World'


Sub SendDataToSerial()
On Error GoTo SendError
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
MSComm1.Settings = "1200,n,8,1"
MSComm1.CommPort = 2
MSComm1.PortOpen = True
MSComm1.Output = "Hello World"
MSComm1.PortOpen = False
Exit Sub
SendError: Exit Sub
End Sub

 

Home