Can't open the port again when colse it first. on windows
wanglinqiao opened this issue · 8 comments
Hello sir,I encountered a problem, when I successfully opened the serial port for the first time, after reading the data, I closed the serial port, and when I opened it again, I could not open it. The version number I introduced was v2.10.2. This problem occurs on windows.
The code is as follows:
CountDownLatch downLatch = new CountDownLatch(1);
SerialPort serialPort = SerialPort.getCommPort("com3");
serialPort.addDataListener(new SerialPortDataListener() {
@Override
public int getListeningEvents() {
return SerialPort.LISTENING_EVENT_DATA_AVAILABLE;
}
@Override
public void serialEvent(SerialPortEvent event) {
//receive data success
//do something
downLatch.countDown();
}
});
//first open success
serialPort.openPort();
//do something
//.......
downLatch.await();
//close serialPort
serialPort.closePort();
//second open fail
serialPort.openPort();
I implemented the following test application by copy-and-pasting your code (and making some alterations to make it compilable):
static public void testFunction() throws InterruptedException
{
final CountDownLatch downLatch = new CountDownLatch(1);
SerialPort serialPort = SerialPort.getCommPort("com3");
serialPort.addDataListener(new SerialPortDataListener() {
@Override
public int getListeningEvents() {
return SerialPort.LISTENING_EVENT_DATA_AVAILABLE;
}
@Override
public void serialEvent(SerialPortEvent event) {
downLatch.countDown();
}
});
//first open success
System.out.println("Open 1: " + (serialPort.openPort() ? "SUCCESS" : "FAILURE"));
downLatch.await();
//close serialPort
System.out.println("Close 1: " + (serialPort.closePort() ? "SUCCESS" : "FAILURE"));
//second open fail
System.out.println("Open 2: " + (serialPort.openPort() ? "SUCCESS" : "FAILURE"));
System.out.println("Close 2: " + (serialPort.closePort() ? "SUCCESS" : "FAILURE"));
}
static public void main(String[] args)
{
System.out.println("\nUsing Library Version v" + SerialPort.getVersion());
for (int i = 0; i < 10; ++i)
try { testFunction(); } catch (InterruptedException e) { e.printStackTrace(); }
}
Using this code, I am unable to replicate your issue. Do you still see the problem when using the above code directly?
Hello sir, i had the same problem but in RaspbianOS. I have an application that comunnicate with a dispositive and if this doesnt send data I have to reconnect the serial port to view if the dispositive is alive. I debug in Windows and I had to add an sleep of 10000 milliseconds to reconnect with the dispositive and works fine.
In RaspbianOs this sleep doesnt work and I have to unplug the USB converter and plug again. any help ?
Regards
Hi sir, I tested the code that you show. The problem still appear. I pasting your code and making some alterations to do what I need.
When I send 0x0F 0x00 0x0A the dispositive send data. The problem is when I disconnect the dispositive and then reconnect it but NEVER disconnect the serial port converter so the serial port /dev/ttyUSB0 never dissapear, the Listener stops receive data, stops working. Then I pasted the output and mark the time with asterisks when I disconnected the dispositive an reconnected it. The listener stops working. The only way to make it work again is by disconnecting the USB converter in RaspbianOS but in Windows it is not necessary, this code works fine.
static public void testFunction() throws InterruptedException
{
final CountDownLatch downLatch = new CountDownLatch(1);
SerialPort serialPort = SerialPort.getCommPort("/dev/ttyUSB0");
byte[] bytes = {(byte) 0x0F ,(byte) 0x01 ,(byte)0x00};
serialPort.addDataListener(new SerialPortDataListener() {
@Override
public int getListeningEvents() {
return SerialPort.LISTENING_EVENT_DATA_RECEIVED;
}
@Override
public void serialEvent(SerialPortEvent event) {
byte[] message= event.getReceivedData();
if (logger.isDebugEnabled())
logger.debug(String.format("Message: %s", Format.getStringFromByteArray(message)));
}
});
//first open success
System.out.println("Open 1: " + (serialPort.openPort() ? "SUCCESS" : "FAILURE"));
serialPort.writeBytes(bytes, 3);
Thread.sleep(3000);
//downLatch.await();
//close serialPort
System.out.println("Close 1: " + (serialPort.closePort() ? "SUCCESS" : "FAILURE"));
//second open fail
System.out.println("Open 2: " + (serialPort.openPort() ? "SUCCESS" : "FAILURE"));
serialPort.writeBytes(bytes, 3);
Thread.sleep(3000);
//downLatch.await();
System.out.println("Close 2: " + (serialPort.closePort() ? "SUCCESS" : "FAILURE"));
}
_
Open 1: SUCCESS
2023-07-28T11:29:20.772-03:00 DEBUG 171583 --- [ Thread-188] Application : Message: 12
2023-07-28T11:29:20.774-03:00 DEBUG 171583 --- [ Thread-188] Application : Message: 30
2023-07-28T11:29:20.779-03:00 DEBUG 171583 --- [ Thread-188] Application : Message: 3030303030
2023-07-28T11:29:20.783-03:00 DEBUG 171583 --- [ Thread-188] Application : Message: 303030
2023-07-28T11:29:20.787-03:00 DEBUG 171583 --- [ Thread-188] Application : Message: 30303003
Close 1: SUCCESS
Open 2: SUCCESS
2023-07-28T11:29:24.094-03:00 DEBUG 171583 --- [ Thread-189] Application : Message: 12
2023-07-28T11:29:24.095-03:00 DEBUG 171583 --- [ Thread-189] Application : Message: 30
2023-07-28T11:29:24.096-03:00 DEBUG 171583 --- [ Thread-189] Application : Message: 30
2023-07-28T11:29:24.097-03:00 DEBUG 171583 --- [ Thread-189] Application : Message: 30
2023-07-28T11:29:24.098-03:00 DEBUG 171583 --- [ Thread-189] Application : Message: 30
2023-07-28T11:29:24.099-03:00 DEBUG 171583 --- [ Thread-189] Application : Message: 30
2023-07-28T11:29:24.100-03:00 DEBUG 171583 --- [ Thread-189] Application : Message: 30
2023-07-28T11:29:24.101-03:00 DEBUG 171583 --- [ Thread-189] Application : Message: 30
2023-07-28T11:29:24.102-03:00 DEBUG 171583 --- [ Thread-189] Application : Message: 30
2023-07-28T11:29:24.103-03:00 DEBUG 171583 --- [ Thread-189] Application : Message: 30
2023-07-28T11:29:24.104-03:00 DEBUG 171583 --- [ Thread-189] Application : Message: 30
2023-07-28T11:29:24.105-03:00 DEBUG 171583 --- [ Thread-189] Application : Message: 30
2023-07-28T11:29:24.106-03:00 DEBUG 171583 --- [ Thread-189] Application : Message: 30
2023-07-28T11:29:24.107-03:00 DEBUG 171583 --- [ Thread-189] Application : Message: 03
Close 2: SUCCESS
Open 1: SUCCESS
2023-07-28T11:29:27.395-03:00 DEBUG 171583 --- [ Thread-190] Application : Message: 12
2023-07-28T11:29:27.398-03:00 DEBUG 171583 --- [ Thread-190] Application : Message: 303030
2023-07-28T11:29:27.401-03:00 DEBUG 171583 --- [ Thread-190] Application : Message: 303030
2023-07-28T11:29:27.403-03:00 DEBUG 171583 --- [ Thread-190] Application : Message: 303030
2023-07-28T11:29:27.406-03:00 DEBUG 171583 --- [ Thread-190] Application : Message: 3030
2023-07-28T11:29:27.408-03:00 DEBUG 171583 --- [ Thread-190] Application : Message: 3003
Close 1: SUCCESS
Open 2: SUCCESS
2023-07-28T11:29:30.718-03:00 DEBUG 171583 --- [ Thread-191] Application : Message: 12
2023-07-28T11:29:30.720-03:00 DEBUG 171583 --- [ Thread-191] Application : Message: 30
2023-07-28T11:29:30.721-03:00 DEBUG 171583 --- [ Thread-191] Application : Message: 30
2023-07-28T11:29:30.722-03:00 DEBUG 171583 --- [ Thread-191] Application : Message: 30
2023-07-28T11:29:30.723-03:00 DEBUG 171583 --- [ Thread-191] Application : Message: 30
2023-07-28T11:29:30.724-03:00 DEBUG 171583 --- [ Thread-191] Application : Message: 30
2023-07-28T11:29:30.725-03:00 DEBUG 171583 --- [ Thread-191] Application : Message: 30
2023-07-28T11:29:30.726-03:00 DEBUG 171583 --- [ Thread-191] Application : Message: 30
2023-07-28T11:29:30.727-03:00 DEBUG 171583 --- [ Thread-191] Application : Message: 30
2023-07-28T11:29:30.728-03:00 DEBUG 171583 --- [ Thread-191] Application : Message: 30
2023-07-28T11:29:30.729-03:00 DEBUG 171583 --- [ Thread-191] Application : Message: 30
2023-07-28T11:29:30.730-03:00 DEBUG 171583 --- [ Thread-191] Application : Message: 30
2023-07-28T11:29:30.731-03:00 DEBUG 171583 --- [ Thread-191] Application : Message: 30
2023-07-28T11:29:30.732-03:00 DEBUG 171583 --- [ Thread-191] Application : Message: 03
2023-07-28T11:29:33.056-03:00 DEBUG 171583 --- [ Thread-191] Application : Message: 00
2023-07-28T11:29:33.075-03:00 DEBUG 171583 --- [ Thread-191] Application : Message: 1C
2023-07-28T11:29:33.076-03:00 DEBUG 171583 --- [ Thread-191] Application : Message: 00
**************************************************************************************************************
Close 2: SUCCESS
Open 1: SUCCESS
Close 1: SUCCESS
Open 2: SUCCESS
Close 2: SUCCESS
Open 1: SUCCESS
2023-07-28T11:29:43.203-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: 00
2023-07-28T11:29:43.205-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: FF
2023-07-28T11:29:43.210-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: 40
2023-07-28T11:29:43.286-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: DB
2023-07-28T11:29:43.287-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.288-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.289-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.290-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.291-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.292-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.293-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.294-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.295-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.296-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.297-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.298-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.300-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: F0
2023-07-28T11:29:43.540-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: 9B
2023-07-28T11:29:43.541-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.542-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.543-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.544-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.545-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.546-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.547-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.548-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.549-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.550-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.551-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.552-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: B8
2023-07-28T11:29:43.554-03:00 DEBUG 171583 --- [ Thread-194] Application : Message: F0
Close 1: SUCCESS
Open 2: SUCCESS
Close 2: SUCCESS
Open 1: SUCCESS
Close 1: SUCCESS
Open 2: SUCCESS
Close 2: SUCCESS
Open 1: SUCCESS
Close 1: SUCCESS
Open 2: SUCCESS
Close 2: SUCCESS
Open 1: SUCCESS
Close 1: SUCCESS
Open 2: SUCCESS
Close 2: SUCCESS
Using Library Version v2.10.2
Open 1: SUCCESS
Close 1: SUCCESS
Open 2: SUCCESS
Close 2: SUCCESS
Open 1: SUCCESS
Close 1: SUCCESS
Open 2: SUCCESS
Close 2: SUCCESS
Open 1: SUCCESS
Close 1: SUCCESS
Open 2: SUCCESS
Close 2: SUCCESS
Open 1: SUCCESS
Close 1: SUCCESS
Open 2: SUCCESS
Close 2: SUCCESS
Open 1: SUCCESS
Close 1: SUCCESS
Open 2: SUCCESS
Close 2: SUCCESS
Open 1: SUCCESS
Close 1: SUCCESS
Open 2: SUCCESS
Close 2: SUCCESS
Open 1: SUCCESS
Close 1: SUCCESS
Open 2: SUCCESS
Close 2: SUCCESS
Open 1: SUCCESS
Close 1: SUCCESS
Open 2: SUCCESS
Close 2: SUCCESS
Open 1: SUCCESS
Oh okay, so you're talking about physically disconnecting a device from a USB-to-serial converter and then reconnecting it (but leaving the serial converter plugged in). In this case, I don't believe that this is an issue with the jSerialComm library; rather, it sounds like the way the underlying USB-to-serial driver handles serial-side disconnects varies between the two OS's. As a test, you can just run echo -en '\x0F\x01\x00' > /dev/ttyUSB0
followed by cat /dev/ttyUSB0
(or cat < /dev/ttyUSB0
) from your RaspianOS terminal, disconnect the serial device from the converter, Ctrl+C the cat
program, reconnect your device, then run both the echo
and cat
lines again. If you don't see the expected output at that point, then it's definitely an underlying driver issue.
Hi, I did exactly what you mentioned and that happens. I stop receiving information, I don't see anything at the reception. The only way I can think to fix it is by sending a command to reboot the specific USB port.
I can't reboot the USB controller because I have another serial converter connected. do you know any way?
I don't know a way off the top of my head. You can try one of the solutions presented on this Stack Overflow question: https://stackoverflow.com/questions/21580750/disconnect-and-reconnect-ttyusb0-programmatically-in-linux
@wanglinqiao, your issue appears to be different from @nsargo. Have you had a chance to try the test code I posted (and also please test today's release v2.10.3 and see if the problem persists)?
@wanglinqiao, any updates on this?
Closing due to inactivity and unable to reproduce.