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

SerialPort cannot enter listening #116

Closed
HughWick opened this issue Sep 10, 2021 · 3 comments
Closed

SerialPort cannot enter listening #116

HughWick opened this issue Sep 10, 2021 · 3 comments

Comments

@HughWick
Copy link

I was able to correctly access the listen event method after opening the serial port using the following code

  var serialPort = new SerialPort("COM3"); 
  serialPort.openPort();
  serialPort.setParams(19200, DATA_BITS, STOP_BITS, PARITY);
  System.out.println("---->>" + serialPort);
  int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR;
  serialPort.setEventsMask(mask);
  serialPort.addEventListener(new PortListener(serialPort, textPane, actionBO));

However, when I close the serial port, then unplug the USB cable from the serial port and plug it back in, I can't access the listen events (the code above prints statements to see if it's a different serialPort).

 serialPort.closePort();

When I got desperate, I found I could open the serial port with a putty connection, receive the data, close the serial port, then come back and access the listening event again with the code above ......

public class PortListener implements SerialPortEventListener{
  @Override
    public void serialEvent(SerialPortEvent serialPortEvent) {
      System.out.println("=====Entering the listen====");
       .........
  }
}

PS:
As long as I don't unplug the USB I can access the listener events every time, but as soon as I unplug it once the above problem occurs, even if I restart the java service it doesn't recover, I have to connect the serial port once with putty and then the java will resume accessing the listener events.

What am I doing wrong?

@pietrygamat
Copy link
Collaborator

pietrygamat commented Sep 24, 2021

I am investigating potential relation of this to #107. Could you share the code that shows the use of serialPort inside new PortListener(serialPort, textPane, actionBO) when the problem is triggered?

I am also interested in the squencing of the events:

  • is the serialPort.closePort() always executed before unplugging the cable?
  • do you catch or ignore any exceptions when closing the port or when reopenning it after reconnecting the device?
  • do you call serialPort.open() on the same instance of serialPort or after re-attaching the adapter do you create a new SerialPort("COM3"); and add to it a new PortListener?

Also, are you on Windows and jssc 2.9.4?

@HughWick
Copy link
Author

I am investigating potential relation of this to #107. Could you share the code that shows the use of serialPort inside new PortListener(serialPort, textPane, actionBO) when the problem is triggered?

I am also interested in the squencing of the events:

  • is the serialPort.closePort() always executed before unplugging the cable?
  • do you catch or ignore any exceptions when closing the port or when reopenning it after reconnecting the device?
  • do you call serialPort.open() on the same instance of serialPort or after re-attaching the adapter do you create a new SerialPort("COM3"); and add to it a new PortListener?

Also, are you on Windows and jssc 2.9.4?

Sorry, I forgot to mention that I was using version 2.9.3, I upgraded to 2.9.4 and the problem did not reappear, thanks for your contribution, thanks brother

@pietrygamat
Copy link
Collaborator

Perfect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants