Listing 5 shows how to use the event to retrieve
data. The SerialPort class’s DataReceived code runs in a
different program thread. For this reason, if you retrieve
received data in a DataReceived event and want to display
the data on the application’s form, you need to define a
delegate to pass the data to the form. For an example of
how to do so, see the ComPort Terminal example at
Another way to detect received data is to use a Timer
component to check for new data at intervals.
More on Transferring Text
The example application sends and receives data —
including numbers — as codes that represent text
characters. For example, to send “1,” the port transmits
the byte 31h (00110001), which is the ASCII code for
the character 1. To send “ 111,” the port transmits 31h
three times; once for each character in the number.
Treating data as text is the obvious choice for
transferring strings or files that contain text. To transfer
numeric values, one option is to send the bytes as text
in ASCII hex format.
Any byte value can be expressed as a pair of
hexadecimal (base 16) characters where the letters
A–F represent values from 10 to 15.
For example, consider the decimal number 225.
Expressed as a binary value, it’s (2^ 7) + (2^ 6) +(2^ 5)
+(2^0), or: 11100001. In hexadecimal, it’s E1. The ASCII
codes for the characters “E” and “1” expressed in hexadecimal are: 45 31. So the binary representation of E1h
in ASCII hex consists of these two bytes: 01000101
A serial link using ASCII hex format sends the
decimal value 225 by transmitting the two bytes above.
A computer that receives ASCII hex values can convert
the characters to numeric values or use the data as-is.
An advantage to ASCII hex is being able to represent
any byte value using just 16 ASCII codes. The values
30h–39h represent the characters 0– 9, and 41h–46h
represent the characters A–F. Code that allows lower-case
letters uses 61h–66h to represent a–f.
All of the other values are available for alternate
uses, such as software flow-control codes or an
end-of-file indicator. Because the ASCII hex codes are all
less than 80h, a serial link transmitting these values can
save a little time by ignoring the high bit and transmitting
Listing 6 shows Visual Basic functions for converting
between binary values and ASCII hex strings.
Listing 4 used WriteLine and ReadLine to exchange
data. The SerialPort class provides several other options
LISTING5. The DataReceivedeventcan
for reading and writing text. To send a string without
appending an LF, use the Write method. The Write
method can also write a Char array to a port. The
Private Sub SendCommand(ByVal command As String)
Dim response As String
response = myComPort.ReadLine
Select Case response
lblStatus.Text = “LED 1 is OFF”
lblStatus.Text = “LED 1 is ON”
Catch ex As TimeoutException
Catch ex As InvalidOperationException
Catch ex As UnauthorizedAccessException
‘ Define the event handler for received data.
Private SerialDataReceivedEventHandler1 _
As New SerialDataReceivedEventHandler _
‘ Specify the routine that runs when
‘ a DataReceived event occurs at myComPort.
AddHandler myComPort.DataReceived, _
‘ This routine runs when data arrives at myComPort.
Friend Sub DataReceived(ByVal sender As Object, _
ByVal e As SerialDataReceivedEventArgs)
Dim newReceivedData As String
‘ Get data from the COM port.
newReceivedData = myComPort.ReadExisting
April 2008 63