Serial Port Communication in C#
1 din 4
http://dev.emcelettronica.com/print/51947
Your Electronics Open Source (http://dev.emcelettronica.com) Home > Blog > brumbarchris's blog > Contenuti
Serial Port Communication in C# By brumbarchris Created 09/05/2008 - 21:02
Programming C# serial port serial port communication 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:
22.09.2008 09:13
Serial Port Communication in C#
2 din 4
http://dev.emcelettronica.com/print/51947
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
22.09.2008 09:13
Serial Port Communication in C#
3 din 4
http://dev.emcelettronica.com/print/51947
added by default when you create the solution: using using using using using
System; System.Collections.Generic; System.Drawing; System.Windows.Forms; 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. OK, it is now time to write to the serial port:
22.09.2008 09:13
Serial Port Communication in C#
4 din 4
http://dev.emcelettronica.com/print/51947
//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:
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(); }
Now you have a broad path open in front of you, as you will be able to write your own customized applications that can send any data to any device attached to the serial port. The above code is also compatible with USB to Serial converters, provided that their drivers work by emulating COM ports. Trademarks
Source URL: http://dev.emcelettronica.com/serial-port-communication-c
22.09.2008 09:13