124
154
* @param p parity
125
155
* @throws UnsupportedCommOperationException
126
156
* @see gnu.io.UnsupportedCommOperationException
158
* If speed is not a predifined speed it is assumed to be
159
* the actual speed desired.
128
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,
129
165
throws UnsupportedCommOperationException
132
System.out.println("RXTXPort:setSerialPortParams(" +
133
b + " " + d + " " + s + " " + p + ")");
134
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" );
136
174
if( s== STOPBITS_1_5 ) dataBits = DATABITS_5;
137
175
else dataBits = d;
178
z.reportln( "RXTXPort:setSerialPortParams(" +
179
b + " " + d + " " + s + " " + p +
142
/** Set the native serial port parameters */
143
private native void nativeSetSerialPortParams( int speed, int dataBits,
144
int stopBits, int parity )
184
* Set the native serial port parameters
185
* If speed is not a predifined speed it is assumed to be
186
* the actual speed desired.
188
private native boolean nativeSetSerialPortParams( int speed,
189
int dataBits, int stopBits, int parity )
145
190
throws UnsupportedCommOperationException;
147
192
/** Line speed in bits-per-second */
148
193
private int speed=9600;
150
195
* @return int representing the baudrate
196
* This will not behave as expected with custom speeds
152
public int getBaudRate() { return speed; }
198
public int getBaudRate()
201
z.reportln( "RXTXPort:getBaudRate() called and returning " + speed);
154
205
/** Data bits port parameter */
155
206
private int dataBits=DATABITS_8;
157
208
* @return int representing the databits
159
public int getDataBits() { return dataBits; }
210
public int getDataBits()
213
z.reportln( "RXTXPort:getDataBits() called and returning " + dataBits);
161
217
/** Stop bits port parameter */
162
218
private int stopBits=SerialPort.STOPBITS_1;
164
220
* @return int representing the stopbits
166
public int getStopBits() { return stopBits; }
222
public int getStopBits()
225
z.reportln( "RXTXPort:getStopBits() called and returning " + stopBits);
168
229
/** Parity port parameter */
169
230
private int parity= SerialPort.PARITY_NONE;
171
232
* @return int representing the parity
173
public int getParity() { return parity; }
234
public int getParity()
237
z.reportln( "RXTXPort:getParity() called and returning " + parity );
176
242
/** Flow control */
684
784
public void removeEventListener()
687
System.out.println("RXTXPort:removeEventListener()");
787
z.reportln( "RXTXPort:removeEventListener() called");
688
788
waitForTheNativeCodeSilly();
689
789
//if( monThread != null && monThread.isAlive() )
690
790
if( monThreadisInterrupted == true )
692
System.out.println("RXTXPort:removeEventListener() already interrupted");
792
z.reportln( " RXTXPort:removeEventListener() already interrupted");
693
793
monThread = null;
694
794
SPEventListener = null;
695
Runtime.getRuntime().gc();
698
797
else if( monThread != null && monThread.isAlive() )
701
System.out.println("RXTXPort:Interrupt=true");
800
z.reportln( " RXTXPort:Interrupt=true");
702
801
monThreadisInterrupted=true;
704
803
Notify all threads in this PID that something is up
705
804
They will call back to see if its their thread
706
805
using isInterrupted().
708
MonitorThreadCloseLock = true;
710
System.out.println("RXTXPort:calling interruptEventLoop");
808
z.reportln( " RXTXPort:calling interruptEventLoop");
711
809
interruptEventLoop( );
713
System.out.print("RXTXPort:waiting on closelock");
714
while( MonitorThreadCloseLock )
717
System.out.print(".");
720
} catch( Exception e ) {}
723
System.out.println();
725
System.out.println("RXTXPort:calling monThread.join()");
812
z.reportln( " RXTXPort:calling monThread.join()");
727
814
monThread.join(1000);
728
815
} catch (Exception ex) {
1015
1142
public void write( byte b[], int off, int len )
1016
1143
throws IOException
1145
if( speed == 0 ) return;
1018
1146
if( off + len > b.length )
1020
1148
throw new IndexOutOfBoundsException(
1021
1149
"Invalid offset/length passed to read"
1025
1153
byte send[] = new byte[len];
1026
1154
System.arraycopy( b, off, send, 0, len );
1029
System.out.println("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) */ );
1031
1159
if ( fd == 0 ) throw new IOException();
1032
1160
if ( monThreadisInterrupted == true )
1034
throw new IOException( "Port has been Closed" );
1036
1165
waitForTheNativeCodeSilly();
1037
writeArray( send, 0, len );
1039
System.out.println("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 )
1043
1181
public void flush() throws IOException
1046
System.out.println("RXTXPort:SerialOutputStream:flush() enter");
1184
z.reportln( "RXTXPort:SerialOutputStream:flush() enter");
1185
if( speed == 0 ) return;
1047
1186
if ( fd == 0 ) throw new IOException();
1048
1187
if ( monThreadisInterrupted == true )
1190
z.reportln( "RXTXPort:SerialOutputStream:flush() Leaving Interrupted");
1051
/* FIXME Trent this breaks
1052
InstrumentControlSerialPort = hGetPort
1055
//throw new IOException( "flush() Port has been Closed" );
1057
1194
waitForTheNativeCodeSilly();
1060
System.out.println("RXTXPort:SerialOutputStream:flush() leave");
1196
this is probably good on all OS's but for now
1197
just sendEvent from java on Sol
1201
if ( nativeDrain( monThreadisInterrupted ) )
1202
sendEvent( SerialPortEvent.OUTPUT_BUFFER_EMPTY, true );
1204
z.reportln( "RXTXPort:SerialOutputStream:flush() leave");
1206
catch( IOException e )
1068
1219
* @return int the int read
1069
1220
* @throws IOException
1070
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
1072
public int read() throws IOException
1233
public synchronized int read() throws IOException
1075
System.out.println("RXTXPort:SerialInputStream:read()");
1076
if ( monThreadisInterrupted ) return( -1 ) ;
1236
z.reportln( "RXTXPort:SerialInputStream:read() called");
1077
1237
if ( fd == 0 ) throw new IOException();
1078
if ( monThreadisInterrupted == true )
1238
if ( monThreadisInterrupted )
1080
throw new IOException( "Port has been Closed" );
1240
z.reportln( "+++++++++ read() monThreadisInterrupted" );
1243
if (debug_read_results)
1244
z.reportln( "RXTXPort:SerialInputStream:read() L" );
1082
1245
waitForTheNativeCodeSilly();
1083
int result = readByte();
1085
System.out.println( "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" );
1090
1263
* @return int number of bytes read
1091
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
1093
public int read( byte b[] ) throws IOException
1274
public synchronized int read( byte b[] ) throws IOException
1097
System.out.println("RXTXPort:SerialInputStream:read(" + b.length + ")");
1278
z.reportln( "RXTXPort:SerialInputStream:read(" + b.length + ") called");
1098
1279
if ( monThreadisInterrupted == true )
1100
throw new IOException( "Port has been Closed" );
1102
1284
waitForTheNativeCodeSilly();
1103
result = read( b, 0, b.length);
1105
System.out.println( "read = " + result );
1287
result = read( b, 0, b.length);
1288
if (debug_read_results)
1289
z.reportln( "RXTXPort:SerialInputStream:read() returned " + result + " bytes" );
1109
1298
read(byte b[], int, int)
1116
1305
* @return int number of bytes read
1117
1306
* @throws IOException
1119
public int read( byte b[], int off, int len )
1123
System.out.println("RXTXPort:SerialInputStream:read(" + b.length + " " + off + " " + len + ") " /*+ new String(b) */ );
1126
* Some sanity checks
1128
if ( fd == 0 ) throw new IOException();
1131
throw new NullPointerException();
1133
if( (off < 0) || (len < 0) || (off+len > b.length))
1134
throw new IndexOutOfBoundsException();
1137
* Return immediately if len==0
1139
if( len==0 ) return 0;
1142
* See how many bytes we should read
1149
* If threshold is disabled, read should return as soon
1150
* as data are available (up to the amount of available
1151
* bytes in order to avoid blocking)
1152
* Read may return earlier depending of the receive time
1155
if( available()==0 )
1158
Minimum = Math.min(Minimum,available());
1163
* Threshold is enabled. Read should return when
1164
* 'threshold' bytes have been received (or when the
1165
* receive timeout expired)
1167
Minimum = Math.min(Minimum, threshold);
1169
if ( monThreadisInterrupted == true )
1171
throw new IOException( "Port has been Closed" );
1173
waitForTheNativeCodeSilly();
1174
result = readArray( b, off, Minimum);
1176
System.out.println("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) */);
1180
1518
* @return int bytes available
1181
1519
* @throws IOException
1183
public int available() throws IOException
1521
public synchronized int available() throws IOException
1185
1523
if ( monThreadisInterrupted == true )
1187
throw new IOException( "Port has been Closed" );
1189
int r = nativeavailable();
1190
if ( debug_verbose && r > 0 )
1191
System.out.println("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 " +
1722
* Extension to CommAPI
2072
* Extension to CommAPI. Set Baud Base to 38600 on Linux and W32
1723
2074
* @param int BaudBase The clock frequency divided by 16. Default
1724
2075
* BaudBase is 115200.
1725
2076
* @return boolean true on success
1726
* @throws UnsupportedCommOperationException
2077
* @throws UnsupportedCommOperationException, IOException
1729
2080
public boolean setBaudBase(int BaudBase)
1730
throws UnsupportedCommOperationException
2081
throws UnsupportedCommOperationException,
1733
System.out.println( "RXTXPort:setBaudBase()");
2085
z.reportln( "RXTXPort:setBaudBase()");
1734
2086
return nativeSetBaudBase(BaudBase);
1738
2090
* Extension to CommAPI
1739
2091
* @return int BaudBase
1740
* @throws UnsupportedCommOperationException
2092
* @throws UnsupportedCommOperationException, IOException
1743
public int getBaudBase() throws UnsupportedCommOperationException
2095
public int getBaudBase() throws UnsupportedCommOperationException,
1746
System.out.println( "RXTXPort:getBaudBase()");
2099
z.reportln( "RXTXPort:getBaudBase()");
1747
2100
return nativeGetBaudBase();
1751
* Extension to CommAPI
2104
* Extension to CommAPI. Set Baud Base to 38600 on Linux and W32
1752
2106
* @param int Divisor;
1753
* @throws UnsupportedCommOperationException
2107
* @throws UnsupportedCommOperationException, IOException
1756
2110
public boolean setDivisor(int Divisor)
1757
throws UnsupportedCommOperationException
2111
throws UnsupportedCommOperationException, IOException
1760
System.out.println( "RXTXPort:setDivisor()");
2114
z.reportln( "RXTXPort:setDivisor()");
1761
2115
return nativeSetDivisor(Divisor);
1765
2119
* Extension to CommAPI
1766
2120
* @returns int Divisor;
1767
* @throws UnsupportedCommOperationException
2121
* @throws UnsupportedCommOperationException, IOException
1770
public int getDivisor() throws UnsupportedCommOperationException
2124
public int getDivisor() throws UnsupportedCommOperationException,
1773
System.out.println( "RXTXPort:getDivisor()");
2128
z.reportln( "RXTXPort:getDivisor()");
1774
2129
return nativeGetDivisor();