16 #define TTY_WAIT 1 //ms
17 #define MIN_BUFFER_SIZE 256
26 :
XPort(interface), m_scifd(-1) {
29 XSerialPort::~XSerialPort() {
30 if(m_scifd >= 0) close(m_scifd);
42 :
XPort(intf), m_handle(INVALID_HANDLE_VALUE) {
43 C_ASSERT(
sizeof(
void *) ==
sizeof(HANDLE));
45 XSerialPort::~XSerialPort() {
46 if(m_handle != INVALID_HANDLE_VALUE)
47 CloseHandle(m_handle);
56 struct termios ttyios;
58 if((m_scifd = ::open(QString(shot[ *pInterface->port()].to_str()).toLocal8Bit().data(),
59 O_RDWR | O_NOCTTY | O_SYNC | O_NONBLOCK)) == -1) {
63 tcsetpgrp(m_scifd, getpgrp());
65 bzero( &ttyios,
sizeof(ttyios));
68 switch(static_cast<int>(pInterface->serialBaudRate())) {
69 case 2400: baudrate = B2400;
break;
70 case 4800: baudrate = B4800;
break;
71 case 9600: baudrate = B9600;
break;
72 case 19200: baudrate = B19200;
break;
73 case 38400: baudrate = B38400;
break;
74 case 57600: baudrate = B57600;
break;
75 case 115200: baudrate = B115200;
break;
76 case 230400: baudrate = B230400;
break;
81 cfsetispeed( &ttyios, baudrate);
82 cfsetospeed( &ttyios, baudrate);
84 ttyios.c_cflag &= ~(PARENB | CSIZE);
85 if(pInterface->serialParity() == XCharInterface::PARITY_EVEN)
86 ttyios.c_cflag |= PARENB;
87 if(pInterface->serialParity() == XCharInterface::PARITY_ODD)
88 ttyios.c_cflag |= PARENB | PARODD;
89 if(pInterface->serial7Bits())
90 ttyios.c_cflag |= CS7;
92 ttyios.c_cflag |= CS8;
93 ttyios.c_cflag |= HUPCL | CLOCAL | CREAD;
94 if(pInterface->serialStopBits() == 2)
95 ttyios.c_cflag |= CSTOPB;
96 ttyios.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
97 ttyios.c_iflag |= IGNBRK;
98 if(pInterface->serialParity() == XCharInterface::PARITY_NONE)
99 ttyios.c_iflag |= IGNPAR;
100 ttyios.c_cc[VMIN] = 0;
101 ttyios.c_cc[VTIME] = 30;
102 if(tcsetattr(m_scifd, TCSAFLUSH, &ttyios ) < 0)
105 if(fcntl(m_scifd, F_SETFL, (~O_NONBLOCK) & fcntl(m_scifd, F_GETFL)) == - 1) {
111 m_handle = CreateFileA( QString(shot[ *pInterface->port()].to_str()).toLocal8Bit().data(),
112 GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
113 if (m_handle == INVALID_HANDLE_VALUE)
117 GetCommState(m_handle, &dcb);
118 dcb.BaudRate =
static_cast<int>(pInterface->serialBaudRate());
119 dcb.ByteSize = pInterface->serial7Bits() ? 7 : 8;
120 switch((
int)pInterface->serialParity()) {
121 case XCharInterface::PARITY_EVEN:
123 dcb.Parity = EVENPARITY;
125 case XCharInterface::PARITY_ODD:
127 dcb.Parity = ODDPARITY;
130 case XCharInterface::PARITY_NONE:
132 dcb.Parity = NOPARITY;
135 dcb.StopBits = (pInterface->serialStopBits() == 2) ? TWOSTOPBITS : ONESTOPBIT;
136 if( !SetCommState(m_handle, &dcb))
140 GetCommTimeouts(m_handle, &cto);
141 cto.ReadIntervalTimeout = 0;
142 cto.ReadTotalTimeoutMultiplier = 0;
143 cto.ReadTotalTimeoutConstant = 3000;
144 cto.WriteTotalTimeoutMultiplier = 0;
145 cto.WriteTotalTimeoutConstant = 3000;
146 if( !SetCommTimeouts(m_handle, &cto))
150 m_serialFlushBeforeWrite = pInterface->serialFlushBeforeWrite();
151 m_serialHasEchoBack = pInterface->serialHasEchoBack();
153 fprintf(stderr,
"Serial port opened w/ baudrate=%d\n", (
int)pInterface->serialBaudRate());
159 if(m_serialHasEchoBack) {
160 this->write(str, strlen(str));
161 this->write(buf.c_str() + strlen(str), buf.length() - strlen(str));
165 this->write(buf.c_str(), buf.length());
170 if(m_serialHasEchoBack && (size >= 2) && isprint(sendbuf[0])) {
171 for(
int cnt = 0; cnt < size; ++cnt) {
174 write(sendbuf + cnt, 1);
178 WriteFile(m_handle, sendbuf + cnt, 1, &wcnt, NULL);
183 if(buffer()[0] == sendbuf[cnt])
185 if(isspace(buffer()[0]))
188 formatString(
"inconsistent echo back %c against %c", buffer()[0], sendbuf[cnt]).c_str(),
195 if(m_serialFlushBeforeWrite) {
198 int ret = tcflush(m_scifd, TCIFLUSH);
201 dbgPrint(
"Serial, EINTR, try to continue.");
210 if( !PurgeComm(m_handle, PURGE_RXCLEAR)) {
221 int ret = ::write(m_scifd, sendbuf, size - wlen);
224 dbgPrint(
"Serial, EINTR, try to continue.");
234 WriteFile(m_handle, sendbuf, size - wlen, &ret, NULL);
240 }
while (wlen < size);
243 XSerialPort::receive() throw (
XInterface::XCommError &) {
254 buffer().resize(MIN_BUFFER_SIZE);
256 const char *ceos = eos().c_str();
257 unsigned int eos_len = strlen(ceos);
258 unsigned int len = 0;
260 if(
buffer().size() <= len + 1)
261 buffer().resize(len + MIN_BUFFER_SIZE);
263 int rlen = ::read(m_scifd, &
buffer().at(len), 1);
266 dbgPrint(
"Serial, EINTR, try to continue.");
275 ReadFile(m_handle, &
buffer().at(len), 1, &rlen, NULL);
283 if( !strncmp(&
buffer().at(len - eos_len), ceos, eos_len)) {
304 buffer().resize(length);
305 unsigned int len = 0;
307 while(len < length) {
309 int rlen = ::read(m_scifd, &buffer().at(len), 1);
312 dbgPrint(
"Serial, EINTR, try to continue.");
321 ReadFile(m_handle, &buffer().at(len), 1, &rlen, NULL);