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,79 → 70,61 |
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 { |
avail = stream.available(); |
} catch (IOException e1) { |
byteRead = stream.read(); |
} catch (IOException e) { |
synchronized( buffer ){ |
exceptionToThrow = e; |
buffer.notify(); |
} |
break; |
} |
SerialLineState s = new SerialLineState(); |
|
if( avail > 0 ){ |
try { |
byteRead = stream.read(); |
} catch (IOException e) { |
synchronized( buffer ){ |
exceptionToThrow = e; |
buffer.notify(); |
if( ( byteRead & ( 0x01 << 9) ) > 0 ){ |
s.carrierDetect = true; |
} |
if( ( byteRead & (0x01 << 10 ) ) > 0 ){ |
s.clearToSend = true; |
} |
if( ( byteRead & (0x01 << 11 ) ) > 0 ){ |
s.dataSetReady = true; |
} |
if( ( byteRead & (0x01 << 12 ) ) > 0 ){ |
s.dataTerminalReady = true; |
} |
if( ( byteRead & (0x01 << 13 ) ) > 0 ){ |
s.requestToSend = true; |
} |
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; |
} |
break; |
} |
|
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[ 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 ){ |
152,15 → 134,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++; |
168,7 → 150,7 |
break; |
} |
} |
|
|
return readSoFar; |
} |
|