linux.js 5.2 KB

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