Subversion Repositories Programming Utils

Compare Revisions

Ignore whitespace Rev 77 → Rev 76

/trunk/JavaSerial/NativeCode/SerialPortImpl.c
26,7 → 26,6
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <pthread.h>
 
#ifdef CRTSCTS
#define HW_FLOW CRTSCTS
66,13 → 65,6
HANDLE port;
#else
int port;
/* There appears to be a case where we can free() the port_descriptor
* after the select() call in SerialInputStream_readByte().
* This means that we segfault on the FD_SET() call.
* This mutex will keep us alive until we have exited the
* readByte() call.
*/
pthread_mutex_t in_use;
#endif
};
 
79,8 → 71,8
//
// Local Variables
//
static struct port_descriptor** port_list = NULL;
static int port_list_size;
struct port_descriptor** port_list = NULL;
int port_list_size;
 
#ifdef _WIN32
//Unfortunately, Windows does not let us get the state of the DTR/RTS lines.
482,7 → 474,6
}
#else
pthread_mutex_init( &(new_port->in_use), NULL );
new_port->port = open( port_to_open, O_RDWR );
if( new_port->port < 0 && errno == ENOENT ){
//That's not a valid serial port, error out
645,7 → 636,6
}
}
#else
pthread_mutex_init( &(new_port->in_use), NULL );
new_port->port = open( port_to_open, O_RDWR );
if( new_port->port < 0 && errno == ENOENT ){
//That's not a valid serial port, error out
715,10 → 705,6
}
close( desc->port );
#ifndef _WIN32
pthread_mutex_lock( &(desc->in_use) );
pthread_mutex_unlock( &(desc->in_use) );
#endif
free( port_list[ array_pos ] );
port_list[ array_pos ] = NULL;
}
1289,12 → 1275,9
int originalState;
int selectStatus;
 
pthread_mutex_lock( &(desc->in_use) );
 
//first get the original state of the serial port lines
if( ioctl( desc->port, TIOCMGET, &originalState ) < 0 ){
throw_io_exception( env, errno );
pthread_mutex_unlock( &(desc->in_use) );
return -1;
}
1302,20 → 1285,12
FD_ZERO( &fdset );
FD_SET( desc->port, &fdset );
timeout.tv_sec = 0;
timeout.tv_usec = 10000; // 10,000 microseconds = 10ms
timeout.tv_usec = 1000; // 1,000 microseconds = 100ms
 
selectStatus = select( desc->port + 1, &fdset, NULL, NULL, &timeout );
if( selectStatus < 0 ){
int errval;
if( errno == EBADF ){
// EOF
errval= 0;
}else{
throw_io_exception( env, errno );
errval = -1;
}
pthread_mutex_unlock( &(desc->in_use) );
return errval;
throw_io_exception( env, errno );
return -1;
}
 
if( selectStatus == 0 ){
1322,7 → 1297,6
//This was a timeout
if( ioctl( desc->port, TIOCMGET, &get_val ) < 0 ){
throw_io_exception( env, errno );
pthread_mutex_unlock( &(desc->in_use) );
return -1;
}
 
1339,7 → 1313,6
if( stat < 0 ){
//throw new exception
throw_io_exception( env, errno );
pthread_mutex_unlock( &(desc->in_use) );
return -1;
}
1359,7 → 1332,6
//information on our serial port state.
if( ioctl( desc->port, TIOCMGET, &get_val ) < 0 ){
throw_io_exception( env, errno );
pthread_mutex_unlock( &(desc->in_use) );
return -1;
}
 
1393,7 → 1365,6
ret_val |= ( 0x01 << 14 );
}
pthread_mutex_unlock( &(desc->in_use) );
#endif
 
return ret_val;