@cutos/drivers
v3.3.1
Published
CUTOS drivers is used to simulate the hardware behavior of the serial port, including receiving and sending data, control signals, etc.
Downloads
169
Readme
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
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()