darwin.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.DarwinPortBinding = exports.DarwinBinding = void 0;
  7. const debug_1 = __importDefault(require("debug"));
  8. const load_bindings_1 = require("./load-bindings");
  9. const poller_1 = require("./poller");
  10. const unix_read_1 = require("./unix-read");
  11. const unix_write_1 = require("./unix-write");
  12. const debug = (0, debug_1.default)('serialport/bindings-cpp');
  13. exports.DarwinBinding = {
  14. list() {
  15. debug('list');
  16. return (0, load_bindings_1.asyncList)();
  17. },
  18. async open(options) {
  19. if (!options || typeof options !== 'object' || Array.isArray(options)) {
  20. throw new TypeError('"options" is not an object');
  21. }
  22. if (!options.path) {
  23. throw new TypeError('"path" is not a valid port');
  24. }
  25. if (!options.baudRate) {
  26. throw new TypeError('"baudRate" is not a valid baudRate');
  27. }
  28. debug('open');
  29. const openOptions = Object.assign({ vmin: 1, vtime: 0, dataBits: 8, lock: true, stopBits: 1, parity: 'none', rtscts: false, xon: false, xoff: false, xany: false, hupcl: true }, options);
  30. const fd = await (0, load_bindings_1.asyncOpen)(openOptions.path, openOptions);
  31. return new DarwinPortBinding(fd, openOptions);
  32. },
  33. };
  34. /**
  35. * The Darwin binding layer for OSX
  36. */
  37. class DarwinPortBinding {
  38. constructor(fd, options) {
  39. this.fd = fd;
  40. this.openOptions = options;
  41. this.poller = new poller_1.Poller(fd);
  42. this.writeOperation = null;
  43. }
  44. get isOpen() {
  45. return this.fd !== null;
  46. }
  47. async close() {
  48. debug('close');
  49. if (!this.isOpen) {
  50. throw new Error('Port is not open');
  51. }
  52. const fd = this.fd;
  53. this.poller.stop();
  54. this.poller.destroy();
  55. this.fd = null;
  56. await (0, load_bindings_1.asyncClose)(fd);
  57. }
  58. async read(buffer, offset, length) {
  59. if (!Buffer.isBuffer(buffer)) {
  60. throw new TypeError('"buffer" is not a Buffer');
  61. }
  62. if (typeof offset !== 'number' || isNaN(offset)) {
  63. throw new TypeError(`"offset" is not an integer got "${isNaN(offset) ? 'NaN' : typeof offset}"`);
  64. }
  65. if (typeof length !== 'number' || isNaN(length)) {
  66. throw new TypeError(`"length" is not an integer got "${isNaN(length) ? 'NaN' : typeof length}"`);
  67. }
  68. debug('read');
  69. if (buffer.length < offset + length) {
  70. throw new Error('buffer is too small');
  71. }
  72. if (!this.isOpen) {
  73. throw new Error('Port is not open');
  74. }
  75. return (0, unix_read_1.unixRead)({ binding: this, buffer, offset, length });
  76. }
  77. async write(buffer) {
  78. if (!Buffer.isBuffer(buffer)) {
  79. throw new TypeError('"buffer" is not a Buffer');
  80. }
  81. debug('write', buffer.length, 'bytes');
  82. if (!this.isOpen) {
  83. debug('write', 'error port is not open');
  84. throw new Error('Port is not open');
  85. }
  86. this.writeOperation = (async () => {
  87. if (buffer.length === 0) {
  88. return;
  89. }
  90. await (0, unix_write_1.unixWrite)({ binding: this, buffer });
  91. this.writeOperation = null;
  92. })();
  93. return this.writeOperation;
  94. }
  95. async update(options) {
  96. if (!options || typeof options !== 'object' || Array.isArray(options)) {
  97. throw TypeError('"options" is not an object');
  98. }
  99. if (typeof options.baudRate !== 'number') {
  100. throw new TypeError('"options.baudRate" is not a number');
  101. }
  102. debug('update');
  103. if (!this.isOpen) {
  104. throw new Error('Port is not open');
  105. }
  106. await (0, load_bindings_1.asyncUpdate)(this.fd, options);
  107. }
  108. async set(options) {
  109. if (!options || typeof options !== 'object' || Array.isArray(options)) {
  110. throw new TypeError('"options" is not an object');
  111. }
  112. debug('set', options);
  113. if (!this.isOpen) {
  114. throw new Error('Port is not open');
  115. }
  116. await (0, load_bindings_1.asyncSet)(this.fd, options);
  117. }
  118. async get() {
  119. debug('get');
  120. if (!this.isOpen) {
  121. throw new Error('Port is not open');
  122. }
  123. return (0, load_bindings_1.asyncGet)(this.fd);
  124. }
  125. async getBaudRate() {
  126. debug('getBaudRate');
  127. if (!this.isOpen) {
  128. throw new Error('Port is not open');
  129. }
  130. throw new Error('getBaudRate is not implemented on darwin');
  131. }
  132. async flush() {
  133. debug('flush');
  134. if (!this.isOpen) {
  135. throw new Error('Port is not open');
  136. }
  137. await (0, load_bindings_1.asyncFlush)(this.fd);
  138. }
  139. async drain() {
  140. debug('drain');
  141. if (!this.isOpen) {
  142. throw new Error('Port is not open');
  143. }
  144. await this.writeOperation;
  145. await (0, load_bindings_1.asyncDrain)(this.fd);
  146. }
  147. }
  148. exports.DarwinPortBinding = DarwinPortBinding;