Subversion Repositories Programming Utils

Compare Revisions

Ignore whitespace Rev 67 → Rev 68

/trunk/JavaSerial/NativeCode/SerialPortImpl.c
17,6 → 17,8
printf("bad set comm\n");\
return 0;\
}
#define close(handle) CloseHandle(handle)
#else
#include <termio.h>
#include <termios.h>
421,7 → 423,14
 
//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_FLAG_OVERLAPPED, 0 );
{
//GOD DAMN IT WINDOWS http://support.microsoft.com/kb/115831
char* special_port = malloc( strlen( port_to_open + 5 ) );
memcpy( special_port, "\\\\.\\", 4 );
memcpy( special_port + 4, port_to_open, strlen( port_to_open ) + 1 );
new_port->port = CreateFile( special_port, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0 );
free( special_port );
}
if( new_port->port == INVALID_HANDLE_VALUE ){
if( GetLastError() == ERROR_FILE_NOT_FOUND ){
LPTSTR error_text = NULL;
578,7 → 587,13
 
//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_FLAG_OVERLAPPED, 0 );
{
char* special_port = malloc( strlen( port_to_open + 5 ) );
memcpy( special_port, "\\\\.\\", 4 );
memcpy( special_port + 4, port_to_open, strlen( port_to_open ) + 1 );
new_port->port = CreateFile( special_port, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0 );
free( special_port );
}
if( new_port->port == INVALID_HANDLE_VALUE ){
if( GetLastError() == ERROR_FILE_NOT_FOUND ){
LPTSTR error_text = NULL;
686,9 → 701,9
throw_io_exception_message( env, "Unable to get descriptor" );
return;
}
 
close( desc->port );
free( desc );
free( port_list[ array_pos ] );
port_list[ array_pos ] = NULL;
}
 
1514,6 → 1529,83
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_getMinorNativeVersion
(JNIEnv * env, jclass cls){
return 2;
return 3;
}
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: getSerialPorts
* Signature: ()[Ljava/lang/String;
*/
JNIEXPORT jobjectArray JNICALL Java_com_rm5248_serial_SerialPort_getSerialPorts
(JNIEnv * env, jclass cls){
jclass stringClass;
jobjectArray array;
char** port_names;
int port_names_size;
int x;
jsize size;
port_names_size = 0;
#ifdef _WIN32
port_names = malloc( sizeof( char* ) * 255 ); //max 255 serial ports
{
//Brute force, baby!
char* port_to_open = malloc( 11 );
HANDLE* port;
for( x = 0; x <= 255; x++ ){
_snprintf_s( port_to_open, 11, 11, "\\\\.\\COM%d", x );
port = CreateFile( port_to_open, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0 );
if( port != INVALID_HANDLE_VALUE ||
( port == INVALID_HANDLE_VALUE &&
GetLastError() != ERROR_FILE_NOT_FOUND ) ){
//This is a valid port
//we could get INVALID_HANDLE_VALUE if the port is already open,
//so make sure we check to see if it is not that value
port_names[ port_names_size ] = malloc( 6 );
memcpy( port_names[ port_names_size ], port_to_open + 4, 6 );
port_names_size++;
}
CloseHandle( port );
}
free( port_to_open );
}
#else
{
struct dirent *entry;
DIR* dir;
int fd;
 
dir = opendir( "/dev" );
while ( entry = readdir( dir ), entry != NULL) {
fd = open( entry->d_name, O_RDONLY );
if( fd < 0 ){
fprintf( stderr, "ERROR: This is a very bad thing that should never happen %s:%s\n", __FILE__, __LINE__ );
continue;
}
if( isatty( fd ) ){
port_names[ port_names_size ] = malloc( strlen( entry->d_name ) + 1 );
memcpy( port_names[ port_names_size ], entry->d_name, strlen( entry->d_name ) + 1 );
port_names_size++;
}
close( fd );
}
closedir( dir );
}
#endif /* _WIN32 */
 
stringClass = (*env)->FindClass(env, "java/lang/String");
array = (*env)->NewObjectArray(env, port_names_size, stringClass, 0);
for( x = 0; x < port_names_size; x++ ){
(*env)->SetObjectArrayElement(env, array, x, (*env)->NewStringUTF( env, port_names[ x ] ) );
free( port_names[ x ] );
}
free( port_names );
return array;
}
 
/trunk/JavaSerial/NativeCode/com_rm5248_serial_SerialPort.h
1,149 → 1,157
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_rm5248_serial_SerialPort */
 
#ifndef _Included_com_rm5248_serial_SerialPort
#define _Included_com_rm5248_serial_SerialPort
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_rm5248_serial_SerialPort
* Method: openPort
* Signature: (Ljava/lang/String;IIIII)I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_openPort__Ljava_lang_String_2IIIII
(JNIEnv *, jobject, jstring, jint, jint, jint, jint, jint);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: openPort
* Signature: (Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_openPort__Ljava_lang_String_2
(JNIEnv *, jobject, jstring);
 
/*
* 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
*/
JNIEXPORT jboolean JNICALL Java_com_rm5248_serial_SerialPort_setBaudRate
(JNIEnv *, jobject, jint);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: getBaudRateInternal
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_getBaudRateInternal
(JNIEnv *, jobject);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: setStopBits
* Signature: (I)Z
*/
JNIEXPORT jboolean JNICALL Java_com_rm5248_serial_SerialPort_setStopBits
(JNIEnv *, jobject, jint);
 
/*
* 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
*/
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);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: getSerialLineStateInternalNonblocking
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_getSerialLineStateInternalNonblocking
(JNIEnv *, jobject);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: setSerialLineStateInternal
* Signature: (Lcom/rm5248/serial/SerialLineState;)I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_setSerialLineStateInternal
(JNIEnv *, jobject, jobject);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: getMajorNativeVersion
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_getMajorNativeVersion
(JNIEnv *, jclass);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: getMinorNativeVersion
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_getMinorNativeVersion
(JNIEnv *, jclass);
 
#ifdef __cplusplus
}
#endif
#endif
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_rm5248_serial_SerialPort */
 
#ifndef _Included_com_rm5248_serial_SerialPort
#define _Included_com_rm5248_serial_SerialPort
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_rm5248_serial_SerialPort
* Method: openPort
* Signature: (Ljava/lang/String;IIIII)I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_openPort__Ljava_lang_String_2IIIII
(JNIEnv *, jobject, jstring, jint, jint, jint, jint, jint);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: openPort
* Signature: (Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_openPort__Ljava_lang_String_2
(JNIEnv *, jobject, jstring);
 
/*
* 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
*/
JNIEXPORT jboolean JNICALL Java_com_rm5248_serial_SerialPort_setBaudRate
(JNIEnv *, jobject, jint);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: getBaudRateInternal
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_getBaudRateInternal
(JNIEnv *, jobject);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: setStopBits
* Signature: (I)Z
*/
JNIEXPORT jboolean JNICALL Java_com_rm5248_serial_SerialPort_setStopBits
(JNIEnv *, jobject, jint);
 
/*
* 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
*/
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);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: getSerialLineStateInternalNonblocking
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_getSerialLineStateInternalNonblocking
(JNIEnv *, jobject);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: setSerialLineStateInternal
* Signature: (Lcom/rm5248/serial/SerialLineState;)I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_setSerialLineStateInternal
(JNIEnv *, jobject, jobject);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: getMajorNativeVersion
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_getMajorNativeVersion
(JNIEnv *, jclass);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: getMinorNativeVersion
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_getMinorNativeVersion
(JNIEnv *, jclass);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: getSerialPorts
* Signature: ()[Ljava/lang/String;
*/
JNIEXPORT jobjectArray JNICALL Java_com_rm5248_serial_SerialPort_getSerialPorts
(JNIEnv *, jclass);
 
#ifdef __cplusplus
}
#endif
#endif
/trunk/JavaSerial/src/com/rm5248/serial/SerialPort.java
11,11 → 11,11
* When creating a SerialPort object, you give the SerialPort the name of the port that you
* wish to open.
*
* When opening a SerialPort and setting values, an IllegalArgumentException may be thrown
* When opening a SerialPort and setting values, an {@link IllegalArgumentException} may be thrown
* if any of the values are NULL.
*
* When getting and settings the properties of the SerialPort using one of the getXXX()
* or setXXX() methods, an IllegalStateException will be thrown if the port has been closed.
* When getting and settings the properties of the SerialPort using one of the {@code getXXX() }
* or {@code setXXX() } methods, an IllegalStateException will be thrown if the port has been closed.
*
* @author rm5248
*
93,7 → 93,7
 
private class SerialStateListener implements Runnable{
private boolean stop;
private volatile boolean stop;
private SerialChangeListener listen;
SerialStateListener( SerialChangeListener listen ){
107,6 → 107,9
synchronized( serialListenSync ){
try {
serialListenSync.wait();
if( stop ){
break;
}
listen.serialStateChanged( state );
} catch (Exception e) {}
}
158,7 → 161,7
*
* @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 )
* acts the same as {@link #SerialPort(String) SerialPort( String portName ) }
* @throws NoSuchPortException If the port does not exist
* @throws NotASerialPortException If the port is not in fact a serial port
*/
454,6 → 457,10
if( closed ) return;
closed = true;
doClose();
serialListen.doStop();
synchronized( serialListenSync ){
serialListenSync.notify();
}
}
 
/**
904,12 → 911,12
* 0.2, this returns 2.
*/
public static int getMinorVersion(){
return 2;
return 3;
}
/**
* Get the major version of the native code. This should match up with
* {@link getMajorVersion() getMajorVersion()}, although this is not
* {@link #getMajorVersion() getMajorVersion()}, although this is not
* guaranteed. For example, if this is version 0.2, this returns 0
*/
public static native int getMajorNativeVersion();
916,9 → 923,22
/**
* Get the minor version of the native code. This should match up with
* {@link getMinorVersion() getMinorVersion()}, although this is not
* {@link #getMinorVersion() getMinorVersion()}, although this is not
* guaranteed. For example, if this is version 0.2, this returns 2.
*/
public static native int getMinorNativeVersion();
/**
* Get an array of all the serial ports on the system. For example, on
* Windows this will return {@code { "COM1", "COM3", .... } } depending on how
* many serial devices you have plugged in. On Linux, this returns
* {@code { "/dev/ttyS0", "/dev/ttyUSB0", "/dev/symlink", ... } }
* It will not resolve symlinks, such that if there is a symlink
* from {@code /dev/symlink } to {@code /dev/ttyUSB0 }, they will both show up.
*
*
* @return
*/
public static native String[] getSerialPorts();
 
}