Subversion Repositories Programming Utils

Compare Revisions

Ignore whitespace Rev 19 → Rev 20

/JavaSerial/NativeCode/SerialPortImpl.c
2,6 → 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_SERIAL_PORT_STRUCT( port, io_name ) DCB io_name = {0};\
io_name.DCBlength = sizeof( io_name ); \
23,7 → 24,8
#define HW_FLOW CNEW_RTSCTS
#endif
#define SPEED_SWITCH(SPD,io) case SPD: cfsetospeed( &io, B##SPD ); break;
#define SPEED_SWITCH(SPD,io) case SPD: cfsetospeed( &io, B##SPD ); cfsetispeed( &io, B##SPD ); break;
#define GET_SPEED_SWITCH(SPD,io) case B##SPD: return SPD;
#define GET_SERIAL_PORT_STRUCT( port, io_name ) struct termios io_name; \
411,6 → 413,71
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: getBaudRateInternal
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_getBaudRateInternal
(JNIEnv * env, jobject obj){
jint array_pos;
struct port_descriptor* desc;
 
array_pos = get_handle( env, obj );
 
if( array_pos < 0 ){
//throw new exception
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 0;
}
 
desc = port_list[ array_pos ];
 
if( desc == NULL ){
//throw new exception
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 0;
}
 
//Now, let's get the baud rate information
{
GET_SERIAL_PORT_STRUCT( desc->port, newio );
#ifdef _WIN32
#else
switch( cfgetispeed( &newio ) ){
#endif
GET_SPEED_SWITCH( 0, newio );
GET_SPEED_SWITCH( 50, newio );
GET_SPEED_SWITCH( 75, newio );
GET_SPEED_SWITCH( 110, newio );
GET_SPEED_SWITCH( 134, newio );
GET_SPEED_SWITCH( 150, newio );
GET_SPEED_SWITCH( 200, newio );
GET_SPEED_SWITCH( 300, newio );
GET_SPEED_SWITCH( 600, newio );
GET_SPEED_SWITCH( 1200, newio );
GET_SPEED_SWITCH( 1800, newio );
GET_SPEED_SWITCH( 2400, newio );
GET_SPEED_SWITCH( 4800, newio );
GET_SPEED_SWITCH( 9600, newio );
GET_SPEED_SWITCH( 19200, newio );
GET_SPEED_SWITCH( 38400, newio );
GET_SPEED_SWITCH( 115200, newio );
default:
return 0;
} /* end switch */
}
 
}
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: doClose
* Signature: ()V
*/
456,6 → 523,52
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: getStopBitsInternal
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_getStopBitsInternal
(JNIEnv * env, jobject obj){
jint array_pos;
struct port_descriptor* desc;
 
array_pos = get_handle( env, obj );
 
if( array_pos < 0 ){
//throw new exception
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 0;
}
 
desc = port_list[ array_pos ];
 
if( desc == NULL ){
//throw new exception
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 0;
}
 
{
GET_SERIAL_PORT_STRUCT( desc->port, newio );
#ifdef _WIN32
#else
if( newio.c_cflag & CSTOPB ){
return 2;
}else{
return 1;
}
#endif
}
}
/*
* Class: com_rm5248_serial_SerialPort
* Method: setCharSize
* Signature: (I)Z
*/
476,6 → 589,62
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: getCharSizeInternal
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_getCharSizeInternal
(JNIEnv * env, jobject obj){
jint array_pos;
struct port_descriptor* desc;
 
array_pos = get_handle( env, obj );
 
if( array_pos < 0 ){
//throw new exception
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 0;
}
 
desc = port_list[ array_pos ];
 
if( desc == NULL ){
//throw new exception
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 0;
}
 
//Now get the char size
{
GET_SERIAL_PORT_STRUCT( desc->port, newio );
#ifdef _WIN32
#else
if( ( newio.c_cflag | CS8 ) == CS8 ){
return 8;
}else if( ( newio.c_cflag | CS7 ) == CS7 ){
return 7;
}else if( ( newio.c_cflag | CS6 ) == CS6 ){
return 6;
}else if( ( newio.c_cflag | CS5 ) == CS5 ){
return 5;
}else{
return 0;
}
#endif
}
 
}
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: setParity
* Signature: (I)Z
*/
494,6 → 663,61
return set_parity( desc, parity );
}
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: getParityInternal
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_getParityInternal
(JNIEnv * env, jobject obj){
jint array_pos;
struct port_descriptor* desc;
 
array_pos = get_handle( env, obj );
 
if( array_pos < 0 ){
//throw new exception
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 0;
}
 
desc = port_list[ array_pos ];
 
if( desc == NULL ){
//throw new exception
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 0;
}
 
{
GET_SERIAL_PORT_STRUCT( desc->port, newio );
#ifdef _WIN32
#else
if( !( newio.c_cflag & PARENB ) ){
//No parity
return 0;
}else if( newio.c_cflag & PARODD ){
//Odd parity
return 1;
}else if( !( newio.c_cflag & PARODD ) ){
//Even parity
return 2;
}else{
return -1;
}
#endif
}
 
}
 
//
// ------------------------------------------------------------------------
// ------------------Input/Output methods below here-----------------------
/JavaSerial/src/com/rm5248/serial/SerialPort.java
87,6 → 87,8
private SerialOutputStream outputStream;
/* Make sure we don't close ourselves twice */
private boolean closed;
/* The name of the port that's currently open */
private String portName;
 
/**
* Open the specified port, 8 bit characters, 1 stop bit, no parity, no flow control, 9600 baud.
151,7 → 153,8
int myStop = 0;
int myParity = 0;
int myFlow = 0;
 
this.portName = portName;
closed = false;
 
switch( rate ){
477,6 → 480,18
return cont;
}
public String toString(){
return "Serial Port " + portName + ":" + getBaudRate();
}
/**
* Get the name of the serial port that this object represents.
* @return
*/
public String getPortName(){
return portName;
}
 
/**
* Open the specified port, return an internal handle to the data structure for this port.
549,5 → 564,7
private native boolean setFlowControl( int flowControl );
 
private native int getFlowControlInternal();
 
 
}