Serial Port Communication in C#
The serial port is a serial communication interface through which information transfers in or out one bit at a time.
A quick search on Google reveals that there are a lot of free serial port monitor applications available for PC users. However, what these applications lack, is the possibility of controlling the serial port in a direct manner. They are generally good “sniffers” but they do not allow the user to actually write to the serial port or control any device attached to it. The applications with the write capability encapsulated are not for free, and the cheapest costs about 50 Euro – a great deal of money taking into account how easy it is to make a personalized application.
This article will show how it is possible to build such an application using the C# environment. It is not intended to be a C# tutorial, but to teach a user who has basic knowledge of C or C# to integrate serial port control in one of his applications.
For the example application, I have used the SharpDevelop development environment which includes a C# compiler. This is an open source IDE which takes up very little space on your hard drive and can be a good alternative to users who do not want to install the gigabytes of Visual Studio on their PCs for a simple serial port application.
Once you have downloaded and installed the SharpDevelop environment, create a Windows Application project (solution) called SerialPort:
Once you have created the application, display the windows form that was automatically created (by clicking on the “Design” button at the bottom of the screen) and unroll the menu available under “Components” available on the left-hand menu:
You will notice that one of the components available here is the one called “SerialPort”. Pick that component and drag&drop it over the surface of the form on the right. This will add the component to your project. The object that is created is called “serialPort1” and it will be used to access the serial port. To be able to use this component, however, you need to add at the beginning of your code the directive for using the System.IO.Ports namespace, as this is not added by default when you create the solution:
using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using System.IO.Ports;
In this function we will perform several tasks. The first one is to configure the baud rate, COM port, number of data bits, parity and stop bits of the communication:
//configuring the serial port serialPort1.PortName="COM1"; serialPort1.BaudRate=9600; serialPort1.DataBits=8; serialPort1.Parity=Parity.None; serialPort1.StopBits= StopBits.One;
//opening the serial port serialPort1.Open();
OK, it is now time to write to the serial port:
//write data to serial port serialPort1.Write("ABC");
Once the write operation is performed, you must not forget to close the port:
//close the port serialPort1.Close();
void Button1Click(object sender, EventArgs e) { //configuring the serial port serialPort1.PortName="COM1"; serialPort1.BaudRate=9600; serialPort1.DataBits=8; serialPort1.Parity=Parity.None; serialPort1.StopBits= StopBits.One; //opening the serial port serialPort1.Open(); //write data to serial port serialPort1.Write("ABC"); //close the port serialPort1.Close(); }