Fazecast / jSerialComm

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

SerialPort.LISTENING_EVENT_CTS not reachable / resolvable #429

Closed tinitus91 closed 2 years ago

tinitus91 commented 2 years ago

I would like to detect CTS events but I don't succeed in implementing the code : Here is what I wrote for now but the "LISTENING_EVENT_CTS" mentionned in the documentation seems not to be reachable.

What am I doing wrong ?

Thank you

package repTest.basicSerialTest;

import com.fazecast.jSerialComm.SerialPort;
import com.fazecast.jSerialComm.SerialPortDataListener;
import com.fazecast.jSerialComm.SerialPortEvent;

public class ClsTest {

    static public void main(String[] args)
    {
        SerialPort[] comPorts = SerialPort.getCommPorts();   // get ports list

         for ( SerialPort pp : comPorts) {   
             System.out.println(pp+"  "+pp.getDescriptivePortName() + "  "+pp.getPortDescription());  // display all ports
             }

        SerialPort comPort = SerialPort.getCommPorts()[0];  // select first one

        comPort.setBaudRate(4800);
        comPort.setNumDataBits(8);
        comPort.setNumStopBits(1);
        comPort.setParity(0);
        comPort.setFlowControl(1);

        comPort.setRTS();

        comPort.addDataListener(new SerialPortDataListener() {

               @Override
               public int getListeningEvents() { 

                   return SerialPort.LISTENING_EVENT_CTS; 
                   }

               @Override
               public void serialEvent(SerialPortEvent event)   { 

                    if (event.getEventType() == SerialPort.LISTENING_EVENT_CTS)

                     System.out.println("CTS detected ");
               }
            });

       comPort.openPort();
       System.out.println("comPort status "+ comPort.isOpen());

       try { Thread.sleep(50*1000); } catch (Exception e) { e.printStackTrace(); }  // 50s for testing

       System.out.println("end");
    }
}
hedgecrw commented 2 years ago

In your serialEvent() callback, you are testing for a flow control flag and not the LISTENING_EVENT_CTS flag. Change your test to event.getEventType() == SerialPort.LISTENING_EVENT_CTS (or just remove the test altogether since you're only listening for a single event type), and you should be good to go.

tinitus91 commented 2 years ago

Thanks for return yes it was an old try I corrected it. Did you execute this code ? the problem is Eclipse cannot resolve "SerialPort.LISTENING_EVENT_CTS" Only those 3 : data available, data received , data written are accessible like if others listening methods don't exist. is there a special release to use to have it all ?

hedgecrw commented 2 years ago

No special release. If those are the only three listening event options available to you, then somehow your setup/configuration in Eclipse is using an older jSerialComm release from before this functionality was added (I believe it was added in 2.8.0).

tinitus91 commented 2 years ago

ok thank you I had two versions in the buildpath. Now I can trigger cts events but using RTS output through a switch it mainly detects "ON" transition vs OFF ratio is 90% vs 10% !

hedgecrw commented 2 years ago

I can't really speak to the low-level behavior of the CTS events you're seeing as that's very hardware-, software-, and setup-dependent, but from a very high level, if you're already in the event handler for a CTS event and the CTS line status changes while you're still handling the previous state change, you won't receive another notification (in other words, the event callback is not reentrant).

My guess would be that you are getting an event when the CTS line goes low, but by the time you check its status, it has already been pulled high again. One of the big problems with using Java for very low-level bit-banging in serial programming is that the required interface going back and forth between Java and native code is quite slow (relatively speaking to the speed of the transmissions). Unfortunately, there's not really much that can be done on that front from this library. The callbacks you are seeing and their timings are coming directly from the OS and underlying device driver.

tinitus91 commented 2 years ago

I understand but switch period is very large , about 2 seconds. I also tried with jssc library and even if there is lacks during transient period ( switch boucing) ratio approach 50/50 for ON and OFF and I never miss the first Off edge of a serie ( considering each Off , on, off, on ...) as a serie.

hedgecrw commented 2 years ago

Are you using Windows? If so, I think I see the problem.

tinitus91 commented 2 years ago

Yes I'm using Windows 10

hedgecrw commented 2 years ago

Gotcha - when I get a second, I'll make a fix and get to you for testing.

tinitus91 commented 2 years ago

Sure with pleasure !

tinitus91 commented 2 years ago

Hello, do you think it could be possible to apply this modifications within the current month ? Thank you

hedgecrw commented 2 years ago

Sorry for the delay. Please test the following updated version and see if it corrects your issue:

https://www.dropbox.com/t/qQJEhdFDRfEsnI8z

If so, I will check in the fix and include it in the next release (coming out shortly).

tinitus91 commented 2 years ago

thanks, i'll let you know you as soon as possible.

hedgecrw commented 2 years ago

Any update on whether your issue was resolved? I'd like to include it in the next release if so. An updated test version is here:

https://www.dropbox.com/t/0eV7icgmxu67mi3m

tinitus91 commented 2 years ago

Hello, After testing jSerialComm-2.9.2-beta5, behaviour seems to be good. Could you say in few word what was the problem ?

hedgecrw commented 2 years ago

Glad to hear! The problem was that the library was only sending a CTS notification event when the line went high instead of on any change.

hedgecrw commented 2 years ago

This fix was included in today's jSerialComm v2.9.2 release. Thanks!

tinitus91 commented 2 years ago

Especially thanks to you ! This will allow me to continue to use this same library and same project classes without any modification with all serial events in my application !