Serial Port Programming Language

AID: 4023
  • Status: Published

3310 points

  • Byalbay
  • TypeResource
  • Posted on2010-10-29 at 11:44:56
Awards
  • Community Pick

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
  • 42 KB
  • Main application window
Main application window

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
  • 48 KB
  • Editor
Editor

Every driver must have an initialization state called "Init". For example;

state Init
  // commands here
end state
                                    
1:
2:
3:

Select allOpen 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
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:

Select allOpen 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
                                    
1:
2:
3:

Select allOpen 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
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:

Select allOpen 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.
 
    Asked On
    2010-10-29 at 11:44:56ID4023
    Tags

    Serial port

    ,

    programming language

    ,

    RS232

    ,

    SerialPort

    Topic

    C# Programming Language

    Views
    2075

    Comments

    Expert Comment

    by: DanRollins on 2010-10-31 at 16:23:27ID: 20981

    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 Comment

    by: albay on 2010-10-31 at 16:29:06ID: 20983

    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.

    Add your Comment

    Please Sign up or Log in to comment on this article.

    Join Experts Exchange Today

    Gain Access to all our Tech Resources

    Get personalized answers

    Ask unlimited questions

    Access Proven Solutions

    Search 3.2 million solutions

    Read In-Depth How-To Guides

    1000+ articles, demos, & tips

    Watch Step by Step Tutorials

    Learn direct from top tech pros

    And Much More!

    Your complete tech resource

    See Plans and Pricing

    30-day free trial. Register in 60 seconds.

    Loading Advertisement...

    Top C# Experts

    1. kaufmed

      566,376

      Sage

      500 points yesterday

      Profile
      Rank: Genius
    2. BuggyCoder

      240,764

      Guru

      10 points yesterday

      Profile
      Rank: Sage
    3. navneethegde

      158,560

      Guru

      0 points yesterday

      Profile
      Rank: Wizard
    4. CodeCruiser

      140,708

      Master

      2,000 points yesterday

      Profile
      Rank: Genius
    5. TheLearnedOne

      137,350

      Master

      2,800 points yesterday

      Profile
      Rank: Savant
    6. wdosanjos

      124,511

      Master

      3,500 points yesterday

      Profile
      Rank: Genius
    7. AndyAinscow

      107,357

      Master

      0 points yesterday

      Profile
      Rank: Genius
    8. Dhaest

      98,335

      Master

      0 points yesterday

      Profile
      Rank: Genius
    9. Idle_Mind

      91,914

      Master

      0 points yesterday

      Profile
      Rank: Savant
    10. tommyBoy

      90,068

      Master

      0 points yesterday

      Profile
      Rank: Genius
    11. nishantcomp2512

      89,000

      Master

      0 points yesterday

      Profile
      Rank: Wizard
    12. MlandaT

      86,553

      Master

      0 points yesterday

      Profile
      Rank: Genius
    13. Chinmay_Patel

      80,818

      Master

      0 points yesterday

      Profile
      Rank: Genius
    14. ddayx10

      66,538

      Master

      0 points yesterday

      Profile
      Rank: Sage
    15. anarki_jimbel

      66,132

      Master

      2,000 points yesterday

      Profile
      Rank: Genius
    16. ambience

      63,764

      Master

      0 points yesterday

      Profile
      Rank: Sage
    17. ukerandi

      62,593

      Master

      1,000 points yesterday

      Profile
      Rank: Guru
    18. apeter

      60,772

      Master

      0 points yesterday

      Profile
      Rank: Sage
    19. JamesBurger

      60,305

      Master

      0 points yesterday

      Profile
      Rank: Sage
    20. sedgwick

      52,750

      Master

      1,600 points yesterday

      Profile
      Rank: Genius
    21. emoreau

      50,917

      Master

      0 points yesterday

      Profile
      Rank: Genius
    22. ged325

      50,311

      Master

      2,000 points yesterday

      Profile
      Rank: Genius
    23. anuradhay

      49,977

      2,500 points yesterday

      Profile
      Rank: Guru
    24. techChallenger1

      47,638

      0 points yesterday

      Profile
      Rank: Guru
    25. mas_oz2003

      43,540

      0 points yesterday

      Profile
      Rank: Genius

    Hall Of Fame