serial-nci-scale
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -69,5 +69,4 @@ "use strict"; | ||
parity: 'even' | ||
}; // USB default commands | ||
var Scp12Commands = { | ||
}; | ||
var commandChars = { | ||
CR: parseInt('0d', 16), | ||
@@ -79,8 +78,4 @@ // Carriage Return | ||
// Get Status | ||
Z: parseInt('5a', 16) // Zero | ||
}; | ||
var ResponseChars = { | ||
CR: parseInt('0d', 16), | ||
// Carriage Return | ||
Z: parseInt('5a', 16), | ||
// Zero | ||
LF: parseInt('0A', 16), | ||
@@ -92,2 +87,8 @@ // Line Feed | ||
}; // USB default commands | ||
var commands = { | ||
weight: new Uint8Array([commandChars.W, commandChars.CR]), | ||
status: new Uint8Array([commandChars.S, commandChars.CR]), | ||
zero: new Uint8Array([commandChars.Z, commandChars.CR]) | ||
}; | ||
@@ -99,4 +100,3 @@ var eventDefaults = { | ||
status: {} | ||
}; // Implements NCI (or H-100), (or 3825) protocol | ||
// No Handshake. | ||
}; // Implements NCI protocol (Scp-12 or H-100 or 3825/3835). No Handshake. | ||
@@ -149,6 +149,2 @@ var SerialNCIScale = /*#__PURE__*/function (_EventTarget) { | ||
_this2.readLoop(); | ||
}).catch(function (e) { | ||
console.error(e); | ||
_this2.disconnect(); | ||
})); | ||
@@ -186,3 +182,3 @@ | ||
this.isDisconnecting = true; // One last poll loop to unlock the reader before disconnecting | ||
this.isDisconnecting = true; // One last query to unlock the reader before disconnecting | ||
@@ -195,2 +191,4 @@ return _context2.abrupt("return", this.getWeight().then(function () { | ||
return _this3.port.close(); | ||
}).catch(function (e) { | ||
console.error(e); | ||
}).finally(function () { | ||
@@ -214,8 +212,8 @@ _this3.isDisconnecting = false; | ||
return disconnect; | ||
}() | ||
}() // Don't bother to disconnect gracefully | ||
}, { | ||
key: "readWriteError", | ||
value: function readWriteError(e) { | ||
console.error(e); // Don't bother to disconnect gracefully | ||
console.error(e); | ||
this.isConnected = false; | ||
@@ -235,3 +233,5 @@ this.isDisconnecting = false; // Try to close the port, but this will likely throw an error | ||
return _this4.writer.write(data).catch(function (e) { | ||
return _this4.readWriteError(e); | ||
_this4.readWriteError(e); | ||
throw e; | ||
}); | ||
@@ -270,5 +270,5 @@ }); | ||
value: function flushResponseBuffer() { | ||
var lfIndex = this.responseBuffer.indexOf(ResponseChars.LF); | ||
var crIndex = this.responseBuffer.indexOf(ResponseChars.CR); | ||
var etxIndex = this.responseBuffer.indexOf(ResponseChars.ETX); // discard anything preceeding <LF> and process again | ||
var lfIndex = this.responseBuffer.indexOf(commandChars.LF); | ||
var crIndex = this.responseBuffer.indexOf(commandChars.CR); | ||
var etxIndex = this.responseBuffer.indexOf(commandChars.ETX); // discard anything preceeding <LF> and process again | ||
@@ -282,3 +282,3 @@ if (lfIndex > 0) { | ||
// Unknown Command Response <LF>?<CR> | ||
if (crIndex === 2 && this.responseBuffer[1] === ResponseChars.Q) { | ||
if (crIndex === 2 && this.responseBuffer[1] === commandChars.Q) { | ||
console.warn('Unrecognized command received'); | ||
@@ -329,3 +329,3 @@ this.responseBuffer = this.responseBuffer.slice(crIndex + 1); | ||
return this.reader.read().then(function (_ref) { | ||
this.reader.read().then(function (_ref) { | ||
var value = _ref.value, | ||
@@ -356,3 +356,3 @@ done = _ref.done; | ||
if (_this6.isConnected && _this6.isPolling && !_this6.isDisconnecting) { | ||
_this6.getWeight(); | ||
_this6.send(commands.weight); | ||
} else { | ||
@@ -372,42 +372,31 @@ clearInterval(poll); | ||
}, { | ||
key: "getWeight", | ||
value: function getWeight() { | ||
key: "sendAndGetResponse", | ||
value: function sendAndGetResponse(type) { | ||
var _this7 = this; | ||
return new Promise(function (resolve) { | ||
var W = Scp12Commands.W, | ||
CR = Scp12Commands.CR; | ||
return new Promise(function (resolve, reject) { | ||
var onResponse = function onResponse(e) { | ||
_this7.removeEventListener(type, onResponse); | ||
var onWeight = function onWeight(e) { | ||
_this7.removeEventListener('weight', onWeight); | ||
resolve(e.detail); | ||
}; | ||
_this7.addEventListener('weight', onWeight); | ||
_this7.addEventListener(type, onResponse); | ||
_this7.send(new Uint8Array([W, CR])); | ||
_this7.send(commands[type]).catch(function (e) { | ||
_this7.removeEventListener(type, onResponse); | ||
reject(e); | ||
}); | ||
}); | ||
} | ||
}, { | ||
key: "getWeight", | ||
value: function getWeight() { | ||
return this.sendAndGetResponse('weight'); | ||
} | ||
}, { | ||
key: "getStatus", | ||
value: function getStatus() { | ||
var _this8 = this; | ||
return new Promise(function (resolve) { | ||
var S = Scp12Commands.S, | ||
CR = Scp12Commands.CR; | ||
var onStatus = function onStatus(_ref2) { | ||
var detail = _ref2.detail; | ||
_this8.removeEventListener('status', onStatus); | ||
resolve(detail); | ||
}; | ||
_this8.addEventListener('status', onStatus); | ||
_this8.send(new Uint8Array([S, CR])); | ||
}); | ||
return this.sendAndGetResponse('status'); | ||
} | ||
@@ -417,5 +406,3 @@ }, { | ||
value: function zero() { | ||
var Z = Scp12Commands.Z, | ||
CR = Scp12Commands.CR; | ||
return this.send(new Uint8Array([Z, CR])); | ||
return this.send(commands.zero); | ||
} | ||
@@ -422,0 +409,0 @@ }]); |
{ | ||
"name": "serial-nci-scale", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Serial interface for Salter Brecknell PS-USB scales using NCI protocol and SCP-12 layout", | ||
@@ -10,2 +10,3 @@ "main": "dist/serial-nci-scale.js", | ||
"scripts": { | ||
"build": "npm run build-dist && npm run build-example", | ||
"build-dist": "npx babel --no-babelrc src/serial-nci-scale.js --out-file dist/serial-nci-scale.js --presets=@babel/preset-env", | ||
@@ -12,0 +13,0 @@ "start": "node scripts/start.js", |
24681
335