您的位置:首页 > 编程语言

MT7688 串口调整每次接收数据的字节数

2016-12-05 14:22 267 查看
零:MT7688 串口编程  最初使用的串口只能每次接收八个字节
  多余八个字节之后就分成多次
进行发送,之前更改过一次后来更改每次能接收64字节 ,时间一长当初怎么改的忘记了,现在我的需求有所增加,每次得接收至少150字节,索性仔细看了看之前的程序,然后做下记录。

1 、串口编程的介绍

我的串口驱动是原厂自带的  所以修改的主要是应用程序对串口的配置。 

打开/关闭 串口

int fd;
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{

perror("open_port: Unable to open /dev/ttyS0 -");
}

close(fd);

2、设置波特率等等一些参数

termios结构体成员


成员描述
c_cflag控制选项
c_lflag行选项
c_iflag输入选项
c_oflag输出选项
c_cc控制字符
c_ispeed输入波特率(NEW)
c_ospeed输出波特率(NEW)
至于termios的各个参数的选项 不在赘述。我的配置如下

struct termios opt;

if (tcgetattr(fd, &opt) != 0) {
perror("set parity");
return FALSE;
}
opt.c_cflag &= ~CSIZE;
switch (databits) {
case 5:
opt.c_cflag |=CS5;
break;
case 6:
opt.c_cflag |=CS6;
break;
case 7:
opt.c_cflag |= CS7;
break;
case 8:
opt.c_cflag |= CS8;
break;
default:
fprintf(stderr, "Unsupported data size\n");
return FALSE;
}
switch (parity) {
case 'n':
case 'N':
opt.c_cflag &= ~PARENB;
opt.c_iflag &= ~INPCK;
break;
case 'o':
case 'O':
opt.c_cflag |= (PARODD | PARENB);
opt.c_iflag |= INPCK;
break;
case 'e':
case 'E':
opt.c_cflag |= PARENB;
opt.c_cflag &= ~PARODD;
opt.c_iflag |= INPCK;
break;
case 's':
case 'S':
opt.c_cflag &= ~PARENB;
opt.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr, "Unsupported parity\n");
return FALSE;
}

switch (stopbits) {
case 0:
case 1:
opt.c_cflag &= ~CSTOPB;
break;
case 2:
opt.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr, "Unsupported stop bits\n");
return FALSE;
}

if (parity != 'n' || parity != 'N')
opt.c_iflag |= INPCK;

   

opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
opt.c_oflag &= ~OPOST;
opt.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
 
opt.c_cc[VTIME] = 1;//150; //15 seconds 150x100ms = 15s
opt.c_cc[VMIN] = 150;//60;
   
tcflush(fd, TCIFLUSH); //update the options and do it now
if (tcsetattr(fd, TCSANOW, &opt) != 0) {
perror("Setup Ser ial");
return FALSE;
}

termios 的c_cflag初始化很简单理解,控制每次传输字节数的配置主要在termios的c_cc【】这个选项中。

成员变量c_cc中的控制字符

常量描述
VINTRInterruptCTRL-C
VQUITQuitCTRL-Z
VERASEEraseBackspace (BS)
VKILLKill-lineCTRL-U
VEOFEnd-of-fileCTRL-D
VEOLEnd-of-lineCarriage return (CR)
VEOL2Second end-of-lineLine feed (LF)
VMINMinimum number of characters to read-
VSTARTStart flowCTRL-Q (XON)
VSTOPStop flowCTRL-S (XOFF)
VTIMETime to wait for data (tenths of seconds)-
我的配置是
opt.c_cc[VTIME] = 1;//150; //15 seconds 150x100ms = 15s  每次读取等待时间
         opt.c_cc[VMIN] = 150;//60; 每次读取的字节数目
所以  主要是更改这个地方
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  编程 串口