Fazecast / jSerialComm

Platform-independent serial port access for Java
GNU Lesser General Public License v3.0
1.35k stars 287 forks source link

Connections with port can only be made if i re-plug the usb - serial converter #516

Closed habuif closed 1 year ago

habuif commented 1 year ago

Hi. I cant openPort() successfully if i dont re-plug my usb - serial converter. And i can only openPort successfully once. If i close the port and try to re-open it without re-plugging the usb-serial converter, it wont work. Please help..

breiler commented 1 year ago

What version of JSerialComm are you using? What OS are you running? What processor architecture are you using (armv5, armv6, armv7, armv8_32, armv8_64, ppc64le, x86, or x86_64) What Java version are you using? Can you please provide a small sample program?

habuif commented 1 year ago

I've tested with JSerial Comm 2.8.3 and 2.10.3 but both didnt work. Im using windows 11 with x64 processor architecture. Also, im using jdk17.0.7.7 I'm using javafx.concurrent.Service to manage creating and closing the Serial Port.

habuif commented 1 year ago

This is the code:

protected Task createTask() { return new Task() { @Override protected Object call() throws Exception { serialPort.setComPortParameters(PortSettingsController.getBaudRate(), PortSettingsController.getDataBits(), PortSettingsController.getStopBits(), PortSettingsController.getParity()); serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING,100,100);

            if(serialPort.openPort(500)){
                System.out.println("Open Successfully:" + serialPort.isOpen());
                InputStreamReader inputStreamReader = new InputStreamReader(serialPort.getInputStream());
                Scanner reader = new Scanner(inputStreamReader);
                Pattern pattern = Pattern.compile("ww.*");

                while(reader.hasNextLine()){
                    if(!isCancelled()){
                        Matcher matcher = pattern.matcher(reader.nextLine());
                        System.out.println(reader.nextLine());
                        if (matcher.find()){
                            String find = matcher.group().substring(2,matcher.group().length()-2).strip();
                            updateValue(find);
                        }
                    }else {
                        System.out.println("broke");
                        break;
                    }
                }
                reader.close();
                inputStreamReader.close();
                serialPort.closePort();
                for ( SerialPort serialPort1 : SerialPort.getCommPorts()) {
                    System.out.println(serialPort1.getSystemPortName());
                }
                System.out.println("_____________");
            }else {
                System.out.println("Open failed");
            }
            return null;
        }
    };
habuif commented 1 year ago

I've initialize the SerialPort as:

private static volatile SerialPort serialPort = SerialPort.getCommPort("COM6");

When i try to switch my device to different ComPort this problem still arises.

hedgecrw commented 1 year ago

1) Are your PortSettingsController.getXXX() functions returning the internal jSerialComm Constants for the respective items (data bits, stop bits, etc.)? 2) You are using TIMEOUT_READ_SEMI_BLOCKING along with a Java Scanner...I believe there is a Scanner-specific TIMEOUT (TIMEOUT_SCANNER) that you should be using to make sure it returns from a stalled scan. 3) When you say you can't re-open, do you mean you simply close in software then try to re-open in software, or do you mean something more involved (like physically unplugging the device, then plugging it back in and trying to re-open)?

habuif commented 1 year ago
  1. Yes the get function is returning the correct values.
  2. I've tried with TIMEOUT_SCANNER it still didnt solve the problem.
  3. When i close the port in the software, i cant re-open the port. The SerialPort.openPort() returns false. I have to physically plugging the device again in order to re-open the port.

Everytime i close the port it returns true. I dont understand why the problem is happening. When i used getLastErrorCode() and getLastErrorLocation(), it returned 31 and 850 respectively. image

hedgecrw commented 1 year ago

Same issue as #518.

hedgecrw commented 1 year ago

Closing as duplicate issue of #518.