You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
One problem we have had in the past is that we close the port, and then delete the reference to the serial port object from the map. If the close fails, or isn't a complete close, then we have lost the ability to access that port object. Why the port doesn't close and release the object is the question.
But our solution has just been to /never/ close the port once it's open. So the logic is:
Check if the port object exists. If so, just use it. (we can use the SerialPort.update method to change baudrates, etc... ). If not, open the port and wait for the call back indicating it's open, or for the open event to fire.
When we are done, do NOT close the port, just flag it and ignore everything that comes in.
Also, note that the .on('data' callback has a parameter which IS the data, it just needs to be converted to a string. Do not use the .buffer attribute of the port object as is an internal variable.
constSerialPort=require('serialport')constport=newSerialPort('/dev/tty-usbserial1')port.on('open',()=>{console.log('Port Opened')})port.write('main screen turn on',err=>{if(err){returnconsole.log('Error: ',err.message)}console.log('message written')})port.on('data',data=>{/* get a buffer of data from the serial port */console.log(data.toString())})
DDE uses the NPM package "SerialPort" to communicate via serial devices. It is currently using version 8:
https://serialport.io/docs/8.x.x
Carefully reading and following the documentation for that object is critical. Especially:
https://serialport.io/docs/8.x.x/api-stream
One problem we have had in the past is that we close the port, and then delete the reference to the serial port object from the map. If the close fails, or isn't a complete close, then we have lost the ability to access that port object. Why the port doesn't close and release the object is the question.
It MAY be that this describes the issue:
https://stackoverflow.com/questions/46307803/nodejs-serialport-re-establish-connection-to-port-after-closed
see this microscopic bit of cryptic documentation:
https://serialport.io/docs/8.x.x/api-stream#serialportresume
Another possibility is that the issue comes from the "port" and the "stream" being two different things. The SerialPort library documents these separately:
https://serialport.io/docs/8.x.x/api-serialport
https://serialport.io/docs/8.x.x/api-stream
Only the stream API has a close method, but maybe that doesn't close the serial port?
But our solution has just been to /never/ close the port once it's open. So the logic is:
SerialPort.update
method to change baudrates, etc... ). If not, open the port and wait for the call back indicating it's open, or for the open event to fire.This comment on this issue (scroll down to the bottom) may be helpful:
HaddingtonDynamics/Dexter#60 (comment)
The text was updated successfully, but these errors were encountered: