Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import SerialPort from 'serialport';
class Sign {
static hexEncode(string) {
const result = [];
for (let i = 0; i < string.length; i++) {
const hex = string.charCodeAt(i).toString(16);
result.push(('00x' + hex).slice(-4));
}
return result;
}
constructor() {
const tty = process.env.NODE_COMM || 'COM1';
this.serialPort = new SerialPort(tty, {
baudRate: 2400,
dataBits: 7,
stopBits: 2,
parity: 'even',
rtscts: false,
xon: false,
xoff: false,
xany: false,
});
this.modes = {
rotate: 0x61,
hold: 0x62,
flash: 0x63,
};
this.transmissionHeader = [0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x5a, 0x30, 0x30, 0x02, 0x41, 0x41];
//this.transmissionHeaderModes = [0x1B, 0x20, 0x61];
this.trasmissionEnd = [0x04];
console.log('serial port is set up');
}
writeMessage(message, mode) {
console.log(message, mode);
const text = Sign.hexEncode(message);
const transmissionHeaderModes = [0x1B, 0x20, this.modes[mode]];
const buffer = new Buffer.from(this.transmissionHeader.concat(transmissionHeaderModes, text, this.trasmissionEnd));
this.serialPort.write(buffer);
return true;
}
}
export default Sign;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.