Created
March 28, 2020 04:56
-
-
Save dot1q/2169a21aefab73084d0acd20f8858129 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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