平常测试串口通信程序,接受数据是否正常。open 打开的设备需要改成自己的驱动的/dev/ttyS0/1/2/3/4, 将/dev/ttySx 权限改成777测试。避免不必要的麻烦
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h> // Contains POSIX terminal control definitions
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
int g_fpUart = 0;
int main(int argc, char * argv[])
{
int fd;
int r;
char read_byte;
struct pollfd fds[1];
g_fpUart = open("/dev/ttyS1", O_RDWR | O_NOCTTY );
if(g_fpUart<0)
{
return EOF;
}
struct termios tty;
tcgetattr(g_fpUart, &tty);
// In/Out no flow ctrl
tty.c_iflag = 0;
tty.c_oflag = 0;
tty.c_lflag = 0;
tty.c_line = 0;
// config: 115200,8,N,1
cfsetispeed(&tty, B115200);
cfsetospeed(&tty, B115200);
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_cflag &= ~(HUPCL); // disable 'hang up on last close'
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 0;
if (tcsetattr(g_fpUart, TCSANOW, &tty) != 0)
{
printf("Error from tcsetattr\n");
}
fds[0].fd = (int)g_fpUart;
fds[0].events = POLLIN | POLLRDNORM;
while(1)
{
r = poll(fds, 1, -1);
if (r <= 0)
{
printf("get uart errrot.....\n");
return -1;
}
while(read(fd,&read_byte,1))
{
printf("get 0x%x\n", read_byte);
}
}
return 0;
}