3.3.1 • Published 9 months ago

@cutos/drivers v3.3.1

Weekly downloads
-
License
-
Repository
-
Last release
9 months ago

Introduction

Developing with a serial port simulator is a convenient, safe, and efficient way to develop drivers. It does not require real devices and is easier to control the test environment. It is especially suitable for the early development stage and development that requires various test scenarios. It is used to simulate the hardware behavior of the serial port, including receiving and sending data, control signals, etc. The Serialport class is used to handle serial port communication. It uses the serial port path, baud rate, callback function, and simulator to create a new serial port object. The SerialPortSimulator class creates a new serial port simulator to simulate serial port behavior.

Table of Contents

  1. SerialPort

  2. SerialPortSimulator

SerialPort

SerialPort

constructor(path, baudRate, callback = null, simulator = null)
  • path: port path
  • baudRate: baud rate
  • callback: callback function
  • simulator: simulator instance
Example:

The driver creates a new serialport instance and returns the port result. It is often used in device connection related methods.

let serialport = new SerialPort('/dev/ttySS3', 19200, error => {
    if (error) {
        callback({status: false, msg: error.message})
    } else {
        callback({status: true, msg: params})
    }
}, simulator)

SerialPortSimulator

SerialPortSimulator

Constructor, create a serialPortSimulator instance

let serialPortSimulator = new DeviceNFCSimulator(name);
  • name: simulator name, determine whether to use the simulator by the name.
Example:

sdk creates a device

let device = new DeviceFingerprint('mock');

Driver development uses the simulator

if (name === 'mock') {
    this.simulator = new DeviceFingerprintSimulator()
}

serialPortSimulator.onOpen

The listening port is open and can be used to perform certain operations

serialPortSimulator.onOpen()

serialPortSimulator.emitData

Emit serialPortSimulator data

serialPortSimulator.emitData(data)
  • data: emit data
Example:
serialPortSimulator.emitData(Buffer.from([0xF5, 0x09, 0x00, 0x00, 0x00, 0x00, 0x09, 0xF5]))

serialPortSimulator.onEvent

Handle events

serialPortSimulator.onEvent(event, params, callback)
Example:
onEvent(event, params, callback)
{
    if (event === 'wake') {
        this.userID = params.id;
        this.emitData(Buffer.from([0xF5, 0x09, 0x00, 0x00, 0x00, 0x00, 0x09, 0xF5]))
        callback('success')
    } else {
        callback('unsupported event')
    }
} 

serialPortSimulator.getHelp

Get help

serialPortSimulator.getHelp()
3.3.1

9 months ago

3.3.0

9 months ago

3.2.4

10 months ago

3.2.3

11 months ago

3.2.2

11 months ago

3.2.1

11 months ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

2 years ago

0.0.1

2 years ago