Pass the port along with the SerialPortEvent#86
Conversation
|
I'm wondering if better encapsulation would actually be to have I don't vote for this option... this would only fix encapsulation though, not necessarily make it less clunky, I'll explain... My experience with eventLIsteners is mostly limited to Swing and AWT,
Passing callee by reference isn't much different than, say, a JButton. In the case of AWT/Swing events, there's a way to get the caller, so that might be the closest we'd have. I think what is perceived as "clunky" from an OO perspective is that internally, However, I believe this design decision was intended by scr3mer to keep the CPP mapping back to Java as simple as possible. If a pointer to the parent object is attainable, I think we should prefer that, since ports and listeners are nearly always a 1:1 relationship (though, I think you can actually re-use a listener for multiple ports, we should keep that in mind!)
I see no evidence of the API being errorprone in its current state. On the contrary, swallowing exceptions blindly seems to be error prone. 🍻 I realize you've commented "What do we do?" but I think the user should be able to handle the exceptions themselves, which can range from the port being closed, to a timeout occurringg ( For example, jssc/src/main/java/jssc/SerialPort.java Lines 584 to 588 in a9f1f1b
Last, some implementations decided to only |
|
Hmm, clunky and errorprone are definitely too harsh, but I didn't know how else to word it. I think the problem I have with this, is the fact you call port.addEventListener(new MyPortListener(otherPort) );which gives no errors and even kind of works. The listener is called when
All the while the PortName is given along with the event, but I don't think that really does anything for you. I'll try to see about some more options over the weekend, but you raise very valid concerns. And it does work as it is now, so it might indeed not be worth to break API for a non-necessary quality of life change. The PR was mostly a first step in my head, but I wasn't sure what to consider. All I knew was that there was a lot of it ;) So I'm grateful for you brining up
Again, I'll see if I can dig a bit deeper in the C++ and C# implementations and the Java shell in this case, I'll get back to you. |
|
Sorry for the delay... I took a longer look and came up with the following workable example that uses a lambda, doesn't need to keep track of the port. I think this is very close to what you're aiming for, but without removing the functionality of the port.addEventListener(event -> {
if(event.isRXCHAR()){ // data is available
// read data, if 10 bytes available
if(event.getEventValue() == 10){
try {
byte[] buffer = event.getPort().readBytes(10);
// ^------ port can be referenced directly from within the lambda
} catch (SerialPortException ex) {
System.out.println(ex);
}
}
}
// ...
}); To do this, you'll need to apply a diff something like this: https://gist.github.com/tresf/96ed6ce0cf37c4f227f01b342ff5ace9 I haven't tested this code yet, but what it does is attempt to preserve the existing class signatures for backwards compatibility while maintaining a reference to the This should work because although a listener can listen upon multiple ports, the events will have the |
Makes usage of PortName in both cases deprecated. Removed internal usage of the deprecated code
|
The following now indeed works, thanks for the idea @tresf I'll mark this PR ready for review. class MyPortListener implements SerialPortEventListener {
public void serialEvent(SerialPortEvent event) {
if (event.isRXCHAR()) { // data is available
// read data, if 10 bytes available
if (event.getEventValue() == 10) {
try {
byte[] buffer = event.getPort().readBytes();
System.out.println(Arrays.toString(buffer));
} catch (SerialPortException ex) {
System.out.println(ex);
}
}
} else if (event.isCTS()) { // CTS line has changed state
if (event.getEventValue() == 1) { // line is ON
System.out.println("CTS - ON");
} else {
System.out.println("CTS - OFF");
}
} else if (event.isDSR()) { // DSR line has changed state
if (event.getEventValue() == 1) { // line is ON
System.out.println("DSR - ON");
} else {
System.out.println("DSR - OFF");
}
}
}
} |
|
Thanks. I think this is safe to merge. We can update the examples once the next release is finished. |
SerialPortEvent now also saves the byte buffer. It does introduce the SerialPortException to a function that didn't have it before. I'm not sure how this should be handled, thus this is WIP.
Reason for passing along the bytes:
#85 (comment)
And from Slack (me):
This makes the example implementation of the event listener in Java the following:
This code works for me, tested with 0,01 second interval between messages of 10 bytes.
Closes #87