15 static LSIOSR obj(name, speed, fd);
21 printf(
"port = %s, baud_rate = %d\n", port.c_str(), baud_rate);
31 struct termios newtio, oldtio;
33 if (tcgetattr(
fd_, &oldtio) != 0)
35 perror(
"SetupSerial 1");
38 bzero(&newtio,
sizeof(newtio));
40 newtio.c_cflag |= CLOCAL;
41 newtio.c_cflag |= CREAD;
46 newtio.c_cflag |= CS7;
49 newtio.c_cflag |= CS8;
56 newtio.c_iflag |= (INPCK | ISTRIP);
57 newtio.c_cflag |= PARENB;
58 newtio.c_cflag |= PARODD;
61 newtio.c_iflag |= (INPCK | ISTRIP);
62 newtio.c_cflag |= PARENB;
63 newtio.c_cflag &= ~PARODD;
66 newtio.c_cflag &= ~PARENB;
73 cfsetispeed(&newtio, B2400);
74 cfsetospeed(&newtio, B2400);
77 cfsetispeed(&newtio, B4800);
78 cfsetospeed(&newtio, B4800);
81 cfsetispeed(&newtio, B9600);
82 cfsetospeed(&newtio, B9600);
85 cfsetispeed(&newtio, B115200);
86 cfsetospeed(&newtio, B115200);
89 cfsetispeed(&newtio, B230400);
90 cfsetospeed(&newtio, B230400);
93 cfsetispeed(&newtio, B460800);
94 cfsetospeed(&newtio, B460800);
97 cfsetispeed(&newtio, B9600);
98 cfsetospeed(&newtio, B9600);
108 newtio.c_cflag &= ~CSTOPB;
110 newtio.c_cflag |= CSTOPB;
112 newtio.c_cc[VTIME] = 0;
113 newtio.c_cc[VMIN] = 0;
115 tcflush(
fd_, TCIFLUSH);
117 if ((tcsetattr(
fd_, TCSANOW, &newtio)) != 0)
119 perror(
"serial set error");
129 memset(buffer, 0, length);
131 int totalBytesRead = 0;
139 return (rc == 0) ? 0 : -1;
150 totalBytesRead += rc;
179 totalBytesRead += rc;
181 else if ((rc < 0) && (errno != EINTR) && (errno != EAGAIN))
183 printf(
"read error\n");
190 printf(
"Serial Rx: ");
191 for(
int i = 0; i < totalBytesRead; i++)
193 printf(
"%02X ", (buffer[i]) & 0xFF);
198 return totalBytesRead;
217 tv.tv_usec = millis % 1000 * 1000;
218 tv.tv_sec = millis / 1000;
231 FD_SET(serial, &fdset);
233 rc = select(serial + 1, &fdset, NULL, NULL, &tv);
236 rc = (FD_ISSET(serial, &fdset)) ? 1 : -1;
266 tv.tv_usec = millis % 1000 * 1000;
267 tv.tv_sec = millis / 1000;
280 FD_SET(serial, &fdset);
282 rc = select(serial + 1, NULL, &fdset, NULL, &tv);
285 rc = (FD_ISSET(serial, &fdset)) ? 1 : -1;
306 if ((buffer == 0) || (length <= 0))
311 int totalBytesWrite = 0;
313 char* pb = (
char*)buffer;
321 return (rc == 0) ? 0 : -1;
327 rc = write(
fd_, pb, (
size_t)length);
332 totalBytesWrite += rc;
357 rc = write(
fd_, pb, (
size_t)length);
360 totalBytesWrite += rc;
362 else if ((rc < 0) && (errno != EINTR) && (errno != EAGAIN))
370 printf(
"Serial Tx: ");
371 for(
int i = 0; i < totalBytesWrite; i++)
373 printf(
"%02X ", (buffer[i]) & 0xFF);
378 return totalBytesWrite;
385 fd_ = open(
port_.c_str(), O_RDWR|O_NOCTTY|O_NDELAY);
390 printf(
"open_port %s , fd %d OK !\n",
port_.c_str(),
fd_);
395 printf(
"open_port %s ERROR !\n",
port_.c_str());
397 printf(
"LSIOSR::Init\n");
int waitWritable(int millis)
int waitReadable(int millis)
int read(char *buffer, int length, int timeout=30)
static LSIOSR * instance(std::string name, int speed, int fd=0)
int setPort(std::string name)
LSIOSR(std::string name, int speed, int fd)
int setOpt(int nBits, uint8_t nEvent, int nStop)
int send(const char *buffer, int length, int timeout=30)