modify serial device settings instead of using empty template; check if serial fd is a valid terminal

This commit is contained in:
Roland Jax
2014-11-28 14:19:31 +01:00
parent 8c5de31d98
commit 10839f770f
2 changed files with 16 additions and 17 deletions
+13 -14
View File
@@ -135,35 +135,34 @@ unsigned char Device::getByte()
void DeviceSerial::openDevice(const string deviceName, const bool noDeviceCheck) void DeviceSerial::openDevice(const string deviceName, const bool noDeviceCheck)
{ {
m_noDeviceCheck = noDeviceCheck; m_noDeviceCheck = noDeviceCheck;
struct termios settings;
termios newSettings;
m_open = false; m_open = false;
// open file descriptor from serial device // open file descriptor
m_fd = open(deviceName.c_str(), O_RDWR | O_NOCTTY); m_fd = open(deviceName.c_str(), O_RDWR | O_NOCTTY);
if (m_fd < 0) if (m_fd < 0 || isatty(m_fd) == 0)
return; return;
// save current settings of serial device // save current settings
tcgetattr(m_fd, &m_oldSettings); tcgetattr(m_fd, &m_oldSettings);
memset(&newSettings, '\0', sizeof(newSettings)); // modify current settings
tcgetattr(m_fd, &settings);
newSettings.c_cflag |= (B2400 | CS8 | CLOCAL | CREAD); settings.c_cflag |= (B2400 | CS8 | CLOCAL | CREAD);
newSettings.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); settings.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
newSettings.c_iflag |= IGNPAR; settings.c_iflag |= IGNPAR;
newSettings.c_oflag &= ~OPOST; settings.c_oflag &= ~OPOST;
newSettings.c_cc[VMIN] = 1; settings.c_cc[VMIN] = 1;
newSettings.c_cc[VTIME] = 0; settings.c_cc[VTIME] = 0;
// empty device buffer // empty device buffer
tcflush(m_fd, TCIFLUSH); tcflush(m_fd, TCIFLUSH);
// activate new settings of serial device // activate new settings of serial device
tcsetattr(m_fd, TCSANOW, &newSettings); tcsetattr(m_fd, TCSAFLUSH, &settings);
// set serial device into blocking mode // set serial device into blocking mode
fcntl(m_fd, F_SETFL, fcntl(m_fd, F_GETFL) & ~O_NONBLOCK); fcntl(m_fd, F_SETFL, fcntl(m_fd, F_GETFL) & ~O_NONBLOCK);
+3 -3
View File
@@ -106,13 +106,13 @@ public:
ssize_t sizeRecvBuffer() const { return m_recvBuffer.size(); } ssize_t sizeRecvBuffer() const { return m_recvBuffer.size(); }
protected: protected:
/** if of file descriptor */ /** file descriptor from input device */
int m_fd; int m_fd;
/** state of device*/ /** true if device is opened */
bool m_open; bool m_open;
/** state of device check */ /** true if device check is disabled */
bool m_noDeviceCheck; bool m_noDeviceCheck;
/** queue for received bytes */ /** queue for received bytes */