背景
在很多时候需要测试的场景下,或者类似与控制的场景下,我们希望每次从键盘上输入一个字符可以立即被程序读取到而不是需要换行以后才能被程序读取。此时我们需要设置标准输入的ICANON标志。
在linux使用usart和外围硬件进行通信的时候,会有0x11, 0x0d, 0x13等特殊字符不能接收的问题,此时需要设置ICRNL、IXON等标志
标准输入测试例
test.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <linux/serial.h>
#include <termios.h>
#include <asm-generic/ioctl.h>
#include <sys/ioctl.h>
int main()
{
struct termios new_settings;
struct termios stored_settings;
tcgetattr(0,&stored_settings);
new_settings = stored_settings;
new_settings.c_lflag &= (~ICANON);
new_settings.c_cc[VTIME] = 0;
tcgetattr(0,&stored_settings);
new_settings.c_cc[VMIN] = 1;
tcsetattr(0,TCSANOW,&new_settings);
int key = 0;
int ret = -1;
while(1)
{
key = getchar();
printf("\nread a char : '%c'\n", key);
}
}
$ gcc test.c
$ ./a.out
1
read a char : '1'
2
read a char : '2'
3
read a char : '3'
5
read a char : '5'
h
read a char : 'h'
f
read a char : 'f'
t
read a char : 't'
j
read a char : 'j'
h
read a char : 'h'
s
read a char : 's'
a
read a char : 'a'
g
read a char : 'g'
h
read a char : 'h'
r
read a char : 'r'
t
read a char : 't'
h
read a char : 'h'
接收不到0x11
在打开串口之后需要对 options.c_iflag &= ~(ICRNL | IXON);
设置相关参数
参考:https://blog.csdn.net/vipchenvip/article/details/80744948
代码参考:
#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>
#include<signal.h>
#include<time.h>
#include<pthread.h>
#include<unistd.h>
#define FALSE -1
#define TRUE 0
unsigned char recBuff[3]; //接收BUFF 串口有固定BUFF可替换
int HiSerfd;
/*
*Function: Serial_Open(int fd,char* ComDevice)
*Param: fd:file descirbe handle Serial Device: /dev/ttyAMA1 /dev/ttyAMA2
*Output: Ok or Fail
*/
static int Serial_Open(char* SerDevice)
{
int fd;
/* fd = open(SerDevice, O_RDWR|O_NOCTTY|O_NDELAY); */
fd = open(SerDevice, O_RDWR|O_NOCTTY);
if (FALSE == fd)
{
printf("HiSerial Can't Open Serial HiSerDevice");
return(FALSE);
}
//恢复串口为阻塞状态
if(fcntl(fd, F_SETFL, 0) < 0)
{
printf("fcntl failed!\n");
return(FALSE);
}
else
{
printf("fcntl=%d\n",fcntl(fd, F_SETFL,0));
}
//测试是否为终端设备
if(0 == isatty(STDIN_FILENO))
{
printf("standard input is not a terminal device\n");
return(FALSE);
}
else
{
printf("isatty success!\n");
}
printf("fd->open=%d\n",fd);
return fd;
}
/*
*Function: Serial_Close(int fd)
*Param: fd:file descirbe handle
*Output: Null
*/
static void Serial_Close(int fd)
{
if(fd > 0)
close(fd);
return;
}
/*
*Function: Serial_Set(int fd,int speed,int flow_ctrl,int databits,int stopbits,int parity)
*Param1: fd: file descirbe handle
*Param2: speed: select the Serial speed.115200,19200,9600...
*Param3: flow_ctrl: if use flow control
*Param4: databits: data bit select
*Param5: stopbits: stopbits select
*Param5: parity: partiy select
*Output: Ok or Fail
*/
static int Serial_Set(int fd,int speed,int flow_ctrl,int databits,int stopbits,int parity)
{
int i;
int status;
int speed_arr[] = { B115200, B38400 ,B19200, B9600, B4800, B2400, B1200, B300};
int name_arr[] = {115200, 38400, 19200, 9600, 4800, 2400, 1200, 300};
struct termios options;
if( tcgetattr( fd,&options) != 0)
{
perror("SetupSerial 1");
return(FALSE);
}
//set buater rate
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
{
if (speed == name_arr[i])
{
cfsetispeed(&options, speed_arr[i]);
cfsetospeed(&options, speed_arr[i]);
}
}
//set control model
options.c_cflag |= CLOCAL;
options.c_cflag |= CREAD;
//set flow control
switch(flow_ctrl)
{
case 0 ://none
options.c_cflag &= ~CRTSCTS;
break;
case 1 ://use hard ware
options.c_cflag |= CRTSCTS;
break;
case 2 ://use sofware
options.c_cflag |= IXON | IXOFF | IXANY;
break;
}
//select data bit
options.c_cflag &= ~CSIZE;
switch (databits)
{
case 5 :
options.c_cflag |= CS5;
break;
case 6 :
options.c_cflag |= CS6;
break;
case 7 :
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size\n");
return (FALSE);
}
//select parity bit
switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB;
options.c_iflag &= ~INPCK;
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB);
options.c_iflag |= INPCK;
break;
case 'e':
case 'E':
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK;
break;
case 's':
case 'S':
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr,"Unsupported parity\n");
return (FALSE);
}
// set stopbit
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 raw data output
options.c_oflag &= ~OPOST;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
//options.c_lflag &= ~(ISIG | ICANON);
options.c_iflag &= ~(ICRNL | IXON); // 接收0x11, 0x0d, 0x13
//set wait time
options.c_cc[VTIME] = 1;
options.c_cc[VMIN] = 1;
tcflush(fd,TCIFLUSH);
//set the attribute to HiSerial device
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("com set error!\n");
return (FALSE);
}
return (TRUE);
}
/*
*Function: Serial_Init(int fd, int speed,int flow_ctrl,int databits,int stopbits,int parity)
*Param: ...
*Output: TRUE or FALSE
*/
static int Serial_Init(int fd, int speed,int flow_ctrl,int databits,int stopbits,int parity)
{
//设置串口数据帧格式
return Serial_Set(fd,speed,flow_ctrl,databits,stopbits,parity);
}
/*
*Function: Serial_Send(int fd, char *send_buf,int data_len)
*Param1: fd:file descirbe handle
*Param2: send_buf:Data to be send
*Param2: data_len:Data len
*Output: Data send len or FALSE
*/
static int Serial_Send(int fd, char *send_buf,int data_len)
{
int len = 0;
len = write(fd,send_buf,data_len);
if (len == data_len )
{
return len;
}
else
{
tcflush(fd,TCOFLUSH);
return FALSE;
}
}
/*
*Function: Serial_Recv(int fd, char *rcv_buf,int data_len)
*Param1: fd:file descirbe handle
*Param2: rcv_buf:receive Data
*Param2: data_len:receive Data len
*Output: Receive Data len or FALSE
*/
static int Serial_Recv(int fd, char *rcv_buf,int data_len)
{
int len,fs_sel;
fd_set fs_read;
int i=0;
struct timeval time;
FD_ZERO(&fs_read);
FD_SET(fd,&fs_read);
//select fdset
fs_sel = select(fd+1,&fs_read,NULL,NULL,/*&time*/NULL);
if(fs_sel)
{
len = read(fd,rcv_buf,data_len);
return len;
}
else
{
return FALSE;
}
}