@cutos/drivers v3.4.0
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
Serial port
Serial port: an extended interface using serial communication.
Serial port communication: a communication method in which the serial port sends and receives bytes bit by bit.
The Serialport class is used to handle serial port communication. It creates a new serial port object using the serial port path, baud rate, callback function, and simulator. It is used to simulate serial port communication data.
Installation
npm install @cutos/drivers
Introduce dependencies
const {SerialPort} = require("@cutos/drivers");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, which is often used in device connection related methods.
let serialport = new SerialPort('/dev/ttySS3', 19200, error => {
if (error) {
console.log(error)
}
}, simulator)Methods from the base class SerialPortStream
See @serialport/stream
- write
- on('data')
Native Lib
Local libraries, such as ID card reader devices, require calling local dll libraries.
Installation
npm install @cutos/drivers
Introduce dependencies
const {NativeLib} = require("@cutos/drivers");NativeLib
constructor(lib_path, simulator = null)- lib_path: shared library path
- simulator: device simulator
Example:
let lib = new NativeLib('ghcmio.dll');NativeLib.func
Find function
NativeLib.func(declaration);- declaration: function declaration
Reference KOFFI
Example:
this.lib.func('size_t __stdcall mio_open(char* psport, char* psexport, int32_t baud)') DeviceSimulator
DeviceSimulator is the base class for simulator development. Currently, there are two derived classes: SerialPortSimulator and NativeLibSimulator.
DeviceSimulator
Constructor
constructor(config, state)- config: configuration parameters
- state: initial state
DeviceSimulator.getInfo
Function that provides information to the simulator. Currently supported information includes: README.md and protocol.
Need to be rewritten in the derived class of the specific device simulator.
Return result {readme, doc}
Example:
getInfo()
{
let readme = fs.readFileSync(path.join(__dirname, 'README.md'), 'utf-8');
let doc = fs.readFileSync(path.join(__dirname, 'doc.pdf'), 'base64');
return {readme, doc}
}DeviceSimulator.onEmitEmptyCommand
When the emit command without parameters is used in the console, this function will be called to send default data to the driver.
Need to be overridden in the derived class of the specific device simulator.
SerialPortSimulator
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. SerialPortSimulator inherits from DeviceSimulator.
SerialPortSimulator
Constructor, create a serialPortSimulator instance
constructor(config, state = null)- config: configuration parameters
- state: initial state
SerialPortSimulator.onOpen
Port opening event.
If the derived class needs to handle the event after the port is opened, rewrite this function.
SerialPortSimulator.onReceiveData
Receive data from the driver, and the driver uses the SerialPort.write(data) method to send data.
Derived classes need to rewrite this function to handle their own protocols.
serialPortSimulator.onReceiveData(data)- data: received data, type is Buffer
SerialPortSimulator.emitData
Send data to the driver, parameter data type is Buffer. The driver receives data in the SerialPort.on('data',(data)=>{}) function.
serialPortSimulator.emitData(data)- data: emit data, type is Buffer
Example:
serialPortSimulator.emitData(Buffer.from([0xF5, 0x09, 0x00, 0x00, 0x00, 0x00, 0x09, 0xF5]))The following takes the physical examination body height device driver as an example to introduce the development of the simulator
| Name | Description | Download |
|---|---|---|
| device-height-weight-scale-v3.3.3 | Physical examination body height and weight device driver | Download |
simulator/device-height-weight-scale-simulator.js
const fs = require('node:fs');
const {Buffer} = require("node:buffer");
const path = require('node:path');
const config = require('../config.json');
const {SerialPortSimulator} = require('@cutos/drivers')
const {request, response} = require('../device-height-weight-scale-protocol')
class DeviceHeightWeightScaleSimulator extends SerialPortSimulator {
constructor() {
super(config, {height: 170, weight: 70});
this.responsing = false;
}
switchOn() {
if (this.timer) return
this.timer = setInterval(() => {
if (!this.responsing) {
let weight = +this.state.weight
this.emitData(response.generate_weight_data(weight))
}
}, 1000)
}
switchOff() {
clearInterval(this.timer)
}
onReceiveData(data) {
this.responsing = true
if (!Buffer.compare(request.GET_HEIGHT_WEIGHT, data)) {
let count = 0
let timer = setInterval(() => {
let height = +this.state.height
let weight = +this.state.weight
this.emitData(response.generate_height_weight_data(height, weight))
if (++count === 3) {
clearInterval(timer)
this.responsing = false
}
}, 1000)
}
if (!Buffer.compare(request.SWITCH_ON, data)) {
this.switchOn()
this.emitData(response.SWITCH_ON)
this.responsing = false
}
if (!Buffer.compare(request.SWITCH_OFF, data)) {
this.switchOff()
this.emitData(response.SWITCH_OFF)
this.responsing = false
}
}
getInfo() {
const readme = fs.readFileSync(path.join(__dirname, 'README.md'), 'utf8')
return {readme}
}
}
module.exports = {
DeviceHeightWeightScaleSimulator
}NativeLibSimulator
NativeLibSimulator inherits from DeviceSimulator and is used for simulators with local dependency libraries, such as simulators for ID card reader devices.
NativeLibSimulator
Constructor
constructor(config, state = null)- config: configuration parameters
- state: initial state
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()