win32.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.WindowsPortBinding = exports.WindowsBinding = void 0;
  7. const debug_1 = __importDefault(require("debug"));
  8. const _1 = require(".");
  9. const load_bindings_1 = require("./load-bindings");
  10. const win32_sn_parser_1 = require("./win32-sn-parser");
  11. const debug = (0, debug_1.default)('serialport/bindings-cpp');
  12. exports.WindowsBinding = {
  13. async list() {
  14. const ports = await (0, load_bindings_1.asyncList)();
  15. // Grab the serial number from the pnp id
  16. return ports.map(port => {
  17. if (port.pnpId && !port.serialNumber) {
  18. const serialNumber = (0, win32_sn_parser_1.serialNumParser)(port.pnpId);
  19. if (serialNumber) {
  20. return Object.assign(Object.assign({}, port), { serialNumber });
  21. }
  22. }
  23. return port;
  24. });
  25. },
  26. async open(options) {
  27. if (!options || typeof options !== 'object' || Array.isArray(options)) {
  28. throw new TypeError('"options" is not an object');
  29. }
  30. if (!options.path) {
  31. throw new TypeError('"path" is not a valid port');
  32. }
  33. if (!options.baudRate) {
  34. throw new TypeError('"baudRate" is not a valid baudRate');
  35. }
  36. debug('open');
  37. const openOptions = Object.assign({ dataBits: 8, lock: true, stopBits: 1, parity: 'none', rtscts: false, rtsMode: 'handshake', xon: false, xoff: false, xany: false, hupcl: true }, options);
  38. const fd = await (0, load_bindings_1.asyncOpen)(openOptions.path, openOptions);
  39. return new WindowsPortBinding(fd, openOptions);
  40. },
  41. };
  42. /**
  43. * The Windows binding layer
  44. */
  45. class WindowsPortBinding {
  46. constructor(fd, options) {
  47. this.fd = fd;
  48. this.openOptions = options;
  49. this.writeOperation = null;
  50. }
  51. get isOpen() {
  52. return this.fd !== null;
  53. }
  54. async close() {
  55. debug('close');
  56. if (!this.isOpen) {
  57. throw new Error('Port is not open');
  58. }
  59. const fd = this.fd;
  60. this.fd = null;
  61. await (0, load_bindings_1.asyncClose)(fd);
  62. }
  63. async read(buffer, offset, length) {
  64. if (!Buffer.isBuffer(buffer)) {
  65. throw new TypeError('"buffer" is not a Buffer');
  66. }
  67. if (typeof offset !== 'number' || isNaN(offset)) {
  68. throw new TypeError(`"offset" is not an integer got "${isNaN(offset) ? 'NaN' : typeof offset}"`);
  69. }
  70. if (typeof length !== 'number' || isNaN(length)) {
  71. throw new TypeError(`"length" is not an integer got "${isNaN(length) ? 'NaN' : typeof length}"`);
  72. }
  73. debug('read');
  74. if (buffer.length < offset + length) {
  75. throw new Error('buffer is too small');
  76. }
  77. if (!this.isOpen) {
  78. throw new Error('Port is not open');
  79. }
  80. try {
  81. const bytesRead = await (0, load_bindings_1.asyncRead)(this.fd, buffer, offset, length);
  82. return { bytesRead, buffer };
  83. }
  84. catch (err) {
  85. if (!this.isOpen) {
  86. throw new _1.BindingsError(err.message, { canceled: true });
  87. }
  88. throw err;
  89. }
  90. }
  91. async write(buffer) {
  92. if (!Buffer.isBuffer(buffer)) {
  93. throw new TypeError('"buffer" is not a Buffer');
  94. }
  95. debug('write', buffer.length, 'bytes');
  96. if (!this.isOpen) {
  97. debug('write', 'error port is not open');
  98. throw new Error('Port is not open');
  99. }
  100. this.writeOperation = (async () => {
  101. if (buffer.length === 0) {
  102. return;
  103. }
  104. await (0, load_bindings_1.asyncWrite)(this.fd, buffer);
  105. this.writeOperation = null;
  106. })();
  107. return this.writeOperation;
  108. }
  109. async update(options) {
  110. if (!options || typeof options !== 'object' || Array.isArray(options)) {
  111. throw TypeError('"options" is not an object');
  112. }
  113. if (typeof options.baudRate !== 'number') {
  114. throw new TypeError('"options.baudRate" is not a number');
  115. }
  116. debug('update');
  117. if (!this.isOpen) {
  118. throw new Error('Port is not open');
  119. }
  120. await (0, load_bindings_1.asyncUpdate)(this.fd, options);
  121. }
  122. async set(options) {
  123. if (!options || typeof options !== 'object' || Array.isArray(options)) {
  124. throw new TypeError('"options" is not an object');
  125. }
  126. debug('set', options);
  127. if (!this.isOpen) {
  128. throw new Error('Port is not open');
  129. }
  130. await (0, load_bindings_1.asyncSet)(this.fd, options);
  131. }
  132. async get() {
  133. debug('get');
  134. if (!this.isOpen) {
  135. throw new Error('Port is not open');
  136. }
  137. return (0, load_bindings_1.asyncGet)(this.fd);
  138. }
  139. async getBaudRate() {
  140. debug('getBaudRate');
  141. if (!this.isOpen) {
  142. throw new Error('Port is not open');
  143. }
  144. return (0, load_bindings_1.asyncGetBaudRate)(this.fd);
  145. }
  146. async flush() {
  147. debug('flush');
  148. if (!this.isOpen) {
  149. throw new Error('Port is not open');
  150. }
  151. await (0, load_bindings_1.asyncFlush)(this.fd);
  152. }
  153. async drain() {
  154. debug('drain');
  155. if (!this.isOpen) {
  156. throw new Error('Port is not open');
  157. }
  158. await this.writeOperation;
  159. await (0, load_bindings_1.asyncDrain)(this.fd);
  160. }
  161. }
  162. exports.WindowsPortBinding = WindowsPortBinding;