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
}

results matching ""

    No results matching ""