|
4 | 4 | * ORGANIZATION : Pi4J
|
5 | 5 | * PROJECT : Pi4J :: JNI Native Library
|
6 | 6 | * FILENAME : com_pi4j_jni_SerialInterrupt.c
|
7 |
| - * |
| 7 | + * |
8 | 8 | * This file is part of the Pi4J project. More information about
|
9 | 9 | * this project can be found here: http://www.pi4j.com/
|
10 | 10 | * **********************************************************************
|
|
15 | 15 | * it under the terms of the GNU Lesser General Public License as
|
16 | 16 | * published by the Free Software Foundation, either version 3 of the
|
17 | 17 | * License, or (at your option) any later version.
|
18 |
| - * |
| 18 | + * |
19 | 19 | * This program is distributed in the hope that it will be useful,
|
20 | 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
21 | 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
22 | 22 | * GNU General Lesser Public License for more details.
|
23 |
| - * |
| 23 | + * |
24 | 24 | * You should have received a copy of the GNU General Lesser Public
|
25 | 25 | * License along with this program. If not, see
|
26 | 26 | * <http://www.gnu.org/licenses/lgpl-3.0.html>.
|
@@ -157,50 +157,53 @@ int monitorSerialInterrupt(void *threadarg)
|
157 | 157 |
|
158 | 158 | int length = getAvailableDataLength(fileDescriptor);
|
159 | 159 |
|
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 | + } |
204 | 207 | }
|
205 | 208 | }
|
206 | 209 |
|
|
0 commit comments