Windows native part. Added using of "ERR_INCORRECT_SERIAL_PORT", checking the DCB structure on port opening like in _nix based version and a little refactoring was made

This commit is contained in:
Alexey Sokolov 2013-04-22 10:19:21 +03:00
parent f756417b29
commit 9544ad46cd
1 changed files with 19 additions and 13 deletions

View File

@ -44,8 +44,7 @@ JNIEXPORT jint JNICALL Java_jssc_SerialNativeInterface_openPort(JNIEnv *env, job
strcat(portFullName, port); strcat(portFullName, port);
//<- since 2.1.0 //<- since 2.1.0
HANDLE hComm; HANDLE hComm = CreateFile(portFullName,
hComm = CreateFile(portFullName,
GENERIC_READ | GENERIC_WRITE, GENERIC_READ | GENERIC_WRITE,
0, 0,
0, 0,
@ -54,17 +53,24 @@ JNIEXPORT jint JNICALL Java_jssc_SerialNativeInterface_openPort(JNIEnv *env, job
0); 0);
env->ReleaseStringUTFChars(portName, port); env->ReleaseStringUTFChars(portName, port);
//since 0.9 -> //since 2.3.0 ->
if(hComm == INVALID_HANDLE_VALUE){ if(hComm != INVALID_HANDLE_VALUE){
DCB *dcb = new DCB();
if(!GetCommState(hComm, dcb)){
hComm = (HANDLE)jssc_SerialNativeInterface_ERR_INCORRECT_SERIAL_PORT;//(-4)Incorrect serial port
}
delete dcb;
}
else {
DWORD errorValue = GetLastError(); DWORD errorValue = GetLastError();
if(errorValue == ERROR_ACCESS_DENIED){ if(errorValue == ERROR_ACCESS_DENIED){
hComm = (HANDLE)-1;//Port busy hComm = (HANDLE)jssc_SerialNativeInterface_ERR_PORT_BUSY;//(-1)Port busy
} }
else if(errorValue == ERROR_FILE_NOT_FOUND){ else if(errorValue == ERROR_FILE_NOT_FOUND){
hComm = (HANDLE)-2;//Port not found hComm = (HANDLE)jssc_SerialNativeInterface_ERR_PORT_NOT_FOUND;//(-2)Port not found
} }
} }
//<- since 0.9 //<- since 2.3.0
#if defined(_X86_) #if defined(_X86_)
return (jint)hComm; return (jint)hComm;
#elif defined(__x86_64) #elif defined(__x86_64)