<

Go Premium for a chance to win a PS4. Enter to Win

x

Serial Port Programming Language

Published on
12,911 Points
6,611 Views
3 Endorsements
Last Modified:

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
Comment
Author:albay
2 Comments
 
LVL 49

Expert Comment

by:DanRollins
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!
0
 
LVL 4

Author Comment

by:albay
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.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Join & Write a Comment

Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month