linux串口(epoll 注意ET/LT使用)


linux系统串口初始化

/* set uart
*
* input:
* --uart: uart device string
* --tty: uart config
* --baudrate: uart speed
* output:
* none
*
* return:
* uart handle
*/
int setuart(char *uart, uartconfig *tty, speed_t baudrate)
{
// O_NOCTTY: 非该进程的控制终端
// O_NDELAY: 不关心DCD信号线状态
// O_NONBLOCK: 非阻塞方式打开
int serial_port = open(uart, O_RDWR|O_NOCTTY|O_NDELAY|O_NONBLOCK); // |O_NDELAY|O_NONBLOCK


// Read tty settings, also checking for error
if (tcgetattr(serial_port, tty) != 0) {
printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
debug_output(RTK_LOCATION_ERRORMSG, NULL, 0, (char*)"Error %i from tcgetattr: %s\n", errno, strerror(errno));
close(serial_port);
return -1;
}


// 清除配置
bzero(tty, sizeof(uartconfig));


// Set in/out baud rate to be baudrate
cfsetispeed(tty, baudrate);
cfsetospeed(tty, baudrate);


tty->c_cflag &= ~PARENB; // Clear parity bit, disabling parity (most common)
tty->c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common)
tty->c_cflag &= ~CSIZE; // Clear all bits that set the data size
tty->c_cflag |= CS8; // 8 bits per byte (most common)
tty->c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control (most common)
tty->c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)


tty->c_lflag &= ~(ICANON|IEXTEN);
tty->c_lflag &= ~ECHO; // Disable echo
tty->c_lflag &= ~ECHOE; // Disable erasure
tty->c_lflag &= ~ECHONL; // Disable new-line echo
tty->c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP

tty->c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
tty->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|INPCK); // Disable any special handling of received bytes


tty->c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
tty->c_oflag &= ~(ONLCR|OCRNL); // Prevent conversion of newline to carriage return/line feed
// tty.c_oflag &= ~OXTABS; // Prevent conversion of tabs to spaces (NOT PRESENT ON LINUX)
// tty.c_oflag &= ~ONOEOT; // Prevent removal of C-d chars (0x004) in output (NOT PRESENT ON LINUX)


tty->c_cc[VTIME] = TIME_LEN; // Wait for up to 1/10s (10 deciseconds), returning as soon as any data is received.
tty->c_cc[VMIN] = TIME_LEN;


// enable raw mode
cfmakeraw(tty);


//清除输入输出缓冲区
tcflush(serial_port, TCIOFLUSH);


// Save tty settings, also checking for error
if (tcsetattr(serial_port, TCSANOW, tty) != 0) {
printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
debug_output(RTK_LOCATION_ERRORMSG, NULL, 0, (char*)"Error %i from tcsetattr: %s\n", errno, strerror(errno));
close(serial_port);
return -1;
}


return serial_port;
}

linux系统串口数据接收

void
*thread_basedata(void* arg) { int i, ret; // Allocate memory for read buffer, set size according to your needs unsigned char read_buf [DATA_LEN]; // Normally you wouldn't do this memset() call, but since we will just receive // ASCII data for this example, we'll set everything to 0 so we can // call printf() easily. memset(&read_buf, '\0', sizeof(read_buf)); // Read bytes. The behaviour of read() (e.g. does it block?, // how long does it block for?) depends on the configuration // settings above, specifically VMIN and VTIME int num_bytes = 0; int epfd, nfds; struct epoll_event event; struct epoll_event* events; events=calloc(10, sizeof(event)); epfd = epoll_create(10); // 创建epoll实例 event.data.fd = uart3; // 添加标准输入到epoll event.events = EPOLLIN | EPOLLET; epoll_ctl(epfd, EPOLL_CTL_ADD, uart3, &event); for(;;) { ret = epoll_wait(epfd, events, 10, -1);// -1 :wait until it happen for(i=0; i<ret; i++) { if (events[i].data.fd == uart3) { num_bytes = read(events[i].data.fd, read_buf, DATA_LEN); if (num_bytes < 0) { UARTSendData((char*)"Error reading: %s\r\n", strerror(errno)); //while(1); } RTK_LOCATION_HandleRoverData(read_buf, num_bytes); WriteFIFO(&save_fifo,read_buf, num_bytes); num_bytes = 0; } } } close(uart3); }

 

posted @ 2024-09-30 10:57  妖岭  阅读(115)  评论(0)    收藏  举报