Beruflich Dokumente
Kultur Dokumente
Scans for installed serial ports, queries the supported baud rates, and starts listening to
the selected serial port.
CodeProject is missing a simple serial port application. Serial port listening applications
usually have this only as a part of a bigger solution, while this application does nothing
else than list the available COM-ports, list the available baud rates for the selected COM-
port, and starts sending the data. In this solution, a form converts the data to ASCII-text
and displays it in a text box.
public SerialPortManager()
{
// Finding installed serial ports on hardware
_currentSerialSettings.PortNameCollection =
System.IO.Ports.SerialPort.GetPortNames();
_currentSerialSettings.PropertyChanged +=
new System.ComponentModel.PropertyChangedEventHandler
(_currentSerialSettings_PropertyChanged);
When a serial port is selected by the user, a query for supported baud rates is done.
Depending on the hardware, different collections of baud rates may be supported. The
eld dwSettableBaud from the COMMPROP structure is a join of all supported baud rates.
_serialPort.Close();
_currentSerialSettings.UpdateBaudRateCollection(dwSettableBaud);
}
The class named SerialSettings contains the currently selected serial port settings, and
also includes lists of alternatives for the different setting properties. Everything is data
bound to the GUI.
The actual serial port reading runs in a threadpool. When data is received on the serial
port, an event is raised and _serialPort_DataReceived is called.
The received byte array is sent to those listening for the event. The class
SerialDataEventArgs houses a byte array.
Stop listening
We stop listening by simply closing the serial port. Note that this might deadlock your UI-
thread if you are using Invoke in the event handling in your form.
To work around this possible deadlock, a BeginInvoke is needed. And, that is generally
good practice as well.
if (this.InvokeRequired)
{
// Using this.Invoke causes deadlock when closing serial port,
// and BeginInvoke is good practice anyway.
this.BeginInvoke(new EventHandler<SerialDataEventArgs>(
_spManager_NewSerialDataRecieved), new object[] { sender, e });
return;
}
Summary
A rather simple sample in how to implement serial port listening has been provided.
Updates
27 April 2010 - Code clean-up and getting < > to show in the article.
10 May 2010 - Fixing some misspells in the article.
License
This article, along with any associated source code and les, is licensed under The Code
Project Open License (CPOL)
Share
EMAIL TWITTER
Application to Debug Serial Port Communication A Scalable Path to Commercial IoT Solutions
Search Go
write straight to le
darioits 7-May-15 23:23
how can i send data to serial port using the same program
Sulman Bin Khurshid 1-May-15 0:03
Bad zip
Member 10489226 15-May-14 9:16
My vote of 1
Member 10698149 6-Apr-14 23:59
Re: My vote of 1
Amund Gjerse 26-May-14 20:43
HELP
LytYrs 12-Feb-14 5:36
Re: HELP
Amund Gjerse 26-May-14 20:45
time out
Lycian 21-Aug-13 22:25