当前位置: 技术问答>linux和unix
linux 串口编程高手进
来源: 互联网 发布时间:2016-11-01
本文导语: 高手好 我写的程序出了个问题,是这样的,程序可以运行,收发数据也正常,就是运行一会就发不了数据了。我观察到的规律是单片机给我发的数据发生变化时容易产生这个问题。如果稳定运行就能一直持续下去。...
高手好
我写的程序出了个问题,是这样的,程序可以运行,收发数据也正常,就是运行一会就发不了数据了。我观察到的规律是单片机给我发的数据发生变化时容易产生这个问题。如果稳定运行就能一直持续下去。关键代码如下。请高手赐教,感激涕零!
enum BaudRateType {
BAUD300,
BAUD600,
BAUD1200,
BAUD2400,
BAUD4800,
BAUD9600,
BAUD19200,
BAUD38400,
BAUD57600,
BAUD115200,
};
enum DataBitsType {
DATA_5,
DATA_6,
DATA_7,
DATA_8
};
enum ParityType {
PAR_NONE,
PAR_ODD,
PAR_EVEN,
PAR_SPACE
};
enum StopBitsType {
STOP_1,
STOP_2
};
enum FlowType {
FLOW_OFF,
FLOW_HARDWARE,
FLOW_XONXOFF
};
/*
* structure to contain port settings
*/
struct PortSettings {
BaudRateType baudRate;
DataBitsType dataBits;
ParityType parity;
StopBitsType stopBits;
FlowType flowControl;
};
bool SerialPort::setTermios(struct PortSettings *settings)
{
struct termios config;
if (tcgetattr(m_fd, &config) == -1)
return false;
/* set baud rate */
speed_t baud_rate;
switch (settings->baudRate) {
case BAUD300:
baud_rate = B300;
break;
case BAUD600:
baud_rate = B600;
break;
case BAUD1200:
baud_rate = B1200;
break;
case BAUD2400:
baud_rate = B2400;
break;
case BAUD4800:
baud_rate = B4800;
break;
case BAUD9600:
baud_rate = B9600;
break;
case BAUD19200:
baud_rate = B19200;
break;
case BAUD38400:
baud_rate = B38400;
break;
case BAUD57600:
baud_rate = B57600;
break;
case BAUD115200:
baud_rate = B115200;
break;
default:
baud_rate = B115200;
break;
}
tcflush(m_fd, TCIOFLUSH);
if (cfsetispeed(&config, baud_rate) == -1)
return false;
if (cfsetospeed(&config, baud_rate) == -1)
return false;
/* set flow control */
switch (settings->flowControl) {
case FLOW_OFF:
config.c_cflag &= ~CRTSCTS;
break;
case FLOW_HARDWARE:
config.c_cflag |= CRTSCTS;
break;
case FLOW_XONXOFF:
config.c_cflag |= IXON | IXOFF | IXANY;
break;
default:
config.c_cflag &= ~CRTSCTS;
break;
}
/* set data bits */
config.c_cflag &= ~CSIZE;
switch (settings->dataBits) {
case DATA_5:
config.c_cflag |=CS5;
break;
case DATA_6:
config.c_cflag |= CS6;
break;
case DATA_7:
config.c_cflag |= CS7;
break;
case DATA_8:
config.c_cflag |= CS8;
break;
default:
config.c_cflag |= CS8;
break;
}
/* set parity */
switch (settings->parity) {
case PAR_NONE:
config.c_cflag &= ~PARENB;
config.c_iflag &= ~INPCK;
break;
case PAR_ODD:
config.c_cflag |= (PARENB | PARODD);
config.c_iflag |= INPCK;
break;
case PAR_EVEN:
config.c_cflag |= PARENB;
config.c_cflag |= ~PARODD;
config.c_iflag |= INPCK;
break;
case PAR_SPACE:
config.c_cflag &= ~PARENB;
config.c_cflag &= ~CSTOPB;
config.c_iflag |= INPCK;
break;
default:
config.c_cflag &= ~PARENB;
config.c_iflag &= ~INPCK;
break;
}
/* set stop bits */
switch (settings->stopBits) {
case STOP_1:
config.c_cflag &= ~CSTOPB;
break;
case STOP_2:
config.c_cflag |= CSTOPB;
break;
default:
config.c_cflag &= ~CSTOPB;
break;
}
/* set control mode */
config.c_cflag |= CLOCAL;
config.c_cflag |= CREAD;
/* set output mode */
config.c_lflag &= ~(ICANON | ECHO | ISIG);
config.c_oflag &= ~OPOST;
/* set control char */
config.c_cc[VMIN] = 34;
config.c_cc[VTIME] = 1;
tcflush(m_fd, TCIFLUSH);
if (tcsetattr(m_fd, TCSANOW, &config) == -1)
return false;
return true;
}
我写的程序出了个问题,是这样的,程序可以运行,收发数据也正常,就是运行一会就发不了数据了。我观察到的规律是单片机给我发的数据发生变化时容易产生这个问题。如果稳定运行就能一直持续下去。关键代码如下。请高手赐教,感激涕零!
enum BaudRateType {
BAUD300,
BAUD600,
BAUD1200,
BAUD2400,
BAUD4800,
BAUD9600,
BAUD19200,
BAUD38400,
BAUD57600,
BAUD115200,
};
enum DataBitsType {
DATA_5,
DATA_6,
DATA_7,
DATA_8
};
enum ParityType {
PAR_NONE,
PAR_ODD,
PAR_EVEN,
PAR_SPACE
};
enum StopBitsType {
STOP_1,
STOP_2
};
enum FlowType {
FLOW_OFF,
FLOW_HARDWARE,
FLOW_XONXOFF
};
/*
* structure to contain port settings
*/
struct PortSettings {
BaudRateType baudRate;
DataBitsType dataBits;
ParityType parity;
StopBitsType stopBits;
FlowType flowControl;
};
bool SerialPort::setTermios(struct PortSettings *settings)
{
struct termios config;
if (tcgetattr(m_fd, &config) == -1)
return false;
/* set baud rate */
speed_t baud_rate;
switch (settings->baudRate) {
case BAUD300:
baud_rate = B300;
break;
case BAUD600:
baud_rate = B600;
break;
case BAUD1200:
baud_rate = B1200;
break;
case BAUD2400:
baud_rate = B2400;
break;
case BAUD4800:
baud_rate = B4800;
break;
case BAUD9600:
baud_rate = B9600;
break;
case BAUD19200:
baud_rate = B19200;
break;
case BAUD38400:
baud_rate = B38400;
break;
case BAUD57600:
baud_rate = B57600;
break;
case BAUD115200:
baud_rate = B115200;
break;
default:
baud_rate = B115200;
break;
}
tcflush(m_fd, TCIOFLUSH);
if (cfsetispeed(&config, baud_rate) == -1)
return false;
if (cfsetospeed(&config, baud_rate) == -1)
return false;
/* set flow control */
switch (settings->flowControl) {
case FLOW_OFF:
config.c_cflag &= ~CRTSCTS;
break;
case FLOW_HARDWARE:
config.c_cflag |= CRTSCTS;
break;
case FLOW_XONXOFF:
config.c_cflag |= IXON | IXOFF | IXANY;
break;
default:
config.c_cflag &= ~CRTSCTS;
break;
}
/* set data bits */
config.c_cflag &= ~CSIZE;
switch (settings->dataBits) {
case DATA_5:
config.c_cflag |=CS5;
break;
case DATA_6:
config.c_cflag |= CS6;
break;
case DATA_7:
config.c_cflag |= CS7;
break;
case DATA_8:
config.c_cflag |= CS8;
break;
default:
config.c_cflag |= CS8;
break;
}
/* set parity */
switch (settings->parity) {
case PAR_NONE:
config.c_cflag &= ~PARENB;
config.c_iflag &= ~INPCK;
break;
case PAR_ODD:
config.c_cflag |= (PARENB | PARODD);
config.c_iflag |= INPCK;
break;
case PAR_EVEN:
config.c_cflag |= PARENB;
config.c_cflag |= ~PARODD;
config.c_iflag |= INPCK;
break;
case PAR_SPACE:
config.c_cflag &= ~PARENB;
config.c_cflag &= ~CSTOPB;
config.c_iflag |= INPCK;
break;
default:
config.c_cflag &= ~PARENB;
config.c_iflag &= ~INPCK;
break;
}
/* set stop bits */
switch (settings->stopBits) {
case STOP_1:
config.c_cflag &= ~CSTOPB;
break;
case STOP_2:
config.c_cflag |= CSTOPB;
break;
default:
config.c_cflag &= ~CSTOPB;
break;
}
/* set control mode */
config.c_cflag |= CLOCAL;
config.c_cflag |= CREAD;
/* set output mode */
config.c_lflag &= ~(ICANON | ECHO | ISIG);
config.c_oflag &= ~OPOST;
/* set control char */
config.c_cc[VMIN] = 34;
config.c_cc[VTIME] = 1;
tcflush(m_fd, TCIFLUSH);
if (tcsetattr(m_fd, TCSANOW, &config) == -1)
return false;
return true;
}
|
不会是运行一会,单片机的波特率发生变化了吧?