Subversion Repositories Programming Utils

Compare Revisions

Ignore whitespace Rev 10 → Rev 11

/JavaSerial/NativeCode/SerialPortImpl.c
39,7 → 39,7
//
// Helper Methods
//
static jint get_handle
static inline jint get_handle
(JNIEnv * env, jobject obj){
jfieldID fid;
jint array_pos;
55,6 → 55,42
return array_pos;
}
 
static inline int set_baud_rate( struct port_descriptor* desc, int baud_rate ){
#ifdef __WIN32
#else
struct termios newio;
 
tcgetattr( desc->port, &newio );
 
switch( baud_rate ){
SPEED_SWITCH(0,newio);
SPEED_SWITCH(50,newio);
SPEED_SWITCH(75,newio);
SPEED_SWITCH(110,newio);
SPEED_SWITCH(134,newio);
SPEED_SWITCH(150,newio);
SPEED_SWITCH(200,newio);
SPEED_SWITCH(300,newio);
SPEED_SWITCH(600,newio);
SPEED_SWITCH(1200,newio);
SPEED_SWITCH(1800,newio);
SPEED_SWITCH(2400,newio);
SPEED_SWITCH(4800,newio);
SPEED_SWITCH(9600,newio);
SPEED_SWITCH(19200,newio);
SPEED_SWITCH(38400,newio);
SPEED_SWITCH(115200,newio);
}
 
if( tcsetattr( desc->port, TCSANOW, &newio ) < 0 ){
return 0;
}
#endif /* __WIN32 */
 
return 1;
 
}
 
//
// JNI Methods
//
62,10 → 98,10
/*
* Class: com_rm5248_serial_SerialPort
* Method: openPort
* Signature: (Ljava/lang/String;)I
* Signature: (Ljava/lang/String;IIIII)I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_openPort
(JNIEnv * env, jobject obj, jstring port){
(JNIEnv * env, jobject obj, jstring port, jint baudRate, jint dataBits, jint stopBits, jint parity, jint flowControl){
struct port_descriptor* new_port;
int list_pos;
int found = 0;
137,6 → 173,13
 
port_list[ list_pos ] = new_port;
 
//Set the baud rate
set_baud_rate( new_port, baudRate );
//Set the data bits( character size )
//Set the stop bits
//Set the parity
 
 
return list_pos;
}
 
149,10 → 192,6
(JNIEnv * env, jobject obj, jint baud_rate ){
jint array_pos;
struct port_descriptor* desc;
#ifdef __WIN32
#else
struct termios newio;
#endif /* __WIN32 */
 
array_pos = get_handle( env, obj );
 
178,37 → 217,7
return;
}
 
#ifdef __WIN32
#else
tcgetattr( desc->port, &newio );
 
switch( baud_rate ){
SPEED_SWITCH(0,newio);
SPEED_SWITCH(50,newio);
SPEED_SWITCH(75,newio);
SPEED_SWITCH(110,newio);
SPEED_SWITCH(134,newio);
SPEED_SWITCH(150,newio);
SPEED_SWITCH(200,newio);
SPEED_SWITCH(300,newio);
SPEED_SWITCH(600,newio);
SPEED_SWITCH(1200,newio);
SPEED_SWITCH(1800,newio);
SPEED_SWITCH(2400,newio);
SPEED_SWITCH(4800,newio);
SPEED_SWITCH(9600,newio);
SPEED_SWITCH(19200,newio);
SPEED_SWITCH(38400,newio);
SPEED_SWITCH(115200,newio);
}
 
if( tcsetattr( desc->port, TCSANOW, &newio ) < 0 ){
return 0;
}
#endif /* __WIN32 */
 
return 1;
 
return set_baud_rate( desc, baud_rate );
}
 
/*
256,6 → 265,23
}
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: setParity
* Signature: (I)Z
*/
JNIEXPORT jboolean JNICALL Java_com_rm5248_serial_SerialPort_setParity
(JNIEnv * env, jobject obj, jint parity){
 
return 1;
}
 
//
// ------------------------------------------------------------------------
// ------------------Input/Output methods below here-----------------------
// ------------------------------------------------------------------------
//
 
/*
* Class: com_rm5248_serial_SerialPort_SerialInputStream
* Method: readByte
* Signature: ()I
/JavaSerial/NativeCode/com_rm5248_serial_SerialPort.h
10,13 → 10,21
/*
* Class: com_rm5248_serial_SerialPort
* Method: openPort
* Signature: (Ljava/lang/String;)I
* Signature: (Ljava/lang/String;IIIII)I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_openPort
(JNIEnv *, jobject, jstring);
(JNIEnv *, jobject, jstring, jint, jint, jint, jint, jint);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: doClose
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_rm5248_serial_SerialPort_doClose
(JNIEnv *, jobject);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: setBaudRate
* Signature: (I)Z
*/
25,10 → 33,10
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: doClose
* Signature: ()V
* Method: getBaudRateInternal
* Signature: ()I
*/
JNIEXPORT void JNICALL Java_com_rm5248_serial_SerialPort_doClose
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_getBaudRateInternal
(JNIEnv *, jobject);
 
/*
41,6 → 49,14
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: getStopBitsInternal
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_getStopBitsInternal
(JNIEnv *, jobject);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: setCharSize
* Signature: (I)Z
*/
47,6 → 63,46
JNIEXPORT jboolean JNICALL Java_com_rm5248_serial_SerialPort_setCharSize
(JNIEnv *, jobject, jint);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: getCharSizeInternal
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_getCharSizeInternal
(JNIEnv *, jobject);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: setParity
* Signature: (I)Z
*/
JNIEXPORT jboolean JNICALL Java_com_rm5248_serial_SerialPort_setParity
(JNIEnv *, jobject, jint);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: getParityInternal
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_getParityInternal
(JNIEnv *, jobject);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: setFlowControl
* Signature: (I)Z
*/
JNIEXPORT jboolean JNICALL Java_com_rm5248_serial_SerialPort_setFlowControl
(JNIEnv *, jobject, jint);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: getFlowControlInternal
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_getFlowControlInternal
(JNIEnv *, jobject);
 
#ifdef __cplusplus
}
#endif
/JavaSerial/NativeCode/Makefile
62,7 → 62,7
# Main targets
#
linux: SerialPortImpl.o
gcc -o librmserial.so -shared -Wl,-soname,librmserial.so -I$(JNI_INCLUDE_DIR) -I$(JNI_INCLUDE_DIR)/linux -static -lc
gcc -o libjavaserial.so -shared -Wl,-soname,libjavaserial.so -I$(JNI_INCLUDE_DIR) -I$(JNI_INCLUDE_DIR)/linux -static -lc $(OBJFILES)
#
# Dependencies
#
/JavaSerial/src/com/rm5248/serial/SerialPort.java
4,6 → 4,10
import java.io.OutputStream;
 
public class SerialPort {
static{
System.loadLibrary( "javaserial" );
}
 
/**
* Represents the BaudRate that the SerialPort uses.
26,7 → 30,7
B38400,
B115200
}
 
/**
* Represents the number of bits that the serial port uses.
* Typically, this is 8-bit characters.
37,7 → 41,7
DATABITS_7,
DATABITS_8
}
 
/**
* The number of stop bits for data. Typically this is 1.
*/
45,7 → 49,7
STOPBITS_1,
STOPBITS_2
}
 
/**
* The parity bit for the data. Typically None.
*/
54,7 → 58,7
EVEN,
ODD
}
 
/**
* The Flow control scheme for the data, typically None.
*/
87,6 → 91,23
public SerialPort( String portName ) throws NoSuchPortException, NotASerialPortException {
this( portName, BaudRate.B9600 );
}
/**
* Open up a serial port, but allow the user to keep the current settings of the serial port.
*
* @param portName The port to open
* @param keepSettings If true, will simply open the serial port without setting anything. If false, this method
* acts the same as SerialPort( String portName )
* @throws NoSuchPortException If the port does not exist
* @throws NotASerialPortException If the port is not in fact a serial port
*/
public SerialPort( String portName, boolean keepSettings ) throws NoSuchPortException, NotASerialPortException{
if( keepSettings ){
this.handle = openPort( portName );
}else{
this.handle = openPort( portName, 9600, 8, 1, 0, 0 );
}
}
 
/**
* Open the specified port, 8 bit characters, 1 stop bit, no parity, no flow control.
98,7 → 119,7
public SerialPort( String portName, BaudRate rate )throws NoSuchPortException, NotASerialPortException {
this( portName, rate, DataBits.DATABITS_8 );
}
 
/**
* Open the specified port, 1 stop bit, no parity, no flow control.
*
109,15 → 130,15
public SerialPort( String portName, BaudRate rate, DataBits data ) throws NoSuchPortException, NotASerialPortException {
this( portName, rate, data, StopBits.STOPBITS_1 );
}
 
public SerialPort( String portName, BaudRate rate, DataBits data, StopBits stop ) throws NoSuchPortException, NotASerialPortException {
this( portName, rate, data, stop, Parity.NONE );
}
 
public SerialPort( String portName, BaudRate rate, DataBits data, StopBits stop, Parity parity ) throws NoSuchPortException, NotASerialPortException {
this( portName, rate, data, stop, parity, FlowControl.NONE );
}
 
public SerialPort( String portName, BaudRate rate, DataBits data, StopBits stop, Parity parity, FlowControl flow ) throws NoSuchPortException, NotASerialPortException {
int myRate = 0;
int myData = 0;
124,9 → 145,9
int myStop = 0;
int myParity = 0;
int myFlow = 0;
 
closed = false;
 
switch( rate ){
case B0 : myRate = 0; break;
case B50 : myRate = 50; break;
145,7 → 166,7
case B38400 : myRate = 38400; break;
case B115200: myRate = 115200; break;
}
 
switch( data ){
case DATABITS_5: myData = 5; break;
case DATABITS_6: myData = 6; break;
152,24 → 173,24
case DATABITS_7: myData = 7; break;
case DATABITS_8: myData = 8; break;
}
 
switch( stop ){
case STOPBITS_1: myStop = 1; break;
case STOPBITS_2: myStop = 2; break;
}
 
switch( parity ){
case NONE: myParity = 0; break;
case ODD: myParity = 1; break;
case EVEN: myParity = 2; break;
}
 
switch( flow ){
case NONE: myFlow = 0; break;
case HARDWARE: myFlow = 1; break;
case SOFTWARE: myFlow = 2; break;
}
 
handle = openPort(portName, myRate, myData, myStop, myParity, myFlow);
inputStream = new SerialInputStream( handle );
outputStream = new SerialOutputStream( handle );
183,11 → 204,11
*/
public void setBaudRate( BaudRate rate ){
int myRate = 0;
 
if( closed ){
throw new IllegalStateException( "Cannot set the BaudRate once the port has been closed." );
}
 
switch( rate ){
case B0 : myRate = 0; break;
case B50 : myRate = 50; break;
206,7 → 227,7
case B38400 : myRate = 38400; break;
case B115200: myRate = 115200; break;
}
 
setBaudRate( myRate );
}
 
220,7 → 241,7
if( closed ){
throw new IllegalStateException( "Cannot get the input stream once the port has been closed." );
}
 
return inputStream;
}
 
233,7 → 254,7
if( closed ){
throw new IllegalStateException( "Cannot get the output stream once the port has been closed." );
}
 
return outputStream;
}
 
245,7 → 266,7
closed = true;
doClose();
}
 
/**
* See if the port has been closed already.
* @return
257,7 → 278,7
public void finalize(){
close();
}
 
/**
* Set the stop bits of the serial port, after the port has been opened.
*
266,19 → 287,19
*/
public void setStopBits( StopBits stop ){
int myStop = 0;
 
if( closed ){
throw new IllegalStateException( "Cannot set the StopBits once the port has been closed." );
}
 
switch( stop ){
case STOPBITS_1: myStop = 1; break;
case STOPBITS_2: myStop = 2; break;
}
 
setStopBits( myStop );
}
 
/**
* Set the data bits size, after the port has been opened.
*
287,11 → 308,11
*/
public void setDataSize( DataBits data ){
int myData = 0;
 
if( closed ){
throw new IllegalStateException( "Cannot set the DataBits once the port has been closed." );
}
 
switch( data ){
case DATABITS_5: myData = 5; break;
case DATABITS_6: myData = 6; break;
298,10 → 319,10
case DATABITS_7: myData = 7; break;
case DATABITS_8: myData = 8; break;
}
 
setCharSize( myData );
}
 
/**
* Set the parity of the serial port, after the port has been opened.
*
310,20 → 331,20
*/
public void setParity( Parity parity ){
int myParity = 0;
 
if( closed ){
throw new IllegalStateException( "Cannot set the parity once the port has been closed." );
}
 
switch( parity ){
case NONE: myParity = 0; break;
case ODD: myParity = 1; break;
case EVEN: myParity = 2; break;
}
 
setParity( myParity );
}
 
/**
* Get the serial line state for the specified serial port.
*
331,14 → 352,14
*/
public SerialLineState getSerialLineState(){
SerialLineState state = new SerialLineState();
 
if( closed ){
throw new IllegalStateException( "Cannot get the serial line state once the port has been closed." );
}
 
return state;
}
 
/**
* Set the serial line state to the parameters given.
*
345,9 → 366,9
* @param state
*/
public void setSerialLineState( SerialLineState state ){
 
}
 
/**
* Get the baud rate of the serial port.
*
354,9 → 375,31
* @return
*/
public SerialPort.BaudRate getBaudRate(){
return BaudRate.B0;
int baudRate = getBaudRateInternal();
BaudRate toReturn = BaudRate.B0;
 
switch( baudRate ){
case 0 : toReturn = BaudRate.B0 ; break;
case 50 : toReturn = BaudRate.B50 ; break;
case 75 : toReturn = BaudRate.B75 ; break;
case 110 : toReturn = BaudRate.B110 ; break;
case 134 : toReturn = BaudRate.B134 ; break;
case 150 : toReturn = BaudRate.B150 ; break;
case 200 : toReturn = BaudRate.B200 ; break;
case 300 : toReturn = BaudRate.B300 ; break;
case 600 : toReturn = BaudRate.B600 ; break;
case 1200 : toReturn = BaudRate.B1200 ; break;
case 1800 : toReturn = BaudRate.B1800 ; break;
case 2400 : toReturn = BaudRate.B2400 ; break;
case 4800 : toReturn = BaudRate.B4800 ; break;
case 9600 : toReturn = BaudRate.B9600 ; break;
case 38400 : toReturn = BaudRate.B38400 ; break;
case 115200: toReturn = BaudRate.B115200; break;
}
 
return toReturn;
}
 
/**
* Get the number of data bits.
*
363,9 → 406,19
* @return
*/
public SerialPort.DataBits getDataBits(){
return DataBits.DATABITS_8;
int dataBits = getCharSizeInternal();
DataBits bits = DataBits.DATABITS_8;
switch( dataBits ){
case 8: bits = DataBits.DATABITS_8; break;
case 7: bits = DataBits.DATABITS_7; break;
case 6: bits = DataBits.DATABITS_6; break;
case 5: bits = DataBits.DATABITS_5; break;
}
return bits;
}
 
/**
* Get the number of stop bits.
*
372,9 → 425,17
* @return
*/
public SerialPort.StopBits getStopBits(){
return StopBits.STOPBITS_1;
int stopBits = getStopBitsInternal();
StopBits bits = StopBits.STOPBITS_1;
switch( stopBits ){
case 1: bits = StopBits.STOPBITS_1; break;
case 2: bits = StopBits.STOPBITS_2; break;
}
return bits;
}
 
/**
* Get the parity of the serial port.
*
381,9 → 442,18
* @return
*/
public SerialPort.Parity getParity(){
return Parity.NONE;
int parity = getParityInternal();
Parity par = Parity.NONE;
switch( parity ){
case 0: par = Parity.NONE; break;
case 1: par = Parity.ODD; break;
case 2: par = Parity.EVEN; break;
}
return par;
}
 
/**
* Get the flow control for the serial port.
*
390,7 → 460,16
* @return
*/
public SerialPort.FlowControl getFlowControl(){
return FlowControl.NONE;
int flowControl = getFlowControlInternal();
FlowControl cont = FlowControl.NONE;
switch( flowControl ){
case 0: cont = FlowControl.NONE; break;
case 1: cont = FlowControl.HARDWARE; break;
case 2: cont = FlowControl.SOFTWARE; break;
}
return cont;
}
 
/**
399,8 → 478,22
* @param portName
* @return
*/
private native int openPort( String portName, int baudRate, int dataBits, int stopBits, int parity, int flowControl ) throws NoSuchPortException;
private native int openPort( String portName, int baudRate, int dataBits, int stopBits, int parity, int flowControl ) throws NoSuchPortException, NotASerialPortException;
 
/**
* Open the specified port, return an internal handle for the data of this port.
* This method DOES NOT set any of the serial port settings
*
* @param portName The port to open
* @return
*/
private native int openPort( String portName ) throws NoSuchPortException, NotASerialPortException;
/**
* Close this port, release all native resources
*/
private native void doClose();
 
/**
* Set the baud rate in the native code.
*
409,10 → 502,7
*/
private native boolean setBaudRate( int baudRate );
 
/**
* Close this port, release all native resources
*/
private native void doClose();
private native int getBaudRateInternal();
 
/**
* Set the number of stop bits, once the port has been opened.
422,19 → 512,36
*/
private native boolean setStopBits( int stopBits );
 
private native int getStopBitsInternal();
 
/**
* Set the character size, once theport has been opened.
* Set the character size, once the port has been opened.
* This should probably be called 'setDataBits'
*
* @param charSize
* @return
*/
private native boolean setCharSize( int charSize );
 
private native int getCharSizeInternal();
 
/** Set the parity once the port has been opened.
*
* @param parity
* @param parity 0 = None, 1 = Odd, 2 = Even
* @return
*/
private native boolean setParity( int parity );
 
private native int getParityInternal();
 
/** Set the flow control once the port has been opened.
*
*
* @param flowControl 0 = None, 1 = hardware, 2 = software
* @return
*/
private native boolean setFlowControl( int flowControl );
 
private native int getFlowControlInternal();
 
}