9.1设置规范模式
规范模式是面向行的输入方式,输入字符被放入用于和用户交互可以编辑的缓冲区内,直接到读入回车或者换行符号时才结束。
可以通过如下方式来设置
option.c_lflag |= (ICANON | ECHO | ECHOE);
9.2设置原始输入模式
原始输入模式是没有处理过的,当接收数据时,输入的字符在它们被接收后立即被传送,使用原始输入模式时候,一般可以选择取消ICANON,ECHO,ECHOE和ISIG选项。
例如:
option.c_lflag &= ~(ICANON | ECHO | ECHOE);
=========================================================================
总结:上面的问题应该比较清楚了,就是在输入模式的设置上出了问题,默认的可能是行缓冲的,导致在接收串口的时候,串口会自行把数据存到缓冲里面,只有接收到回车、换行等结束标志后,才把数据一起送到内存,这样串口的read函数才能读到数据,也就会触发相应的标志(SIGIO)。所以,需要在对串口设置的时候,要设置为原始输入模式。
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<termios.h>
#include<errno.h>
#include<string.h>
#define FALSE -1
#define TRUE 0
int bit_recv=0;
int uart_fd;
int speed_arr[] = { B57600, B38400, B19200, B9600, B4800, B2400, B1200, B300,B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {57600,38400, 19200, 9600, 4800, 2400, 1200, 300, 38400, 19200, 9600, 4800, 2400, 1200, 300, };
void 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[i]) {
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0) {
perror("tcsetattr fd1");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
}
int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
if ( tcgetattr( fd,&options) != 0) {
perror("SetupSerial 1");
return(FALSE);
}
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 (FALSE);
}
switch (parity)
{
case 'n':
case 'N':
// options.c_cflag &= ~PARENB; /* Clear parity enable */
// options.c_iflag &= ~INPCK; /* Enable parity checking */
options.c_lflag &= ~(ICANON|ECHO|ISIG); /* Clear parity enable */
options.c_oflag &= ~OPOST; /* Enable parity checking */
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB);
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'S':
case 's': /*as no parity*/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;break;
default:
fprintf(stderr,"Unsupported parity\n");
return (FALSE);
}
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return (FALSE);
}
/* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK;
tcflush(fd,TCIFLUSH);
// options.c_cc[VTIME] = 150;
// options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}
int main(int argc, char *argv[])
{
int ret;
unsigned char Flag,ReadBuf[15];
/*鎵撳紑骞跺垵濮嬪寲涓插彛*/
uart_fd = open("/dev/ttyUSB4",O_RDWR);
if(uart_fd<0)
{
printf("error\n");
}
set_speed(uart_fd,57600);
if (set_Parity(uart_fd,8,1,'N') == FALSE) {
printf("Set Parity Error\n");
exit (0);
}
tcflush(uart_fd, TCIFLUSH) ;
SendDspCmd(Get_Image,uart_fd);
while(1)