25 #include <sys/types.h> 28 #include <sys/ioctl.h> 29 #include <linux/serial.h> 47 const int baudTable[] = {
48 0, 50, 75, 110, 134, 150, 200, 300, 600,
49 1200, 1800, 2400, 4800,
50 9600, 19200, 38400, 57600, 115200, 230400,
51 460800, 500000, 576000, 921600, 1000000
53 const int baudCodes[] = {
54 B0, B50, B75, B110, B134, B150, B200, B300, B600,
55 B1200, B1800, B2400, B4800,
56 B9600, B19200, B38400, B57600, B115200, B230400,
57 B460800, B500000, B576000, B921600, B1000000
59 const int iBaudsLen =
sizeof(baudTable) /
sizeof(
int);
62 *iBaudrateCode = B38400;
64 for( i=0; i<iBaudsLen; i++ ) {
65 if( baudTable[i] == iBaudrate ) {
66 *iBaudrateCode = baudCodes[i];
110 m_Handshake(HS_NONE),
112 m_WriteBufSize(m_ReadBufSize),
114 m_ShortBytePeriod(false)
135 std::cout <<
"Trying to open " <<
m_DeviceName <<
" failed: " 136 << strerror(errno) <<
" (Error code " << errno <<
")" << std::endl;
145 std::cout <<
"tcgetattr of " <<
m_DeviceName <<
" failed: " 146 << strerror(errno) <<
" (Error code " << errno <<
")" << std::endl;
157 m_tio.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
159 cfsetispeed(&
m_tio, B9600);
160 cfsetospeed(&
m_tio, B9600);
162 m_tio.c_cc[VINTR] = 3;
163 m_tio.c_cc[VQUIT] = 28;
164 m_tio.c_cc[VERASE] = 127;
165 m_tio.c_cc[VKILL] = 21;
166 m_tio.c_cc[VEOF] = 4;
167 m_tio.c_cc[VTIME] = 0;
168 m_tio.c_cc[VMIN] = 1;
169 m_tio.c_cc[VSWTC] = 0;
170 m_tio.c_cc[VSTART] = 17;
171 m_tio.c_cc[VSTOP] = 19;
172 m_tio.c_cc[VSUSP] = 26;
173 m_tio.c_cc[VEOL] = 0;
174 m_tio.c_cc[VREPRINT] = 18;
175 m_tio.c_cc[VDISCARD] = 15;
176 m_tio.c_cc[VWERASE] = 23;
177 m_tio.c_cc[VLNEXT] = 22;
178 m_tio.c_cc[VEOL2] = 0;
183 std::cout <<
"Setting Baudrate to " << iNewBaudrate << std::endl;
185 int iBaudrateCode = 0;
188 cfsetispeed(&
m_tio, iBaudrateCode);
189 cfsetospeed(&
m_tio, iBaudrateCode);
191 if( !bBaudrateValid ) {
192 std::cout <<
"Baudrate code not available - setting baudrate directly" << std::endl;
193 struct serial_struct ss;
194 ioctl(
m_Device, TIOCGSERIAL, &ss );
195 ss.flags |= ASYNC_SPD_CUST;
196 ss.custom_divisor = ss.baud_base / iNewBaudrate;
197 ioctl(
m_Device, TIOCSSERIAL, &ss );
202 m_tio.c_cflag &= ~CSIZE;
206 m_tio.c_cflag |= CS5;
209 m_tio.c_cflag |= CS6;
212 m_tio.c_cflag |= CS7;
216 m_tio.c_cflag |= CS8;
219 m_tio.c_cflag &= ~ (PARENB | PARODD);
224 m_tio.c_cflag |= PARODD;
228 m_tio.c_cflag |= PARENB;
238 m_tio.c_cflag |= CSTOPB;
243 m_tio.c_cflag &= ~CSTOPB;
250 m_tio.c_cflag &= ~CRTSCTS;
251 m_tio.c_iflag &= ~(IXON | IXOFF | IXANY);
254 m_tio.c_cflag |= CRTSCTS;
255 m_tio.c_iflag &= ~(IXON | IXOFF | IXANY);
258 m_tio.c_cflag &= ~CRTSCTS;
259 m_tio.c_iflag |= (IXON | IXOFF | IXANY);
263 m_tio.c_oflag &= ~OPOST;
264 m_tio.c_lflag &= ~ICANON;
271 std::cout <<
"tcsetattr " <<
m_DeviceName <<
" failed: " 272 << strerror(errno) <<
" (Error code " << errno <<
")" << std::endl;
343 BytesRead = read(
m_Device, Buffer, Length);
345 printf(
"%2d Bytes read:", BytesRead);
346 for(
int i=0; i<BytesRead; i++)
347 printf(
" %.2x", (
unsigned char)Buffer[i]);
356 int iBytesToRead = (Length < iAvaibleBytes) ? Length : iAvaibleBytes;
360 BytesRead = read(
m_Device, Buffer, iBytesToRead);
378 ssize_t BytesWritten;
383 for (i = 0; i < Length; i++)
385 BytesWritten = write(
m_Device, Buffer + i, 1);
386 if (BytesWritten != 1)
393 BytesWritten = write(
m_Device, Buffer, Length);
395 printf(
"%2d Bytes sent:", BytesWritten);
396 for(
int i=0; i<BytesWritten; i++)
397 printf(
" %.2x", (
unsigned char)Buffer[i]);
407 int Res = ioctl(
m_Device, FIONREAD, &cbInQue);
void setBytePeriod(double Period)
virtual ~SerialIO()
Destructor.
HandshakeFlags m_Handshake
void setTimeout(double Timeout)
void changeBaudRate(int BaudRate)
int writeIO(const char *Buffer, int Length)
int readBlocking(char *Buffer, int Length)
SerialIO()
Default constructor.
int readNonBlocking(char *Buffer, int Length)
bool getBaudrateCode(int iBaudrate, int *iBaudrateCode)