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; |
688,7 → 703,7 |
} |
|
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; |
} |
|