Click here to Skip to main content
13,199,596 members (27,974 online)
Click here to Skip to main content
Add your own
alternative version

Stats

348.4K views
58.6K downloads
138 bookmarked
Posted 26 Apr 2010

Basic serial port listening application

, 9 May 2010
Rate this:
Please Sign up or sign in to vote.
Scans for installed serial ports, queries the supported baud rates, and starts listening to the selected serial port.

SerialPortListenerApp

Introduction

This is a basic sample of serial port (COM port) listening in C#. This application is connected to a GPS sending ASCII text for test, but the serial port listening part is all byte-oriented.

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.

Using the code

The serial port handling code is placed in a class called SerialPortManager. This class contains methods to start and stop listening for data on the serial port.

Finding the installed serial ports

Rather than just assuming the number of serial ports, or leaving it up to the user to know this beforehand, the code finds the installed serial ports. A string array of serial ports is received through a call made in the constructor of the class SerialPortManager.

public SerialPortManager()
{
    // Finding installed serial ports on hardware
    _currentSerialSettings.PortNameCollection = 
            System.IO.Ports.SerialPort.GetPortNames();
    _currentSerialSettings.PropertyChanged += 
                       new System.ComponentModel.PropertyChangedEventHandler
                       (_currentSerialSettings_PropertyChanged);

    // If serial ports is found, we select the first found
    if (_currentSerialSettings.PortNameCollection.Length > 0)
        _currentSerialSettings.PortName = 
             _currentSerialSettings.PortNameCollection[0];
}

Updating baud rates supported by the selected device

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 field dwSettableBaud from the COMMPROP structure is a join of all supported baud rates.

private void UpdateBaudRateCollection()
{
    _serialPort = new SerialPort(_currentSerialSettings.PortName);
    _serialPort.Open();

    // Getting COMMPROP structure, and its property dwSettableBaud.
    object p = _serialPort.BaseStream.GetType().GetField("commProp", 
       BindingFlags.Instance | BindingFlags.NonPublic).GetValue(_serialPort.BaseStream);
    Int32 dwSettableBaud = (Int32)p.GetType().GetField("dwSettableBaud", 
       BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).GetValue(p);

    _serialPort.Close();
    _currentSerialSettings.UpdateBaudRateCollection(dwSettableBaud);
}

Serial port settings

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.

Start listening to a serial port

The serial port is instantiated using the currently selected settings:

// Connects to a serial port defined through the current settings
public void StartListening()
{
    // Closing serial port if it is open
    if (_serialPort != null && _serialPort.IsOpen)
        _serialPort.Close();

    // Setting serial port settings
    _serialPort = new SerialPort(
        _currentSerialSettings.PortName,
        _currentSerialSettings.BaudRate,
        _currentSerialSettings.Parity,
        _currentSerialSettings.DataBits,
        _currentSerialSettings.StopBits);

     // Subscribe to event and open serial port for data
     _serialPort.DataReceived += 
         new SerialDataReceivedEventHandler(_serialPort_DataReceived);
     _serialPort.Open();
}

The actual serial port reading

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.

void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    int dataLength = _serialPort.BytesToRead;
    byte[] data = new byte[dataLength];
    int nbrDataRead = _serialPort.Read(data, 0, dataLength);
    if (nbrDataRead == 0)
        return;
          
    // Send data to whom ever interested
    if (NewSerialDataRecieved != null)
        NewSerialDataRecieved(this, new SerialDataEventArgs(data));
}

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.

/// Closes the serial port
public void StopListening()
{
     _serialPort.Close();
}

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 files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Amund Gjersøe
Engineer
Norway Norway
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
AnswerRe: time out Pin
Amund Gjersøe31-Aug-13 23:41
memberAmund Gjersøe31-Aug-13 23:41 
QuestionDiscrete values Pin
CatOfTheCanals2-Jul-13 3:03
memberCatOfTheCanals2-Jul-13 3:03 
AnswerRe: Discrete values Pin
Amund Gjersøe2-Jul-13 3:21
memberAmund Gjersøe2-Jul-13 3:21 
GeneralRe: Discrete values Pin
CatOfTheCanals2-Jul-13 22:30
memberCatOfTheCanals2-Jul-13 22:30 
GeneralMy vote of 4 Pin
hamid.mousavinasab26-Mar-13 2:21
memberhamid.mousavinasab26-Mar-13 2:21 
GeneralMy vote of 1 Pin
Member 993964224-Mar-13 22:58
memberMember 993964224-Mar-13 22:58 
GeneralRe: My vote of 1 Pin
Amund Gjersøe24-Mar-13 23:32
memberAmund Gjersøe24-Mar-13 23:32 
QuestionWriting to the serial port Pin
vasch2-Jan-13 3:33
membervasch2-Jan-13 3:33 
Hi Amund, great atricle, it helped me alot!
I am quite new in programming and am trying to build a serial monitor that can analyse strings and send email or sms messages if on some predefined conditions.
So I have implemented parts of your code in my code and that works fine (except for the default com port settings: in my app these are on an other tab page and I always need to select every field when restarting the app).
But the bigger problem is writing: I 'v added a text field and a send button and want to send this string over the same open serial port when in listening mode. I have been struggeling with this for more then a day because I can't find how to write to the port from the main app (as the port handling is done from the SerialPortManager.cs).
Any idea would be more then welcome!
cheers,
Val
QuestionGood Application: Do you have the MVC 3 application for this? Pin
matimu ngobeni18-Nov-12 21:45
membermatimu ngobeni18-Nov-12 21:45 
QuestionTesting Serial Listener ? Pin
ZARk_be26-Sep-12 0:16
memberZARk_be26-Sep-12 0:16 
AnswerRe: Testing Serial Listener ? Pin
Amund Gjersøe26-Sep-12 0:24
memberAmund Gjersøe26-Sep-12 0:24 
QuestionBinary Data Parse Pin
Embedded Planet17-Jul-12 11:22
memberEmbedded Planet17-Jul-12 11:22 
GeneralRe: Binary Data Parse Pin
Amund Gjersøe30-Jul-12 1:47
memberAmund Gjersøe30-Jul-12 1:47 
Generalis there any other serial port like articles? Pin
lyglary9-Jul-12 22:51
memberlyglary9-Jul-12 22:51 
Generalhi...Thank yu for saving my day Pin
dombanyasha7-Jun-12 10:09
memberdombanyasha7-Jun-12 10:09 
QuestionThanx Pin
dombanyasha7-Jun-12 10:08
memberdombanyasha7-Jun-12 10:08 
QuestionThanks , Just what I was searching for , BUT, Pin
Member 228177127-Nov-10 7:45
memberMember 228177127-Nov-10 7:45 
AnswerRe: Thanks , Just what I was searching for , BUT, Pin
Amund9-Dec-10 1:38
memberAmund9-Dec-10 1:38 
GeneralProblem to get the Baudrate Pin
NMehta8317-Nov-10 19:21
memberNMehta8317-Nov-10 19:21 
GeneralRe: Problem to get the Baudrate Pin
Amund18-Nov-10 21:53
memberAmund18-Nov-10 21:53 
QuestionCan't open port COM1? Pin
Jun Du22-Jul-10 4:26
memberJun Du22-Jul-10 4:26 
AnswerRe: Can't open port COM1? Pin
Amund2-Aug-10 2:27
memberAmund2-Aug-10 2:27 
QuestionAnyone knows a tip to reduce serial port latency Pin
vertex8526-Apr-10 21:38
membervertex8526-Apr-10 21:38 
AnswerRe: Anyone knows a tip to reduce serial port latency Pin
Amund26-Apr-10 23:36
memberAmund26-Apr-10 23:36 
AnswerRe: Anyone knows a tip to reduce serial port latency Pin
Luc Pattyn27-Apr-10 3:06
mvpLuc Pattyn27-Apr-10 3:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171020.1 | Last Updated 10 May 2010
Article Copyright 2010 by Amund Gjersøe
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid