SerialPort

Read/Write to/from serial port, physically attached to host computer.

class SerialPort {
        constructor SerialPort('a name');
        bool open( String filename );
        bool close();
        bool hasData();
        bool writePort( string );
        bool writeBytes( Array );
        string readPort();

        string getLastErrorMsg();
}

SerialPort

var p = new SerialPort('ACM0');

.open

.open( String name [, Speed])
open 'name' port at speed 'speed', allowed values : ouvre le port 'name' à la vitesse 'speed', valeurs possibles

  • not specified : vitesse par défaut du périphérique
  • or one of these values : 1200,1800,2400,4800,9600,38400,115200,230400,460800

.close

Close serial port of an object SerialPort

.close();

.hasData

.hasData();

.writePort

Send the given string to the serial port

.writePort( string );

.writeBytes

Send the given array to the serial port

.writeBytes( Array );

example :

var serial = new SerialPort('comm');
if( serial.open( '/dev/ttyUSB0', 9600) == false ) {
    print('cannot open serial communication');
    exit(0);
}

var data = new Array();
datas.push( 0x00 );
datas.push( 0x01 );

serial.writeBytes( datas );

.readPort

Retrieve serial buffer into text object

.readPort();

.readBytes

Retrieve serial buffer into Uint8Array object

.readBytes();

.readInt16

Retrieve serial buffer into Uint16Array object

.readInt16();

.getLastErrorMsg

getLastErrorMsg();
var p = new SerialPort('toto');
    if( p.open('/dev/ttyUSB0', 9600 ) != true ) {
        print('cannot open port');
        exit();
    }
    p.writePort('Hello world');
    if( p.hasData() ) {
        var rep = p.readPort();
        print('reply:' + rep );
        var raw = p.readBytes();
        for( var i=0 ; i < raw.length, i++ ) {
             print(raw[i]);
        }
    }
    p.close();

Last update: July 6, 2023