diff options
-rw-r--r-- | glcddrivers/port.c | 16 | ||||
-rw-r--r-- | glcddrivers/port.h | 1 | ||||
-rw-r--r-- | glcddrivers/usbserlcd.c | 13 |
3 files changed, 26 insertions, 4 deletions
diff --git a/glcddrivers/port.c b/glcddrivers/port.c index 2c123e1..bf5df86 100644 --- a/glcddrivers/port.c +++ b/glcddrivers/port.c @@ -340,9 +340,6 @@ int cSerialPort::Open(const char * device) options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; - // No "hangup" (prevents Arduino firmware reboots) - options.c_cflag &= ~HUPCL; - // No hardware flow control options.c_cflag &= ~CRTSCTS; @@ -392,6 +389,19 @@ void cSerialPort::SetBaudRate(int speed) } } +// Configures the serial port to not send a "hangup" signal. +// Returns true if the flag was set and had to be removed and false otherwise. +bool cSerialPort::DisableHangup() +{ + struct termios options; + tcgetattr(fd, &options); + if (!(options.c_cflag & HUPCL)) + return false; + options.c_cflag &= ~HUPCL; + tcsetattr(fd, TCSANOW, &options); + return true; +} + int cSerialPort::ReadData(unsigned char * data) { if (fd == -1) diff --git a/glcddrivers/port.h b/glcddrivers/port.h index fc97293..9280661 100644 --- a/glcddrivers/port.h +++ b/glcddrivers/port.h @@ -91,6 +91,7 @@ public: int Open(const char * device); int Close(); void SetBaudRate(int speed); + bool DisableHangup(); int ReadData(unsigned char * data); void WriteData(unsigned char data); diff --git a/glcddrivers/usbserlcd.c b/glcddrivers/usbserlcd.c index 47d1e7e..44efb31 100644 --- a/glcddrivers/usbserlcd.c +++ b/glcddrivers/usbserlcd.c @@ -12,6 +12,7 @@ #include <syslog.h> #include <cstring> #include <cstdlib> +#include <unistd.h> #include "common.h" #include "config.h" @@ -19,6 +20,8 @@ #include "usbserlcd.h" #include "stdint.h" +const int BAUD_RATE = 150000; +const int BOOT_TIME = 300000; namespace GLCD { @@ -73,13 +76,21 @@ int cDriverUSBserLCD::Init() if (port->Open(config->device.c_str()) != 0) return -1; - port->SetBaudRate(150000); + port->SetBaudRate(BAUD_RATE); *oldConfig = *config; // clear display Clear(); + // Disable the "hangup" signal to prevent Arduino reboots on connect. + // We can't catch the first reboot with this (Hangup flag was set). + // In this case, wait some milliseconds and do a full refresh. + if (port->DisableHangup()) { + usleep(BOOT_TIME); + Refresh(true); + } + syslog(LOG_INFO, "%s: USBserLCD initialized.\n", config->name.c_str()); return 0; } |