当前位置: 技术问答>linux和unix
读串口数据的问题
来源: 互联网 发布时间:2015-07-26
本文导语: 我的应用是 1.先写串口, 2.再读串口答复数据 我现在读到的数据中即有自已写入中数据,又有答复的数据,这样解释起来很困难,请问有没有控制参数能在读时屏闭自已写入中数据. | #include...
我的应用是
1.先写串口,
2.再读串口答复数据
我现在读到的数据中即有自已写入中数据,又有答复的数据,这样解释起来很困难,请问有没有控制参数能在读时屏闭自已写入中数据.
1.先写串口,
2.再读串口答复数据
我现在读到的数据中即有自已写入中数据,又有答复的数据,这样解释起来很困难,请问有没有控制参数能在读时屏闭自已写入中数据.
|
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int open_port(int port_num)
{
int port_handle = 0 ;
char device_name[50] ;
struct termios options ;
int status ;
if(port_num 4) {
perror("bad serial port number !n") ;
exit(1) ;
}
sprintf(device_name, "/dev/ttyS%d", port_num) ;
port_handle = open(device_name, O_RDWR | O_NOCTTY | O_NDELAY) ;
if(port_handle == -1) {
perror("open_port func failed !n") ;
exit(1) ;
} else {
/* restore async mode, use block mode */
fcntl(port_handle, F_SETFL, 0);
/* configure this serial port 2400, N, 8, 1 */
tcgetattr(port_handle, &options) ;
cfsetispeed(&options, B2400) ;
cfsetospeed(&options, B2400) ;
/* start the read and local mode */
options.c_cflag |= (CLOCAL | CREAD);
/* set parity and other options */
options.c_cflag &= ~PARENB ;
options.c_iflag &= ~INPCK ;
/* options.c_cflag |= CMSPAR ; */
/* options.c_cflag |= PARODD ; */
options.c_cflag &= ~CSTOPB ;
options.c_cflag &= ~CSIZE ;
options.c_cflag |= CS8 ;
/* close hardware flow control */
options.c_cflag &= ~CRTSCTS ;
/* close software flow control */
options.c_iflag &= ~(IXON | IXOFF | IXANY) ;
options.c_iflag &= ~ICRNL ;
options.c_cc[VMIN] = 0 ;
options.c_cc[VTIME] = 10 ;
/* select raw input mode */
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
/* select raw output mode */
//options.c_oflag &= ~OPOST ;
tcsetattr(port_handle, TCSANOW, &options) ;
}
return port_handle ;
}
int depress_bcd(unsigned char bcd)
{
unsigned char high_val = bcd >> 4 ;
unsigned char low_val = (bcd 4 ;
return (high_val * 10 + low_val) ;
}
unsigned char press_bcd(int val)
{
unsigned char original = (unsigned char)(val) ;
unsigned char high_val = original / 10 ;
unsigned char low_val = original % 10 ;
return ((high_val
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int open_port(int port_num)
{
int port_handle = 0 ;
char device_name[50] ;
struct termios options ;
int status ;
if(port_num 4) {
perror("bad serial port number !n") ;
exit(1) ;
}
sprintf(device_name, "/dev/ttyS%d", port_num) ;
port_handle = open(device_name, O_RDWR | O_NOCTTY | O_NDELAY) ;
if(port_handle == -1) {
perror("open_port func failed !n") ;
exit(1) ;
} else {
/* restore async mode, use block mode */
fcntl(port_handle, F_SETFL, 0);
/* configure this serial port 2400, N, 8, 1 */
tcgetattr(port_handle, &options) ;
cfsetispeed(&options, B2400) ;
cfsetospeed(&options, B2400) ;
/* start the read and local mode */
options.c_cflag |= (CLOCAL | CREAD);
/* set parity and other options */
options.c_cflag &= ~PARENB ;
options.c_iflag &= ~INPCK ;
/* options.c_cflag |= CMSPAR ; */
/* options.c_cflag |= PARODD ; */
options.c_cflag &= ~CSTOPB ;
options.c_cflag &= ~CSIZE ;
options.c_cflag |= CS8 ;
/* close hardware flow control */
options.c_cflag &= ~CRTSCTS ;
/* close software flow control */
options.c_iflag &= ~(IXON | IXOFF | IXANY) ;
options.c_iflag &= ~ICRNL ;
options.c_cc[VMIN] = 0 ;
options.c_cc[VTIME] = 10 ;
/* select raw input mode */
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
/* select raw output mode */
//options.c_oflag &= ~OPOST ;
tcsetattr(port_handle, TCSANOW, &options) ;
}
return port_handle ;
}
int depress_bcd(unsigned char bcd)
{
unsigned char high_val = bcd >> 4 ;
unsigned char low_val = (bcd 4 ;
return (high_val * 10 + low_val) ;
}
unsigned char press_bcd(int val)
{
unsigned char original = (unsigned char)(val) ;
unsigned char high_val = original / 10 ;
unsigned char low_val = original % 10 ;
return ((high_val