Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error With Disconnecting Ports #1465

Closed
flakjacket95 opened this issue Feb 5, 2018 · 8 comments
Closed

Error With Disconnecting Ports #1465

flakjacket95 opened this issue Feb 5, 2018 · 8 comments

Comments

@flakjacket95
Copy link

  • SerialPort Version: 6.0.5

  • NodeJS Version: 8.9.3

  • Operating System and Hardware Platform: MacOS High Sierra

  • Have you checked the right version of the api docs?: Yes

  • Are you having trouble installing and you checked the Installation Special Cases docs? Not an installing issue

  • Are you using Electron and have you checked the Electron Docs?: Yes

Summary of Problem

Connected serialports work fine in general when actually connected but, disconnection is not working properly.

Steps and Code to Reproduce the Issue

For me, using a basic electron app - connect to a serial port, communicate a bit (read and write a few bytes), and then unplug the USB cable connecting the device to the computer. Serialport is not detecting this, even though the associated tty file in /dev/ is removed.
I'm tried to detect disconnects using the error, disconnect, and disconnected events. I also attempt to read and write to the serialport and then check the isOpen value.

With the USB cable to the serial device unplugged, I receive no calls to the events above but, the error event is properly called if I provide improper arguments to a function, for example sp.write(1) so, the error handler is functioning properly.

A call to the write() function returns true.
A call to the read() function returns undefined, regardless of the length of bytes requested.
The isOpen property never changes from true.

A few months ago, I did not have this issue and disconnection worked properly, I have not attempted to determine where the change occurred yet.

@reconbot
Copy link
Member

reconbot commented Feb 5, 2018

There is no longer a disconnect event, but the close event should fire on disconnect with an error object. Could you provide sample code?

@flakjacket95
Copy link
Author

flakjacket95 commented Feb 8, 2018

I updated to the close event and I have the same issue. As an important note however, on Windows this does not cause an issue. The code below functions properly on Windows but not on Mac.

var sp;
var serialPort = require("serialport");
var sp = new serialPort(name,{
   baudRate: 115200
});
sp.on("error", function () {
  console.log("Error.");
});
sp.on("close", function () {
  console.log("Disconnected.");
});

Like I mentioned before, neither the close event or the error event are fired in this case, even when trying to write to the port after it's been unplugged from the USB port.

@reconbot
Copy link
Member

reconbot commented Feb 8, 2018 via email

@flakjacket95
Copy link
Author

My apologies, I removed that code from my post above to keep it smaller. Didn't think it'd be necessary. I can provide some example code if necessary but, my application does read and write to the serial port after connection. The close event is still not fired.

@ccurtis0
Copy link

I'm having the same problem (I think) under Ubuntu 18.04.
Node version 8.10.0
Serialport version 7.0.2

My serial port is a USB device plugged into a USB hub.
I execute:
./node_modules/serialport/bin/terminal.js -p /dev/ttyUSB0

Using an external loopback, I see the characters I type.
I then power off the hub.
Entries in /dev/ disappear but 'terminal.js' reports no error.
I can continue to type but no characters are received.
^C exits.

I didn't dig into the source for 'terminal.js', but my code likewise receives neither 'error' nor 'close' events when the USB device is disconnected.

It looks like this used to be detected, judging by this bug:
#1243

I'm wondering if the fix went too far and is masking the error condition.

@ccurtis0
Copy link

Please ignore my comment above. After investigating further I found:
a) terminal.js does not listen for the 'close' event
b) My code was not listening for 'data' events, as you mentioned was necessary.

I first noticed this when using version 4, so I upgraded to version 7. But before the upgrade I added some test code to force an error state, and that code didn't try to read. Version 7 on Linux appears to work as expected.

@reconbot
Copy link
Member

reconbot commented Sep 12, 2018 via email

@stale
Copy link

stale bot commented Nov 11, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in a week no further activity occurs. Feel free continue the discussion or ask for a never-stale label to keep it open. If this is a support issue, consider sharing it on stack overflow to get more eyes on your problem.

@stale stale bot added the stale-issue label Nov 11, 2018
@stale stale bot closed this as completed Nov 18, 2018
@lock lock bot locked as resolved and limited conversation to collaborators May 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

3 participants