Note that there are some explanatory texts on larger screens.

plurals
  1. PODuplex communication with a file representing serial port (com) in node.js
    primarykey
    data
    text
    <p>I have a gadget connected via USB and showing itself on a COM port when connected (on Windows). I can access the port like this and write and read in sequence:</p> <pre><code>var fs = require('fs'); fs.open('\\\\.\\COM3', 'w+', function (error, fd) { var data = new Buffer('some command\r\n', 'ascii'); fs.write(fd, data, 0, data.length, null, function (error) { var data = new Buffer(1); fs.read(fd, data, 0, 1, null, function (error) { // process char from data[0] and check whether we should read more } } } </code></pre> <p>The problem is, however, that when I want to read more bytes than available, the fs.read is pending and fs.write does not work.</p> <p>In addition, I do not know whether there are any data waiting when the device is connected and I would like to read them before sending any commands (otherwise I do not know whether the data read is a response or just unread data before sending a command).</p> <p><strong>Solution candidate #1:</strong> Find a way to detect whether there are data available for reading. That would also mean a very complicated logic of retries and polling using timeouts... I have not seen such function for a file descriptor in node anyway.</p> <p><strong>Solution candidate #2:</strong> Use a read function with a timeout and do not write while one is pending until it has timed out. Or at least cancel reading when proceeding with write...</p> <p><strong>Solution candidate #3:</strong> Use the new stream API, especially the Duplex stream... however I am unable to do so for a file (or a file descriptor)</p> <p>I know that native functions do have these features, so I may have missed some "hidden" API, or it had not been exposed in node.js yet...</p> <p>There are some solutions for serial port communication that use native extensions to node. I cannot use those for various reasons. Moreover, what I have in plain node mostly works...</p> <p>The question is, of course, is what I am trying to do even possible in the current node.js version and if, what would be the best way.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload