| 1 |
/*------------------------------------------------------------------------- |
| 2 |
| RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface. |
| 3 |
| RXTX is a native interface to serial ports in java. |
| 4 |
| Copyright 2002-2004 Michal Hobot [email protected] |
| 5 |
| Copyright 1997-2007 by Trent Jarvi [email protected] and others who |
| 6 |
| actually wrote it. See individual source files for more information. |
| 7 |
| |
| 8 |
| A copy of the LGPL v 2.1 may be found at |
| 9 |
| http://www.gnu.org/licenses/lgpl.txt on March 4th 2007. A copy is |
| 10 |
| here for your convenience. |
| 11 |
| |
| 12 |
| This library is free software; you can redistribute it and/or |
| 13 |
| modify it under the terms of the GNU Lesser General Public |
| 14 |
| License as published by the Free Software Foundation; either |
| 15 |
| version 2.1 of the License, or (at your option) any later version. |
| 16 |
| |
| 17 |
| This library is distributed in the hope that it will be useful, |
| 18 |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 |
| Lesser General Public License for more details. |
| 21 |
| |
| 22 |
| An executable that contains no derivative of any portion of RXTX, but |
| 23 |
| is designed to work with RXTX by being dynamically linked with it, |
| 24 |
| is considered a "work that uses the Library" subject to the terms and |
| 25 |
| conditions of the GNU Lesser General Public License. |
| 26 |
| |
| 27 |
| The following has been added to the RXTX License to remove |
| 28 |
| any confusion about linking to RXTX. We want to allow in part what |
| 29 |
| section 5, paragraph 2 of the LGPL does not permit in the special |
| 30 |
| case of linking over a controlled interface. The intent is to add a |
| 31 |
| Java Specification Request or standards body defined interface in the |
| 32 |
| future as another exception but one is not currently available. |
| 33 |
| |
| 34 |
| http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface |
| 35 |
| |
| 36 |
| As a special exception, the copyright holders of RXTX give you |
| 37 |
| permission to link RXTX with independent modules that communicate with |
| 38 |
| RXTX solely through the Sun Microsytems CommAPI interface version 2, |
| 39 |
| regardless of the license terms of these independent modules, and to copy |
| 40 |
| and distribute the resulting combined work under terms of your choice, |
| 41 |
| provided that every copy of the combined work is accompanied by a complete |
| 42 |
| copy of the source code of RXTX (the version of RXTX used to produce the |
| 43 |
| combined work), being distributed under the terms of the GNU Lesser General |
| 44 |
| Public License plus this exception. An independent module is a |
| 45 |
| module which is not derived from or based on RXTX. |
| 46 |
| |
| 47 |
| Note that people who make modified versions of RXTX are not obligated |
| 48 |
| to grant this special exception for their modified versions; it is |
| 49 |
| their choice whether to do so. The GNU Lesser General Public License |
| 50 |
| gives permission to release a modified version without this exception; this |
| 51 |
| exception also makes it possible to release a modified version which |
| 52 |
| carries forward this exception. |
| 53 |
| |
| 54 |
| You should have received a copy of the GNU Lesser General Public |
| 55 |
| License along with this library; if not, write to the Free |
| 56 |
| Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 57 |
| All trademarks belong to their respective owners. |
| 58 |
--------------------------------------------------------------------------*/ |
| 59 |
#include "StdAfx.h" |
| 60 |
#include "rxtxHelpers.h" |
| 61 |
|
| 62 |
/* |
| 63 |
Initialize |
| 64 |
|
| 65 |
accept: none |
| 66 |
perform: Initialize the native library |
| 67 |
return: none |
| 68 |
exceptions: none |
| 69 |
comments: |
| 70 |
* Class: gnu_io_RXTXPort |
| 71 |
* Method: Initialize |
| 72 |
* Signature: ()V |
| 73 |
*/ |
| 74 |
JNIEXPORT void JNICALL Java_gnu_io_RXTXPort_Initialize(JNIEnv *env, jclass cls) |
| 75 |
{ |
| 76 |
} |
| 77 |
|
| 78 |
/* |
| 79 |
open |
| 80 |
|
| 81 |
accept: The device to open. ie "COM1:" |
| 82 |
perform: open the device |
| 83 |
return: handle |
| 84 |
exceptions: IOExcepiton |
| 85 |
comments: Very often people complain about not being able to get past |
| 86 |
this function and it turns out to be permissions on the |
| 87 |
device file or bios has the device disabled. |
| 88 |
* Class: gnu_io_RXTXPort |
| 89 |
* Method: open |
| 90 |
* Signature: (Ljava/lang/String{ })I |
| 91 |
*/ |
| 92 |
JNIEXPORT jint JNICALL Java_gnu_io_RXTXPort_open(JNIEnv *env, jobject jobj, jstring name) |
| 93 |
{ |
| 94 |
DCB PortDCB; |
| 95 |
COMMTIMEOUTS CommTimeouts; |
| 96 |
LPCWSTR lpMsgBuf; |
| 97 |
EventInfoStruct *EventInfo; |
| 98 |
DWORD dwErr; |
| 99 |
|
| 100 |
LPCWSTR wszName = env->GetStringChars(name, NULL); |
| 101 |
HANDLE hPort = CreateFileW(wszName, // Pointer to the name of the port |
| 102 |
GENERIC_READ | GENERIC_WRITE,// Access (read-write) mode |
| 103 |
0, // Share mode |
| 104 |
NULL, // Pointer to the security attribute |
| 105 |
OPEN_EXISTING,// How to open the serial port |
| 106 |
0, // Port attributes |
| 107 |
NULL); // Handle to port with attribute to copy |
| 108 |
// If it fails to open the port, return FALSE. |
| 109 |
if ( hPort == INVALID_HANDLE_VALUE ) |
| 110 |
{ // Could not open the port. |
| 111 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 112 |
throw_java_exceptionW(env, PORT_IN_USE_EXCEPTION, L"open - CreateFile", lpMsgBuf ); |
| 113 |
// Free the buffers. |
| 114 |
ReleaseErrorMsg(lpMsgBuf); |
| 115 |
env->ReleaseStringChars(name, wszName); |
| 116 |
return (jint)INVALID_HANDLE_VALUE; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
PortDCB.DCBlength = sizeof (DCB); |
| 121 |
|
| 122 |
// Get the default port setting information. |
| 123 |
GetCommState(hPort, &PortDCB); |
| 124 |
|
| 125 |
// Change the DCB structure settings. |
| 126 |
PortDCB.fBinary = TRUE; // Binary mode; no EOF check |
| 127 |
PortDCB.fParity = TRUE; // Enable parity checking |
| 128 |
PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control |
| 129 |
PortDCB.fDtrControl = DTR_CONTROL_HANDSHAKE;// DTR flow control type |
| 130 |
PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity |
| 131 |
PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx |
| 132 |
PortDCB.fErrorChar = FALSE; // Disable error replacement |
| 133 |
PortDCB.fNull = FALSE; // Disable null stripping |
| 134 |
PortDCB.fRtsControl = RTS_CONTROL_ENABLE;// RTS flow control |
| 135 |
PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on error |
| 136 |
|
| 137 |
// Configure the port according to the specifications of the DCB structure. |
| 138 |
if (!SetCommState(hPort, &PortDCB)) |
| 139 |
{ // Could not set comm port state |
| 140 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 141 |
throw_java_exceptionW(env, PORT_IN_USE_EXCEPTION, L"open - SetCommState", lpMsgBuf); |
| 142 |
// Free the buffers. |
| 143 |
ReleaseErrorMsg(lpMsgBuf); |
| 144 |
env->ReleaseStringChars(name, wszName); |
| 145 |
return (jint)INVALID_HANDLE_VALUE; |
| 146 |
} |
| 147 |
|
| 148 |
// Retrieve the time-out parameters for all read and write operations |
| 149 |
// on the port. |
| 150 |
GetCommTimeouts(hPort, &CommTimeouts); |
| 151 |
|
| 152 |
// Change the COMMTIMEOUTS structure settings. |
| 153 |
CommTimeouts.ReadIntervalTimeout = 0; |
| 154 |
CommTimeouts.ReadTotalTimeoutMultiplier = 0; |
| 155 |
CommTimeouts.ReadTotalTimeoutConstant = MAXDWORD; |
| 156 |
CommTimeouts.WriteTotalTimeoutMultiplier = 10; |
| 157 |
CommTimeouts.WriteTotalTimeoutConstant = 1000; |
| 158 |
|
| 159 |
// Set the time-out parameters for all read and write operations |
| 160 |
// on the port. |
| 161 |
if (!SetCommTimeouts(hPort, &CommTimeouts)) |
| 162 |
{ // Unable to set the time-out parameters |
| 163 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 164 |
throw_java_exceptionW(env, PORT_IN_USE_EXCEPTION, L"open - SetCommTimeouts", lpMsgBuf); |
| 165 |
// Free the buffers. |
| 166 |
ReleaseErrorMsg(lpMsgBuf); |
| 167 |
env->ReleaseStringChars(name, wszName); |
| 168 |
return (jint)INVALID_HANDLE_VALUE; |
| 169 |
} |
| 170 |
|
| 171 |
// SETRTS: Sends the RTS (request-to-send) signal. |
| 172 |
EscapeCommFunction(hPort, SETRTS); |
| 173 |
|
| 174 |
|
| 175 |
if(dwErr = InitialiseEventInfoStruct(hPort, &EventInfo)) |
| 176 |
{ // Unable to set up EventInfo structure for event processing |
| 177 |
CreateErrorMsg(dwErr, lpMsgBuf); |
| 178 |
throw_java_exceptionW(env, IO_EXCEPTION, L"open - InitialiseEventInfoStruct", lpMsgBuf); |
| 179 |
// Free the buffers. |
| 180 |
ReleaseErrorMsg(lpMsgBuf); |
| 181 |
env->ReleaseStringChars(name, wszName); |
| 182 |
return (jint)INVALID_HANDLE_VALUE; |
| 183 |
} |
| 184 |
|
| 185 |
jclass cls = env->GetObjectClass(jobj); |
| 186 |
jfieldID jfEis = env->GetFieldID(cls, "eis", "I"); |
| 187 |
if( !jfEis ) { |
| 188 |
IF_DEBUG |
| 189 |
( |
| 190 |
env->ExceptionDescribe(); |
| 191 |
) |
| 192 |
env->ExceptionClear(); |
| 193 |
// Free the buffers. |
| 194 |
env->ReleaseStringChars(name, wszName); |
| 195 |
return (jint)INVALID_HANDLE_VALUE; |
| 196 |
} |
| 197 |
env->SetIntField(jobj, jfEis, (jint)EventInfo); |
| 198 |
|
| 199 |
env->ReleaseStringChars(name, wszName); |
| 200 |
// Returning HANDLE (which is a pointer) as file descriptor... Anyway, 32 bits are 32 bits |
| 201 |
return (jint)hPort; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
/* |
| 206 |
nativeSetSerialPortParams |
| 207 |
|
| 208 |
accept: speed, data bits, stop bits, parity |
| 209 |
perform: set the serial port parameters |
| 210 |
return: void |
| 211 |
exceptions: UnsupportedCommOperationException |
| 212 |
* Class: gnu_io_RXTXPort |
| 213 |
* Method: nativeSetSerialPortParams |
| 214 |
* Signature: (IIII)V |
| 215 |
*/ |
| 216 |
JNIEXPORT void JNICALL Java_gnu_io_RXTXPort_nativeSetSerialPortParams(JNIEnv *env, jobject jobj, jint speed, jint dataBits, jint stopBits, jint parity) |
| 217 |
{ |
| 218 |
DCB PortDCB; |
| 219 |
LPCWSTR lpMsgBuf; |
| 220 |
|
| 221 |
HANDLE hPort = get_fd(env, jobj); |
| 222 |
PortDCB.DCBlength = sizeof(DCB); |
| 223 |
|
| 224 |
IF_DEBUG |
| 225 |
( |
| 226 |
printj(env, L"--- RXTXPort.nativeSetSerialPortParams(%ld, %ld, %ld, %ld) called\n", speed, dataBits, stopBits, parity); |
| 227 |
) |
| 228 |
|
| 229 |
// Get the default port setting information. |
| 230 |
if(!GetCommState(hPort, &PortDCB)) |
| 231 |
{ //Unable to get configuration of the serial port |
| 232 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 233 |
throw_java_exceptionW(env, UNSUPPORTED_COMM_OPERATION, L"nativeSetSerialPortParams - GetCommState", lpMsgBuf); |
| 234 |
// Free the buffers. |
| 235 |
ReleaseErrorMsg(lpMsgBuf); |
| 236 |
return; |
| 237 |
} |
| 238 |
|
| 239 |
// Change the DCB structure settings. |
| 240 |
PortDCB.BaudRate = speed; // Current baud |
| 241 |
PortDCB.ByteSize = (BYTE)dataBits; // Number of bits/byte, 4-8 |
| 242 |
PortDCB.Parity = (BYTE)parity; // 0-4=no,odd,even,mark,space |
| 243 |
switch(stopBits) |
| 244 |
{ |
| 245 |
case gnu_io_RXTXPort_STOPBITS_1: |
| 246 |
PortDCB.StopBits = ONESTOPBIT; |
| 247 |
break; |
| 248 |
|
| 249 |
case gnu_io_RXTXPort_STOPBITS_2: |
| 250 |
PortDCB.StopBits = TWOSTOPBITS; |
| 251 |
break; |
| 252 |
|
| 253 |
case gnu_io_RXTXPort_STOPBITS_1_5: |
| 254 |
PortDCB.StopBits = ONE5STOPBITS; |
| 255 |
break; |
| 256 |
|
| 257 |
default: |
| 258 |
throw_java_exceptionW(env, UNSUPPORTED_COMM_OPERATION, L"nativeSetSerialPortParams", L"Incorrect stopBits"); |
| 259 |
|
| 260 |
} |
| 261 |
|
| 262 |
// Configure the port according to the specifications of the DCB structure. |
| 263 |
if (!SetCommState (hPort, &PortDCB)) |
| 264 |
{ //Unable to configure the serial port |
| 265 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 266 |
throw_java_exceptionW(env, UNSUPPORTED_COMM_OPERATION, L"nativeSetSerialPortParams - SetCommState", lpMsgBuf); |
| 267 |
// Free the buffers. |
| 268 |
ReleaseErrorMsg(lpMsgBuf); |
| 269 |
return; |
| 270 |
} |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
/* |
| 275 |
setflowcontrol |
| 276 |
|
| 277 |
accept: flowmode |
| 278 |
FLOWCONTROL_NONE none |
| 279 |
FLOWCONTROL_RTSCTS_IN hardware flow control |
| 280 |
FLOWCONTROL_RTSCTS_OUT "" |
| 281 |
FLOWCONTROL_XONXOFF_IN input software flow control |
| 282 |
FLOWCONTROL_XONXOFF_OUT output software flow control |
| 283 |
perform: set flow control to flowmode |
| 284 |
return: none |
| 285 |
exceptions: UnsupportedCommOperationException |
| 286 |
comments: there is no differentiation between input and output hardware |
| 287 |
flow control |
| 288 |
* Class: gnu_io_RXTXPort |
| 289 |
* Method: setflowcontrol |
| 290 |
* Signature: (I)V |
| 291 |
*/ |
| 292 |
JNIEXPORT void JNICALL Java_gnu_io_RXTXPort_setflowcontrol(JNIEnv *env, jobject jobj, jint flowcontrol) |
| 293 |
{ |
| 294 |
DCB PortDCB; |
| 295 |
LPCWSTR lpMsgBuf; |
| 296 |
|
| 297 |
IF_DEBUG |
| 298 |
( |
| 299 |
printj(env, L"--- RXTXPort.setflowcontrol(%ld) called\n", flowcontrol); |
| 300 |
) |
| 301 |
|
| 302 |
HANDLE hPort = get_fd(env, jobj); |
| 303 |
|
| 304 |
PortDCB.DCBlength = sizeof(DCB); |
| 305 |
if(!GetCommState (hPort, &PortDCB)) |
| 306 |
{ //Unable to get configuration of the serial port |
| 307 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 308 |
throw_java_exceptionW(env, UNSUPPORTED_COMM_OPERATION, L"setflowcontrol - GetCommState", lpMsgBuf); |
| 309 |
// Free the buffers. |
| 310 |
ReleaseErrorMsg(lpMsgBuf); |
| 311 |
return; |
| 312 |
} |
| 313 |
|
| 314 |
if(flowcontrol & (gnu_io_RXTXPort_FLOWCONTROL_RTSCTS_IN | gnu_io_RXTXPort_FLOWCONTROL_RTSCTS_OUT)) |
| 315 |
PortDCB.fRtsControl = RTS_CONTROL_HANDSHAKE; |
| 316 |
else |
| 317 |
PortDCB.fRtsControl = RTS_CONTROL_ENABLE; |
| 318 |
|
| 319 |
if(flowcontrol & gnu_io_RXTXPort_FLOWCONTROL_RTSCTS_OUT) |
| 320 |
PortDCB.fOutxCtsFlow = TRUE; |
| 321 |
else |
| 322 |
PortDCB.fOutxCtsFlow = FALSE; |
| 323 |
|
| 324 |
if(flowcontrol & gnu_io_RXTXPort_FLOWCONTROL_XONXOFF_IN) |
| 325 |
PortDCB.fInX = TRUE; |
| 326 |
else |
| 327 |
PortDCB.fInX = FALSE; |
| 328 |
|
| 329 |
if(flowcontrol & gnu_io_RXTXPort_FLOWCONTROL_XONXOFF_OUT) |
| 330 |
PortDCB.fOutX = TRUE; |
| 331 |
else |
| 332 |
PortDCB.fOutX = FALSE; |
| 333 |
|
| 334 |
// Configure the port according to the specifications of the DCB structure. |
| 335 |
if (!SetCommState (hPort, &PortDCB)) |
| 336 |
{ // Could not set params |
| 337 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 338 |
throw_java_exceptionW(env, UNSUPPORTED_COMM_OPERATION, L"setflowcontrol - SetCommState", lpMsgBuf); |
| 339 |
// Free the buffers. |
| 340 |
ReleaseErrorMsg(lpMsgBuf); |
| 341 |
return; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
/* |
| 346 |
NativegetReceiveTimeout |
| 347 |
|
| 348 |
accept: none |
| 349 |
perform: get termios.c_cc[VTIME] |
| 350 |
return: VTIME |
| 351 |
comments: see NativeEnableReceiveTimeoutThreshold |
| 352 |
* Class: gnu_io_RXTXPort |
| 353 |
* Method: NativegetReceiveTimeout |
| 354 |
* Signature: ()I |
| 355 |
*/ |
| 356 |
JNIEXPORT jint JNICALL Java_gnu_io_RXTXPort_NativegetReceiveTimeout(JNIEnv *env, jobject jobj) |
| 357 |
{ |
| 358 |
COMMTIMEOUTS CommTimeouts; |
| 359 |
LPCWSTR lpMsgBuf; |
| 360 |
HANDLE hPort = get_fd(env, jobj); |
| 361 |
|
| 362 |
IF_DEBUG |
| 363 |
( |
| 364 |
printj(env, L"--- RXTXPort.NativegetReceiveTimeout() called\n"); |
| 365 |
) |
| 366 |
|
| 367 |
// Retrieve the time-out parameters for all read and write operations on the port. |
| 368 |
if (!GetCommTimeouts(hPort, &CommTimeouts)) |
| 369 |
{ // Unable to get the time-out parameters |
| 370 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 371 |
throw_java_exceptionW(env, IO_EXCEPTION, L"NativegetReceiveTimeout - GetCommTimeouts", lpMsgBuf); |
| 372 |
// Free the buffers. |
| 373 |
ReleaseErrorMsg(lpMsgBuf); |
| 374 |
return -1; |
| 375 |
} |
| 376 |
|
| 377 |
// As far as I know c_cc is byte table |
| 378 |
return CommTimeouts.ReadIntervalTimeout/100 <= 255 ? CommTimeouts.ReadIntervalTimeout / 100 : 255; |
| 379 |
} |
| 380 |
|
| 381 |
/* |
| 382 |
NativeisReceiveTimeoutEnabled |
| 383 |
|
| 384 |
accept: none |
| 385 |
perform: determine if VTIME is none 0 |
| 386 |
return: JNI_TRUE if VTIME > 0 else JNI_FALSE |
| 387 |
comments: see NativeEnableReceiveTimeoutThreshold |
| 388 |
* Class: gnu_io_RXTXPort |
| 389 |
* Method: NativeisReceiveTimeoutEnabled |
| 390 |
* Signature: ()Z |
| 391 |
*/ |
| 392 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXPort_NativeisReceiveTimeoutEnabled(JNIEnv *env, jobject jobj) |
| 393 |
{ |
| 394 |
COMMTIMEOUTS CommTimeouts; |
| 395 |
LPCWSTR lpMsgBuf; |
| 396 |
HANDLE hPort = get_fd(env, jobj); |
| 397 |
|
| 398 |
IF_DEBUG |
| 399 |
( |
| 400 |
printj(env, L"--- RXTXPort.NativeisReceiveTimeoutEnabled() called\n"); |
| 401 |
) |
| 402 |
|
| 403 |
// Retrieve the time-out parameters for all read and write operations |
| 404 |
// on the port. |
| 405 |
if (!GetCommTimeouts(hPort, &CommTimeouts)) |
| 406 |
{ // Unable to get the time-out parameters |
| 407 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 408 |
throw_java_exceptionW(env, IO_EXCEPTION, L"NativeisReceiveTimeoutEnabled - GetCommTimeouts", lpMsgBuf); |
| 409 |
// Free the buffers. |
| 410 |
ReleaseErrorMsg(lpMsgBuf); |
| 411 |
return -1; |
| 412 |
} |
| 413 |
|
| 414 |
return CommTimeouts.ReadIntervalTimeout || CommTimeouts.ReadTotalTimeoutConstant || CommTimeouts.ReadTotalTimeoutMultiplier ? JNI_TRUE : JNI_FALSE; |
| 415 |
} |
| 416 |
|
| 417 |
/* |
| 418 |
NativeEnableReceiveTimeoutThreshold |
| 419 |
accept: int threshold, int vtime,int buffer |
| 420 |
perform: Set c_cc->VMIN to threshold and c_cc=>VTIME to vtime |
| 421 |
return: void |
| 422 |
exceptions: IOException |
| 423 |
comments: This is actually all handled in read with select in |
| 424 |
canonical input mode. |
| 425 |
* Class: gnu_io_RXTXPort |
| 426 |
* Method: NativeEnableReceiveTimeoutThreshold |
| 427 |
* Signature: (III)V |
| 428 |
*/ |
| 429 |
JNIEXPORT void JNICALL Java_gnu_io_RXTXPort_NativeEnableReceiveTimeoutThreshold(JNIEnv *env, jobject jobj, jint time, jint threshold, jint InputBuffer) |
| 430 |
{ |
| 431 |
COMMTIMEOUTS CommTimeouts; |
| 432 |
LPCWSTR lpMsgBuf; |
| 433 |
HANDLE hPort = get_fd(env, jobj); |
| 434 |
|
| 435 |
IF_DEBUG |
| 436 |
( |
| 437 |
printj(env, L"--- RXTXPort.NativeEnableReceiveTimeoutThreshold(%ld, %ld) called\n", time, threshold); |
| 438 |
) |
| 439 |
|
| 440 |
// Retrieve the time-out parameters for all read and write operations |
| 441 |
// on the port. |
| 442 |
if (!GetCommTimeouts(hPort, &CommTimeouts)) |
| 443 |
{ // Unable to get the time-out parameters |
| 444 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 445 |
throw_java_exceptionW(env, IO_EXCEPTION, L"NativeEnableReceiveTimeoutThreshold - GetCommTimeouts", lpMsgBuf); |
| 446 |
// Free the buffers. |
| 447 |
ReleaseErrorMsg(lpMsgBuf); |
| 448 |
return; |
| 449 |
} |
| 450 |
|
| 451 |
/* ------ from javax.comm.CommPort javadoc ------------------------------------------------------------- |
| 452 |
| Threshold | Timeout |Read Buffer Size | Read Behaviour |
| 453 |
|State |Value |State |Value| | |
| 454 |
|disabled| - |disabled| - | n bytes | block until any data is available |
| 455 |
|enabled |m bytes|disabled| - | n bytes | block until min(m,n) bytes are available |
| 456 |
|disabled| - |enabled |x ms | n bytes | block for x ms or until any data is available |
| 457 |
|enabled |m bytes|enabled |x ms | n bytes | block for x ms or until min(m,n) bytes are available |
| 458 |
-------------------------------------------------------------------------------------------------------- |
| 459 |
|
| 460 |
Enabling the Timeout OR Threshold with a value a zero is a special case. |
| 461 |
This causes the underlying driver to poll for incoming data instead being event driven. |
| 462 |
Otherwise, the behaviour is identical to having both the Timeout and Threshold disabled. |
| 463 |
*/ |
| 464 |
|
| 465 |
// Following is based on my understanding of timeout parameters meaning. |
| 466 |
// Not completely precise (threshold?!) |
| 467 |
|
| 468 |
if(time == 0) |
| 469 |
{ // polling mode - return if no data |
| 470 |
CommTimeouts.ReadIntervalTimeout = MAXDWORD; |
| 471 |
CommTimeouts.ReadTotalTimeoutMultiplier = MAXDWORD; |
| 472 |
CommTimeouts.ReadTotalTimeoutConstant = 1; |
| 473 |
} |
| 474 |
else if(time == -1) |
| 475 |
{ // disable timeout |
| 476 |
CommTimeouts.ReadIntervalTimeout = 0; |
| 477 |
CommTimeouts.ReadTotalTimeoutMultiplier = 0; |
| 478 |
CommTimeouts.ReadTotalTimeoutConstant = 0; |
| 479 |
} |
| 480 |
else |
| 481 |
{ // set timeout |
| 482 |
CommTimeouts.ReadIntervalTimeout = 0; |
| 483 |
CommTimeouts.ReadTotalTimeoutMultiplier = 0; |
| 484 |
CommTimeouts.ReadTotalTimeoutConstant = time; |
| 485 |
} |
| 486 |
|
| 487 |
// Set the time-out parameters for all read and write operations |
| 488 |
// on the port. |
| 489 |
if (!SetCommTimeouts (hPort, &CommTimeouts)) |
| 490 |
{ // Unable to set the time-out parameters |
| 491 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 492 |
throw_java_exceptionW(env, IO_EXCEPTION, L"NativeEnableReceiveTimeoutThreshold - SetCommTimeouts", lpMsgBuf); |
| 493 |
// Free the buffers. |
| 494 |
ReleaseErrorMsg(lpMsgBuf); |
| 495 |
return; |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
/* |
| 500 |
isDTR |
| 501 |
|
| 502 |
accept: none |
| 503 |
perform: check status of DTR |
| 504 |
return: true if TIOCM_DTR is set |
| 505 |
false if TIOCM_DTR is not set |
| 506 |
exceptions: none |
| 507 |
comments: DTR stands for Data Terminal Ready |
| 508 |
* Class: gnu_io_RXTXPort |
| 509 |
* Method: isDTR |
| 510 |
* Signature: ()Z |
| 511 |
*/ |
| 512 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXPort_isDTR(JNIEnv *env, jobject jobj) |
| 513 |
{ |
| 514 |
DCB PortDCB; |
| 515 |
HANDLE hPort = get_fd(env, jobj); |
| 516 |
|
| 517 |
PortDCB.DCBlength = sizeof(DCB); |
| 518 |
|
| 519 |
// Get the default port setting information. |
| 520 |
if(!GetCommState(hPort, &PortDCB)) |
| 521 |
{ |
| 522 |
GetLastError(); |
| 523 |
return JNI_FALSE; |
| 524 |
} |
| 525 |
|
| 526 |
// This is not completely correct: I assume that nobody's playing with |
| 527 |
// manually setting/resetting DTR. Instead, DTR is in handshake mode (ON unless |
| 528 |
// port closed) or disable mode (OFF). |
| 529 |
// So, don't use EscapeCommFunction(SETDTR/CLRDTR) in this library! |
| 530 |
return PortDCB.fDtrControl != DTR_CONTROL_DISABLE ? JNI_TRUE : JNI_FALSE; |
| 531 |
} |
| 532 |
|
| 533 |
/* |
| 534 |
setDTR |
| 535 |
|
| 536 |
accept: new DTR state |
| 537 |
perform: if state is true, TIOCM_DTR is set |
| 538 |
if state is false, TIOCM_DTR is unset |
| 539 |
return: none |
| 540 |
exceptions: none |
| 541 |
comments: DTR stands for Data Terminal Ready |
| 542 |
* Class: gnu_io_RXTXPort |
| 543 |
* Method: setDTR |
| 544 |
* Signature: (Z)V |
| 545 |
*/ |
| 546 |
JNIEXPORT void JNICALL Java_gnu_io_RXTXPort_setDTR(JNIEnv *env, jobject jobj, jboolean state) |
| 547 |
{ |
| 548 |
DCB PortDCB; |
| 549 |
|
| 550 |
HANDLE hPort = get_fd(env, jobj); |
| 551 |
PortDCB.DCBlength = sizeof(DCB); |
| 552 |
|
| 553 |
// Don't use EscapeCommFunction(SETDTR/CLRDTR) in this library! |
| 554 |
|
| 555 |
// Get the default port setting information. |
| 556 |
if(!GetCommState (hPort, &PortDCB)) |
| 557 |
{ //Unable to get configuration of the serial port |
| 558 |
GetLastError(); |
| 559 |
return; |
| 560 |
} |
| 561 |
|
| 562 |
if(state == JNI_TRUE) |
| 563 |
PortDCB.fDtrControl = DTR_CONTROL_HANDSHAKE; |
| 564 |
else |
| 565 |
PortDCB.fDtrControl = DTR_CONTROL_DISABLE; |
| 566 |
|
| 567 |
|
| 568 |
// Configure the port according to the specifications of the DCB structure. |
| 569 |
if (!SetCommState (hPort, &PortDCB)) |
| 570 |
{ //Unable to configure the serial port |
| 571 |
GetLastError(); |
| 572 |
return; |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
/* |
| 577 |
setRTS |
| 578 |
|
| 579 |
accept: state flag to set/unset. |
| 580 |
perform: depends on the state flag |
| 581 |
if true TIOCM_RTS is set |
| 582 |
if false TIOCM_RTS is unset |
| 583 |
return: none |
| 584 |
exceptions: none |
| 585 |
comments: tcsetattr with c_cflag CRTS_IFLOW |
| 586 |
* Class: gnu_io_RXTXPort |
| 587 |
* Method: setRTS |
| 588 |
* Signature: (Z)V |
| 589 |
*/ |
| 590 |
JNIEXPORT void JNICALL Java_gnu_io_RXTXPort_setRTS(JNIEnv *env, jobject jobj, jboolean state) |
| 591 |
{ |
| 592 |
HANDLE hPort = get_fd(env, jobj); |
| 593 |
|
| 594 |
// EscapeCommFunction will fail if there's RTS/CTS flowcontrol. If it is incorrect, |
| 595 |
// we could turn flowcontrol off when getting to this function |
| 596 |
if(state == JNI_TRUE) |
| 597 |
EscapeCommFunction(hPort, SETRTS); |
| 598 |
else |
| 599 |
EscapeCommFunction(hPort, CLRRTS); |
| 600 |
} |
| 601 |
|
| 602 |
/* |
| 603 |
setDSR |
| 604 |
|
| 605 |
accept: state flag to set/unset. |
| 606 |
perform: depends on the state flag |
| 607 |
if true TIOCM_DSR is set |
| 608 |
if false TIOCM_DSR is unset |
| 609 |
return: none |
| 610 |
exceptions: none |
| 611 |
comments: tcsetattr with c_cflag CRTS_IFLOW |
| 612 |
* Class: gnu_io_RXTXPort |
| 613 |
* Method: setDSR |
| 614 |
* Signature: (Z)V |
| 615 |
*/ |
| 616 |
JNIEXPORT void JNICALL Java_gnu_io_RXTXPort_setDSR(JNIEnv *env, jobject jobj, jboolean state) |
| 617 |
{ |
| 618 |
DCB PortDCB; |
| 619 |
|
| 620 |
HANDLE hPort = get_fd(env, jobj); |
| 621 |
PortDCB.DCBlength = sizeof(DCB); |
| 622 |
|
| 623 |
// Get the default port setting information. |
| 624 |
if(!GetCommState (hPort, &PortDCB)) |
| 625 |
{ //Unable to get configuration of the serial port |
| 626 |
GetLastError(); |
| 627 |
return; |
| 628 |
} |
| 629 |
|
| 630 |
// Change the DCB structure settings. |
| 631 |
if(state != JNI_FALSE) |
| 632 |
PortDCB.fOutxDsrFlow = TRUE; |
| 633 |
else |
| 634 |
PortDCB.fOutxDsrFlow = FALSE; |
| 635 |
|
| 636 |
// Configure the port according to the specifications of the DCB |
| 637 |
// structure. |
| 638 |
if (!SetCommState (hPort, &PortDCB)) |
| 639 |
{ //Unable to configure the serial port |
| 640 |
GetLastError(); |
| 641 |
return; |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
/* |
| 646 |
isCTS |
| 647 |
|
| 648 |
accept: none |
| 649 |
perform: check status of CTS |
| 650 |
return: true if TIOCM_CTS is set |
| 651 |
false if TIOCM_CTS is not set |
| 652 |
exceptions: none |
| 653 |
comments: CTS stands for Clear To Send. |
| 654 |
* Class: gnu_io_RXTXPort |
| 655 |
* Method: isCTS |
| 656 |
* Signature: ()Z |
| 657 |
*/ |
| 658 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXPort_isCTS(JNIEnv *env, jobject jobj) |
| 659 |
{ |
| 660 |
DWORD ModemStat; |
| 661 |
HANDLE hPort = get_fd(env, jobj); |
| 662 |
|
| 663 |
if(!GetCommModemStatus(hPort, &ModemStat)) |
| 664 |
return JNI_FALSE; |
| 665 |
|
| 666 |
return ModemStat|MS_CTS_ON ? JNI_TRUE : JNI_FALSE; |
| 667 |
} |
| 668 |
|
| 669 |
/* |
| 670 |
isDSR |
| 671 |
|
| 672 |
accept: none |
| 673 |
perform: check status of DSR |
| 674 |
return: true if TIOCM_DSR is set |
| 675 |
false if TIOCM_DSR is not set |
| 676 |
exceptions: none |
| 677 |
comments: DSR stands for Data Set Ready |
| 678 |
* Class: gnu_io_RXTXPort |
| 679 |
* Method: isDSR |
| 680 |
* Signature: ()Z |
| 681 |
*/ |
| 682 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXPort_isDSR(JNIEnv *env, jobject jobj) |
| 683 |
{ |
| 684 |
DWORD ModemStat; |
| 685 |
HANDLE hPort = get_fd(env, jobj); |
| 686 |
|
| 687 |
if(!GetCommModemStatus(hPort, &ModemStat)) |
| 688 |
return JNI_FALSE; |
| 689 |
|
| 690 |
return ModemStat|MS_DSR_ON ? JNI_TRUE : JNI_FALSE; |
| 691 |
} |
| 692 |
|
| 693 |
/* |
| 694 |
isCD |
| 695 |
|
| 696 |
accept: none |
| 697 |
perform: check status of CD |
| 698 |
return: true if TIOCM_CD is set |
| 699 |
false if TIOCM_CD is not set |
| 700 |
exceptions: none |
| 701 |
comments: CD stands for Carrier Detect |
| 702 |
The following comment has been made... |
| 703 |
"well, it works, there might ofcourse be a bug, but making DCD |
| 704 |
permanently on fixed it for me so I don't care" |
| 705 |
* Class: gnu_io_RXTXPort |
| 706 |
* Method: isCD |
| 707 |
* Signature: ()Z |
| 708 |
*/ |
| 709 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXPort_isCD(JNIEnv *env, jobject jobj) |
| 710 |
{ |
| 711 |
DWORD ModemStat; |
| 712 |
HANDLE hPort = get_fd(env, jobj); |
| 713 |
|
| 714 |
if(!GetCommModemStatus(hPort, &ModemStat)) |
| 715 |
return JNI_FALSE; |
| 716 |
|
| 717 |
return ModemStat|MS_RLSD_ON ? JNI_TRUE : JNI_FALSE; |
| 718 |
} |
| 719 |
|
| 720 |
/* |
| 721 |
isRI |
| 722 |
|
| 723 |
accept: none |
| 724 |
perform: check status of RI |
| 725 |
return: true if TIOCM_RI is set |
| 726 |
false if TIOCM_RI is not set |
| 727 |
exceptions: none |
| 728 |
comments: RI stands for Ring Indicator |
| 729 |
* Class: gnu_io_RXTXPort |
| 730 |
* Method: isRI |
| 731 |
* Signature: ()Z |
| 732 |
*/ |
| 733 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXPort_isRI(JNIEnv *env, jobject jobj) |
| 734 |
{ |
| 735 |
DWORD ModemStat; |
| 736 |
HANDLE hPort = get_fd(env, jobj); |
| 737 |
|
| 738 |
if(!GetCommModemStatus(hPort, &ModemStat)) |
| 739 |
return JNI_FALSE; |
| 740 |
|
| 741 |
return ModemStat|MS_RING_ON ? JNI_TRUE : JNI_FALSE; |
| 742 |
} |
| 743 |
|
| 744 |
/* |
| 745 |
isRTS |
| 746 |
|
| 747 |
accept: none |
| 748 |
perform: check status of RTS |
| 749 |
return: true if TIOCM_RTS is set |
| 750 |
false if TIOCM_RTS is not set |
| 751 |
exceptions: none |
| 752 |
comments: tcgetattr with c_cflag CRTS_IFLOW |
| 753 |
* Class: gnu_io_RXTXPort |
| 754 |
* Method: isRTS |
| 755 |
* Signature: ()Z |
| 756 |
*/ |
| 757 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXPort_isRTS(JNIEnv *env, jobject jobj) |
| 758 |
{ |
| 759 |
DCB PortDCB; |
| 760 |
HANDLE hPort = get_fd(env, jobj); |
| 761 |
|
| 762 |
PortDCB.DCBlength = sizeof(DCB); |
| 763 |
if(!GetCommState (hPort, &PortDCB)) |
| 764 |
{ //Unable to get configuration of the serial port |
| 765 |
GetLastError(); |
| 766 |
return JNI_FALSE; |
| 767 |
} |
| 768 |
|
| 769 |
if(PortDCB.fRtsControl == RTS_CONTROL_DISABLE) |
| 770 |
return JNI_FALSE; |
| 771 |
|
| 772 |
if(PortDCB.fRtsControl == RTS_CONTROL_ENABLE) |
| 773 |
return JNI_TRUE; |
| 774 |
|
| 775 |
if(PortDCB.fRtsControl == RTS_CONTROL_HANDSHAKE) |
| 776 |
{ |
| 777 |
//MessageBox(NULL, L"Undefined condition: isRTS() but control is RTS_CONTROL_HANDSHAKE\r\nReturning FALSE", L"Warning", MB_OK | MB_ICONWARNING | MB_SETFOREGROUND); |
| 778 |
printj(env, L"Undefined condition: isRTS() but control is RTS_CONTROL_HANDSHAKE. Returning FALSE\n"); |
| 779 |
return JNI_FALSE; |
| 780 |
} |
| 781 |
|
| 782 |
return JNI_FALSE; |
| 783 |
} |
| 784 |
|
| 785 |
/* |
| 786 |
sendBreak |
| 787 |
|
| 788 |
accept: duration in milliseconds. |
| 789 |
perform: send break for actual time. not less than 0.25 seconds. |
| 790 |
exceptions: none |
| 791 |
comments: not very precise |
| 792 |
* Class: gnu_io_RXTXPort |
| 793 |
* Method: sendBreak |
| 794 |
* Signature: (I)V |
| 795 |
*/ |
| 796 |
JNIEXPORT void JNICALL Java_gnu_io_RXTXPort_sendBreak(JNIEnv *env, jobject jobj, jint duration) |
| 797 |
{ |
| 798 |
HANDLE hPort = get_fd(env, jobj); |
| 799 |
|
| 800 |
SetCommBreak(hPort); |
| 801 |
Sleep(duration); |
| 802 |
ClearCommBreak(hPort); |
| 803 |
} |
| 804 |
|
| 805 |
/* |
| 806 |
writeByte |
| 807 |
|
| 808 |
accept: byte to write (passed as int) |
| 809 |
perform: write a single byte to the port |
| 810 |
return: none |
| 811 |
exceptions: IOException |
| 812 |
* Class: gnu_io_RXTXPort |
| 813 |
* Method: writeByte |
| 814 |
* Signature: (IZ)V |
| 815 |
*/ |
| 816 |
JNIEXPORT void JNICALL Java_gnu_io_RXTXPort_writeByte(JNIEnv *env, jobject jobj, jint b, jboolean i) |
| 817 |
{ |
| 818 |
DWORD dwNumBytesWritten; |
| 819 |
LPCWSTR lpMsgBuf; |
| 820 |
HANDLE hPort = get_fd(env, jobj); |
| 821 |
BYTE bb = (BYTE)b; |
| 822 |
|
| 823 |
do { |
| 824 |
if (!WriteFile(hPort, // Port handle |
| 825 |
&bb, // Pointer to the data to write |
| 826 |
1, // Number of bytes to write |
| 827 |
&dwNumBytesWritten, // Pointer to the number of bytes written |
| 828 |
NULL)) // Must be NULL for Windows CE |
| 829 |
{ // WriteFile failed. Report error. |
| 830 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 831 |
throw_java_exceptionW(env, IO_EXCEPTION, L"writeByte - WriteFile", lpMsgBuf ); |
| 832 |
// Free the buffers. |
| 833 |
ReleaseErrorMsg(lpMsgBuf); |
| 834 |
return; |
| 835 |
} |
| 836 |
} while(dwNumBytesWritten == 0); |
| 837 |
} |
| 838 |
|
| 839 |
/* |
| 840 |
writeArray |
| 841 |
|
| 842 |
accept: jbarray: bytes used for writing |
| 843 |
off: offset in array to start writing |
| 844 |
len: Number of bytes to write |
| 845 |
perform: write length bytes of jbarray |
| 846 |
return: none |
| 847 |
exceptions: IOException |
| 848 |
* Class: gnu_io_RXTXPort |
| 849 |
* Method: writeArray |
| 850 |
* Signature: ([BIIZ)V |
| 851 |
*/ |
| 852 |
JNIEXPORT void JNICALL Java_gnu_io_RXTXPort_writeArray(JNIEnv *env, jobject jobj, jbyteArray b, jint off, jint len, jboolean i) |
| 853 |
{ |
| 854 |
LPCWSTR lpMsgBuf; |
| 855 |
DWORD dwNumBytesWritten; |
| 856 |
jint total=0; |
| 857 |
HANDLE hPort = get_fd(env, jobj); |
| 858 |
|
| 859 |
jbyte *body = env->GetByteArrayElements(b, NULL); |
| 860 |
|
| 861 |
do { |
| 862 |
IF_DEBUG |
| 863 |
( |
| 864 |
printj(env, L"--- writeArray - %d bytes to write\n", len-total); |
| 865 |
) |
| 866 |
if (!WriteFile(hPort, // Port handle |
| 867 |
body+total+off, // Pointer to the data to write |
| 868 |
len-total, // Number of bytes to write |
| 869 |
&dwNumBytesWritten, // Pointer to the number of bytes written |
| 870 |
NULL)) // Must be NULL for Windows CE |
| 871 |
{ // WriteFile failed. Report error. |
| 872 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 873 |
throw_java_exceptionW(env, IO_EXCEPTION, L"writeArray - WriteFile", lpMsgBuf ); |
| 874 |
// Free the buffers. |
| 875 |
ReleaseErrorMsg(lpMsgBuf); |
| 876 |
env->ReleaseByteArrayElements(b, body, JNI_ABORT); |
| 877 |
return; |
| 878 |
} |
| 879 |
total += dwNumBytesWritten; |
| 880 |
} while( total < len ); |
| 881 |
|
| 882 |
env->ReleaseByteArrayElements(b, body, JNI_ABORT); |
| 883 |
} |
| 884 |
|
| 885 |
/* |
| 886 |
nativeDrain |
| 887 |
|
| 888 |
accept: none |
| 889 |
perform: wait until all data is transmitted |
| 890 |
return: none |
| 891 |
exceptions: IOException |
| 892 |
comments: java.io.OutputStream.flush() is equivalent to tcdrain, |
| 893 |
not tcflush, which throws away unsent bytes |
| 894 |
|
| 895 |
count logic added to avoid infinite loops when EINTR is |
| 896 |
true... Thread.yeild() was suggested. |
| 897 |
* Class: gnu_io_RXTXPort |
| 898 |
* Method: nativeDrain |
| 899 |
* Signature: ()V |
| 900 |
*/ |
| 901 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXPort_nativeDrain(JNIEnv *env, jobject jobj, jboolean i) |
| 902 |
{ |
| 903 |
//COMSTAT Stat; |
| 904 |
//DWORD dwErrors; |
| 905 |
HANDLE hPort = get_fd(env, jobj); |
| 906 |
|
| 907 |
IF_DEBUG |
| 908 |
( |
| 909 |
printj(env, L"--- RXTXPort.nativeDrain() called\n"); |
| 910 |
) |
| 911 |
/* |
| 912 |
if(!FlushFileBuffers(hPort)) |
| 913 |
{ |
| 914 |
IF_DEBUG |
| 915 |
( |
| 916 |
printj(env, L"!!! FlushFileBuffers() error %ld\n", GetLastError()); |
| 917 |
) |
| 918 |
return JNI_FALSE; |
| 919 |
} |
| 920 |
*/ |
| 921 |
// Alternative implementation: |
| 922 |
/* |
| 923 |
do |
| 924 |
{ |
| 925 |
if(!ClearCommError(hPort, &dwErrors, &Stat)) |
| 926 |
{ |
| 927 |
GetLastError(); |
| 928 |
return; |
| 929 |
} |
| 930 |
Sleep(10); |
| 931 |
} while(Stat.cbOutQue > 0);*/ |
| 932 |
|
| 933 |
return JNI_FALSE; |
| 934 |
} |
| 935 |
|
| 936 |
/* |
| 937 |
nativeavailable |
| 938 |
|
| 939 |
accept: none |
| 940 |
perform: find out the number of bytes available for reading |
| 941 |
return: available bytes |
| 942 |
-1 on error |
| 943 |
exceptions: none /// should be IOException |
| 944 |
* Class: gnu_io_RXTXPort |
| 945 |
* Method: nativeavailable |
| 946 |
* Signature: ()I |
| 947 |
*/ |
| 948 |
JNIEXPORT jint JNICALL Java_gnu_io_RXTXPort_nativeavailable(JNIEnv *env, jobject jobj) |
| 949 |
{ |
| 950 |
DWORD dwErrors; |
| 951 |
COMSTAT Stat; |
| 952 |
LPCWSTR lpMsgBuf; |
| 953 |
HANDLE hPort = get_fd(env, jobj); |
| 954 |
|
| 955 |
IF_DEBUG |
| 956 |
( |
| 957 |
printj(env, L"--- RXTXPort.nativeavailable() called"); |
| 958 |
) |
| 959 |
|
| 960 |
if(!ClearCommError(hPort, &dwErrors, &Stat)) |
| 961 |
{ |
| 962 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 963 |
throw_java_exceptionW(env, IO_EXCEPTION, L"nativeavailable - ClearCommError", lpMsgBuf ); |
| 964 |
// Free the buffers. |
| 965 |
ReleaseErrorMsg(lpMsgBuf); |
| 966 |
return -1; |
| 967 |
} |
| 968 |
|
| 969 |
IF_DEBUG |
| 970 |
( |
| 971 |
printj(env, L" - returning %ld\n", Stat.cbInQue); |
| 972 |
) |
| 973 |
|
| 974 |
return Stat.cbInQue; |
| 975 |
} |
| 976 |
|
| 977 |
/* |
| 978 |
readByte |
| 979 |
|
| 980 |
accept: none |
| 981 |
perform: Read a single byte from the port |
| 982 |
return: The byte read |
| 983 |
exceptions: IOException |
| 984 |
* Class: gnu_io_RXTXPort |
| 985 |
* Method: readByte |
| 986 |
* Signature: ()I |
| 987 |
*/ |
| 988 |
JNIEXPORT jint JNICALL Java_gnu_io_RXTXPort_readByte(JNIEnv *env, jobject jobj) |
| 989 |
{ |
| 990 |
BYTE bb; |
| 991 |
DWORD dwNumBytesRead; |
| 992 |
LPCWSTR lpMsgBuf; |
| 993 |
HANDLE hPort = get_fd(env, jobj); |
| 994 |
|
| 995 |
if (!ReadFile(hPort, &bb, 1, &dwNumBytesRead, NULL)) |
| 996 |
{ // ReadFile failed. Report error. |
| 997 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 998 |
throw_java_exceptionW(env, IO_EXCEPTION, L"readByte - ReadFile", lpMsgBuf); |
| 999 |
// Free the buffers. |
| 1000 |
ReleaseErrorMsg(lpMsgBuf); |
| 1001 |
return -1; |
| 1002 |
} |
| 1003 |
|
| 1004 |
return dwNumBytesRead > 0 ? bb : -1; |
| 1005 |
} |
| 1006 |
|
| 1007 |
/* |
| 1008 |
readArray |
| 1009 |
|
| 1010 |
accept: offset (offset to start storing data in the jbarray) and |
| 1011 |
Length (bytes to read) |
| 1012 |
perform: read bytes from the port into a byte array |
| 1013 |
return: bytes read on success |
| 1014 |
0 on read timeout |
| 1015 |
exceptions: IOException |
| 1016 |
comments: throws ArrayIndexOutOfBoundsException if asked to |
| 1017 |
read more than SSIZE_MAX bytes |
| 1018 |
* Class: gnu_io_RXTXPort |
| 1019 |
* Method: readArray |
| 1020 |
* Signature: ([BII)I |
| 1021 |
*/ |
| 1022 |
JNIEXPORT jint JNICALL Java_gnu_io_RXTXPort_readArray(JNIEnv *env, jobject jobj, jbyteArray b, jint off, jint len) |
| 1023 |
{ |
| 1024 |
LPCWSTR lpMsgBuf; |
| 1025 |
DWORD dwNumBytesRead, dwTotalRead = 0; |
| 1026 |
jint threshold; |
| 1027 |
HANDLE hPort = get_fd(env, jobj); |
| 1028 |
|
| 1029 |
if(len < 0) |
| 1030 |
{ |
| 1031 |
throw_java_exceptionW(env, ARRAY_INDEX_OUT_OF_BOUNDS, L"readArray", L"Negative number of character to read"); |
| 1032 |
return -1; |
| 1033 |
} |
| 1034 |
|
| 1035 |
jbyte *body = env->GetByteArrayElements(b, NULL); |
| 1036 |
threshold = get_java_int_var(env, jobj, "threshold"); |
| 1037 |
|
| 1038 |
do |
| 1039 |
{ |
| 1040 |
if (!ReadFile(hPort, (unsigned char *)(body+off), len, &dwNumBytesRead, NULL)) |
| 1041 |
{ // ReadFile failed. Report error. |
| 1042 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 1043 |
throw_java_exceptionW(env, IO_EXCEPTION, L"readArray - WriteFile", lpMsgBuf ); |
| 1044 |
// Free the buffers. |
| 1045 |
ReleaseErrorMsg(lpMsgBuf); |
| 1046 |
env->ReleaseByteArrayElements(b, body, JNI_ABORT); |
| 1047 |
return -1; |
| 1048 |
} |
| 1049 |
|
| 1050 |
dwTotalRead += dwNumBytesRead; |
| 1051 |
} while(dwNumBytesRead > 0 && threshold != 0 && dwTotalRead <= (DWORD)len && dwTotalRead < (DWORD)threshold); |
| 1052 |
|
| 1053 |
env->ReleaseByteArrayElements(b, body, 0); |
| 1054 |
return dwTotalRead; |
| 1055 |
} |
| 1056 |
|
| 1057 |
/* |
| 1058 |
eventLoop |
| 1059 |
|
| 1060 |
accept: none |
| 1061 |
perform: periodically check for SerialPortEvents |
| 1062 |
return: none |
| 1063 |
exceptions: none |
| 1064 |
comments: please keep this function clean. |
| 1065 |
* Class: gnu_io_RXTXPort |
| 1066 |
* Method: eventLoop |
| 1067 |
* Signature: ()V |
| 1068 |
*/ |
| 1069 |
JNIEXPORT void JNICALL Java_gnu_io_RXTXPort_eventLoop(JNIEnv *env, jobject jobj) |
| 1070 |
{ |
| 1071 |
jfieldID jfMonitorThreadCloseLock, jfMonitorThreadLock, jfMonThreadisInterrupted; |
| 1072 |
jmethodID jmSendEvent; |
| 1073 |
HANDLE hCommEventThread; |
| 1074 |
DWORD dwThreadID, dwWaitResult, dwEvent; |
| 1075 |
int RetVal; |
| 1076 |
HANDLE hPort = get_fd(env, jobj); |
| 1077 |
EventInfoStruct *EventInfo = get_eis(env, jobj); |
| 1078 |
|
| 1079 |
IF_DEBUG |
| 1080 |
( |
| 1081 |
printj(env, L"--- RXTXPort.eventLoop() start\n"); |
| 1082 |
) |
| 1083 |
|
| 1084 |
jclass cls = env->GetObjectClass(jobj); |
| 1085 |
|
| 1086 |
// Get pointers to some Java variables and methods: |
| 1087 |
if( !(jfMonitorThreadLock = env->GetFieldID(cls, "MonitorThreadLock", "Z")) || |
| 1088 |
!(jfMonThreadisInterrupted = env->GetFieldID(cls, "monThreadisInterrupted", "Z")) || |
| 1089 |
!(jfMonitorThreadCloseLock = env->GetFieldID(cls, "MonitorThreadCloseLock", "Z")) || |
| 1090 |
!(jmSendEvent = env->GetMethodID(cls, "sendEvent", "(IZ)Z")) |
| 1091 |
) |
| 1092 |
{ |
| 1093 |
IF_DEBUG |
| 1094 |
( |
| 1095 |
env->ExceptionDescribe(); |
| 1096 |
) |
| 1097 |
env->ExceptionClear(); |
| 1098 |
return; |
| 1099 |
} |
| 1100 |
|
| 1101 |
hCommEventThread = CreateThread(NULL, 0, CommEventThread, (LPVOID)EventInfo, 0, &dwThreadID); |
| 1102 |
if(hCommEventThread == NULL) |
| 1103 |
{ |
| 1104 |
LPCWSTR lpMsgBuf; |
| 1105 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 1106 |
printj(env, L"!!! eventLoop - CreateThread() error: %s\n", lpMsgBuf); |
| 1107 |
ReleaseErrorMsg(lpMsgBuf); |
| 1108 |
return; |
| 1109 |
} |
| 1110 |
|
| 1111 |
CloseHandle(hCommEventThread); |
| 1112 |
|
| 1113 |
do |
| 1114 |
{ |
| 1115 |
if(env->GetBooleanField(jobj, jfMonThreadisInterrupted) == JNI_TRUE) |
| 1116 |
{ |
| 1117 |
IF_DEBUG |
| 1118 |
( |
| 1119 |
printj(env, L"--- RXTXPort.eventLoop() interrupted - exiting\n"); |
| 1120 |
) |
| 1121 |
|
| 1122 |
CloseHandle(EventInfo->eventHandle); |
| 1123 |
|
| 1124 |
env->SetBooleanField(jobj, jfMonitorThreadCloseLock, JNI_FALSE); |
| 1125 |
return; |
| 1126 |
} |
| 1127 |
|
| 1128 |
if(EventInfo->eventThreadReady) |
| 1129 |
{ // Thread is ready to work - pass signal to Java |
| 1130 |
IF_DEBUG |
| 1131 |
( |
| 1132 |
printj(env, L"--- RXTXPort.eventLoop() - EventThread is ready\n"); |
| 1133 |
) |
| 1134 |
env->SetBooleanField(jobj, jfMonitorThreadLock, JNI_FALSE); |
| 1135 |
EventInfo->eventThreadReady = false; |
| 1136 |
} |
| 1137 |
|
| 1138 |
if((dwWaitResult = WaitForSingleObject(EventInfo->eventHandle, 250)) == WAIT_FAILED) |
| 1139 |
{ |
| 1140 |
IF_DEBUG |
| 1141 |
( |
| 1142 |
LPCWSTR lpMsgBuf; |
| 1143 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 1144 |
printj(env, L"!!! eventLoop - WaitForSingleObject() error: %s\n", lpMsgBuf); |
| 1145 |
ReleaseErrorMsg(lpMsgBuf); |
| 1146 |
) |
| 1147 |
CloseHandle(EventInfo->eventHandle); |
| 1148 |
env->SetBooleanField(jobj, jfMonitorThreadCloseLock, JNI_FALSE); |
| 1149 |
return; |
| 1150 |
} |
| 1151 |
|
| 1152 |
if(dwWaitResult == WAIT_OBJECT_0) |
| 1153 |
{ |
| 1154 |
dwEvent = EventInfo->event; |
| 1155 |
// Clearing event - event thread will continue |
| 1156 |
EventInfo->event = 0; |
| 1157 |
// Send events to Java |
| 1158 |
if(RetVal = SendEvents(env, jobj, dwEvent, EventInfo, jmSendEvent)) |
| 1159 |
{ |
| 1160 |
IF_DEBUG |
| 1161 |
( |
| 1162 |
printj(env, L"!!! eventLoop - SendEvents() result: %d\n", RetVal); |
| 1163 |
) |
| 1164 |
CloseHandle(EventInfo->eventHandle); |
| 1165 |
env->SetBooleanField(jobj, jfMonitorThreadCloseLock, JNI_FALSE); |
| 1166 |
return; |
| 1167 |
} |
| 1168 |
} |
| 1169 |
else |
| 1170 |
{ |
| 1171 |
IF_DEBUG |
| 1172 |
( |
| 1173 |
//printj(env, L"--- eventLoop() looping\n"); |
| 1174 |
MessageBeep(MB_OK); |
| 1175 |
) |
| 1176 |
} |
| 1177 |
} while(TRUE); |
| 1178 |
} |
| 1179 |
|
| 1180 |
|
| 1181 |
/* |
| 1182 |
interruptEventLoop |
| 1183 |
|
| 1184 |
accept: nothing |
| 1185 |
perform: sets monThreadisInterrupted |
| 1186 |
return: nothing |
| 1187 |
exceptions: none |
| 1188 |
comments: real monitoring thread will stay until port is closed |
| 1189 |
* Class: gnu_io_RXTXPort |
| 1190 |
* Method: interruptEventLoop |
| 1191 |
* Signature: ()V |
| 1192 |
*/ |
| 1193 |
JNIEXPORT void JNICALL Java_gnu_io_RXTXPort_interruptEventLoop(JNIEnv *env, jobject jobj) |
| 1194 |
{ |
| 1195 |
jfieldID jfid; |
| 1196 |
HANDLE hPort = get_fd(env, jobj); |
| 1197 |
|
| 1198 |
jclass cls = env->GetObjectClass(jobj); |
| 1199 |
jfid = env->GetFieldID(cls, "monThreadisInterrupted", "Z"); |
| 1200 |
env->SetBooleanField(jobj, jfid, JNI_TRUE); |
| 1201 |
} |
| 1202 |
|
| 1203 |
|
| 1204 |
/* |
| 1205 |
nativeSetEventFlag |
| 1206 |
|
| 1207 |
accept: fd for finding the struct, event to flag, flag. |
| 1208 |
perform: toggle the flag |
| 1209 |
return: none |
| 1210 |
exceptions: none |
| 1211 |
comments: all the logic used to be done in Java but its too noisy |
| 1212 |
* Class: gnu_io_RXTXPort |
| 1213 |
* Method: nativeSetEventFlag |
| 1214 |
* Signature: (IIZ)V |
| 1215 |
*/ |
| 1216 |
JNIEXPORT void JNICALL Java_gnu_io_RXTXPort_nativeSetEventFlag(JNIEnv *env, jobject jobj, jint fd, jint event, jboolean flag) |
| 1217 |
{ |
| 1218 |
DWORD dwErr, NewFlag; |
| 1219 |
EventInfoStruct *EventInfo = get_eis(env, jobj); |
| 1220 |
HANDLE hPort = (HANDLE)fd; |
| 1221 |
|
| 1222 |
IF_DEBUG |
| 1223 |
( |
| 1224 |
printj(env, L"--- nativeSetEventFlag(%ld, %d) called\n", event, (int)flag); |
| 1225 |
) |
| 1226 |
|
| 1227 |
switch(event) |
| 1228 |
{ |
| 1229 |
case SPE_DATA_AVAILABLE: |
| 1230 |
NewFlag = EV_RXCHAR; |
| 1231 |
break; |
| 1232 |
|
| 1233 |
case SPE_OUTPUT_BUFFER_EMPTY: |
| 1234 |
NewFlag = EV_TXEMPTY; |
| 1235 |
break; |
| 1236 |
|
| 1237 |
case SPE_CTS: |
| 1238 |
NewFlag = EV_CTS; |
| 1239 |
break; |
| 1240 |
|
| 1241 |
case SPE_DSR: |
| 1242 |
NewFlag = EV_DSR; |
| 1243 |
break; |
| 1244 |
|
| 1245 |
case SPE_RI: |
| 1246 |
NewFlag = EV_RING; |
| 1247 |
break; |
| 1248 |
|
| 1249 |
case SPE_CD: |
| 1250 |
NewFlag = EV_RLSD; |
| 1251 |
break; |
| 1252 |
|
| 1253 |
case SPE_OE: |
| 1254 |
case SPE_PE: |
| 1255 |
case SPE_FE: |
| 1256 |
NewFlag = EV_ERR; |
| 1257 |
break; |
| 1258 |
|
| 1259 |
case SPE_BI: |
| 1260 |
NewFlag = EV_BREAK; |
| 1261 |
break; |
| 1262 |
|
| 1263 |
} |
| 1264 |
|
| 1265 |
if(flag == JNI_TRUE) |
| 1266 |
EventInfo->ef = EventInfo->ef | NewFlag; |
| 1267 |
else |
| 1268 |
EventInfo->ef = EventInfo->ef & ~NewFlag; |
| 1269 |
|
| 1270 |
if(!SetCommMask(hPort, EventInfo->ef)) |
| 1271 |
{ |
| 1272 |
dwErr = GetLastError(); |
| 1273 |
IF_DEBUG |
| 1274 |
( |
| 1275 |
LPCWSTR lpMsgBuf; |
| 1276 |
CreateErrorMsg(dwErr, lpMsgBuf); |
| 1277 |
printj(env, L"!!! nativeSetEventFlag - SetCommMask() error: %ld %s\n", dwErr, lpMsgBuf ); |
| 1278 |
ReleaseErrorMsg(lpMsgBuf); |
| 1279 |
) |
| 1280 |
return; |
| 1281 |
} |
| 1282 |
|
| 1283 |
} |
| 1284 |
|
| 1285 |
/* |
| 1286 |
nativeClose |
| 1287 |
|
| 1288 |
accept: none |
| 1289 |
perform: get the fd from the java end and close it |
| 1290 |
return: none |
| 1291 |
exceptions: none |
| 1292 |
* Class: gnu_io_RXTXPort |
| 1293 |
* Method: nativeClose |
| 1294 |
* Signature: (Ljava/lang/String{ })V |
| 1295 |
*/ |
| 1296 |
JNIEXPORT void JNICALL Java_gnu_io_RXTXPort_nativeClose(JNIEnv *env, jobject jobj, jstring name) |
| 1297 |
{ |
| 1298 |
HANDLE hPort = get_fd(env, jobj); |
| 1299 |
|
| 1300 |
IF_DEBUG |
| 1301 |
( |
| 1302 |
LPCWSTR wszName = env->GetStringChars(name, NULL); |
| 1303 |
printj(env, L"--- RXTXPort.nativeClose(%s) called\n", wszName); |
| 1304 |
env->ReleaseStringChars(name, wszName); |
| 1305 |
) |
| 1306 |
|
| 1307 |
if (!CloseHandle(hPort)) |
| 1308 |
{ |
| 1309 |
GetLastError(); |
| 1310 |
return; |
| 1311 |
} |
| 1312 |
|
| 1313 |
} |
| 1314 |
|
| 1315 |
/* |
| 1316 |
nativeGetParityErrorChar |
| 1317 |
|
| 1318 |
accept: - |
| 1319 |
perform: check the ParityErrorChar |
| 1320 |
return: The ParityErrorChar as an jbyte. |
| 1321 |
exceptions: UnsupportedCommOperationException if not implemented |
| 1322 |
comments: It appears the Parity char is usually \0. The windows |
| 1323 |
API allows for this to be changed. I cant find may |
| 1324 |
examples of this being done. Maybe for a reason. |
| 1325 |
|
| 1326 |
Use a direct call to the termios file until we find a |
| 1327 |
solution. |
| 1328 |
* Class: gnu_io_RXTXPort |
| 1329 |
* Method: nativeGetParityErrorChar |
| 1330 |
* Signature: ()I |
| 1331 |
*/ |
| 1332 |
JNIEXPORT jbyte JNICALL Java_gnu_io_RXTXPort_nativeGetParityErrorChar(JNIEnv *env, jobject jobj) |
| 1333 |
{ |
| 1334 |
DCB PortDCB; |
| 1335 |
LPCWSTR lpMsgBuf; |
| 1336 |
|
| 1337 |
HANDLE hPort = get_fd(env, jobj); |
| 1338 |
PortDCB.DCBlength = sizeof(DCB); |
| 1339 |
|
| 1340 |
IF_DEBUG |
| 1341 |
( |
| 1342 |
printj(env, L"--- RXTXPort.nativeGetParityErrorChar() called\n"); |
| 1343 |
) |
| 1344 |
|
| 1345 |
// Get port setting information. |
| 1346 |
if(!GetCommState(hPort, &PortDCB)) |
| 1347 |
{ //Unable to get configuration of the serial port |
| 1348 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 1349 |
IF_DEBUG |
| 1350 |
( |
| 1351 |
printj(env, L"!!! nativeGetParityErrorChar - GetCommState() error: %s\n", lpMsgBuf); |
| 1352 |
) |
| 1353 |
ReleaseErrorMsg(lpMsgBuf); |
| 1354 |
return -1; |
| 1355 |
} |
| 1356 |
|
| 1357 |
return (jbyte)PortDCB.ErrorChar; |
| 1358 |
} |
| 1359 |
|
| 1360 |
/* |
| 1361 |
nativeSetParityErrorChar |
| 1362 |
|
| 1363 |
accept: the ParityArrorCharacter as an int. |
| 1364 |
perform: Set the ParityErrorChar |
| 1365 |
return: JNI_TRUE on success |
| 1366 |
exceptions: UnsupportedCommOperationException if not implemented |
| 1367 |
comments: It appears the Parity char is usually \0. The windows |
| 1368 |
API allows for this to be changed. I cant find may |
| 1369 |
examples of this being done. Maybe for a reason. |
| 1370 |
|
| 1371 |
Use a direct call to the termios file until we find a |
| 1372 |
solution. |
| 1373 |
* Class: gnu_io_RXTXPort |
| 1374 |
* Method: nativeSetParityErrorChar |
| 1375 |
* Signature: (B)Z |
| 1376 |
*/ |
| 1377 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXPort_nativeSetParityErrorChar(JNIEnv *env, jobject jobj, jbyte b) |
| 1378 |
{ |
| 1379 |
DCB PortDCB; |
| 1380 |
LPCWSTR lpMsgBuf; |
| 1381 |
|
| 1382 |
HANDLE hPort = get_fd(env, jobj); |
| 1383 |
PortDCB.DCBlength = sizeof(DCB); |
| 1384 |
|
| 1385 |
IF_DEBUG |
| 1386 |
( |
| 1387 |
printj(env, L"--- RXTXPort.nativeSetParityErrorChar(%#x) called\n", (int)b); |
| 1388 |
) |
| 1389 |
|
| 1390 |
// Get port setting information. |
| 1391 |
if(!GetCommState(hPort, &PortDCB)) |
| 1392 |
{ //Unable to get configuration of the serial port |
| 1393 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 1394 |
IF_DEBUG |
| 1395 |
( |
| 1396 |
printj(env, L"!!! nativeSetParityErrorChar - GetCommState() error: %s\n", lpMsgBuf); |
| 1397 |
) |
| 1398 |
ReleaseErrorMsg(lpMsgBuf); |
| 1399 |
return JNI_FALSE; |
| 1400 |
} |
| 1401 |
|
| 1402 |
if(b != 0) |
| 1403 |
{ |
| 1404 |
PortDCB.fErrorChar = TRUE; |
| 1405 |
PortDCB.fParity = TRUE; |
| 1406 |
PortDCB.ErrorChar = b; |
| 1407 |
} |
| 1408 |
else |
| 1409 |
{ |
| 1410 |
PortDCB.fErrorChar = FALSE; |
| 1411 |
PortDCB.fParity = FALSE; |
| 1412 |
PortDCB.ErrorChar = 0; |
| 1413 |
} |
| 1414 |
|
| 1415 |
if (!SetCommState(hPort, &PortDCB)) |
| 1416 |
{ |
| 1417 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 1418 |
IF_DEBUG |
| 1419 |
( |
| 1420 |
printj(env, L"!!! nativeSetParityErrorChar - SetCommState() error: %s\n", lpMsgBuf); |
| 1421 |
) |
| 1422 |
ReleaseErrorMsg(lpMsgBuf); |
| 1423 |
return JNI_FALSE; |
| 1424 |
} |
| 1425 |
|
| 1426 |
return JNI_TRUE; |
| 1427 |
} |
| 1428 |
|
| 1429 |
/* |
| 1430 |
nativeGetEndOfInputChar |
| 1431 |
|
| 1432 |
accept: - |
| 1433 |
perform: check the EndOf InputChar |
| 1434 |
return: the EndOfInputChar as an jbyte. -1 on error |
| 1435 |
exceptions: UnsupportedCommOperationException if not implemented |
| 1436 |
comments: |
| 1437 |
* Class: gnu_io_RXTXPort |
| 1438 |
* Method: nativeGetEndOfInputChar |
| 1439 |
* Signature: ()I |
| 1440 |
*/ |
| 1441 |
JNIEXPORT jbyte JNICALL Java_gnu_io_RXTXPort_nativeGetEndOfInputChar(JNIEnv *env, jobject jobj) |
| 1442 |
{ |
| 1443 |
DCB PortDCB; |
| 1444 |
LPCWSTR lpMsgBuf; |
| 1445 |
|
| 1446 |
HANDLE hPort = get_fd(env, jobj); |
| 1447 |
PortDCB.DCBlength = sizeof(DCB); |
| 1448 |
|
| 1449 |
IF_DEBUG |
| 1450 |
( |
| 1451 |
printj(env, L"--- RXTXPort.nativeGetEndOfInputChar() called\n"); |
| 1452 |
) |
| 1453 |
|
| 1454 |
// Get port setting information. |
| 1455 |
if(!GetCommState(hPort, &PortDCB)) |
| 1456 |
{ //Unable to get configuration of the serial port |
| 1457 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 1458 |
IF_DEBUG |
| 1459 |
( |
| 1460 |
printj(env, L"!!! nativeGetEndOfInputChar - GetCommState() error: %s\n", lpMsgBuf); |
| 1461 |
) |
| 1462 |
ReleaseErrorMsg(lpMsgBuf); |
| 1463 |
return -1; |
| 1464 |
} |
| 1465 |
|
| 1466 |
return (jbyte)PortDCB.EofChar; |
| 1467 |
} |
| 1468 |
|
| 1469 |
|
| 1470 |
/* |
| 1471 |
nativeSetEndOfInputChar |
| 1472 |
|
| 1473 |
accept: The EndOfInputChar as an int |
| 1474 |
perform: set the EndOfInputChar |
| 1475 |
return: JNI_TRUE on success |
| 1476 |
exceptions: UnsupportedCommOperationException if not implemented |
| 1477 |
comments: This may cause troubles on Windows. |
| 1478 |
Lets give it a shot and see what happens. |
| 1479 |
|
| 1480 |
See termios.c for the windows bits. |
| 1481 |
|
| 1482 |
EofChar = val; |
| 1483 |
fBinary = false; //winapi docs say always use true. ? |
| 1484 |
* Class: gnu_io_RXTXPort |
| 1485 |
* Method: nativeSetEndOfInputChar |
| 1486 |
* Signature: (B)Z |
| 1487 |
*/ |
| 1488 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXPort_nativeSetEndOfInputChar(JNIEnv *env, jobject jobj, jbyte b) |
| 1489 |
{ |
| 1490 |
DCB PortDCB; |
| 1491 |
LPCWSTR lpMsgBuf; |
| 1492 |
|
| 1493 |
HANDLE hPort = get_fd(env, jobj); |
| 1494 |
PortDCB.DCBlength = sizeof(DCB); |
| 1495 |
|
| 1496 |
IF_DEBUG |
| 1497 |
( |
| 1498 |
printj(env, L"--- RXTXPort.nativeSetEndOfInputChar(%#x) called\n", (int)b); |
| 1499 |
) |
| 1500 |
|
| 1501 |
// Get port setting information. |
| 1502 |
if(!GetCommState(hPort, &PortDCB)) |
| 1503 |
{ //Unable to get configuration of the serial port |
| 1504 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 1505 |
IF_DEBUG |
| 1506 |
( |
| 1507 |
printj(env, L"!!! nativeSetEndOfInputChar - GetCommState() error: %s\n", lpMsgBuf); |
| 1508 |
) |
| 1509 |
ReleaseErrorMsg(lpMsgBuf); |
| 1510 |
return JNI_FALSE; |
| 1511 |
} |
| 1512 |
|
| 1513 |
if( b != 0) |
| 1514 |
{ |
| 1515 |
PortDCB.fBinary = FALSE; |
| 1516 |
PortDCB.EofChar = b; |
| 1517 |
} |
| 1518 |
else |
| 1519 |
{ |
| 1520 |
PortDCB.fBinary = TRUE; |
| 1521 |
PortDCB.EofChar = 0; |
| 1522 |
} |
| 1523 |
|
| 1524 |
if (!SetCommState(hPort, &PortDCB)) |
| 1525 |
{ |
| 1526 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 1527 |
IF_DEBUG |
| 1528 |
( |
| 1529 |
printj(env, L"!!! nativeSetEndOfInputChar - SetCommState() error: %s\n", lpMsgBuf); |
| 1530 |
) |
| 1531 |
throw_java_exceptionW(env, UNSUPPORTED_COMM_OPERATION, L"nativeSetEndOfInputChar", lpMsgBuf); |
| 1532 |
ReleaseErrorMsg(lpMsgBuf); |
| 1533 |
return JNI_FALSE; |
| 1534 |
} |
| 1535 |
|
| 1536 |
return JNI_TRUE; |
| 1537 |
} |
| 1538 |
|
| 1539 |
|
| 1540 |
/* |
| 1541 |
* Class: gnu_io_RXTXPort |
| 1542 |
* Method: nativeSetUartType |
| 1543 |
* Signature: (Ljava/lang/String{ }Z)Z |
| 1544 |
*/ |
| 1545 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXPort_nativeSetUartType(JNIEnv *env, jobject jobj, jstring type, jboolean test) |
| 1546 |
{ |
| 1547 |
throw_java_exceptionW(env, UNSUPPORTED_COMM_OPERATION, L"nativeSetUartType", L"Operation not implemented"); |
| 1548 |
return JNI_FALSE; |
| 1549 |
} |
| 1550 |
|
| 1551 |
/* |
| 1552 |
* Class: gnu_io_RXTXPort |
| 1553 |
* Method: nativeGetUartType |
| 1554 |
* Signature: ()Ljava/lang/String{ } |
| 1555 |
*/ |
| 1556 |
JNIEXPORT jstring JNICALL Java_gnu_io_RXTXPort_nativeGetUartType(JNIEnv *env, jobject jobj) |
| 1557 |
{ |
| 1558 |
throw_java_exceptionW(env, UNSUPPORTED_COMM_OPERATION, L"nativeSetUartType", L"Operation not implemented"); |
| 1559 |
return env->NewStringUTF("Unknown"); |
| 1560 |
} |
| 1561 |
|
| 1562 |
/* |
| 1563 |
* Class: gnu_io_RXTXPort |
| 1564 |
* Method: nativeSetBaudBase |
| 1565 |
* Signature: (I)Z |
| 1566 |
*/ |
| 1567 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXPort_nativeSetBaudBase(JNIEnv *env, jobject jobj, jint baudBase) |
| 1568 |
{ |
| 1569 |
throw_java_exceptionW(env, UNSUPPORTED_COMM_OPERATION, L"nativeSetBaudBase", L"Operation not implemented"); |
| 1570 |
return JNI_FALSE; |
| 1571 |
} |
| 1572 |
|
| 1573 |
/* |
| 1574 |
* Class: gnu_io_RXTXPort |
| 1575 |
* Method: nativeGetBaudBase |
| 1576 |
* Signature: ()I |
| 1577 |
*/ |
| 1578 |
JNIEXPORT jint JNICALL Java_gnu_io_RXTXPort_nativeGetBaudBase(JNIEnv *env, jobject jobj) |
| 1579 |
{ |
| 1580 |
throw_java_exceptionW(env, UNSUPPORTED_COMM_OPERATION, L"nativeGetBaudBase", L"Operation not implemented"); |
| 1581 |
return -1; |
| 1582 |
} |
| 1583 |
|
| 1584 |
/* |
| 1585 |
* Class: gnu_io_RXTXPort |
| 1586 |
* Method: nativeSetDivisor |
| 1587 |
* Signature: (I)Z |
| 1588 |
*/ |
| 1589 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXPort_nativeSetDivisor(JNIEnv *env, jobject jobj, jint divisor) |
| 1590 |
{ |
| 1591 |
throw_java_exceptionW(env, UNSUPPORTED_COMM_OPERATION, L"nativeSetDivisor", L"Operation not implemented"); |
| 1592 |
return JNI_FALSE; |
| 1593 |
} |
| 1594 |
|
| 1595 |
/* |
| 1596 |
* Class: gnu_io_RXTXPort |
| 1597 |
* Method: nativeGetDivisor |
| 1598 |
* Signature: ()I |
| 1599 |
*/ |
| 1600 |
JNIEXPORT jint JNICALL Java_gnu_io_RXTXPort_nativeGetDivisor(JNIEnv *env, jobject jobj) |
| 1601 |
{ |
| 1602 |
throw_java_exceptionW(env, UNSUPPORTED_COMM_OPERATION, L"nativeGetDivisor", L"Operation not implemented"); |
| 1603 |
return -1; |
| 1604 |
} |
| 1605 |
|
| 1606 |
/* |
| 1607 |
* Class: gnu_io_RXTXPort |
| 1608 |
* Method: nativeSetLowLatency |
| 1609 |
* Signature: ()Z |
| 1610 |
*/ |
| 1611 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXPort_nativeSetLowLatency(JNIEnv *env, jobject jobj) |
| 1612 |
{ |
| 1613 |
throw_java_exceptionW(env, UNSUPPORTED_COMM_OPERATION, L"nativeSetLowLatency", L"Operation not implemented"); |
| 1614 |
return JNI_FALSE; |
| 1615 |
} |
| 1616 |
|
| 1617 |
/* |
| 1618 |
* Class: gnu_io_RXTXPort |
| 1619 |
* Method: nativeGetLowLatency |
| 1620 |
* Signature: ()Z |
| 1621 |
*/ |
| 1622 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXPort_nativeGetLowLatency(JNIEnv *env, jobject jobj) |
| 1623 |
{ |
| 1624 |
throw_java_exceptionW(env, UNSUPPORTED_COMM_OPERATION, L"nativeGetLowLatency", L"Operation not implemented"); |
| 1625 |
return JNI_FALSE; |
| 1626 |
} |
| 1627 |
|
| 1628 |
/* |
| 1629 |
* Class: gnu_io_RXTXPort |
| 1630 |
* Method: nativeSetCallOutHangup |
| 1631 |
* Signature: (Z)Z |
| 1632 |
*/ |
| 1633 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXPort_nativeSetCallOutHangup(JNIEnv *env, jobject jobj, jboolean noHup) |
| 1634 |
{ |
| 1635 |
throw_java_exceptionW(env, UNSUPPORTED_COMM_OPERATION, L"nativeSetCallOutHangup", L"Operation not implemented"); |
| 1636 |
return JNI_FALSE; |
| 1637 |
} |
| 1638 |
|
| 1639 |
/* |
| 1640 |
* Class: gnu_io_RXTXPort |
| 1641 |
* Method: nativeGetCallOutHangup |
| 1642 |
* Signature: ()Z |
| 1643 |
*/ |
| 1644 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXPort_nativeGetCallOutHangup(JNIEnv *env, jobject jobj) |
| 1645 |
{ |
| 1646 |
throw_java_exceptionW(env, UNSUPPORTED_COMM_OPERATION, L"nativeGetCallOutHangup", L"Operation not implemented"); |
| 1647 |
return JNI_FALSE; |
| 1648 |
} |
| 1649 |
|