Monday, 15 September 2014

Programming and Interfacing the Serial/UART/RS-232 port of a Computer/PC in Windows and Linux Part-1

From now on in this article i will be using the Serial/COM/UART words interchangeably so dear readers please cope with it. RS-232  is also the same just that it defines the same communication protocol with different voltage levels. Read more about it at RS-232 wiki.

After many futile attempts of trying to write a program in C/C++ to access the serial port of a computer especially in windows, I finally succeeded in writing a simple program in C to access the serial port of windows to send and receive data.

Now in this era of modern computers where high-speed lan, wifi and optical fibre like communications method exist some of you might think what is the use of serial port, also most of computers nowadays don't have a serial/com port any more.

But ask any electronic engineer or an embedded system designer or any networking engineer dealing with programming of modems, network switches etc or to my fellow hobbyists and electronic enthusiasts out there, a serial port is as important and crucial tool of their day to day requirement as you can say is a hammer is to a blacksmith.

I agree that serial ports have disappeared from modern PCs but to fill the void and especially suited for the needs of people like us are Usb to Serial converters available in the market from many vendors to list a few FTDI232RL/230x/231x  and Prolific PL2303 or Silicon labs CP2102 etc.There are many more also.

Now using them for sending and receiving data from a device having a UART is very easily accomplished in windows or linux or osx, by using the appropriate drivers for the device and using a console application like hyper-terminal or Termite in windows or Putty which is cross-platform. So that we can communicate with our device may be for programming or debugging or just for using as input/output console for example with microcontroller platforms like Arduino, AVR, ARM, PIC, 8051 etc.

But sometimes need arises to write a custom application so that you can make it a total standalone app fine tuning it according to your requirement. 

Like for example suppose i want a windows app to have a few buttons such that when i press a button a particular character is sent to the serial port; now i can send a character using any serial console apps described above, but suppose i need it for use by people of non-technical background to operate this. Then it would be a bad idea to think of training the end user for operating the product using a terminal app which would obviously be difficult for non-technical people instead we can just ask the end user to press a button.

But for that we will have to build a custom app which can interface with serial port. Many people have built this kind of stuff but there are rarely good articles on the web which can explain people with very less experience in windows coding where to begin from.

For people like me with not so much experience with windows programming it is a difficult feat to achieve especially the receiving data part(sending data is comparatively easier).

After doing a lot of search on the web and reading a few books i was able to send data using a program built in Visual C++/CLR dotnet. But still i was not able to receive data. I wrote this program for an embedded systems project i did "Bluetooth based home automation". In this project, by sending certain characters to the device using serial port i could turn on or off household electrical appliances connected to it.

But recently i figured it out how to write a simple ( totally basic command line) program to send and receive data from serial port. well this was a good start then building upon it, I decided to build a console like Windows app in C/C++( not .net) and i can say i somewhat succeeded as i was able to send and receive data using it. I built the GUI part using wxWidgets(had to mess around a lot first to get the wxWidgets to work at beginning) and used the same C code used in the console app to deal with the UART interfacing part. Well it has a few bugs but i consider it not bad for beginners like me.

Below you can find the complete code for the command line program. The program below uses WINAPI. You can compile it using any free version of Visual Studio or using mingW. You can use Codeblocks also it comes with a pre-configured version of mingW.

Most of the code is self explanatory but i will explain the important ones.

Here is the link to whole project you can download it and modify it if you like.

I think after going through the whole code thoroughly by now you must have understood the basics of sending and receiving data from the serial port in C using WINAPI.

So lets now proceed to make a more user-friendly app i.e. a GUI(Graphical user interface) program, which should be easy to use even by the non-programmers.

So using the same functions used above to send and receive data i made a GUI program GUI_SerialConsole_wx. I made the GUI part using wxWidgets and CodeBlocks IDE. 

Well Codeblocks i a great IDE and when combined with wxWidgets makes building the GUI part as easy as possible.

Here is a screen shot of my app 

Now if you want the program only and want to skip the hassle of searching for the dependencies then here it is Release.7z. It contains all necessary files related to the program.

However i have to warn you that this is test-only initial release and it has lot of bugs especially related to the receiving part. Because the program needs to actively keep polling the serial port buffer for any new characters received, So that it can be shown in the app window. 

But it requires multi-threaded programming or Inter process communication, with which i am not much familiar. 

So what i did was i used wx_timer to initiate a 1-Second timer and whenever the timer event was triggered i would display the characters in the lower Receive Data Box. Well this works but isn't a very clean solution to our problem.

So i welcome you to modify the code and solve the issue if you like.

For basic programming of serial port in linux please read the next part of this blog..
Unknown An Electronics and Tech Enthusiast

I am a Tech enthusiast who loves to build stuff like new gadgets and tinker around with electronic devices doing some DIY stuff, writing some code and in the process providing something back to the community.

No comments :

Post a Comment