Community Pick: Many members of our community have endorsed this article.

Serial Port Programming Language

Published:

Introduction

Although it is an old technology, serial ports are still being used by many hardware manufacturers.

If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to work on many clinical instruments as a job requirement to parse, extract patient test results and send them to a database backend for reporting. This means there are many serial ports attached to the computer.  I developed different C# applications for each instrument. However, whenever I needed to make any changes, I had to open source code of the application, compile and deploy over and over again. It was very time consuming and a headache.

This is why I developed a serial port programming language:  To reduce development time.  I have made the tool available as project on SourceForge.  In this article, I'll discuss some of its features and give you an idea of how to use it.


Application Features

It is a handy, simple application specially designed to simplify the development of RS232 based applications. It makes it easy not only to communicate with the serial port but also data parsing and extraction.

It requires .NET Framework 3.5 or above and supports all major Windows platforms including Windows XP, Windows Vista and Windows 7.

Key Features

Able to log multiple serial ports at the same time. The data logger has the capability to log multiple ports simultaneously so that multiple external serial devices can be logged.
Integrated Development Environment (IDE). You do NOT have to install any other development tools.
Built-in debugger makes it easy to find and fix errors. It saves a lot of time.
Regular expression support for easy data extraction.


Getting Started

Application has two main windows.

1- Main Application Window
Starts/stops the application specific driver to communicate with the serial port and configures the driver.
main.png
The Driver works similar to a state machine. Every state runs continually until it is jumped to another state or the driver stops.
2- IDE
The application has over 50 different basic commands which make it easy not only to communicate with the serial port but also to perform data parsing and extraction. These commands are written in this editor and compiled into a binary file.
ide-main.png
Every driver must have an initialization state called "Init". For example;
state Init
                        // commands here
                      end state

Open in new window

ASCII Control characters (aka non-printing characters, 0x00-0x1F and 0x7F ) are predefined in the core module as below.
Char  Hex   Meaning
                      ====  ====  =======
                      NUL   0x00  Ctrl-@ | NULL
                      SOH   0x01  Ctrl-A | START OF HEADING
                      STX   0x02  Ctrl-B | START OF TEXT
                      ETX   0x03  Ctrl-C | END OF TEXT
                      EOT   0x04  Ctrl-D | END OF TRANSMISSION
                      ENQ   0x05  Ctrl-E | ENQUIRY
                      ACK   0x06  Ctrl-F | ACKNOWLEDGE
                      BEL   0x07  Ctrl-G | BELL
                      BS    0x08  Ctrl-H | BACKSPACE
                      HT    0x09  Ctrl-I | HORIZONTAL TABULATION
                      LF    0x0A  Ctrl-J | LINE FEED
                      VT    0x0B  Ctrl-K | VERTICAL TABULATION
                      FF    0x0C  Ctrl-L | FORM FEED
                      CR    0x0D  Ctrl-M | CARRIAGE RETURN
                      SO    0x0E  Ctrl-N | SHIFT OUT
                      SI    0x0F  Ctrl-O | SHIFT IN
                      DLE   0x10  Ctrl-P | DATA LINK ESCAPE
                      DC1   0x11  Ctrl-Q | DEVICE CONTROL 1
                      DC2   0x12  Ctrl-R | DEVICE CONTROL 2
                      DC3   0x13  Ctrl-S | DEVICE CONTROL 3
                      DC4   0x14  Ctrl-T | DEVICE CONTROL 4
                      NAK   0x15  Ctrl-U | NEGATIVE ACKNOWLEDGE
                      SYN   0x16  Ctrl-V | SYNCHRONOUS IDLE
                      ETB   0x17  Ctrl-W | END OF TRANSMISSION BLOCK
                      CAN   0x18  Ctrl-X | CANCEL
                      EM    0x19  Ctrl-Y | END OF MEDIUM
                      SUB   0x1A  Ctrl-Z | SUBSTITUTE
                      ESC   0x1B  Ctrl-[ | ESCAPE
                      FS    0x1C  Ctrl-\ | FILE SEPARATOR
                      GS    0x1D  Ctrl-] | GROUP SEPARATOR
                      RS    0x1E  Ctrl-^ | RECORD SEPARATOR
                      US    0x1F  Ctrl-_ | UNIT SEPARATOR
                      DEL   0x7F  Ctrl-? | DELETE

Open in new window


Example - 1

Two serial port commands ( recv and send ) will allow you to interact with the serial port.

For example; if you want to send an ACK to the serial port;
state Init
                        send("<ACK>");
                      end state

Open in new window

NOTE: Control characters must be enclosed with prefix "<" and suffix ">"


Example - 2

Since bytes may come in at any time, buffering incoming data is critical. To solve this;
   1. Buffer the incoming data.
   2. Scan your buffer to find complete data.
   3. remove the used data from the buffer.

The application program has a solution in order to buffer the incoming data.
state Init
                        // define a global variable
                        our $BUFFER = "";
                        jump(Receive);
                      end state
                      
                      state Receive
                        recv();
                        $len = length($DATA_PACKET);
                        if("$len > 0") {
                          // 1. buffer the incoming data
                          $BUFFER += $DATA_PACKET;
                          call(Parser);
                        }
                      end state
                      
                      state Parser
                        // 2. scan your buffer to find complete data
                        // command "match" will check if buffer matches a regular expression
                        if(match($BUFFER, "(?<WILLDELETE>.*?<STX>(?<DATA>.*?)<ETX>(?<CHECKSUM>[0-9A-F]{2}))")) {
                          // Received complete data
                          // 3. remove the used data from the buffer
                          $lenData = length($WILLDELETE);
                          $BUFFER = remove($BUFFER, 0, $lenData);
                      
                          // Do operations with the other parsed fields. $DATA and $CHECKSUM in this example.
                        }
                      end state

Open in new window

The application provides many, many options and abilities to help you work with serial ports.  It supports a rich programming language, including variables, expression evaluation, debugging, ... you name it.    It supports over 50 useful commands -- too many to describe in this article.

For more information, please visit the project homepage or the SourceForge project page.   The User Guide documentation is here.
 
3
8,238 Views

Comments (2)

CERTIFIED EXPERT
Author of the Year 2009

Commented:
This looks like an excellent tool.  You got my YES vote, above!

One of the very first projects I worked on (back in about 1980), was an interpretive language for use in controlling a modem during the log-in process when connecting to remote host systems (such as CompuServe).  It is VERY useful to be able to just tweak a script and retry -- much better than re-coding!

Author

Commented:
I got the first YES vote from you. That is great.

Yes, I hate re-compile big projects over and over again.That is why I developed this application.
by the way I will appreciate, if you can change the link.

Thank you again.

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.