-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Sequential writing/reading #1758
Comments
@JBtje To help clean up this code, you can also use the Serialport bindings as a separate package and avoid streams altogether. https://www.npmjs.com/package/@serialport/bindings You can see the list of methods available to the bindings via the This will also eliminate the internal |
Yeah the bindings is the way to use promises right now, it's pretty full featured. they're the lower level underneath the If you need to work with streams I do recommend this BlueStream method https://www.npmjs.com/package/bluestream#readasync for reading some information off of a stream. |
Thank you both for the reply. Before I ended with the above (hack), I examined your code and thought that the @serialport/bindings was the way to go. I started with a very simple (i thought) method: copy But "moving" the files was enough reason for Babel to complain with exceptions about not being able to import the module, and many hours later I gave up :( So, for my understanding: I can make a simple wrapper around @serialport/bindings, which implements the methods of https://serialport.io/docs/en/api-binding-abstract ? Regarding "so you can set the value returned by bindings.read to a variable", could you give me a hint? function read( bytesToRead ) {
...
return this.binding.read( pool, start, toRead ).then(
bytesRead => {
if( bytesRead === 0 ) {
return;
}
this.package = pool.slice( start, start + bytesRead ) );
},
err => {
...
},
);
} My problem with this is, that bytesRead appears out of nowhere. I assume that read(buffer, offset, length) {
return super
.read(buffer, offset, length)
.then(() => promisify(binding.read)(this.fd, buffer, offset, length))
.catch(err => {
if (!this.isOpen) {
err.canceled = true
}
throw err
})
} but its a bit magical to me where I now see I still have |
Here is an example of using the const Binding = require('@serialport/bindings');
const comName = 'COM1';
const openOptions = {
baudRate: 115200,
dataBits: 8,
stopBits: 1,
parity: 'none',
};
const binding = new Binding();
// allows using await
(async () => {
await binding.open(comName, openOptions);
await binding.write(Buffer.from( 'AA000002', 'hex' ));
// buffer to read header info into
const header = Buffer.alloc(2);
const bytesRead = await binding.read(header, 0, 2);
console.log( 'Header: ', header );
const L = header[1] - 1;
// buffer to read package data into
const package = Buffer.alloc(L);
await binding.read(package, 0, L);
console.log( 'Package: ', package );
await binding.close();
})(); The
It returns the number of I hope this helps clear up how to use the |
in one word: amazing its even less code then I expected, and I now understand the bytesRead part: thank you! I would vote to add that code to the docs! |
Happy to help!
I'll add it to my list for this weekend. 😄 |
Perhaps nice to add with the above doc, is that the user needs to run: If not, you'll see the exception: And when using Electron, chance is that you'll need to run Note: Edit Binding.list().then(
ports => ports.forEach( console.log ),
err => console.error( err ),
); |
@JBtje Good notes. For Electron, we have instructions about |
💥 Proposal
Update documentation with below sample code
What feature you'd like to see
Basically #1679
Motivation
tldr; It took me way to long to figure out the answer to question #811
I've worked with serialport in C#, but had to port it to a Mac system, and thought it would be easiest to just use Electron and node-serialport. However, it took me way to long to figure out how it works, and how to make it work with sequential writing/reading, even though I think it is a very common use.
Write -> read -> wait for response -> write -> read -> wait for response.
Even though the wrapping code in stream.js uses Promises, it does not return promises, wich I think is too bad...
After many tries, and lots of babel errors, I went with just overwriting the methods, to make sure they DO return promises.
In short: It imports serialport the way you normally do, then it overwrites the essential methods (open / _write / _read) and makes sure they return a Promise.
./stream2.js
main.js
No guarantee it works, it is a cleaned up code dump (not tested afterwards). I hope I can help someone with it.
The text was updated successfully, but these errors were encountered: