134
158
* If speed is not a predifined speed it is assumed to be
135
159
* the actual speed desired.
137
public synchronized void setSerialPortParams( int b, int d, int s, int p )
161
private native int nativeGetParity( int fd );
162
private native int nativeGetFlowControlMode( int fd );
163
public synchronized void setSerialPortParams( int b, int d, int s,
138
165
throws UnsupportedCommOperationException
141
System.out.println(System.currentTimeMillis() + ": " + "RXTXPort:setSerialPortParams(" +
142
b + " " + d + " " + s + " " + p + ")");
143
nativeSetSerialPortParams( b, d, s, p );
168
z.reportln( "RXTXPort:setSerialPortParams(" +
169
b + " " + d + " " + s + " " + p + ") called");
170
if ( nativeSetSerialPortParams( b, d, s, p ) )
171
throw new UnsupportedCommOperationException(
172
"Invalid Parameter" );
145
174
if( s== STOPBITS_1_5 ) dataBits = DATABITS_5;
146
175
else dataBits = d;
178
z.reportln( "RXTXPort:setSerialPortParams(" +
179
b + " " + d + " " + s + " " + p +
719
784
public void removeEventListener()
722
System.out.println(System.currentTimeMillis() + ": " + "RXTXPort:removeEventListener()");
787
z.reportln( "RXTXPort:removeEventListener() called");
723
788
waitForTheNativeCodeSilly();
724
789
//if( monThread != null && monThread.isAlive() )
725
790
if( monThreadisInterrupted == true )
727
System.out.println(System.currentTimeMillis() + ": " + "RXTXPort:removeEventListener() already interrupted");
792
z.reportln( " RXTXPort:removeEventListener() already interrupted");
728
793
monThread = null;
729
794
SPEventListener = null;
730
Runtime.getRuntime().gc();
733
797
else if( monThread != null && monThread.isAlive() )
736
System.out.println(System.currentTimeMillis() + ": " + "RXTXPort:Interrupt=true");
800
z.reportln( " RXTXPort:Interrupt=true");
737
801
monThreadisInterrupted=true;
739
803
Notify all threads in this PID that something is up
740
804
They will call back to see if its their thread
741
805
using isInterrupted().
743
MonitorThreadCloseLock = true;
745
System.out.println(System.currentTimeMillis() + ": " + "RXTXPort:calling interruptEventLoop");
808
z.reportln( " RXTXPort:calling interruptEventLoop");
746
809
interruptEventLoop( );
748
System.out.print("RXTXPort:waiting on closelock");
749
while( MonitorThreadCloseLock )
752
System.out.print(".");
755
} catch( Exception e ) {}
758
System.out.println();
760
System.out.println(System.currentTimeMillis() + ": " + "RXTXPort:calling monThread.join()");
812
z.reportln( " RXTXPort:calling monThread.join()");
762
814
monThread.join(1000);
763
815
} catch (Exception ex) {
961
1014
public synchronized void close()
964
System.out.println(System.currentTimeMillis() + ": " + "RXTXPort:close( " + this.name + " )");
1017
z.reportln( "RXTXPort:close( " + this.name + " )");
965
1018
if( closeLock ) return;
966
1019
closeLock = true;
1020
while( IOLocked > 0 )
1023
z.reportln("IO is locked " + IOLocked);
1026
} catch( Exception e ) {}
969
System.out.println( System.currentTimeMillis() + ": " + "RXTXPort:close detected bad File Descriptor" );
1030
z.reportln( "RXTXPort:close detected bad File Descriptor" );
975
System.out.println(System.currentTimeMillis() + ": " + "RXTXPort:close( " + this.name + " ) setting monThreadisInterrupted");
1036
z.reportln( "RXTXPort:close( " + this.name + " ) setting monThreadisInterrupted");
976
1037
if ( ! monThreadisInterrupted )
978
1039
removeEventListener();
981
System.out.println(System.currentTimeMillis() + ": " + "RXTXPort:close( " + this.name + " ) calling nativeClose");
1042
z.reportln( "RXTXPort:close( " + this.name + " ) calling nativeClose");
982
1043
nativeClose( this.name );
984
System.out.println(System.currentTimeMillis() + ": " + "RXTXPort:close( " + this.name + " ) calling super.close");
1045
z.reportln( "RXTXPort:close( " + this.name + " ) calling super.close");
987
System.out.println(System.currentTimeMillis() + ": " + "RXTXPort:close( " + this.name + " ) calling System.gc");
990
Runtime.getRuntime().gc();
991
1048
closeLock = false;
993
System.out.println(System.currentTimeMillis() + ": " + "RXTXPort:close( " + this.name + " ) leaving");
1050
z.reportln( "RXTXPort:close( " + this.name + " ) leaving");
1065
1149
"Invalid offset/length passed to read"
1069
1153
byte send[] = new byte[len];
1070
1154
System.arraycopy( b, off, send, 0, len );
1071
1155
if (debug_write)
1073
System.out.println(System.currentTimeMillis() + ": " + "Entering RXTXPort:SerialOutputStream:write(" + send.length + " " + off + " " + len + " " +") " /*+ new String(send) */ );
1157
z.reportln( "Entering RXTXPort:SerialOutputStream:write(" + send.length + " " + off + " " + len + " " +") " /*+ new String(send) */ );
1075
1159
if ( fd == 0 ) throw new IOException();
1076
/* hmm this turns out to be a very bad idea
1077
1160
if ( monThreadisInterrupted == true )
1079
throw new IOException( "Port has been Closed" );
1082
1165
waitForTheNativeCodeSilly();
1083
writeArray( send, 0, len, monThreadisInterrupted );
1085
System.out.println(System.currentTimeMillis() + ": " + "Leaving RXTXPort:SerialOutputStream:write(" + send.length + " " + off + " " + len + " " +") " /*+ new String(send)*/ );
1168
writeArray( send, 0, len, monThreadisInterrupted );
1170
z.reportln( "Leaving RXTXPort:SerialOutputStream:write(" + send.length + " " + off + " " + len + " " +") " /*+ new String(send)*/ );
1172
catch( IOException e )
1089
1181
public void flush() throws IOException
1092
System.out.println(System.currentTimeMillis() + ": " + "RXTXPort:SerialOutputStream:flush() enter");
1184
z.reportln( "RXTXPort:SerialOutputStream:flush() enter");
1093
1185
if( speed == 0 ) return;
1094
1186
if ( fd == 0 ) throw new IOException();
1095
/* hmm this turns out to be a very bad idea
1096
1187
if ( monThreadisInterrupted == true )
1190
z.reportln( "RXTXPort:SerialOutputStream:flush() Leaving Interrupted");
1099
// FIXME Trent this breaks
1100
//throw new IOException( "flush() Port has been Closed" );
1103
1194
waitForTheNativeCodeSilly();
1105
1196
this is probably good on all OS's but for now
1106
1197
just sendEvent from java on Sol
1108
if ( nativeDrain( monThreadisInterrupted ) )
1109
sendEvent( SerialPortEvent.OUTPUT_BUFFER_EMPTY, true );
1111
System.out.println(System.currentTimeMillis() + ": " + "RXTXPort:SerialOutputStream:flush() leave");
1201
if ( nativeDrain( monThreadisInterrupted ) )
1202
sendEvent( SerialPortEvent.OUTPUT_BUFFER_EMPTY, true );
1204
z.reportln( "RXTXPort:SerialOutputStream:flush() leave");
1206
catch( IOException e )
1119
1219
* @return int the int read
1120
1220
* @throws IOException
1121
1221
* @see java.io.InputStream
1223
*timeout threshold Behavior
1224
*------------------------------------------------------------------------
1225
*0 0 blocks until 1 byte is available timeout > 0,
1226
* threshold = 0, blocks until timeout occurs, returns -1
1228
*>0 >0 blocks until timeout, returns - 1 on timeout, magnitude
1229
* of threshold doesn't play a role.
1230
*0 >0 Blocks until 1 byte, magnitude of threshold doesn't
1123
public int read() throws IOException
1233
public synchronized int read() throws IOException
1125
1235
if (debug_read)
1126
System.out.println(System.currentTimeMillis() + ": " + "RXTXPort:SerialInputStream:read()");
1127
/* hmm this turns out to be a very bad idea
1128
if ( monThreadisInterrupted ) return( -1 ) ;
1236
z.reportln( "RXTXPort:SerialInputStream:read() called");
1130
1237
if ( fd == 0 ) throw new IOException();
1131
/* hmm this turns out to be a very bad idea
1132
if ( monThreadisInterrupted == true )
1238
if ( monThreadisInterrupted )
1134
throw new IOException( "Port has been Closed" );
1240
z.reportln( "+++++++++ read() monThreadisInterrupted" );
1243
if (debug_read_results)
1244
z.reportln( "RXTXPort:SerialInputStream:read() L" );
1137
1245
waitForTheNativeCodeSilly();
1138
int result = readByte();
1140
System.out.println(System.currentTimeMillis() + ": " + "readByte= " + result );
1246
if (debug_read_results)
1247
z.reportln( "RXTXPort:SerialInputStream:read() N" );
1250
int result = readByte();
1251
if (debug_read_results)
1252
//z.reportln( "RXTXPort:SerialInputStream:read() returns byte = " + result );
1253
z.reportln( "RXTXPort:SerialInputStream:read() returns" );
1145
1263
* @return int number of bytes read
1146
1264
* @throws IOException
1266
*timeout threshold Behavior
1267
*------------------------------------------------------------------------
1268
*0 0 blocks until 1 byte is available
1269
*>0 0 blocks until timeout occurs, returns 0 on timeout
1270
*>0 >0 blocks until timeout or reads threshold bytes,
1271
returns 0 on timeout
1272
*0 >0 blocks until reads threshold bytes
1148
public int read( byte b[] ) throws IOException
1274
public synchronized int read( byte b[] ) throws IOException
1151
1277
if (debug_read)
1152
System.out.println(System.currentTimeMillis() + ": " + "RXTXPort:SerialInputStream:read(" + b.length + ")");
1153
/* hmm this turns out to be a very bad idea
1278
z.reportln( "RXTXPort:SerialInputStream:read(" + b.length + ") called");
1154
1279
if ( monThreadisInterrupted == true )
1156
throw new IOException( "Port has been Closed" );
1159
1284
waitForTheNativeCodeSilly();
1160
result = read( b, 0, b.length);
1162
System.out.println(System.currentTimeMillis() + ": " + "read = " + result );
1287
result = read( b, 0, b.length);
1288
if (debug_read_results)
1289
z.reportln( "RXTXPort:SerialInputStream:read() returned " + result + " bytes" );
1166
1298
read(byte b[], int, int)
1173
1305
* @return int number of bytes read
1174
1306
* @throws IOException
1176
public int read( byte b[], int off, int len )
1180
System.out.println(System.currentTimeMillis() + ": " + "RXTXPort:SerialInputStream:read(" + b.length + " " + off + " " + len + ") " /*+ new String(b) */ );
1183
* Some sanity checks
1185
if ( fd == 0 ) throw new IOException();
1188
throw new NullPointerException();
1190
if( (off < 0) || (len < 0) || (off+len > b.length))
1191
throw new IndexOutOfBoundsException();
1194
* Return immediately if len==0
1196
if( len==0 ) return 0;
1199
* See how many bytes we should read
1206
* If threshold is disabled, read should return as soon
1207
* as data are available (up to the amount of available
1208
* bytes in order to avoid blocking)
1209
* Read may return earlier depending of the receive time
1212
if( available()==0 )
1215
Minimum = Math.min(Minimum,available());
1220
* Threshold is enabled. Read should return when
1221
* 'threshold' bytes have been received (or when the
1222
* receive timeout expired)
1224
Minimum = Math.min(Minimum, threshold);
1226
/* hmm this turns out to be a very bad idea
1227
if ( monThreadisInterrupted == true )
1229
throw new IOException( "Port has been Closed" );
1232
waitForTheNativeCodeSilly();
1233
result = readArray( b, off, Minimum);
1235
System.out.println(System.currentTimeMillis() + ": " + "RXTXPort:SerialInputStream:read(" + b.length + " " + off + " " + len + ") = " + result + " bytes containing " /*+ new String(b) */);
1308
*timeout threshold Behavior
1309
*------------------------------------------------------------------------
1310
*0 0 blocks until 1 byte is available
1311
*>0 0 blocks until timeout occurs, returns 0 on timeout
1312
*>0 >0 blocks until timeout or reads threshold bytes,
1313
returns 0 on timeout
1314
*0 >0 blocks until either threshold # of bytes or len bytes,
1315
whichever was lower.
1317
public synchronized int read( byte b[], int off, int len )
1321
z.reportln( "RXTXPort:SerialInputStream:read(" + b.length + " " + off + " " + len + ") called" /*+ new String(b) */ );
1324
* Some sanity checks
1329
z.reportln( "RXTXPort:SerialInputStream:read() fd == 0");
1330
z.reportln("+++++++ IOException()\n");
1331
throw new IOException();
1336
z.reportln("+++++++ NullPointerException()\n");
1338
z.reportln( "RXTXPort:SerialInputStream:read() b == 0");
1339
throw new NullPointerException();
1342
if( (off < 0) || (len < 0) || (off+len > b.length))
1344
z.reportln("+++++++ IndexOutOfBoundsException()\n");
1346
z.reportln( "RXTXPort:SerialInputStream:read() off < 0 ..");
1347
throw new IndexOutOfBoundsException();
1351
* Return immediately if len==0
1356
z.reportln( "RXTXPort:SerialInputStream:read() off < 0 ..");
1360
* See how many bytes we should read
1367
* If threshold is disabled, read should return as soon
1368
* as data are available (up to the amount of available
1369
* bytes in order to avoid blocking)
1370
* Read may return earlier depending of the receive time
1373
int a = nativeavailable();
1377
Minimum = Math.min( Minimum, a );
1382
* Threshold is enabled. Read should return when
1383
* 'threshold' bytes have been received (or when the
1384
* receive timeout expired)
1386
Minimum = Math.min(Minimum, threshold);
1388
if ( monThreadisInterrupted == true )
1391
z.reportln( "RXTXPort:SerialInputStream:read() Interrupted");
1395
waitForTheNativeCodeSilly();
1398
result = readArray( b, off, Minimum);
1399
if (debug_read_results)
1400
z.reportln( "RXTXPort:SerialInputStream:read(" + b.length + " " + off + " " + len + ") returned " + result + " bytes" /*+ new String(b) */);
1414
* @return int number of bytes read
1415
* @throws IOException
1417
We are trying to catch the terminator in the native code
1418
Right now it is assumed that t[] is an array of 2 bytes.
1420
if the read encounters the two bytes, it will return and the
1421
array will contain the terminator. Otherwise read behavior should
1422
be the same as read( b[], off, len ). Timeouts have not been well
1426
public synchronized int read( byte b[], int off, int len, byte t[] )
1430
z.reportln( "RXTXPort:SerialInputStream:read(" + b.length + " " + off + " " + len + ") called" /*+ new String(b) */ );
1433
* Some sanity checks
1438
z.reportln( "RXTXPort:SerialInputStream:read() fd == 0");
1439
z.reportln("+++++++ IOException()\n");
1440
throw new IOException();
1445
z.reportln("+++++++ NullPointerException()\n");
1447
z.reportln( "RXTXPort:SerialInputStream:read() b == 0");
1448
throw new NullPointerException();
1451
if( (off < 0) || (len < 0) || (off+len > b.length))
1453
z.reportln("+++++++ IndexOutOfBoundsException()\n");
1455
z.reportln( "RXTXPort:SerialInputStream:read() off < 0 ..");
1456
throw new IndexOutOfBoundsException();
1460
* Return immediately if len==0
1465
z.reportln( "RXTXPort:SerialInputStream:read() off < 0 ..");
1469
* See how many bytes we should read
1476
* If threshold is disabled, read should return as soon
1477
* as data are available (up to the amount of available
1478
* bytes in order to avoid blocking)
1479
* Read may return earlier depending of the receive time
1482
int a = nativeavailable();
1486
Minimum = Math.min( Minimum, a );
1491
* Threshold is enabled. Read should return when
1492
* 'threshold' bytes have been received (or when the
1493
* receive timeout expired)
1495
Minimum = Math.min(Minimum, threshold);
1497
if ( monThreadisInterrupted == true )
1500
z.reportln( "RXTXPort:SerialInputStream:read() Interrupted");
1504
waitForTheNativeCodeSilly();
1507
result = readTerminatedArray( b, off, Minimum, t );
1508
if (debug_read_results)
1509
z.reportln( "RXTXPort:SerialInputStream:read(" + b.length + " " + off + " " + len + ") returned " + result + " bytes" /*+ new String(b) */);
1239
1518
* @return int bytes available
1240
1519
* @throws IOException
1242
public int available() throws IOException
1521
public synchronized int available() throws IOException
1244
/* hmm this turns out to be a very bad idea
1245
1523
if ( monThreadisInterrupted == true )
1247
throw new IOException( "Port has been Closed" );
1250
int r = nativeavailable();
1251
if ( debug_verbose && r > 0 )
1252
System.out.println(System.currentTimeMillis() + ": " + "available() returning " +
1527
if ( debug_verbose )
1528
z.reportln( "RXTXPort:available() called" );
1532
int r = nativeavailable();
1533
if ( debug_verbose )
1534
z.reportln( "RXTXPort:available() returning " +
1784
* Extension to CommAPI
2072
* Extension to CommAPI. Set Baud Base to 38600 on Linux and W32
1785
2074
* @param int BaudBase The clock frequency divided by 16. Default
1786
2075
* BaudBase is 115200.
1787
2076
* @return boolean true on success
1788
* @throws UnsupportedCommOperationException
2077
* @throws UnsupportedCommOperationException, IOException
1791
2080
public boolean setBaudBase(int BaudBase)
1792
throws UnsupportedCommOperationException
2081
throws UnsupportedCommOperationException,
1795
System.out.println(System.currentTimeMillis() + ": " + "RXTXPort:setBaudBase()");
2085
z.reportln( "RXTXPort:setBaudBase()");
1796
2086
return nativeSetBaudBase(BaudBase);
1800
2090
* Extension to CommAPI
1801
2091
* @return int BaudBase
1802
* @throws UnsupportedCommOperationException
2092
* @throws UnsupportedCommOperationException, IOException
1805
public int getBaudBase() throws UnsupportedCommOperationException
2095
public int getBaudBase() throws UnsupportedCommOperationException,
1808
System.out.println(System.currentTimeMillis() + ": " + "RXTXPort:getBaudBase()");
2099
z.reportln( "RXTXPort:getBaudBase()");
1809
2100
return nativeGetBaudBase();
1813
* Extension to CommAPI
2104
* Extension to CommAPI. Set Baud Base to 38600 on Linux and W32
1814
2106
* @param int Divisor;
1815
* @throws UnsupportedCommOperationException
2107
* @throws UnsupportedCommOperationException, IOException
1818
2110
public boolean setDivisor(int Divisor)
1819
throws UnsupportedCommOperationException
2111
throws UnsupportedCommOperationException, IOException
1822
System.out.println(System.currentTimeMillis() + ": " + "RXTXPort:setDivisor()");
2114
z.reportln( "RXTXPort:setDivisor()");
1823
2115
return nativeSetDivisor(Divisor);
1827
2119
* Extension to CommAPI
1828
2120
* @returns int Divisor;
1829
* @throws UnsupportedCommOperationException
2121
* @throws UnsupportedCommOperationException, IOException
1832
public int getDivisor() throws UnsupportedCommOperationException
2124
public int getDivisor() throws UnsupportedCommOperationException,
1835
System.out.println(System.currentTimeMillis() + ": " + "RXTXPort:getDivisor()");
2128
z.reportln( "RXTXPort:getDivisor()");
1836
2129
return nativeGetDivisor();