From cf71459044e5996b399a50a106035c08719bba47 Mon Sep 17 00:00:00 2001 From: Edwin vd Oetelaar PA2LVD Date: Sun, 19 Jun 2016 20:46:03 +0200 Subject: [PATCH] fix for Linux. Baud rate handling fixed. Listing of serial Ports is now implemented. --- development/dll/include/gurux.serial.h | 9 +- development/dll/src/gurux.serial.cpp | 864 +++++++++++++------------ 2 files changed, 460 insertions(+), 413 deletions(-) diff --git a/development/dll/include/gurux.serial.h b/development/dll/include/gurux.serial.h index 0dc62ce..e954da8 100644 --- a/development/dll/include/gurux.serial.h +++ b/development/dll/include/gurux.serial.h @@ -43,7 +43,8 @@ using namespace std; #include //Linux headers. #else -#include // string function definitions +#include // string function definitions +#include #include // UNIX standard function definitions #include // File control definitions #include // Error number definitions @@ -80,16 +81,16 @@ int GXSetCommState(HANDLE hWnd, LPDCB DCB); static void ReportError(JNIEnv* env, DWORD err); #else //LINUX - +typedef uint32_t DWORD; static basic_string GetDriver(const basic_string& tty); static void GetComPort(const string& dir, vector >& ports); void GetLinuxSerialPorts(JNIEnv* env, std::vector >& ports); - +//static void ReportError(JNIEnv* env, DWORD err); #endif void ReportError(JNIEnv* env, const char* pError) { - env->FatalError(pError); + env->FatalError(pError); } extern "C" diff --git a/development/dll/src/gurux.serial.cpp b/development/dll/src/gurux.serial.cpp index 49ff12a..8c4dece 100644 --- a/development/dll/src/gurux.serial.cpp +++ b/development/dll/src/gurux.serial.cpp @@ -33,83 +33,85 @@ //--------------------------------------------------------------------------- #include +#include +#include +#include #include "../include/gurux.serial.h" #if defined(_WIN32) || defined(_WIN64) LONG EnumerateSerialPorts(char* deviceName, DWORD maxLen, DWORD index, bool bShowAll) { - HKEY hKey; - char ClassName[MAX_PATH] = ""; // Buffer for class name. - DWORD dwcClassLen = MAX_PATH; // Length of class string. - DWORD dwcSubKeys; // Number of sub keys. - DWORD dwcMaxSubKey; // Longest sub key size. - DWORD dwcMaxClass; // Longest class string. - DWORD dwcValues; // Number of values for this key. - char valueName[MAX_PATH] ; - DWORD dwcValueName = MAX_PATH; - DWORD dwcMaxValueName; // Longest Value name. - DWORD dwcMaxValueData; // Longest Value data. - DWORD dwcSecDesc; // Security descriptor. - FILETIME ftLastWriteTime; // Last write time. - DWORD dwType; - DWORD retValue; - DWORD cbData; + HKEY hKey; + char ClassName[MAX_PATH] = ""; // Buffer for class name. + DWORD dwcClassLen = MAX_PATH;// Length of class string. + DWORD dwcSubKeys;// Number of sub keys. + DWORD dwcMaxSubKey;// Longest sub key size. + DWORD dwcMaxClass;// Longest class string. + DWORD dwcValues;// Number of values for this key. + char valueName[MAX_PATH]; + DWORD dwcValueName = MAX_PATH; + DWORD dwcMaxValueName;// Longest Value name. + DWORD dwcMaxValueData;// Longest Value data. + DWORD dwcSecDesc;// Security descriptor. + FILETIME ftLastWriteTime;// Last write time. + DWORD dwType; + DWORD retValue; + DWORD cbData; + // Use RegOpenKeyEx() with the new Registry path to get an open handle + // to the child key you want to enumerate. + DWORD retCode = RegOpenKeyEx (HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM", + 0, KEY_ENUMERATE_SUB_KEYS | KEY_EXECUTE | KEY_QUERY_VALUE, &hKey); - // Use RegOpenKeyEx() with the new Registry path to get an open handle - // to the child key you want to enumerate. - DWORD retCode = RegOpenKeyEx (HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM", - 0, KEY_ENUMERATE_SUB_KEYS | KEY_EXECUTE | KEY_QUERY_VALUE, &hKey); - - //If Registry read failed - if (retCode != ERROR_SUCCESS) - { - return -1; - } + //If Registry read failed + if (retCode != ERROR_SUCCESS) + { + return -1; + } - // Get Class name, Value count. - RegQueryInfoKey ( hKey, // Key handle. - ClassName, // Buffer for class name. - &dwcClassLen, // Length of class string. - NULL, // Reserved. - &dwcSubKeys, // Number of sub keys. - &dwcMaxSubKey, // Longest sub key size. - &dwcMaxClass, // Longest class string. - &dwcValues, // Number of values for this key. - &dwcMaxValueName, // Longest Value name. - &dwcMaxValueData, // Longest Value data. - &dwcSecDesc, // Security descriptor. - &ftLastWriteTime); // Last write time. + // Get Class name, Value count. + RegQueryInfoKey ( hKey,// Key handle. + ClassName,// Buffer for class name. + &dwcClassLen,// Length of class string. + NULL,// Reserved. + &dwcSubKeys,// Number of sub keys. + &dwcMaxSubKey,// Longest sub key size. + &dwcMaxClass,// Longest class string. + &dwcValues,// Number of values for this key. + &dwcMaxValueName,// Longest Value name. + &dwcMaxValueData,// Longest Value data. + &dwcSecDesc,// Security descriptor. + &ftLastWriteTime);// Last write time. - // Enumerate the Key Values - cbData = maxLen; - dwcValueName = MAX_PATH; - valueName[0] = '\0'; + // Enumerate the Key Values + cbData = maxLen; + dwcValueName = MAX_PATH; + valueName[0] = '\0'; - retValue = RegEnumValue(hKey, index, valueName, - &dwcValueName, NULL, &dwType, - (BYTE*) deviceName, &cbData); - RegCloseKey (hKey); // Close the key handle. - if(dwType != REG_SZ || retValue != (DWORD)ERROR_SUCCESS) + retValue = RegEnumValue(hKey, index, valueName, + &dwcValueName, NULL, &dwType, + (BYTE*) deviceName, &cbData); + RegCloseKey (hKey);// Close the key handle. + if(dwType != REG_SZ || retValue != (DWORD)ERROR_SUCCESS) { - return -2; + return -2; } if(!bShowAll) { /* - CComBSTR tmp = L"\\\\.\\"; - tmp += deviceName; - //Try to open port. Ignore port if it's open failed. - HANDLE hComPort = CreateFileW(tmp, - GENERIC_READ | GENERIC_WRITE, 0, //Exclusive access, - NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - //Return 1 if port is already opened. - if (hComPort == INVALID_HANDLE_VALUE) - { - return 1; - } - CloseHandle(hComPort); - */ + CComBSTR tmp = L"\\\\.\\"; + tmp += deviceName; + //Try to open port. Ignore port if it's open failed. + HANDLE hComPort = CreateFileW(tmp, + GENERIC_READ | GENERIC_WRITE, 0, //Exclusive access, + NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + //Return 1 if port is already opened. + if (hComPort == INVALID_HANDLE_VALUE) + { + return 1; + } + CloseHandle(hComPort); + */ } return 0; } @@ -179,12 +181,12 @@ void ReportError(JNIEnv* env, DWORD err) { char buff[MAX_PATH]; ::FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, - NULL, - err, - MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), - buff, - MAX_PATH - 1, - NULL); + NULL, + err, + MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), + buff, + MAX_PATH - 1, + NULL); jclass exClass = env->FindClass("java/lang/Exception"); if (exClass == NULL) { @@ -193,78 +195,68 @@ void ReportError(JNIEnv* env, DWORD err) else { env->ThrowNew(exClass, buff); - } + } } #else -/* -#include -#include -#include -*/ -#include #include -#include #include #include +#include +#include +#include #include -#include #include #include -#include #include #include -#include -#include #include - -#include #include #define _POSIX_SOURCE 1 /* POSIX compliant source */ - basic_string GetDriver(const basic_string& tty) { - struct stat st; - string devicedir = tty; + struct stat st; + string devicedir = tty; - // Append '/device' to the tty-path - devicedir += "/device"; + // Append '/device' to the tty-path + devicedir += "/device"; - // Stat the device dir and handle it if it is a symlink - if (lstat(devicedir.c_str(), &st)==0 && S_ISLNK(st.st_mode)) - { - char buffer[256]; - memset(buffer, 0, sizeof(buffer)); - // Append '/driver' and return basename of the target - devicedir += "/driver"; - if (readlink(devicedir.c_str(), buffer, sizeof(buffer)) > 0) - { - return basename(buffer); - } - } - return ""; + // Stat the device dir and handle it if it is a symlink + if (lstat(devicedir.c_str(), &st) == 0 && S_ISLNK(st.st_mode)) + { + char buffer[256]; + memset(buffer, 0, sizeof(buffer)); + // Append '/driver' and return basename of the target + devicedir += "/driver"; + if (readlink(devicedir.c_str(), buffer, sizeof(buffer)) > 0) + { + return basename(buffer); + } + } + return ""; } void GetComPort(const string& dir, vector >& ports) { - // Get the driver the device is using - string driver = GetDriver(dir); - struct serial_struct serinfo; - // Skip devices without a driver - if (driver.size() > 0) - { - string devfile = string("/dev/") + basename(dir.c_str()); - if (driver == "serial") - { - // Try to open the device + // Get the driver the device is using + string driver = GetDriver(dir); + struct serial_struct serinfo; + // Skip devices without a driver + if (driver.size() > 0) + { + string devfile = string("/dev/") + basename(dir.c_str()); + // any driver will do eg. serial8250 pl2303 cdc_acm ftdi_sio, not just "serial" + if (driver == "serial" || true) + { + // Try to open the device int fd = open(devfile.c_str(), O_RDWR | O_NONBLOCK | O_NOCTTY); if (fd >= 0) { // Get serial_info - if (ioctl(fd, TIOCGSERIAL, &serinfo)==0) + if (ioctl(fd, TIOCGSERIAL, &serinfo) == 0) { // If device type is no PORT_UNKNOWN we accept the port if (serinfo.type != PORT_UNKNOWN) @@ -274,11 +266,12 @@ void GetComPort(const string& dir, vector >& ports) } close(fd); } - } - } + } + } } -void GetLinuxSerialPorts(JNIEnv* env, std::vector >& ports) +void GetLinuxSerialPorts(JNIEnv* env, + std::vector >& ports) { int pos; struct dirent **namelist; @@ -307,41 +300,46 @@ void GetLinuxSerialPorts(JNIEnv* env, std::vector >& por } #endif -JNIEXPORT jobjectArray JNICALL Java_gurux_io_NativeCode_getPortNames(JNIEnv* env, jclass clazz) +JNIEXPORT jobjectArray JNICALL +Java_gurux_io_NativeCode_getPortNames (JNIEnv* env, jclass clazz) { std::vector > portItems; #if defined(_WIN32) || defined(_WIN64) char portname[MAX_PATH]; DWORD count = 0; - long retCode = 0; + long retCode = 0; while ((retCode = EnumerateSerialPorts(portname, MAX_PATH, count++, FALSE)) > -1) - { + { std::basic_string str; str.append(portname); portItems.push_back(str); - } + } #else //If Linux -// GetLinuxSerialPorts(env, portItems); + GetLinuxSerialPorts (env, portItems); #endif - jclass stringClass = env->FindClass("java/lang/String"); - jobjectArray ports = env->NewObjectArray((jsize) portItems.size(), stringClass, 0); - jsize pos = -1; - for(std::vector >::iterator it = portItems.begin(); it != portItems.end(); ++it) - { - jobject item = env->NewStringUTF(it->c_str()); - env->SetObjectArrayElement(ports, ++pos, item); - } + jclass stringClass = env->FindClass ("java/lang/String"); + jobjectArray ports = env->NewObjectArray ((jsize) portItems.size (), + stringClass, 0); + jsize pos = -1; + for (std::vector >::iterator it = portItems.begin (); + it != portItems.end (); ++it) + { + jobject item = env->NewStringUTF (it->c_str ()); + env->SetObjectArrayElement (ports, ++pos, item); + } return ports; } -void signal_handler_IO (int status) +void signal_handler_IO(int status) { -printf("received SIGIO signal.\n"); + printf("received SIGIO signal.\n"); // wait_flag = FALSE; } -JNIEXPORT jlong JNICALL Java_gurux_io_NativeCode_openSerialPort(JNIEnv* env, jclass clazz, jstring port, jlongArray closing) +JNIEXPORT jlong JNICALL +Java_gurux_io_NativeCode_openSerialPort (JNIEnv* env, jclass clazz, + jstring port, jlongArray closing) { jboolean isCopy; #if defined(_WIN32) || defined(_WIN64) @@ -351,71 +349,72 @@ JNIEXPORT jlong JNICALL Java_gurux_io_NativeCode_openSerialPort(JNIEnv* env, jcl buff.append(pPort); env->ReleaseStringUTFChars(port, pPort); //Open serial port for read / write. Port can't share. - HANDLE hComPort = CreateFileA(buff.c_str(), - GENERIC_READ | GENERIC_WRITE, 0, NULL, - OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); + HANDLE hComPort = CreateFileA(buff.c_str(), + GENERIC_READ | GENERIC_WRITE, 0, NULL, + OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); if (hComPort == INVALID_HANDLE_VALUE) { - ReportError(env, GetLastError()); - } - DCB dcb = {0}; + ReportError(env, GetLastError()); + } + DCB dcb = + { 0}; dcb.DCBlength = sizeof(DCB); dcb.BaudRate = 9600; dcb.fBinary = 1; dcb.fDtrControl = DTR_CONTROL_ENABLE; dcb.fRtsControl = DTR_CONTROL_ENABLE; dcb.fOutX = dcb.fInX = 0; - //CTS handshaking on output. + //CTS handshaking on output. dcb.fOutxCtsFlow = DTR_CONTROL_DISABLE; - //DSR handshaking on output - dcb.fOutxDsrFlow = DTR_CONTROL_DISABLE; + //DSR handshaking on output + dcb.fOutxDsrFlow = DTR_CONTROL_DISABLE; //Abort all reads and writes on Error. dcb.fAbortOnError = 1; - dcb.ByteSize = 8; - //Parity = None. + dcb.ByteSize = 8; + //Parity = None. dcb.Parity = 0; //StopBits = 1; dcb.StopBits = 0; - int ret; - if ((ret = GXSetCommState(hComPort, &dcb)) != 0) + int ret; + if ((ret = GXSetCommState(hComPort, &dcb)) != 0) { ReportError(env, ret); } jlong hClosing = (jlong) ::CreateEvent(NULL, TRUE, FALSE, NULL); - env->SetLongArrayRegion(closing, 0, 1, &hClosing); + env->SetLongArrayRegion(closing, 0, 1, &hClosing); return (long) hComPort; #else //#if defined(__LINUX__) - const char* pPort = env->GetStringUTFChars(port, &isCopy); - std::string buff(pPort); - env->ReleaseStringUTFChars(port, pPort); + const char* pPort = env->GetStringUTFChars (port, &isCopy); + std::string buff (pPort); + env->ReleaseStringUTFChars (port, pPort); // file description for the serial port - int hComPort; + int hComPort; // read/write | not controlling term | don't wait for DCD line signal. - hComPort = open(buff.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK); - if(hComPort == -1) // if open is unsuccessful. + hComPort = open (buff.c_str (), O_RDWR | O_NOCTTY | O_NONBLOCK); + if (hComPort == -1)// if open is unsuccessful. { - buff.insert(0, "Failed to Open port: "); - ReportError(env, buff.c_str()); + buff.insert (0, "Failed to Open port: "); + ReportError (env, buff.c_str ()); } else { - if(!isatty(hComPort)) + if (!isatty (hComPort)) { - ReportError(env, "Failed to Open port. This is not a serial port."); + ReportError (env, "Failed to Open port. This is not a serial port."); } - if ((ioctl(hComPort, TIOCEXCL) == -1)) + if ((ioctl (hComPort, TIOCEXCL) == -1)) { - ReportError(env, "Failed to Open port. Exclusive access denied."); + ReportError (env, "Failed to Open port. Exclusive access denied."); } struct termios options; - memset(&options,0,sizeof(options)); - options.c_iflag=0; - options.c_oflag=0; - options.c_cflag=CS8|CREAD|CLOCAL; // 8n1, see termios.h for more information - options.c_lflag=0; + memset (&options, 0, sizeof(options)); + options.c_iflag = 0; + options.c_oflag = 0; + options.c_cflag = CS8 | CREAD | CLOCAL; // 8n1, see termios.h for more information + options.c_lflag = 0; options.c_cc[VMIN] = 1; options.c_cc[VTIME] = 5; //Set Baud Rates @@ -424,9 +423,9 @@ JNIEXPORT jlong JNICALL Java_gurux_io_NativeCode_openSerialPort(JNIEnv* env, jcl //hardware flow control is used as default. //options.c_cflag |= CRTSCTS; - if (tcsetattr(hComPort, TCSAFLUSH, &options) != 0) + if (tcsetattr (hComPort, TCSAFLUSH, &options) != 0) { - ReportError(env, "Failed to Open port. tcsetattr failed."); + ReportError (env, "Failed to Open port. tcsetattr failed."); //errno } } @@ -435,26 +434,28 @@ JNIEXPORT jlong JNICALL Java_gurux_io_NativeCode_openSerialPort(JNIEnv* env, jcl } JNIEXPORT void JNICALL Java_gurux_io_NativeCode_closeSerialPort(JNIEnv* env, jclass clazz, jlong hComPort, jlong closing) -{ +{ if (hComPort != 0) { #if defined(_WIN32) || defined(_WIN64) - ::SetEvent((HANDLE) closing); - if (!CloseHandle((HANDLE) hComPort)) - { - ReportError(env, GetLastError()); - } + ::SetEvent((HANDLE) closing); + if (!CloseHandle((HANDLE) hComPort)) + { + ReportError(env, GetLastError()); + } #else - int ret = close(hComPort); - if (ret < 0) - { - ReportError(env, "Failed to close port."); - } + int ret = close(hComPort); + if (ret < 0) + { + ReportError(env, "Failed to close port."); + } #endif } } -JNIEXPORT jbyteArray JNICALL Java_gurux_io_NativeCode_read(JNIEnv* env, jclass clazz, jlong hComPort, jint readTimeout, jlong closing) +JNIEXPORT jbyteArray JNICALL +Java_gurux_io_NativeCode_read (JNIEnv* env, jclass clazz, jlong hComPort, + jint readTimeout, jlong closing) { int readBufferSize = 1; #if defined(_WIN32) || defined(_WIN64) @@ -468,9 +469,9 @@ JNIEXPORT jbyteArray JNICALL Java_gurux_io_NativeCode_read(JNIEnv* env, jclass c { DWORD err = GetLastError(); ReportError(env, err); - } + } //Try to read at least one byte. - if (comstat.cbInQue > 0) + if (comstat.cbInQue > 0) { readBufferSize = comstat.cbInQue; } @@ -483,14 +484,14 @@ JNIEXPORT jbyteArray JNICALL Java_gurux_io_NativeCode_read(JNIEnv* env, jclass c if (!ReadFile((HANDLE) hComPort, pBuff, readBufferSize, &NumberOfBytesRead, &osRead)) { DWORD err = GetLastError(); - if (err != ERROR_IO_PENDING) + if (err != ERROR_IO_PENDING) { delete pBuff; - if (err == ERROR_INVALID_HANDLE) + if (err == ERROR_INVALID_HANDLE) { CloseHandle((HANDLE) closing); return env->NewByteArray(0); - } + } ReportError(env, err); } HANDLE h[2]; @@ -529,7 +530,7 @@ JNIEXPORT jbyteArray JNICALL Java_gurux_io_NativeCode_read(JNIEnv* env, jclass c { return env->NewByteArray(0); } - ReportError(env, err); + ReportError(env, err); } } CloseHandle(osRead.hEvent); @@ -539,10 +540,10 @@ JNIEXPORT jbyteArray JNICALL Java_gurux_io_NativeCode_read(JNIEnv* env, jclass c return data; #else //Get bytes available. - int ret = ioctl(hComPort, FIONREAD, &readBufferSize); + int ret = ioctl (hComPort, FIONREAD, &readBufferSize); if (ret < 0) { - ReportError(env, "getBytesToRead failed."); + ReportError (env, "getBytesToRead failed."); } //Try to read at least one byte. if (readBufferSize == 0) @@ -550,15 +551,15 @@ JNIEXPORT jbyteArray JNICALL Java_gurux_io_NativeCode_read(JNIEnv* env, jclass c readBufferSize = 1; } unsigned char* pBuff = new unsigned char[readBufferSize]; - do - { - ret = read(hComPort, pBuff, readBufferSize); + do + { + ret = read (hComPort, pBuff, readBufferSize); if (ret == -1) { if (errno == EAGAIN) { ret = 0; - usleep(100000); + usleep (100000); } //Return empty list if connection is closed. else if (errno == EBADF) @@ -570,14 +571,14 @@ JNIEXPORT jbyteArray JNICALL Java_gurux_io_NativeCode_read(JNIEnv* env, jclass c { delete pBuff; char buff[50]; - sprintf(buff, "Read failed %d", errno); - ReportError(env, buff); + sprintf (buff, "Read failed %d", errno); + ReportError (env, buff); } } - } - while(ret == 0); - jbyteArray data = env->NewByteArray(ret); - env->SetByteArrayRegion(data, 0, ret, (jbyte*) pBuff); + } + while (ret == 0); + jbyteArray data = env->NewByteArray (ret); + env->SetByteArrayRegion (data, 0, ret, (jbyte*) pBuff); delete pBuff; return data; #endif @@ -594,7 +595,7 @@ JNIEXPORT void JNICALL Java_gurux_io_NativeCode_write(JNIEnv* env, jclass clazz, writeTimeout = INFINITE; } DWORD NumberOfBytesWrite = 0; - BYTE* pData = new BYTE[len]; + BYTE* pData = new BYTE[len]; env->GetByteArrayRegion(data, 0, len, (jbyte*) pData); OVERLAPPED osWrite; ZeroMemory(&osWrite, sizeof(osWrite)); @@ -602,7 +603,7 @@ JNIEXPORT void JNICALL Java_gurux_io_NativeCode_write(JNIEnv* env, jclass clazz, if(!::WriteFile((HANDLE) hComPort, pData, len, &NumberOfBytesWrite, &osWrite)) { DWORD err = GetLastError(); - if (err != ERROR_IO_PENDING) + if (err != ERROR_IO_PENDING) { delete pData; ReportError(env, err); @@ -627,70 +628,81 @@ JNIEXPORT void JNICALL Java_gurux_io_NativeCode_write(JNIEnv* env, jclass clazz, } } #else - unsigned char* pData = new unsigned char[len]; - env->GetByteArrayRegion(data, 0, len, (jbyte*) pData); - int ret = write(hComPort, pData, len); - delete pData; - if (ret != len) - { - ReportError(env, "Write failed."); - } + unsigned char* pData = new unsigned char[len]; + env->GetByteArrayRegion(data, 0, len, (jbyte*) pData); + int ret = write(hComPort, pData, len); + delete pData; + if (ret != len) + { + ReportError(env, "Write failed."); + } #endif } -JNIEXPORT jint JNICALL Java_gurux_io_NativeCode_getBaudRate(JNIEnv* env, jclass clazz, jlong hComPort) +JNIEXPORT jint JNICALL +Java_gurux_io_NativeCode_getBaudRate (JNIEnv* env, jclass clazz, jlong hComPort) { #if defined(_WIN32) || defined(_WIN64) int ret; DCB dcb; - if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) + if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) { ReportError(env, ret); } return dcb.BaudRate; #else - struct termios options; - // Get the current options for the serial port. - int ret = tcgetattr(hComPort, &options); + struct termios options; + // Get the current options for the serial port. + int ret = tcgetattr (hComPort, &options); if (ret < 0) { - ReportError(env, "Failed to get settings for serial port."); + ReportError (env, "Failed to get settings for serial port."); } // Get input baud rate. - ret = cfgetispeed(&options); - switch(ret) - { - case 50: - return 50; - case 75: - return 75; - case 110: - return 110; - case 134: - return 134; - case 150: - return 150; - case 200: - return 200; - case 600: - return 600; - case 1200: - return 1200; - case 1800: - return 1800; - case 2400: - return 2400; - case 4800: - return 4800; - case 9600: - return 9600; - case 19200: - return 19200; - case 38400: - return 38400; + ret = cfgetispeed (&options); + + switch (ret) + { + case B50: + return 50; + case B75: + return 75; + case B110: + return 110; + case B134: + return 134; + case B150: + return 150; + case B200: + return 200; + case B300: + return 300; + case B600: + return 600; + case B1200: + return 1200; + case B1800: + return 1800; + case B2400: + return 2400; + case B4800: + return 4800; + case B9600: + return 9600; + case B19200: + return 19200; + case B38400: + return 38400; + case B57600: + return 57600; + case B115200: + return 115200; + case B230400: + return 230400; + } - ReportError(env, "Invalid baud rate."); + ReportError (env, "Invalid baud rate."); return -1; #endif } @@ -700,103 +712,120 @@ JNIEXPORT void JNICALL Java_gurux_io_NativeCode_setBaudRate(JNIEnv* env, jclass #if defined(_WIN32) || defined(_WIN64) int ret; DCB dcb; - if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) + if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) { ReportError(env, ret); } dcb.BaudRate = value; - if ((ret = GXSetCommState((HANDLE) hComPort, &dcb)) != 0) + if ((ret = GXSetCommState((HANDLE) hComPort, &dcb)) != 0) { ReportError(env, ret); } #else - struct termios options; - // Get the current options for the serial port. - int ret = tcgetattr(hComPort, &options); + struct termios options; + speed_t baudrate = B9600; // sane default init + // Get the current options for the serial port. + int ret = tcgetattr(hComPort, &options); if (ret < 0) { - ReportError(env, "Failed to get settings for serial port."); + ReportError(env, "Failed to get settings (tcgetattr) for serial port."); } switch(value) { case 50: - value = 50; + baudrate = B50; break; case 75: - value = 75; + baudrate = B75; break; case 110: - value = 110; + baudrate = B110; break; case 134: - value = 134; + baudrate = B134; break; case 150: - value = 150; + baudrate = B150; break; case 200: - value = 200; + baudrate = B200; break; case 300: - value = 300; + baudrate = B300; break; case 600: - value = 600; + baudrate = B600; break; case 1200: - value = 1200; + baudrate = B1200; break; case 1800: - value = 1800; + baudrate = B1800; break; case 2400: - value = 2400; + baudrate = B2400; break; case 4800: - value = 4800; + baudrate = B4800; break; case 9600: - value = 9600; + baudrate = B9600; break; case 19200: - value = 19200; + baudrate = B19200; break; case 38400: - value = 38400; + baudrate = B38400; + break; + case 57600: + baudrate = B57600; + break; + case 115200: + baudrate = B115200; + break; + case 230400: + baudrate = B230400; break; default: - ReportError(env, "Invalid value."); + ReportError(env, "Invalid value."); } - // Set baud rates. - cfsetispeed(&options, value); - cfsetospeed(&options, value); + // Set input baud rate + ret = cfsetispeed(&options, baudrate); + if (ret < 0) + ReportError(env, "Failed to baud rate (cfsetispeed)"); + + // set output baud rate + ret = cfsetospeed(&options, baudrate); + if (ret < 0) + ReportError(env, "Failed to baud rate (cfsetospeed)"); + // Apply the settings to the serial port. ret = tcsetattr(hComPort, TCSANOW, &options); if (ret < 0) - { - ReportError(env, "Failed to set settings for serial port."); - } + ReportError(env, "Failed to apply settings for serial port. (tcsetattr)"); + #endif } -JNIEXPORT jint JNICALL Java_gurux_io_NativeCode_getDataBits(JNIEnv* env, jclass clazz, jlong hComPort) +JNIEXPORT jint JNICALL +Java_gurux_io_NativeCode_getDataBits (JNIEnv* env, jclass clazz, jlong hComPort) { #if defined(_WIN32) || defined(_WIN64) int ret; DCB dcb; - if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) + if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) { ReportError(env, ret); } return dcb.ByteSize; #else - struct termios options; - // Get the current options for the serial port. - int ret = tcgetattr(hComPort, &options); + struct termios options; + // Get the current options for the serial port. + int ret = tcgetattr (hComPort, &options); if (ret < 0) { - ReportError(env, "getDataBits failed."); + ReportError (env, "getDataBits failed."); } // 8-bit chars if ((options.c_cflag & CS8) != 0) @@ -818,7 +847,7 @@ JNIEXPORT jint JNICALL Java_gurux_io_NativeCode_getDataBits(JNIEnv* env, jclass { return 5; } - ReportError(env, "getDataBits failed. Unknown value"); + ReportError (env, "getDataBits failed. Unknown value"); return -1; #endif } @@ -828,19 +857,19 @@ JNIEXPORT void JNICALL Java_gurux_io_NativeCode_setDataBits(JNIEnv* env, jclass #if defined(_WIN32) || defined(_WIN64) int ret; DCB dcb; - if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) + if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) { ReportError(env, ret); } dcb.ByteSize = (BYTE) value; - if ((ret = GXSetCommState((HANDLE) hComPort, &dcb)) != 0) + if ((ret = GXSetCommState((HANDLE) hComPort, &dcb)) != 0) { ReportError(env, ret); } #else - struct termios options; - // Get the current options for the serial port. - int ret = tcgetattr(hComPort, &options); + struct termios options; + // Get the current options for the serial port. + int ret = tcgetattr(hComPort, &options); if (ret < 0) { ReportError(env, "setDataBits failed."); @@ -869,23 +898,24 @@ JNIEXPORT void JNICALL Java_gurux_io_NativeCode_setDataBits(JNIEnv* env, jclass #endif } -JNIEXPORT jint JNICALL Java_gurux_io_NativeCode_getParity(JNIEnv* env, jclass clazz, jlong hComPort) +JNIEXPORT jint JNICALL +Java_gurux_io_NativeCode_getParity (JNIEnv* env, jclass clazz, jlong hComPort) { #if defined(_WIN32) || defined(_WIN64) int ret; DCB dcb; - if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) + if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) { ReportError(env, ret); } return dcb.Parity; #else - struct termios options; - // Get the current options for the serial port. - int ret = tcgetattr(hComPort, &options); + struct termios options; + // Get the current options for the serial port. + int ret = tcgetattr (hComPort, &options); if (ret < 0) { - ReportError(env, "Failed to get settings for serial port."); + ReportError (env, "Failed to get settings for serial port."); } //Even parity is used. if ((options.c_oflag & PARENB) != 0) @@ -907,71 +937,71 @@ JNIEXPORT void JNICALL Java_gurux_io_NativeCode_setParity(JNIEnv* env, jclass cl #if defined(_WIN32) || defined(_WIN64) int ret; DCB dcb; - if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) + if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) { ReportError(env, ret); } dcb.Parity = (BYTE) value; - if ((ret = GXSetCommState((HANDLE) hComPort, &dcb)) != 0) + if ((ret = GXSetCommState((HANDLE) hComPort, &dcb)) != 0) { ReportError(env, ret); } #else - struct termios options; - // Get the current options for the serial port. - int ret = tcgetattr(hComPort, &options); + struct termios options; + // Get the current options for the serial port. + int ret = tcgetattr(hComPort, &options); if (ret < 0) { ReportError(env, "Failed to get settings for serial port."); } switch ( value) - { - case 0://None: disable parity bit. - options.c_cflag &= ~PARENB; - options.c_iflag &= ~INPCK; - break; - case 1: //odd parity - options.c_cflag |= PARENB; - options.c_cflag |= PARODD; - options.c_iflag &= ~IGNPAR; - options.c_iflag |= INPCK; - break; - case 2: //even parity - options.c_cflag |= PARENB; - options.c_cflag &= ~PARODD; - options.c_iflag &= ~IGNPAR; - options.c_iflag |= INPCK; - break; - case 3: //Mark. - case 4: //Space. - //Use parity but don't test. - options.c_cflag |= PARENB; - options.c_iflag |= IGNPAR; - break; - default: + { + case 0: //None: disable parity bit. + options.c_cflag &= ~PARENB; + options.c_iflag &= ~INPCK; + break; + case 1://odd parity + options.c_cflag |= PARENB; + options.c_cflag |= PARODD; + options.c_iflag &= ~IGNPAR; + options.c_iflag |= INPCK; + break; + case 2://even parity + options.c_cflag |= PARENB; + options.c_cflag &= ~PARODD; + options.c_iflag &= ~IGNPAR; + options.c_iflag |= INPCK; + break; + case 3://Mark. + case 4://Space. + //Use parity but don't test. + options.c_cflag |= PARENB; + options.c_iflag |= IGNPAR; + break; + default: ReportError(env, "setParity failed. Invalid value."); - } + } #endif } - -JNIEXPORT jint JNICALL Java_gurux_io_NativeCode_getStopBits(JNIEnv* env, jclass clazz, jlong hComPort) +JNIEXPORT jint JNICALL +Java_gurux_io_NativeCode_getStopBits (JNIEnv* env, jclass clazz, jlong hComPort) { #if defined(_WIN32) || defined(_WIN64) int ret; DCB dcb; - if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) + if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) { ReportError(env, ret); } return dcb.StopBits; #else - struct termios options; - // Get the current options for the serial port. - int ret = tcgetattr(hComPort, &options); + struct termios options; + // Get the current options for the serial port. + int ret = tcgetattr (hComPort, &options); if (ret < 0) { - ReportError(env, "Failed to get settings for serial port."); + ReportError (env, "Failed to get settings for serial port."); } //One stop bit is used. if ((options.c_cflag & CSTOPB) == 0) @@ -991,19 +1021,19 @@ JNIEXPORT void JNICALL Java_gurux_io_NativeCode_setStopBits(JNIEnv* env, jclass #if defined(_WIN32) || defined(_WIN64) int ret; DCB dcb; - if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) + if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) { ReportError(env, ret); } dcb.StopBits = (BYTE) value; - if ((ret = GXSetCommState((HANDLE) hComPort, &dcb)) != 0) + if ((ret = GXSetCommState((HANDLE) hComPort, &dcb)) != 0) { ReportError(env, ret); } #else - struct termios options; - // Get the current options for the serial port. - int ret = tcgetattr(hComPort, &options); + struct termios options; + // Get the current options for the serial port. + int ret = tcgetattr(hComPort, &options); if (ret < 0) { ReportError(env, "Failed to get settings for serial port."); @@ -1011,19 +1041,19 @@ JNIEXPORT void JNICALL Java_gurux_io_NativeCode_setStopBits(JNIEnv* env, jclass //One stop bit is used. if (value == 1) { - options.c_cflag &= ~CSTOPB; - tcsetattr(hComPort,TCSANOW,&options); + options.c_cflag &= ~CSTOPB; + tcsetattr(hComPort,TCSANOW,&options); } //Two stop bits are used. else if (value == 2) { - options.c_cflag |= CSTOPB; - tcsetattr(hComPort,TCSANOW,&options); + options.c_cflag |= CSTOPB; + tcsetattr(hComPort,TCSANOW,&options); } else { ReportError(env, "setStopBits failed. Invalid value"); - } + } #endif } @@ -1066,22 +1096,24 @@ JNIEXPORT void JNICALL Java_gurux_io_NativeCode_setBreakState(JNIEnv* env, jclas #endif } -JNIEXPORT jboolean JNICALL Java_gurux_io_NativeCode_getRtsEnable(JNIEnv* env, jclass clazz, jlong hComPort) -{ +JNIEXPORT jboolean JNICALL +Java_gurux_io_NativeCode_getRtsEnable (JNIEnv* env, jclass clazz, + jlong hComPort) +{ #if defined(_WIN32) || defined(_WIN64) int ret; DCB dcb; - if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) + if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) { ReportError(env, ret); } return dcb.fRtsControl == RTS_CONTROL_ENABLE; #else int status = 0; - int ret = ioctl(hComPort, TIOCMGET, &status); + int ret = ioctl (hComPort, TIOCMGET, &status); if (ret < 0) { - ReportError(env, "getRtsEnable failed."); + ReportError (env, "getRtsEnable failed."); } return (status & TIOCM_RTS) != 0; #endif @@ -1100,7 +1132,7 @@ JNIEXPORT void JNICALL Java_gurux_io_NativeCode_setRtsEnable(JNIEnv* env, jclass tmp = CLRRTS; } - if (EscapeCommFunction((HANDLE) hComPort, tmp) == 0) + if (EscapeCommFunction((HANDLE) hComPort, tmp) == 0) { ReportError(env, GetLastError()); } @@ -1113,11 +1145,11 @@ JNIEXPORT void JNICALL Java_gurux_io_NativeCode_setRtsEnable(JNIEnv* env, jclass } if (value) { - status |= TIOCM_RTS; + status |= TIOCM_RTS; } else { - status &= ~TIOCM_RTS; + status &= ~TIOCM_RTS; } ret = ioctl(hComPort, TIOCMSET, &status); if (ret != 0) @@ -1127,19 +1159,21 @@ JNIEXPORT void JNICALL Java_gurux_io_NativeCode_setRtsEnable(JNIEnv* env, jclass #endif } -JNIEXPORT jboolean JNICALL Java_gurux_io_NativeCode_getDtrEnable(JNIEnv* env, jclass clazz, jlong hComPort) +JNIEXPORT jboolean JNICALL +Java_gurux_io_NativeCode_getDtrEnable (JNIEnv* env, jclass clazz, + jlong hComPort) { #if defined(_WIN32) || defined(_WIN64) int ret; DCB dcb; - if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) + if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) { ReportError(env, ret); } return dcb.fDtrControl == DTR_CONTROL_ENABLE; #else int status = 0; - ioctl(hComPort, TIOCMGET, &status); + ioctl (hComPort, TIOCMGET, &status); return (status & TIOCM_DTR) != 0; #endif } @@ -1156,7 +1190,7 @@ JNIEXPORT void JNICALL Java_gurux_io_NativeCode_setDtrEnable(JNIEnv* env, jclass { tmp = CLRDTR; } - if (EscapeCommFunction((HANDLE) hComPort, tmp) == 0) + if (EscapeCommFunction((HANDLE) hComPort, tmp) == 0) { ReportError(env, GetLastError()); } @@ -1169,11 +1203,11 @@ JNIEXPORT void JNICALL Java_gurux_io_NativeCode_setDtrEnable(JNIEnv* env, jclass } if (value) { - status |= TIOCM_DTR; + status |= TIOCM_DTR; } else { - status &= ~TIOCM_DTR; + status &= ~TIOCM_DTR; } ret = ioctl(hComPort, TIOCMSET, &status); if (ret < 0) @@ -1183,7 +1217,9 @@ JNIEXPORT void JNICALL Java_gurux_io_NativeCode_setDtrEnable(JNIEnv* env, jclass #endif } -JNIEXPORT jboolean JNICALL Java_gurux_io_NativeCode_getDsrHolding(JNIEnv* env, jclass clazz, jlong hComPort) +JNIEXPORT jboolean JNICALL +Java_gurux_io_NativeCode_getDsrHolding (JNIEnv* env, jclass clazz, + jlong hComPort) { #if defined(_WIN32) || defined(_WIN64) COMSTAT comstat; @@ -1195,16 +1231,18 @@ JNIEXPORT jboolean JNICALL Java_gurux_io_NativeCode_getDsrHolding(JNIEnv* env, j return comstat.fDsrHold != 0; #else int status = 0; - int ret = ioctl(hComPort, TIOCMGET, &status); + int ret = ioctl (hComPort, TIOCMGET, &status); if (ret < 0) { - ReportError(env, "getDsrHolding failed."); + ReportError (env, "getDsrHolding failed."); } return (status & TIOCM_DSR) != 0; #endif } -JNIEXPORT jboolean JNICALL Java_gurux_io_NativeCode_getCtsHolding(JNIEnv* env, jclass clazz, jlong hComPort) +JNIEXPORT jboolean JNICALL +Java_gurux_io_NativeCode_getCtsHolding (JNIEnv* env, jclass clazz, + jlong hComPort) { #if defined(_WIN32) || defined(_WIN64) COMSTAT comstat; @@ -1216,16 +1254,18 @@ JNIEXPORT jboolean JNICALL Java_gurux_io_NativeCode_getCtsHolding(JNIEnv* env, j return comstat.fCtsHold != 0; #else int status = 0; - int ret = ioctl(hComPort, TIOCMGET, &status); + int ret = ioctl (hComPort, TIOCMGET, &status); if (ret < 0) { - ReportError(env, "getCtsHolding failed."); + ReportError (env, "getCtsHolding failed."); } return (status & TIOCM_CTS) != 0; #endif } -JNIEXPORT jint JNICALL Java_gurux_io_NativeCode_getBytesToRead(JNIEnv* env, jclass clazz, jlong hComPort) +JNIEXPORT jint JNICALL +Java_gurux_io_NativeCode_getBytesToRead (JNIEnv* env, jclass clazz, + jlong hComPort) { #if defined(_WIN32) || defined(_WIN64) COMSTAT comstat; @@ -1237,16 +1277,18 @@ JNIEXPORT jint JNICALL Java_gurux_io_NativeCode_getBytesToRead(JNIEnv* env, jcla return comstat.cbInQue; #else int value = 0; - int ret = ioctl(hComPort, FIONREAD, &value); + int ret = ioctl (hComPort, FIONREAD, &value); if (ret < 0) { - ReportError(env, "getBytesToRead failed."); + ReportError (env, "getBytesToRead failed."); } return value; #endif } -JNIEXPORT jint JNICALL Java_gurux_io_NativeCode_getBytesToWrite(JNIEnv* env, jclass clazz, jlong hComPort) +JNIEXPORT jint JNICALL +Java_gurux_io_NativeCode_getBytesToWrite (JNIEnv* env, jclass clazz, + jlong hComPort) { #if defined(_WIN32) || defined(_WIN64) COMSTAT comstat; @@ -1260,18 +1302,20 @@ JNIEXPORT jint JNICALL Java_gurux_io_NativeCode_getBytesToWrite(JNIEnv* env, jcl //There is no FIONWRITE in Linux so return always Zero. return 0; /* - int value = 0; - int ret = ioctl(hComPort, FIONWRITE, &value); - if (ret < 0) - { - ReportError(env, "getBytesToWrite failed in Linux."); - } - return value; - */ + int value = 0; + int ret = ioctl(hComPort, FIONWRITE, &value); + if (ret < 0) + { + ReportError(env, "getBytesToWrite failed in Linux."); + } + return value; + */ #endif } -JNIEXPORT jint JNICALL Java_gurux_io_NativeCode_getCDHolding(JNIEnv* env, jclass clazz, jlong hComPort) +JNIEXPORT jint JNICALL +Java_gurux_io_NativeCode_getCDHolding (JNIEnv* env, jclass clazz, + jlong hComPort) { #if defined(_WIN32) || defined(_WIN64) DWORD status = 0; @@ -1282,33 +1326,35 @@ JNIEXPORT jint JNICALL Java_gurux_io_NativeCode_getCDHolding(JNIEnv* env, jclass return (status & MS_RLSD_ON) != 0; #else int status = 0; - int ret = ioctl(hComPort, TIOCMGET, &status); + int ret = ioctl (hComPort, TIOCMGET, &status); if (ret < 0) { - ReportError(env, "getCDHolding failed."); + ReportError (env, "getCDHolding failed."); } return (status & TIOCM_CD) != 0; #endif } -JNIEXPORT jint JNICALL Java_gurux_io_NativeCode_getHandshake(JNIEnv* env, jclass clazz, jlong hComPort) +JNIEXPORT jint JNICALL +Java_gurux_io_NativeCode_getHandshake (JNIEnv* env, jclass clazz, + jlong hComPort) { #if defined(_WIN32) || defined(_WIN64) DCB dcb; int ret; - if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) + if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) { ReportError(env, ret); } // Disable DTR monitoring - if (dcb.fDtrControl == DTR_CONTROL_DISABLE && - // Disable RTS (Ready To Send) - dcb.fRtsControl == DTR_CONTROL_DISABLE) + if (dcb.fDtrControl == DTR_CONTROL_DISABLE && + // Disable RTS (Ready To Send) + dcb.fRtsControl == DTR_CONTROL_DISABLE) { // Enable XON/XOFF for transmission - if (dcb.fOutX && - // Enable XON/XOFF for receiving - dcb.fInX) + if (dcb.fOutX && + // Enable XON/XOFF for receiving + dcb.fInX) { //XOnXOff return 1; @@ -1318,9 +1364,9 @@ JNIEXPORT jint JNICALL Java_gurux_io_NativeCode_getHandshake(JNIEnv* env, jclass } // Enable XON/XOFF for transmission - if (dcb.fOutX && - // Enable XON/XOFF for receiving - dcb.fInX) + if (dcb.fOutX && + // Enable XON/XOFF for receiving + dcb.fInX) { //RequestToSendXOnXOff return 3; @@ -1329,40 +1375,40 @@ JNIEXPORT jint JNICALL Java_gurux_io_NativeCode_getHandshake(JNIEnv* env, jclass return 2; #else struct termios options; - // Get the current options for the serial port. - int ret = tcgetattr(hComPort, &options); - if (ret < 0) - { - ReportError(env, "Failed to get settings for serial port."); - } - // Disable DTR monitoring - if ((options.c_cflag & CRTSCTS) == 0) - { - if ((options.c_iflag & (IXON | IXOFF | IXANY)) == (IXON | IXOFF | IXANY)) - { - //XOnXOff - return 1; - } - //None - return 0; - } - if ((options.c_iflag & (IXON | IXOFF | IXANY)) == (IXON | IXOFF | IXANY)) + // Get the current options for the serial port. + int ret = tcgetattr (hComPort, &options); + if (ret < 0) + { + ReportError (env, "Failed to get settings for serial port."); + } + // Disable DTR monitoring + if ((options.c_cflag & CRTSCTS) == 0) + { + if ((options.c_iflag & (IXON | IXOFF | IXANY)) == (IXON | IXOFF | IXANY)) { - //RequestToSendXOnXOff - return 3; + //XOnXOff + return 1; } - //hardware flow control is used. - return 2; + //None + return 0; + } + if ((options.c_iflag & (IXON | IXOFF | IXANY)) == (IXON | IXOFF | IXANY)) + { + //RequestToSendXOnXOff + return 3; + } + //hardware flow control is used. + return 2; #endif } JNIEXPORT void JNICALL Java_gurux_io_NativeCode_setHandshake(JNIEnv* env, jclass clazz, jlong hComPort, jint value) -{ +{ #if defined(_WIN32) || defined(_WIN64) DCB dcb; int ret; - if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) + if ((ret = GXGetCommState((HANDLE) hComPort, &dcb)) != 0) { ReportError(env, ret); } @@ -1399,7 +1445,7 @@ JNIEXPORT void JNICALL Java_gurux_io_NativeCode_setHandshake(JNIEnv* env, jclass dcb.fRtsControl = DTR_CONTROL_ENABLE; dcb.fOutX = dcb.fInX = 1; } - if ((ret = GXSetCommState((HANDLE) hComPort, &dcb)) != 0) + if ((ret = GXSetCommState((HANDLE) hComPort, &dcb)) != 0) { ReportError(env, ret); } @@ -1415,26 +1461,26 @@ JNIEXPORT void JNICALL Java_gurux_io_NativeCode_setHandshake(JNIEnv* env, jclass if (value == 0) { options.c_cflag &= ~CRTSCTS; // no flow control - options.c_iflag &= ~(IXON | IXOFF | IXANY);// turn off s/w flow control. + options.c_iflag &= ~(IXON | IXOFF | IXANY);// turn off s/w flow control. } //XOnXOff else if (value == 1) { options.c_cflag &= ~CRTSCTS; // no flow control - options.c_iflag |= (IXON | IXOFF | IXANY);// turn s/w flow control On. + options.c_iflag |= (IXON | IXOFF | IXANY);// turn s/w flow control On. } //hardware flow control is used. else if (value == 2) { options.c_cflag |= CRTSCTS; // flow control is used. - options.c_iflag &= ~(IXON | IXOFF | IXANY);// turn off s/w flow ctcontrolrl + options.c_iflag &= ~(IXON | IXOFF | IXANY);// turn off s/w flow ctcontrolrl } //RequestToSendXOnXOff else if (value == 3) { options.c_cflag |= CRTSCTS; // flow control is used. - options.c_iflag |= (IXON | IXOFF | IXANY);// turn s/w flow control On. + options.c_iflag |= (IXON | IXOFF | IXANY);// turn s/w flow control On. } if (tcsetattr(hComPort, TCSANOW, &options) != 0) {