Serial port Communication

Baud rate:

Some products (use USB/serial converter chips): 9600bps, 1b stop bit, no parity check
Other products (use USB-CDC protocol): no limitation (always transfer at max USB speed)

Command EOL (End Of Line) char:

CR+LF, in hex format 0x0D+0x0A, in C language \r\n

Special char for console:

Ctrl-C Reset: single binary char 0x03, in C language \x03
in most cases, send this char will stop current running command or reset the console
and new prompt '=>' will be printed

Debug tool (Linux):

miniterm.py is included in pyserial library, install with pip
no extra setting for TX/RX EOL char

a tiny interactive serial debugger is provided in mcush_util shell mode


Debug tool (Windows):

Tera Term https://ttssh2.osdn.jp/
notice to set receive/transmit new line EOL char:


Port number and priority under Ubuntu system:

Ports are often enumerated as /dev/ttyUSBxxx or /dev/ttyACMxxx with increasing ID.
To avoid the hopping id, you can retrieve the device by device VID/PID:
/dev/serial/by-id/usb-???????????
Or by bus path:
/dev/serial/by-path/pci-???????????
Normal user needs to add to dialout group for device priority (modify /etc/group).

Send serial port command in Windows/Cmd.exe:

eg: echo a -c 0xff00ff > \\.\comXX  (XX is the enumerated serial port number, check it in Device Manager)
The command above will send the raw command "a -c 0xff00ff" to a specific port.
You can put the command into .bat script for automation.
As no feedback is checked, this is only suitable for simple commands.

Send serial command with console tool mcush_util:

sending serial commands directly in console may fail because of:
  • baudrate is not correctly set before
  • some chars has been buffered in the console, new inputs are concatenated as invalid command
  • last commands lasts too long and is still not finished
  • intactive command is executing, current inputs mis-understood as sub commands
    you can use console program mcush_util to handle these conditions (support Win/Linux). eg:
    $ mcush_util -p /dev/ttyUSB0 run "cat /r/readme"
    VAP200,1.0
    30CA01A034678900
    Vibration Analysis Platform
    8 synchronous channels with external trigger
    sample rate up to 204ksps
    Shanghai Linkong Software Technologies Co., Ltd. 2021
    www.xrsoft.com/vap/ 
    this command opens specific port, send Ctrl-C to reset console and get prompt,
    check *idn?, and run following multiple commands (with arguments)
    download: Win(32bit) mcush_util.exe, Linux(64bit) mcush_util
    compile for other platform from source codes (provide CodeBlocks project, or Makefile/GCC that supports cross-compile)

    Send command with DLL in LabVIEW:

    call DLLmcush_util.dll mcush_command function to send command to deivce,
    only port and command (with arguments) are required



  • C Programming

    C Programming API example mcush_api.c mcush_api.h
    Call example
    
    #include < stdio.h >
    #include "mcush_api.h"
    
    int main( int argc, char *argv[] )
    {
        mcush_dev_t dev;
    
        if( mcush_open( &dev, "/dev/ttyUSB0", 9600, 5 ) <= 0 )
        {
            printf("fail to open port\n");
            return 0;
        }
    
        if( mcush_connect( &dev ) <= 0 )
        {
            printf("fail to connect the device\n");
            return 0;
        }
    
        mcush_write_command( &dev, "*idn?" );
        printf( "%s", dev.result );
    
        mcush_write_command( &dev, "cat /r/readme" );
        printf( "%s", dev.result );
    
        mcush_close( &dev );
        return 0;
    }
    
    ===========RESULT (tested in Ubuntu/GCC)==================
    ShellLab-L2,1.0
    35A003129595680400324D4D
    WS2812 LED Strap controller based on Shell Lab
    http://www.xrsoft.com/shell-lab/
    Shanghai Linkong Software Technologies Co., Ltd. 2020
    
    

    C++ Programming API example mcush_drv.cpp mcush_drv.h mcush_api.c mcush_api.h
    Call example
    
    #include < stdio.h >
    #include < unistd.h >
    #include "mcush_drv.h"
    
    
    int main( int argc, char *argv[] )
    {
        char buf[512];
        McushDrv *drv;
    
        drv = new McushDrv( "/dev/ttyUSB0", 9600, 5 );
        if( drv->err <= 0 )
        {
            printf("fail to open port\n");
            return 0;
        }
    
        if( drv->connect() <= 0 )
        {
            printf( "fail to connect the device\n" );
            return 0;
        }
    
        if( drv->scpiIdn( buf ) )
        {
            printf( "%s", buf );
            drv->scpiRst();
        }
    
        return 0;
    }
    
    
    ===========RESULT (tested in Ubuntu/GCC)==================
    ShellLab-L2,1.0
    35A003129595680400324D4D
    

    Python