Ensure that custom baud rates are properly handled depending on the capabilities of the end device.

This commit is contained in:
hedgecrw85 2018-01-03 15:00:08 -06:00
parent 14c9d4eff7
commit e1e4441484
3 changed files with 50 additions and 18 deletions

View File

@ -315,19 +315,47 @@ unsigned int getBaudRateCode(int baudRate)
case 38400:
return B38400;
case 57600:
#ifdef B57600
return B57600;
#else
return 0;
#endif
case 115200:
#ifdef B115200
return B115200;
#else
return 0;
#endif
case 230400:
#ifdef B230400
return B230400;
#else
return 0;
#endif
case 460800:
#ifdef B460800
return B460800;
#else
return 0;
#endif
case 500000:
#ifdef B500000
return B500000;
#else
return 0;
#endif
case 576000:
#ifdef B576000
return B576000;
#else
return 0;
#endif
case 921600:
#ifdef B921600
return B921600;
#else
return 0;
#endif
default:
return 0;
}
@ -337,6 +365,7 @@ unsigned int getBaudRateCode(int baudRate)
void setBaudRate(int portFD, int baudRate)
{
#ifdef TCSETS2
struct termios2 options = { 0 };
ioctl(portFD, TCGETS2, &options);
options.c_cflag &= ~CBAUD;
@ -344,6 +373,16 @@ void setBaudRate(int portFD, int baudRate)
options.c_ispeed = baudRate;
options.c_ospeed = baudRate;
ioctl(portFD, TCSETS2, &options);
#else
struct serial_struct serInfo;
ioctl(portFD, TIOCGSERIAL, &serInfo);
serInfo.flags &= ~ASYNC_SPD_MASK;
serInfo.flags |= ASYNC_SPD_CUST | ASYNC_LOW_LATENCY;
serInfo.custom_divisor = serInfo.baud_base / baudRate;
if (sersInfo.custom_divisor == 0)
serInfo.custom_divisor = 1;
ioctl(portFD, TIOCSSERIAL, &serInfo);
#endif
}
#endif

View File

@ -235,23 +235,16 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
cfsetispeed(&options, baudRateCode);
cfsetospeed(&options, baudRateCode);
}
else
{
cfsetispeed(&options, baudRate);
cfsetospeed(&options, baudRate);
}
// Apply changes
ioctl(serialPortFD, TIOCEXCL); // Block other non-root users from using this port
jboolean rc = ((tcsetattr(serialPortFD, TCSANOW, &options) == 0) ? JNI_TRUE : JNI_FALSE);
if (baudRateCode == 0) {
// use IOSSIOSPEED to set non-standard baud rate
speed_t speed = (speed_t)baudRate;
ioctl(serialPortFD, IOSSIOSPEED, &speed);
}
return rc;
int retVal = tcsetattr(serialPortFD, TCSANOW, &options);
ioctl(serialPortFD, TIOCEXCL); // Block non-root users from opening this port
if (baudRateCode == 0) // Set custom baud rate
{
speed_t speed = (speed_t)baudRate;
ioctl(serialPortFD, IOSSIOSPEED, &speed);
}
return ((retVal == 0) ? JNI_TRUE : JNI_FALSE);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configTimeouts(JNIEnv *env, jobject obj, jlong serialPortFD)

View File

@ -2,10 +2,10 @@
* SerialPortTest.java
*
* Created on: Feb 27, 2015
* Last Updated on: Dec 05, 2016
* Last Updated on: Jan 03, 2018
* Author: Will Hedgecock
*
* Copyright (C) 2012-2017 Fazecast, Inc.
* Copyright (C) 2012-2018 Fazecast, Inc.
*
* This file is part of jSerialComm.
*
@ -32,7 +32,7 @@ import java.util.Scanner;
* This class provides a test case for the jSerialComm library.
*
* @author Will Hedgecock <will.hedgecock@gmail.com>
* @version 1.4.0
* @version 2.0.0
* @see java.io.InputStream
* @see java.io.OutputStream
*/