Subversion Repositories Programming Utils

Compare Revisions

Ignore whitespace Rev 22 → Rev 23

/JavaSerial/NativeCode/SerialPortImpl.c
2,7 → 2,7
#include <windows.h>
#define SPEED_SWITCH(SPD,io) case SPD: io.BaudRate = CBR_##SPD; break;
#define GET_SPEED_SWITCH(SPD,io) case CBR_SPD: return SPD;
#define GET_SPEED_SWITCH(SPD,io) case CBR_##SPD: return SPD;
#define GET_SERIAL_PORT_STRUCT( port, io_name ) DCB io_name = {0};\
io_name.DCBlength = sizeof( io_name ); \
126,6 → 126,28
GET_SERIAL_PORT_STRUCT( desc->port, newio );
 
#ifdef _WIN32
newio.fBinary = TRUE;
newio.fParity = TRUE;
newio.fOutxCtsFlow = FALSE;
newio.fOutxDsrFlow = FALSE;
newio.fDtrControl = DTR_CONTROL_DISABLE;
newio.fDsrSensitivity = FALSE;
newio.fOutX = FALSE;
newio.fInX = FALSE;
newio.fNull = FALSE;
newio.fRtsControl = FALSE;
//Set the timeouts
{
COMMTIMEOUTS timeouts = {0};
timeouts.ReadIntervalTimeout = 10;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.ReadTotalTimeoutConstant = 0;
timeouts.WriteTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 1000;
if( SetCommTimeouts( desc->port, &timeouts ) == 0 ){
printf("bad timeout\n"); fflush(stdout);}
}
#else
newio.c_iflag |= IGNBRK;
newio.c_iflag &= ~BRKINT;
141,6 → 163,9
return 1;
}
 
/**
* @param data_bits The number of data bits
*/
static int set_data_bits( struct port_descriptor* desc, int data_bits ){
GET_SERIAL_PORT_STRUCT( desc->port, newio );
 
164,6 → 189,9
return 1;
}
 
/**
* @param stop_bits 1 for 1, 2 for 2
*/
static int set_stop_bits( struct port_descriptor* desc, int stop_bits ){
GET_SERIAL_PORT_STRUCT( desc->port, newio );
 
186,6 → 214,9
return 1;
}
 
/**
* @param parity 0 for no parity, 1 for odd parity, 2 for even parity
*/
static int set_parity( struct port_descriptor* desc, int parity ){
GET_SERIAL_PORT_STRUCT( desc->port, newio );
 
212,10 → 243,17
return 1;
}
 
/**
* @param flow_control 0 for none, 1 for hardware, 2 for software
*/
static int set_flow_control( struct port_descriptor* desc, int flow_control ){
GET_SERIAL_PORT_STRUCT( desc->port, newio );
 
#ifdef _WIN32
#ifdef _WIN32
if( flow_control == 0 ){
}else if( flow_control == 1 ){
}else if( flow_control == 2 ){
}
#else
if( flow_control == 0 ){
newio.c_cflag &= ~( IXON | IXOFF | IXANY );
289,7 → 327,7
 
//Now, let's get to the actual opening of our port
#ifdef _WIN32
new_port->port = CreateFile( port_to_open, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
new_port->port = CreateFile( port_to_open, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0 );
if( new_port->port == INVALID_HANDLE_VALUE ){
if( GetLastError() == ERROR_FILE_NOT_FOUND ){
LPTSTR error_text = NULL;
449,20 → 487,26
{
GET_SERIAL_PORT_STRUCT( desc->port, newio );
#ifdef _WIN32
GetCommState( desc->port, &newio );
switch( newio.BaudRate ){
#else
switch( cfgetispeed( &newio ) ){
#endif
GET_SPEED_SWITCH( 0, newio );
GET_SPEED_SWITCH( 50, newio );
GET_SPEED_SWITCH( 75, newio );
#endif /* _WIN32 */
GET_SPEED_SWITCH( 110, newio );
#ifndef _WIN32
GET_SPEED_SWITCH( 134, newio );
GET_SPEED_SWITCH( 150, newio );
GET_SPEED_SWITCH( 200, newio );
#endif /* _WIN32 */
GET_SPEED_SWITCH( 300, newio );
GET_SPEED_SWITCH( 600, newio );
GET_SPEED_SWITCH( 1200, newio );
#ifndef _WIN32
GET_SPEED_SWITCH( 1800, newio );
#endif /* _WIN32 */
GET_SPEED_SWITCH( 2400, newio );
GET_SPEED_SWITCH( 4800, newio );
GET_SPEED_SWITCH( 9600, newio );
558,6 → 602,13
{
GET_SERIAL_PORT_STRUCT( desc->port, newio );
#ifdef _WIN32
if( newio.StopBits == 1 ){
return 1;
}else if( newio.StopBits == 2 ){
return 2;
}else{
return -1;
}
#else
if( newio.c_cflag & CSTOPB ){
return 2;
626,6 → 677,7
GET_SERIAL_PORT_STRUCT( desc->port, newio );
#ifdef _WIN32
return newio.ByteSize;
#else
if( ( newio.c_cflag | CS8 ) == CS8 ){
return 8;
700,6 → 752,15
{
GET_SERIAL_PORT_STRUCT( desc->port, newio );
#ifdef _WIN32
if( newio.Parity == NOPARITY ){
return 0;
}else if( newio.Parity == ODDPARITY ){
return 1;
}else if( newio.Parity == EVENPARITY ){
return 2;
}else{
return -1;
}
#else
if( !( newio.c_cflag & PARENB ) ){
//No parity
760,6 → 821,14
}
 
#ifdef _WIN32
if( !ReadFile( desc->port, &ret_val, 1, &stat, NULL) ){
jclass exception_class;
(*env)->ExceptionDescribe( env );
(*env)->ExceptionClear( env );
exception_class = (*env)->FindClass(env, "java/io/IOException");
(*env)->ThrowNew(env, exception_class, strerror( errno ) );
return -1;
}
#else
stat = read( desc->port, &ret_val, sizeof( ret_val ) );
if( stat < 0 ){
810,6 → 879,21
}
 
#ifdef _WIN32
{
LPDWORD comErrors = {0};
LPCOMSTAT portStatus = {0};
if( !ClearCommError( desc->port, comErrors, portStatus ) ){
//return value zero = fail
jclass exception_class;
(*env)->ExceptionDescribe( env );
(*env)->ExceptionClear( env );
exception_class = (*env)->FindClass(env, "java/io/IOException");
(*env)->ThrowNew(env, exception_class, strerror( errno ) );
return -1;
}else{
ret_val = portStatus->cbInQue;
}
}
#else
if( ioctl( desc->port, FIONREAD, &ret_val ) < 0 ){
//throw new exception
866,7 → 950,7
}
 
#ifdef _WIN32
if( !WriteFile( desc->port, &byte_write, sizeof( byte_write ), &bytes_written, NULL) ){
if( !WriteFile( desc->port, &byte_write, sizeof( byte_write ), &bytes_written, NULL ) ){
//throw new exception
jclass exception_class;
(*env)->ExceptionDescribe( env );
890,3 → 974,13
 
}
 
//BOOL EscapeCommFunction(HANDLE, DWORD).
/*
1. CLRDTR
2. SETDTR
3. SETRTS
4. CLRRTS
5. SETBREAK
6. CLRBREAK
*/