Skip to content
This repository was archived by the owner on Jan 11, 2024. It is now read-only.

Commit 3e7488d

Browse files
Fixed #272; SerialDataEvent should not fire when event.length() == 0
1 parent 4595b62 commit 3e7488d

File tree

3 files changed

+55
-48
lines changed

3 files changed

+55
-48
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ Copyright (C) 2012-2017 Pi4J
8181
* Fixed #351; Pi4J packages excluded in OSGI manifest ImportPackage declaration
8282
* Fixed #360; Serial.setBreak(TRUE|FALSE) is printing debug lines to stdout
8383
* Fixed #355; ButtonBase Executor Service should be closed on GpioController.shutdown()
84-
* Fixed #356; SystemInfoProvider CPU temperature was divided by 1000 on OrangePi
84+
* Fixed #356; SystemInfoProvider CPU temperature was divided by 1000 on OrangePi
85+
* Fixed #272; SerialDataEvent should not fire when event.length() == 0
8586

8687
## RELEASES
8788

pi4j-core/src/main/java/com/pi4j/io/serial/impl/SerialImpl.java

+3
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ public void open(String device, int baud, int dataBits, int parity, int stopBits
171171
@Override
172172
public void onDataReceive(SerialInterruptEvent event) {
173173

174+
// ignore any event triggers that are missing data
175+
if(event.getLength() <= 0) return;
176+
174177
try {
175178
SerialDataEvent sde = null;
176179

pi4j-native/src/main/native/com_pi4j_jni_SerialInterrupt.c

+50-47
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* ORGANIZATION : Pi4J
55
* PROJECT : Pi4J :: JNI Native Library
66
* FILENAME : com_pi4j_jni_SerialInterrupt.c
7-
*
7+
*
88
* This file is part of the Pi4J project. More information about
99
* this project can be found here: http://www.pi4j.com/
1010
* **********************************************************************
@@ -15,12 +15,12 @@
1515
* it under the terms of the GNU Lesser General Public License as
1616
* published by the Free Software Foundation, either version 3 of the
1717
* License, or (at your option) any later version.
18-
*
18+
*
1919
* This program is distributed in the hope that it will be useful,
2020
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2121
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2222
* GNU General Lesser Public License for more details.
23-
*
23+
*
2424
* You should have received a copy of the GNU General Lesser Public
2525
* License along with this program. If not, see
2626
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
@@ -157,50 +157,53 @@ int monitorSerialInterrupt(void *threadarg)
157157

158158
int length = getAvailableDataLength(fileDescriptor);
159159

160-
//printf("SERIAL EPOLL - Bytes available: %d\n", length);
161-
162-
// create a new payload result byte array
163-
jbyte result[length];
164-
165-
// copy the data bytes from the serial receive buffer into the payload result
166-
int i;
167-
for (i = 0; i < length; i++) {
168-
169-
// read a single byte from the RX buffer
170-
uint8_t x ;
171-
if (read (fileDescriptor, &x, 1) != 1){
172-
int err_number = errno;
173-
char err_message[100];
174-
sprintf(err_message, "Failed to read data from serial port. (Error #%d)", err_number);
175-
perror("SERIAL FAILED TO READ DATA\n");
176-
close(epfd);
177-
continue;
178-
}
179-
180-
// assign the single byte; cast to unsigned char
181-
result[i] = (unsigned char)(((int)x) & 0xFF);
182-
}
183-
184-
// ensure the callback class and method are available
185-
if (serial_callback_class != NULL && serial_callback_method != NULL)
186-
{
187-
// get attached JVM
188-
JNIEnv *env;
189-
(*serial_callback_jvm)->AttachCurrentThread(serial_callback_jvm, (void **)&env, NULL);
190-
191-
// create a java array object and copy the raw payload bytes
192-
jbyteArray payload = (*env)->NewByteArray(env, length);
193-
(*env)->SetByteArrayRegion(env, payload, 0, length, result);
194-
195-
// ensure that the JVM exists
196-
if(serial_callback_jvm != NULL)
197-
{
198-
// invoke callback to java state method to notify event listeners
199-
(*env)->CallStaticVoidMethod(env, serial_callback_class, serial_callback_method, (jint)fileDescriptor, payload);
200-
}
201-
202-
// detach from thread
203-
(*serial_callback_jvm)->DetachCurrentThread(serial_callback_jvm);
160+
// only fire event if there is data length
161+
if(length > 0){
162+
//printf("SERIAL EPOLL - Bytes available: %d\n", length);
163+
164+
// create a new payload result byte array
165+
jbyte result[length];
166+
167+
// copy the data bytes from the serial receive buffer into the payload result
168+
int i;
169+
for (i = 0; i < length; i++) {
170+
171+
// read a single byte from the RX buffer
172+
uint8_t x ;
173+
if (read (fileDescriptor, &x, 1) != 1){
174+
int err_number = errno;
175+
char err_message[100];
176+
sprintf(err_message, "Failed to read data from serial port. (Error #%d)", err_number);
177+
perror("SERIAL FAILED TO READ DATA\n");
178+
close(epfd);
179+
continue;
180+
}
181+
182+
// assign the single byte; cast to unsigned char
183+
result[i] = (unsigned char)(((int)x) & 0xFF);
184+
}
185+
186+
// ensure the callback class and method are available
187+
if (serial_callback_class != NULL && serial_callback_method != NULL)
188+
{
189+
// get attached JVM
190+
JNIEnv *env;
191+
(*serial_callback_jvm)->AttachCurrentThread(serial_callback_jvm, (void **)&env, NULL);
192+
193+
// create a java array object and copy the raw payload bytes
194+
jbyteArray payload = (*env)->NewByteArray(env, length);
195+
(*env)->SetByteArrayRegion(env, payload, 0, length, result);
196+
197+
// ensure that the JVM exists
198+
if(serial_callback_jvm != NULL)
199+
{
200+
// invoke callback to java state method to notify event listeners
201+
(*env)->CallStaticVoidMethod(env, serial_callback_class, serial_callback_method, (jint)fileDescriptor, payload);
202+
}
203+
204+
// detach from thread
205+
(*serial_callback_jvm)->DetachCurrentThread(serial_callback_jvm);
206+
}
204207
}
205208
}
206209

0 commit comments

Comments
 (0)