The serial port of the PC is a very important resource both in industrial environment and in home-made electronics, due to the wide popularity of the UART interface which is to be found on many microcontrollers or on many test and design instrumentation (programmable power supplies, multi-meters, oscilloscopes etc). Even if modern-day computers tend not to have a physical serial port anymore, this obstacle is overcome by the appearance on the market of USB to UART converters (like the FTDI chips for example) which can emulate serial ports. Thus, from a computer application point of view, writing to such a device is as easy as writing to a regular serial port.
The serial port is a serial communication interface through which information transfers in or out one bit at a time. 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. A development environment which includes a C# compiler should be used. There are many 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.
2. Code Design
To be able to use “SerialPort” component is 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;
Once this is done, create a regular button on the surface of the form, call it “button1” and change its label to “Write”. Then double click on it in order to create the function that will be executed when the button is clicked. 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;
Next, before writing to the port, it needs to be opened:
//opening the serial port serialPort1.Open();
Please note that if the COM1 port is already used by an application, you will get an error message when this instruction is executed. Alternatively, if you open the COM1 port with your C# application and then fail to close it, any other application trying to use it will not be able to do that.
The following codes give example of writing to the serial port:
//write data to serial port serialPort1.Write("ABC");
When this instruction is executed, three bytes are sent to the serial port: the ASCII code of “A”, the ASCII code of “B” and the ASCII code of “C”. Once the write operation is performed, you must not forget to close the port:
//close the port serialPort1.Close();
So, as a summary, all the code that makes up the body of the function should be as below:
For More Details: Serial Port Communication in C#