Fazecast / jSerialComm

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

problem assigning multiple data listeners to one serial port - only first is respected #456

Closed benayat closed 1 year ago

benayat commented 2 years ago

I want to add two data listeners with fixed packet size(one for each command input, of type SerialPortPacketListener) to my SerialPort object, but only the first one is working. anything other than that doesn't work. is it intentional behavior and I'm missing something? or just a feature not yet implemented?

NorbertSandor commented 2 years ago

From the javadoc of SerialPort#addDataListener:

Only one listener can be registered at a time; however, that listener can be used to detect multiple types of serial port events.

@return Whether the listener was successfully registered with the serial port.

I guess your second addDataListener() call returned false.

hedgecrw commented 2 years ago

@benayat, @NorbertSandor is correct. The library only allows one listener to be registered at a time. It is up to the user to decide how to handle specific events that their listener is registered for. In the case of registering for a fixed packet size, there is really no way (even under the hood) to allow for multiple events/sizes to be registered. The library knows nothing about the data you are expecting, only that it has received a certain number of bytes, so it has no way of deciding which among a number of different listeners should be called for a given packet size. You will need to handle this differentiation within your own code. Let me know if you have any more questions.

hedgecrw commented 1 year ago

Closing as "will not fix".