Skip to content

Commit

Permalink
Merge pull request #4 from cfficaurzua/master
Browse files Browse the repository at this point in the history
added on close function and pretty print in the console data
  • Loading branch information
gdespirito authored Mar 31, 2021
2 parents 5d1e3dc + a5ec183 commit 63f49e9
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/pos.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ module.exports = class POS {
this.parser = this.port.pipe(new InterByteTimeout({ interval: 100 }))

this.parser.on("data", (data) => {

this.debug(`🤖 > ${data}`, data)

let prettyData = ''
data.forEach(char=>{prettyData += (32 <= char && char<126) ? String.fromCharCode(char) : `{0x${char.toString(16).padStart(2, '0')}}`}, '')
this.debug(`🤖 > ${prettyData}`, data)

// Primero, se recibe un ACK
if (this.itsAnACK(data)) {
Expand All @@ -125,6 +127,7 @@ module.exports = class POS {
resolve(true)
}).catch(async (e) => {
this.connected = false
this.waiting = false
this.currentPort = null
try {
await this.port.close();
Expand All @@ -135,14 +138,19 @@ module.exports = class POS {
})

})

this.port.on("close", ()=> {
this.debug("Port closed")
this.currentPort = null
this.waiting = false
this.connected = false
})
})
}

disconnect() {
return new Promise((resolve, reject) => {
this.port.close((error) => {
this.currentPort = null
this.connected = false
if (error) {
this.debug("Error closing port", error)
reject(error)
Expand Down Expand Up @@ -216,7 +224,10 @@ module.exports = class POS {

// Prepare the message
let buffer = Buffer.from(LRC.asStxEtx(payload))
this.debug(`💻 > `, buffer, " -> ", `${buffer}`)
let prettyData = ''
buffer.forEach(char=>{prettyData += (32 <= char && char<126) ? String.fromCharCode(char) : `{0x${char.toString(16).padStart(2, '0')}}`}, '')

this.debug(`💻 > `, buffer, " -> ", `${prettyData}`)

//Send the message
this.port.write(buffer, function(err) {
Expand Down

0 comments on commit 63f49e9

Please sign in to comment.