Subversion Repositories Programming Utils

Compare Revisions

Ignore whitespace Rev 62 → Rev 63

/trunk/JavaSerial/NativeCode/SerialPortImpl.c
1059,6 → 1059,7
return ret_val;
}
 
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: setSerialLineStateInternal
1135,8 → 1136,6
//
 
 
#include <stdio.h>
 
/*
* Class: com_rm5248_serial_SerialInputStream
* Method: readByte
1219,26 → 1218,68
}
#else
 
//Okay, this here is going to be a bit ugly.
//The problem here is that Linux/POSIX don't specify that TIOCMIWAIT
//has to exist. Also, the fact that if we use TIOCMIWAIT we don't
//timeout or anything. What would be very convenient in this case
//would be to have select() return when the state changed, but
//it's not possible. :(
//Ironically, this is one case in which the Windows serial API
//is better than the POSIX way.
fd_set fdset;
struct timeval timeout;
int originalState;
int selectStatus;
 
FD_ZERO( &fdset );
FD_SET( desc->port, &fdset );
 
if( select( desc->port + 1, &fdset, NULL, NULL, NULL ) < 0 ){
//first get the original state of the serial port lines
if( ioctl( desc->port, TIOCMGET, &originalState ) < 0 ){
throw_io_exception( env, errno );
return -1;
}
if( FD_ISSET( desc->port, &fdset ) ){
stat = read( desc->port, &ret_val, 1 );
if( stat < 0 ){
//throw new exception
while( 1 ){
FD_ZERO( &fdset );
FD_SET( desc->port, &fdset );
timeout.tv_sec = 0;
timeout.tv_usec = 1000; // 1,000 microseconds = 100ms
 
selectStatus = select( desc->port + 1, &fdset, NULL, NULL, &timeout );
if( selectStatus < 0 ){
throw_io_exception( env, errno );
return -1;
}
 
if( selectStatus == 0 ){
//This was a timeout
if( ioctl( desc->port, TIOCMGET, &get_val ) < 0 ){
throw_io_exception( env, errno );
return -1;
}
 
if( get_val == originalState ){
//The state of the lines have not changed,
//continue on until something changes
continue;
}
}
if( FD_ISSET( desc->port, &fdset ) ){
stat = read( desc->port, &ret_val, 1 );
if( stat < 0 ){
//throw new exception
throw_io_exception( env, errno );
return -1;
}
//This is a valid byte, set our valid bit
ret_val |= ( 0x01 << 15 );
//This is a valid byte, set our valid bit
ret_val |= ( 0x01 << 15 );
}
//We get to this point if we either 1. have data or
//2. our state has changed
break;
}
 
//Now that we have read in the character, let's get the serial port line state.
/trunk/JavaSerial/src/com/rm5248/serial/SerialPort.java
549,15 → 549,20
// do some sort of bitwise operations here....
if( ( gotState & 0x01 ) > 0 ){
s.carrierDetect = true;
}else if( ( gotState & (0x01 << 1 ) ) > 0 ){
}
if( ( gotState & (0x01 << 1 ) ) > 0 ){
s.clearToSend = true;
}else if( ( gotState & (0x01 << 2 ) ) > 0 ){
}
if( ( gotState & (0x01 << 2 ) ) > 0 ){
s.dataSetReady = true;
}else if( ( gotState & (0x01 << 3 ) ) > 0 ){
}
if( ( gotState & (0x01 << 3 ) ) > 0 ){
s.dataTerminalReady = true;
}else if( ( gotState & (0x01 << 4 ) ) > 0 ){
}
if( ( gotState & (0x01 << 4 ) ) > 0 ){
s.requestToSend = true;
}else if( ( gotState & (0x01 << 5 ) ) > 0 ){
}
if( ( gotState & (0x01 << 5 ) ) > 0 ){
s.ringIndicator = true;
}
 
/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,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;
}