Subversion Repositories Programming Utils

Compare Revisions

Ignore whitespace Rev 61 → Rev 62

/trunk/JavaSerial/NativeCode/SerialPortImpl.c
247,6 → 247,8
newio.Parity = EVENPARITY;
}
#else
newio.c_iflag &= ~IGNPAR;
newio.c_cflag &= ~( PARODD | PARENB );
if( parity == 0 ){
newio.c_iflag |= IGNPAR;
}else if( parity == 1 ){
285,6 → 287,8
newio.fInX = TRUE;
}
#else
newio.c_iflag &= ~( IXON | IXOFF | IXANY );
newio.c_cflag &= ~HW_FLOW;
if( flow_control == 0 ){
newio.c_iflag &= ~( IXON | IXOFF | IXANY );
}else if( flow_control == 1 ){
1073,21 → 1077,6
ret_val = 0;
#ifdef _WIN32
//Can't set CD on Windows
if( get_bool( env, serial, "carrierDetect" ) ){
}else{
}
 
//Can't set CTS on Windows
if( get_bool( env, serial, "clearToSend" ) ){
}else{
}
 
//Can't set DSR in Windows
if( get_bool( env, serial, "dataSetReady" ) ){
}else{
}
 
if( get_bool( env, serial, "dataTerminalReady" ) ){
if( !EscapeCommFunction( desc->port, SETDTR ) ){
throw_io_exception_message( env, "Could not set DTR" );
1100,11 → 1089,6
}
}
 
//Can't set in Windows
if( get_bool( env, serial, "ringIndicator" ) ){
}else{
}
 
if( get_bool( env, serial, "requestToSend" ) ){
if( !EscapeCommFunction( desc->port, SETRTS ) ){
throw_io_exception_message( env, "Could not set RTS" );
1117,76 → 1101,27
}
}
#else
if( get_bool( env, serial, "carrierDetect" ) ){
if( ioctl( desc->port, TIOCMBIS, TIOCM_CD ) < 0 ){
throw_io_exception_message( env, "Could not set CD" );
return -1;
}
}else{
if( ioctl( desc->port, TIOCMBIC, TIOCM_CD ) < 0 ){
throw_io_exception_message( env, "Could not set CD" );
return -1;
}
}
int toSet = 0;
 
if( get_bool( env, serial, "clearToSend" ) ){
if( ioctl( desc->port, TIOCMBIS, TIOCM_CTS ) < 0 ){
throw_io_exception_message( env, "Could not set CTS" );
return -1;
}
}else{
if( ioctl( desc->port, TIOCMBIC, TIOCM_CTS ) < 0 ){
throw_io_exception_message( env, "Could not set CTS" );
return -1;
}
if( ioctl( desc->port, TIOCMGET, &toSet ) < 0 ){
throw_io_exception_message( env, "Could not get port settings" );
return -1;
}
 
if( get_bool( env, serial, "dataSetReady" ) ){
if( ioctl( desc->port, TIOCMBIS, TIOCM_LE ) < 0 ){
throw_io_exception_message( env, "Could not set DSR" );
return -1;
}
}else{
if( ioctl( desc->port, TIOCMBIC, TIOCM_LE ) < 0 ){
throw_io_exception_message( env, "Could not set DSR" );
return -1;
}
}
 
if( get_bool( env, serial, "dataTerminalReady" ) ){
if( ioctl( desc->port, TIOCMBIS, TIOCM_DTR ) < 0 ){
throw_io_exception_message( env, "Could not set DTR" );
return -1;
}
toSet |= TIOCM_DTR;
}else{
if( ioctl( desc->port, TIOCMBIC, TIOCM_DTR ) < 0 ){
throw_io_exception_message( env, "Could not set DTR" );
return -1;
}
toSet &= ~TIOCM_DTR;
}
 
if( get_bool( env, serial, "ringIndicator" ) ){
if( ioctl( desc->port, TIOCMBIS, TIOCM_RNG ) < 0 ){
throw_io_exception_message( env, "Could not set Ring" );
return -1;
}
if( get_bool( env, serial, "requestToSend" ) ){
toSet |= TIOCM_RTS;
}else{
if( ioctl( desc->port, TIOCMBIC, TIOCM_RNG ) < 0 ){
throw_io_exception_message( env, "Could not set Ring" );
return -1;
}
toSet &= ~TIOCM_RTS;
}
 
if( get_bool( env, serial, "requestToSend" ) ){
if( ioctl( desc->port, TIOCMBIS, TIOCM_RTS ) < 0 ){
throw_io_exception_message( env, "Could not set RTS" );
return -1;
}
}else{
if( ioctl( desc->port, TIOCMBIC, TIOCM_RTS ) < 0 ){
throw_io_exception_message( env, "Could not set RTS" );
return -1;
}
if( ioctl( desc->port, TIOCMSET, &toSet ) < 0 ){
throw_io_exception_message( env, "Could not set port settings" );
}
#endif
 
/trunk/JavaSerial/src/com/rm5248/serial/SerialPort.java
11,6 → 11,12
* When creating a SerialPort object, you give the SerialPort the name of the port that you
* wish to open.
*
* When opening a SerialPort and setting values, an IllegalArgumentException may be thrown
* if any of the values are NULL.
*
* When getting and settings the properties of the SerialPort using one of the getXXX()
* or setXXX() methods, an IllegalStateException will be thrown if the port has been closed.
*
* @author rm5248
*
*/
157,6 → 163,10
* @throws NotASerialPortException If the port is not in fact a serial port
*/
public SerialPort( String portName, boolean keepSettings ) throws NoSuchPortException, NotASerialPortException{
if( portName == null ){
throw new IllegalArgumentException( "portName must not be null" );
}
if( keepSettings ){
this.handle = openPort( portName );
this.portName = portName;
265,7 → 275,33
int myFlow = 0;
SerialLineState s;
int state;
//Check for null values in our arguments
if( portName == null ){
throw new IllegalArgumentException( "portName must not be null" );
}
if( rate == null ){
throw new IllegalArgumentException( "rate must not be null" );
}
if( data == null ){
throw new IllegalArgumentException( "data must not be null" );
}
if( stop == null ){
throw new IllegalArgumentException( "stop must not be null" );
}
if( parity == null ){
throw new IllegalArgumentException( "parity must not be null" );
}
if( flow == null ){
throw new IllegalArgumentException( "flow must not be null" );
}
 
//Okay, looks like we're good!
this.portName = portName;
closed = false;
 
342,7 → 378,6
* Set the Baud Rate for this port.
*
* @param rate
* @throws IllegalStateException if the port has already been closed.
*/
public void setBaudRate( BaudRate rate ){
int myRate = 0;
350,6 → 385,10
if( closed ){
throw new IllegalStateException( "Cannot set the BaudRate once the port has been closed." );
}
if( rate == null ){
throw new IllegalArgumentException( "rate must not be null" );
}
 
switch( rate ){
case B0 : myRate = 0; break;
377,7 → 416,6
* Get the input stream to be used to read from this SerialPort.
*
* @return The input stream.
* @throws IllegalStateException if the port has already been closed.
*/
public InputStream getInputStream(){
if( closed ){
390,7 → 428,6
/** Get the output stream used to write data to this device.
*
* @return The output stream.
* @throws IllegalStateException if the port has already been closed.
*/
public OutputStream getOutputStream(){
if( closed ){
425,7 → 462,6
* Set the stop bits of the serial port, after the port has been opened.
*
* @param stop
* @throws IllegalStateException if the port has already been closed.
*/
public void setStopBits( StopBits stop ){
int myStop = 0;
433,6 → 469,10
if( closed ){
throw new IllegalStateException( "Cannot set the StopBits once the port has been closed." );
}
if( stop == null ){
throw new IllegalArgumentException( "stop must not be null" );
}
 
switch( stop ){
case STOPBITS_1: myStop = 1; break;
446,7 → 486,6
* Set the data bits size, after the port has been opened.
*
* @param data
* @throws IllegalStateException if the port has already been closed.
*/
public void setDataSize( DataBits data ){
int myData = 0;
454,6 → 493,10
if( closed ){
throw new IllegalStateException( "Cannot set the DataBits once the port has been closed." );
}
if( data == null ){
throw new IllegalArgumentException( "data must not be null" );
}
 
switch( data ){
case DATABITS_5: myData = 5; break;
469,7 → 512,6
* Set the parity of the serial port, after the port has been opened.
*
* @param parity
* @throws IllegalStateException if the port has been closed.
*/
public void setParity( Parity parity ){
int myParity = 0;
478,6 → 520,10
throw new IllegalStateException( "Cannot set the parity once the port has been closed." );
}
 
if( parity == null ){
throw new IllegalArgumentException( "parity must not be null" );
}
switch( parity ){
case NONE: myParity = 0; break;
case ODD: myParity = 1; break;
524,6 → 570,10
* @param state
*/
public void setSerialLineState( SerialLineState state ){
if( closed ){
throw new IllegalStateException( "Cannot set the serial line state once the port has been closed." );
}
setSerialLineStateInternal( state );
}
 
533,8 → 583,15
* @return
*/
public SerialPort.BaudRate getBaudRate(){
int baudRate = getBaudRateInternal();
BaudRate toReturn = BaudRate.B0;
int baudRate;
BaudRate toReturn;
if( closed ){
throw new IllegalStateException( "Cannot get the baud rate once the port has been closed." );
}
baudRate = getBaudRateInternal();
toReturn = BaudRate.B0;
 
switch( baudRate ){
case 0 : toReturn = BaudRate.B0 ; break;
564,8 → 621,15
* @return
*/
public SerialPort.DataBits getDataBits(){
int dataBits = getCharSizeInternal();
DataBits bits = DataBits.DATABITS_8;
int dataBits;
DataBits bits;
if( closed ){
throw new IllegalStateException( "Cannot get the data bits once the port has been closed." );
}
dataBits = getCharSizeInternal();
bits = DataBits.DATABITS_8;
 
switch( dataBits ){
case 8: bits = DataBits.DATABITS_8; break;
583,8 → 647,15
* @return
*/
public SerialPort.StopBits getStopBits(){
int stopBits = getStopBitsInternal();
StopBits bits = StopBits.STOPBITS_1;
int stopBits;
StopBits bits;
if( closed ){
throw new IllegalStateException( "Cannot get stop bits once the port has been closed." );
}
stopBits = getStopBitsInternal();
bits = StopBits.STOPBITS_1;
 
switch( stopBits ){
case 1: bits = StopBits.STOPBITS_1; break;
600,8 → 671,15
* @return
*/
public SerialPort.Parity getParity(){
int parity = getParityInternal();
Parity par = Parity.NONE;
int parity;
Parity par;
if( closed ){
throw new IllegalStateException( "Cannot get the parity once the port has been closed." );
}
parity = getParityInternal();
par = Parity.NONE;
 
switch( parity ){
case 0: par = Parity.NONE; break;
618,8 → 696,15
* @return
*/
public SerialPort.FlowControl getFlowControl(){
int flowControl = getFlowControlInternal();
FlowControl cont = FlowControl.NONE;
int flowControl;
FlowControl cont;
if( closed ){
throw new IllegalStateException( "Cannot get the flow once the port has been closed." );
}
flowControl = getFlowControlInternal();
cont = FlowControl.NONE;
 
switch( flowControl ){
case 0: cont = FlowControl.NONE; break;
636,6 → 721,10
* @param flow
*/
public void setFlowControl( FlowControl flow ){
if( closed ){
throw new IllegalStateException( "Cannot set flow once the port has been closed." );
}
switch( flow ){
case HARDWARE: setFlowControl( 1 ); break;
case NONE: setFlowControl( 0 ); break;
648,6 → 737,8
* Note that this activity does NOT include receive and transmit events - this is
* changes on the lines of the serial port, such as RI, DSR, and DTR.
*
* If listen is null, will remove the listener.
*
* @param listen The listener which gets events
*/
public void setSerialChangeListener( SerialChangeListener listen ){
655,8 → 746,10
serialListen.doStop();
}
serialListen = new SerialStateListener( listen );
new Thread( serialListen, "SerialListen" ).start();
if( listen != null ){
serialListen = new SerialStateListener( listen );
new Thread( serialListen, "SerialListen" ).start();
}
 
}
 
/trunk/JavaSerial/src/com/rm5248/serial/BufferedSerialInputStream.java
35,7 → 35,7
@Override
public int read() throws IOException {
int byteToReturn;
 
if( bufferBegin != bufferEnd ){
//we already have a character
byteToReturn = buffer[ bufferBegin++ ];
50,12 → 50,12
} catch (InterruptedException e) {
e.printStackTrace();
}
 
if( exceptionToThrow != null ){
throw exceptionToThrow;
}
}
 
byteToReturn = buffer[ bufferBegin++ ];
if( bufferBegin >= buffer.length ){
//wrap around to the start of the array
62,7 → 62,7
bufferBegin = 0;
}
}
 
return byteToReturn;
}
 
70,56 → 70,79
public void run() {
while( true ){
int byteRead = 0;
SerialLineState s = new SerialLineState();
int avail;
//Okay, this here is rather ugly.
//but what we're going to do is to poll every 100 ms for the serial line state
//to change. that way, we can triggr
try{
Thread.sleep( 100 );
}catch( InterruptedException ex ){}
try {
byteRead = stream.read();
} catch (IOException e) {
synchronized( buffer ){
exceptionToThrow = e;
buffer.notify();
}
avail = stream.available();
} catch (IOException e1) {
break;
}
SerialLineState s = new SerialLineState();
 
if( ( byteRead & ( 0x01 << 9) ) > 0 ){
s.carrierDetect = true;
}else if( ( byteRead & (0x01 << 10 ) ) > 0 ){
s.clearToSend = true;
}else if( ( byteRead & (0x01 << 11 ) ) > 0 ){
s.dataSetReady = true;
}else if( ( byteRead & (0x01 << 12 ) ) > 0 ){
s.dataTerminalReady = true;
}else if( ( byteRead & (0x01 << 13 ) ) > 0 ){
s.requestToSend = true;
}else if( ( byteRead & (0x01 << 14 ) ) > 0 ){
s.ringIndicator = true;
}
if( avail > 0 ){
try {
byteRead = stream.read();
} catch (IOException e) {
synchronized( buffer ){
exceptionToThrow = e;
buffer.notify();
}
break;
}
 
if( ( byteRead & ( 0x01 << 15 ) ) > 0 ){
//this is a valid byte
synchronized( buffer ){
if( bufferEnd + 1 >= buffer.length ){
//loop back around to the beginning
bufferEnd = 0;
}
buffer[ bufferEnd++ ] = (byte)( byteRead & 0xFF );
if( bufferEnd == bufferBegin ){
//the end has wrapped around, increment the beginning
bufferBegin++;
if( bufferBegin >= buffer.length ){
bufferBegin = 0;
if( ( byteRead & ( 0x01 << 9) ) > 0 ){
s.carrierDetect = true;
}else if( ( byteRead & (0x01 << 10 ) ) > 0 ){
s.clearToSend = true;
}else if( ( byteRead & (0x01 << 11 ) ) > 0 ){
s.dataSetReady = true;
}else if( ( byteRead & (0x01 << 12 ) ) > 0 ){
s.dataTerminalReady = true;
}else if( ( byteRead & (0x01 << 13 ) ) > 0 ){
s.requestToSend = true;
}else if( ( byteRead & (0x01 << 14 ) ) > 0 ){
s.ringIndicator = true;
}
 
if( ( byteRead & ( 0x01 << 15 ) ) > 0 ){
//this is a valid byte
synchronized( buffer ){
if( bufferEnd + 1 >= buffer.length ){
//loop back around to the beginning
bufferEnd = 0;
}
buffer[ bufferEnd++ ] = (byte)( byteRead & 0xFF );
if( bufferEnd == bufferBegin ){
//the end has wrapped around, increment the beginning
bufferBegin++;
 
if( bufferBegin >= buffer.length ){
bufferBegin = 0;
}
}
 
buffer.notify();
}
buffer.notify();
}
}else{
try {
callback.postSerialChangedEvent( callback.getSerialLineState() );
} catch (IOException e) {
break;
}
}
 
callback.postSerialChangedEvent( s );
}
}
 
@Override
public int available(){
if( bufferEnd < bufferBegin ){
129,15 → 152,15
return bufferEnd - bufferBegin;
}
}
 
@Override
public int read( byte[] b, int off, int len ) throws IOException{
int readSoFar = 0;
 
if( len == 0 ){
return 0;
}
 
for( int x = off; x < len; x++ ){
b[x] = (byte)read();
readSoFar++;
145,7 → 168,7
break;
}
}
 
return readSoFar;
}