My first entry in this blog is going to be about serial ports. Why? Mostly because I read a lot of posts in various programming forums in regards to interacting with the serial ports. Most programmers frequently forget that serial ports have been around a very long time so they act a little different then most current communication technologies.
When deploying an application that will be interacting with serial ports the first question should always be what device will be attached and how? The “how” is the most important question. If the device only uses the transmit, receive and signals ground pins then the deployment is a lot easier, if it use all 8 pins it could increase the complexity of the project.
First some resources, a complete understanding is not really necessary but if you get some of the more challenging devices you might need a detailed understanding of how serial communication works in order to troubleshoot properly. Here’s a short list below:
On to the programming, before proceeding, you might want to have a quick look at the MSDN
pages for Serial Port class
Below is a very brief implementation of the serial port. There’s more to come however this will get you started. There’s a very important note on this implementation, there must always be a terminator or a set byte length of the data. By set byte length I meant that each block of data that you’ll need to process is x long. So instead of a terminator you use track how many bytes are read to know when you’re received all the data.
Next time – Serial Port Programming Part 2 – Developing a GUI to set the Port Settings and validating the data.
You can download the code below at http://code.msdn.microsoft.com/SerialPort-brief-Example-ac0d5004.
1: using System;
2: using System.IO.Ports;
3: using System.Text;
5: namespace SerialPortExample