Click here to Skip to main content
11,504,689 members (70,340 online)
Click here to Skip to main content

Application to Debug Serial Port Communication

, 14 May 2007 CPOL 112.2K 6.7K 148
Rate this:
Please Sign up or sign in to vote.
This is a very simple app I use to test serial traffic. It enumerates the COM ports, allows you to edit all properties of SerialPort class instance. It also allows you to send files randomly to the serial port and save incoming data to a file.
Screenshot - Complay.jpg

Screenshot - Complay2.jpg

Introduction

This is a very basic testing tool that I created to test my serial port handling code. I did Google for such a basic tool which you would think would exist, but...

A Few Things You Might Need

I bought a few cheap USB to serial port cables which are helpful for testing on one machine. You can also install emulated serial ports, but I'd rather go with a more real scenario for testing.

Basic Features

  1. The application allows you to send / receive data over the serial port and save it.
  2. It allows you to configure all options of the .NET SerialPort class with a propertyGrid.
  3. It allows you to select a directory and randomly send a file from that directory at a random interval.
  4. It enumerates the COM ports on your machine.

Using the Code

Almost all the functionality in the app is a one liner and completely self explanatory because .NET is such a well written high level language. The only tricky part of this app is that the serial port receives data on a separate thread. You cannot access visual controls from a separate thread, so you must invoke a delegate that will set the text and send a copy of the memory with (new object[] { text }).

It is important to notice that InvokeRequired will let you know if the thread ID is different.

What will happen when we receive data is, serialPort1_DataReceived handler will be fired when serial data is received and it will call SetText which is just a little wrapper that will in turn invoke the SetTextCallback delegate. The delegate is set to call SetText so SetText will be invoked again, but now it is on the UI thread. At this point, this.txtData.InvokeRequired will be false and we can set the text like we normally would.

// This delegate enables asynchronous calls for setting
// the text property on a TextBox control.
delegate void SetTextCallback(string text);
private void serialPort1_DataReceived
    (object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
    SetText(this.serialPort1.ReadExisting());
}

private void SetText(string text)
{
    // InvokeRequired required compares the thread ID of the
    // calling thread to the thread ID of the creating thread.
    // If these threads are different, it returns true.
    if (this.txtData.InvokeRequired)
    {
        SetTextCallback d = new SetTextCallback(SetText);     
        this.Invoke(d, new object[] { text });
    }
    else
    {
        this.txtData.Text = text;
    }
}

Limitations

The serial app I am testing deals with old data matrix printer drivers and so I'm dealing with strictly text data going through the port, which is probably not what most people are looking for. I realize it would be more generically useful if it had a binary editor built in.

History

  • 14th May, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

rj45
Software Developer (Senior)
Canada Canada
No Biography provided

Comments and Discussions

 
Answerspecial thanks Pin
Haresh Ambaliya25-Jan-11 10:45
memberHaresh Ambaliya25-Jan-11 10:45 
GeneralRe: special thanks Pin
rj4525-Jan-11 13:52
memberrj4525-Jan-11 13:52 
Generalsmall application using serialport communication running on vista64 Pin
mrdavidch15-Apr-09 8:30
membermrdavidch15-Apr-09 8:30 
GeneralThanks! Pin
Drew Loika14-Jul-08 15:18
memberDrew Loika14-Jul-08 15:18 
GeneralCarraige Return Pin
kamarchand21-May-08 9:15
memberkamarchand21-May-08 9:15 
GeneralRe: Carraige Return Pin
rj4521-May-08 9:47
memberrj4521-May-08 9:47 
GeneralSend ascii Pin
rj4511-Sep-07 11:33
memberrj4511-Sep-07 11:33 
QuestionThanks for the awesome article Pin
AmarjeetSinghMatharu21-Aug-07 17:22
memberAmarjeetSinghMatharu21-Aug-07 17:22 
QuestionHow to turn off the DataReceived Event in SerialPort class Pin
AmarjeetSinghMatharu10-Sep-07 0:25
memberAmarjeetSinghMatharu10-Sep-07 0:25 
AnswerRe: How to turn off the DataReceived Event in SerialPort class Pin
Agnius Vasiliauskas27-Nov-07 3:12
memberAgnius Vasiliauskas27-Nov-07 3:12 
Questionreceive binary data and save it in binary file Pin
MauricioPrincipi23-Jul-07 4:33
memberMauricioPrincipi23-Jul-07 4:33 
GeneralClosing the port Pin
jasperp21-May-07 19:58
memberjasperp21-May-07 19:58 
GeneralRe: Closing the port Pin
rj4522-May-07 7:46
memberrj4522-May-07 7:46 
GeneralRe: Closing the port - Doesn't this work with Virtual ports? Pin
Vijay Chandra Sekhar Parepalli15-Dec-09 16:50
memberVijay Chandra Sekhar Parepalli15-Dec-09 16:50 
GeneralRe: Closing the port - Doesn't this work with Virtual ports? Pin
rj4516-Dec-09 5:57
memberrj4516-Dec-09 5:57 
GeneralRe: Closing the port Pin
rj4522-May-07 7:55
memberrj4522-May-07 7:55 
QuestionRe: Closing the port - App Hangs Pin
Paglia24-Oct-07 1:35
memberPaglia24-Oct-07 1:35 
GeneralSending Reciving Binary file Pin
tarikthecoder19-May-07 23:42
membertarikthecoder19-May-07 23:42 
GeneralRe: Sending Reciving Binary file Pin
rj4522-May-07 7:49
memberrj4522-May-07 7:49 
GeneralRe: Sending using write function in serial port Pin
pallaka29-Aug-09 1:29
memberpallaka29-Aug-09 1:29 
GeneralRe: Sending using write function in serial port Pin
rj4531-Aug-09 9:25
memberrj4531-Aug-09 9:25 
GeneralGood job Pin
The Code Guru14-May-07 21:18
memberThe Code Guru14-May-07 21:18 
GeneralNice Pin
Stuart Dootson14-May-07 21:10
memberStuart Dootson14-May-07 21:10 
GeneralCan't get it running with VSPD XP4. Pin
Bartosz Bien14-May-07 22:03
memberBartosz Bien14-May-07 22:03 
GeneralRe: Can't get it running with VSPD XP4. Pin
rj4515-May-07 8:17
memberrj4515-May-07 8:17 
GeneralRe: Can't get it running with VSPD XP4. Pin
Bartosz Bien15-May-07 11:34
memberBartosz Bien15-May-07 11:34 
GeneralRe: Can't get it running with VSPD XP4. Pin
rj4515-May-07 13:37
memberrj4515-May-07 13:37 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 14 May 2007
Article Copyright 2007 by rj45
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid