- 论坛徽章:
- 0
|
以前做个一个类似的项目,LED,LCD,KEYBOARD,GPIO,RS232
1,丢数据的可能原因:
一,线程调度有问题, ...
消失的地平線 发表于 2010-03-11 15:03 ![]()
觉得你的分析很有可能。我也一直在找这类原因,但因为刚接触线程,对线程的了解还很浅,所以一直没有头绪!!
另外贴一下我的串口初始化代码:
#define SERIAL1 "/dev/ttyAT1" //CONNECT TO MCU FOR PS2 KEYBOARD
#define SERIAL2 "/dev/ttyAT2"
#define SERIAL3 "/dev/ttyAT3"
static int speed_arr[] = {B230400, B115200, B57600, B38400, B19200, B9600, B4800, B2400, B1800, B1200, B600, B300};
static int name_arr[] = {230400, 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1800, 1200, 600, 300};
int set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
{
if (speed == name_arr)
{
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr);
cfsetospeed(&Opt, speed_arr);
status = tcsetattr(fd, TCSANOW, &Opt);
/*if (status != 0)
{
perror("tcsetattr fd");
return -1;
}*/
tcflush(fd,TCIOFLUSH);
return 0;
}
}
return -1;
}
int set_other_attribute(int fd, int databits, int stopbits, int parity)
{
struct termios options;
if (tcgetattr(fd, &options) != 0)
{
perror("SetupSerial 1");
return -1;
}
options.c_cflag &= ~CSIZE;
switch (databits)
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size\n");
return -1;
}
switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB;
options.c_iflag &= ~INPCK;
break;
case 'o':
case 'O':
case 1:
options.c_cflag |= (PARODD | PARENB);
options.c_iflag |= INPCK;
break;
case 'e':
case 'E':
case 2:
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK;
break;
case 'S':
case 's':
case 0:
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr,"Unsupported parity\n");
return -1;
}
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return -1;
}
options.c_cflag &= ~CRTSCTS;
if (parity != 'n')
options.c_iflag |= INPCK;
tcflush(fd,TCIFLUSH);
options.c_oflag = 0;
options.c_lflag = 0;
options.c_cc[VTIME] = 150;
options.c_cc[VMIN] = 0;
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 3");
return -1;
}
#if 0
tcgetattr(fd, &options);
printf("c_iflag: %x\rc_oflag: %x\n", options.c_iflag, options.c_oflag);
printf("c_cflag: %x\nc_lflag: %x\n", options.c_cflag, options.c_lflag);
printf("c_line: %x\nc_cc[VTIME]: %d\nc_cc[VMIN]: %d\n", options.c_line, options.c_cc[VTIME], options.c_cc[VMIN]);
#endif
return 0;
} |
|