Skip to content

Instantly share code, notes, and snippets.

@dot1q
Created March 28, 2020 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dot1q/2169a21aefab73084d0acd20f8858129 to your computer and use it in GitHub Desktop.
Save dot1q/2169a21aefab73084d0acd20f8858129 to your computer and use it in GitHub Desktop.
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