Most PCs and laptops have a serial port. USB serial ports are also very cheap. A serial port is a very easy way to communicate with external electronic hardware and or micro-controllers.

 In this example we will first look at transmitting data out of a serial port. If you are not familiar with the standard characteristics of a serial port please read this article first http://en.wikipedia.org/wiki/Serial_port.

 Firstly using Visual Studio create a new Console application. I don’t want to go straight for a winForms application as this will generate some of the code automatically for you. To understand everything that is going on it is best to do this manually first time.

The first thing you need to do is add a ‘using’ statement to the top of your code. (A using statement is similar to an include statement in C, it tells the compiler to include a particular class library at compile time. This also allows you to use the methods, etc from that particular library.) The statement is “using System.IO.Ports;” shown highlighted in the image below.

using System.IO.Ports

The next stage is to create an instance of the serialport class so that we can use its methods to perform some useful task for our application. We do this with the following line placed inside the main method (or function if your used to C).

SerialPort sp = new SerialPort();

When I was beginning C# I found it hard to understand what the point of this line was for. What actually happens is the system allocates resources for the SerialPort class. It is called an initialisation, or creating a new instance. The reason for this is so that an application can support more than one instance of the same class for example if you wanted to control more than one serial port in the same application.

 I will break down the line for you.

SerialPort sp

 This is similar to when you declare a variable such as an int, bool, double, etc. The first part ’SerialPort’ states that this is a serial port resource or type. ’sp’ defines the name to be assigned to this particular instance, similar to when you assign a name to a variable.

 = new SerialPort();

 This tells the program to fill the resource ’sp’ with the serial port class. This part is the actual creation of the serial port resource once this has happened the serial port is ready to be used.

 

Now we need to setup the serial ports baud rate and port name so that we can use it to communicate.

BaudRate is stored as type int, and PortName as type string. Baud rate can be any supported by the COM port you are using. Port name must be similar to “COM4″ replace the 4 with any number of existing port. If the port does not exist an exception will occur.

 We will set the baud rate to 9600bps, this speed completely depends on the application, you may be free to choose any or this may already be decided by the type of hardware you are communicating with.

 This is done with the line:

sp.BaudRate = 9600;

Setting the port name tells the PC which physical port we are using. This is done by:

sp.PortName = “COM1″;

Note the “” are used as the data we are setting is of string type. Whereas the 9600 is an integer.

sp.BaudRate = 9600; sp.PortName = “COM1″;

Notice we now address our serial port using ’sp’ the name we assigned to the port when we initialised. The serial port is now ready for use, any serial port has 2 states, open and closed. This is similar to when you pickup and/or hand up a telephone.

So the next step we need to do is open the serial port this is done with the ‘Open()’ method. It takes no arguments and so the line is simply.

sp.Open();

Once this has been performed we can actually transmit data from the serial port. You may or may not have some hardware ready to receive an output from the serial port. If you do not then I recommend you download a program called realTerm, this is an open source terminal application.

http://sourceforge.net/projects/realterm/

also download advance virtual comport this allows you to simulate 2 ports connected together and will enable you to send the output from you application to realterm without any cables.

http://www.advancedvirtualcomport.com/

Once this has been done you are ready to send some data from the serial port. To do this we need to use the ‘WriteLine()’ method. This takes 1 argument, a string. It then transmits the string from the serial port.

add the line:

sp.WriteLine(”Hello, from the serialPort”);

Our main method should now look like this.

sp.Open(); sp.Writeline(”x”);

If you have not already setup your virtual comport software so that you have a pair of ports connected via null modem cable (emulated). Open realterm, select one of the ports you connected, change the baud rate to 9600 also and press open.

Change the line ’sp.PortName = “COM1″; so that it represents the second of your port pair.

Now run your application and see what happens. You should see the following results.

realterm_result_sp_tut

If you see nothing then it is most likely you didn’t open the port in realterm, or you selected the incorrect port settings, such as baud rate, etc. You may also have not configured you advanced virtual comport program correctly.

Notice the character ‘LF’ at the end. This is a linefeed character, in C# this is typed as \n. This was added in by the WriteLine method for us. It tells the cursor in the terminal window to move down one line, but not to the beginning of a line, for this you would need a CR (carriage return) character, this is typed in C# as \r so to do a return the same as you would experience when using a word processor you need to use \n\r. Forgive me if this seems irrelevant just now but it will become important later on.

 

        To summarise so far, we have initialised and setup a serial port, opened the port and transmitted some data. Now we need to investigate receiving of data.

 This topic is covered on the next page. Serial Port - Receiving and handling data.

 More to follow shortly.