串口每次read数据可能不是完整的数据,参照网上的代码,写了拼帧的代码#include <stdio.h>

#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <strings.h>
#include <sys/time.h>
#include <sys/types.h> int speed_arr[] = { B115200, B57600, B38400, B19200, B9600, B4800, B2400, B1200, B300};
int name_arr[] = { , , , , , , , , }; /**
*@brief Set Serial Port BitRate
*@param fd Type : int The File Description of Serial Port
*@param speed Type : int Serial Speed
*@return void
*/
void set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for( i=; 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 != )
{
perror("tcsetattr fd");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
} /**
*@brief Set Serial Port Databits, Stopbits and Parity.
*@param fd Type: int The File Description of Serial Port
*@param databits Type: int Databits 7 or 8
*@param stopbits Type: int Stopbits 1 or 2
*@param parity Type: int Parity Type: n,N,e,E,o,O,s,S
*/
int set_Parity(int fd, int databits, int parity, int stopbits)
{
struct termios options;
if ( tcgetattr( fd,&options) != )
{
perror("SetupSerial 1");
return(-);
}
options.c_cflag &= ~CSIZE;
switch (databits) /*Set Datebits*/
{
case :
options.c_cflag |= CS7;
break;
case :
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size\n");
return(-);
} switch (parity) /*Set Parity*/
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB); /* Odd Checking*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD; /* Even Checking*/
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(-);
} switch (stopbits) /*Set Stobits*/
{
case :
options.c_cflag &= ~CSTOPB;
break;
case :
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return(-);
}
/* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK; /*以下两句添加后发送方可以不加回车换行,但是read读取不完整*/
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
options.c_oflag &= ~OPOST; /*Output*/ //屏蔽功能: NL(换行)->CR(回车)、CR->NL、忽略输入回车
options.c_iflag &= ~(INLCR | ICRNL | IGNCR);
options.c_oflag &= ~(ONLCR | OCRNL); tcflush(fd,TCIFLUSH);
//未设置O_NONBLOCK或O_NDELAY
options.c_cc[VTIME] = ; /* Timeout in 15 seconds*/
options.c_cc[VMIN] = ; /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != )
{
perror("SetupSerial 3");
return(-);
}
return();
} void print_frame(unsigned char *buf,int size)
{
int i;
for(i = ; i < size; i++)
{
printf("0x%x ", buf[i]);
}
printf("\n");
} void getCompleteFrame(unsigned char *inBuf,int inCnt,unsigned char *outBuf,int *destCnt,int *readStatus)
{
int i;
for(i = ; i < inCnt; i++)
{
if(inBuf[i] == 0x01 && inBuf[i+] == 0x03)//header
{
outBuf[(*destCnt)++] = inBuf[i];
*readStatus = ;
continue;
}
if(*readStatus == )//body
{
outBuf[(*destCnt)++] = inBuf[i];
}
if(*destCnt == outBuf[] && *readStatus == )//tail
{
print_frame(outBuf,*destCnt);
*readStatus = ;
*destCnt = ;
memset(outBuf, -, sizeof(outBuf));
// memset(inBuf,0,sizeof(inBuf));
continue;
}
}
} int main(void)
{
unsigned char battery_msg[];
int fd;
unsigned char read_buf[] = {};
int nread = ;
fd = open("/dev/ttyS0", O_RDWR|O_NOCTTY);
if (fd > )
{
printf("Open Port Success!\n");
}
else
{
printf("Can't Open port\n");
return(-);
} set_speed(fd, );
if(set_Parity(fd, , 'N', ) == -)
{
close(fd);
return(-);
}
int i = , rc = ;
int flag = ;
fd_set rset;
struct timeval tv;
int read_status = ;
int dest_cnt = ; while () //循环读取数据
{
FD_ZERO(&rset);
FD_SET(fd, &rset); tv.tv_sec = ;
tv.tv_usec = ; rc = select(fd+, &rset, NULL, NULL, &tv); if(rc > )
{
      if(FD_ISSET(fd_485C2, &rset))
      {
memset(read_buf, , sizeof(read_buf));
usleep();
nread = read(fd, read_buf, sizeof(read_buf));
// printf("read %d data\n", nread);
if(nread > )
{
getCompleteFrame(read_buf, nread, battery_msg,&dest_cnt,&read_status);
}
      }
}
}
close(fd);
return ;
}

linux 串口 拼帧处理的更多相关文章

  1. linux串口驱动分析

    linux串口驱动分析 硬件资源及描写叙述 s3c2440A 通用异步接收器和发送器(UART)提供了三个独立的异步串行 I/O(SIO)port,每一个port都能够在中断模式或 DMA 模式下操作 ...

  2. storysnail的Linux串口编程笔记

    storysnail的Linux串口编程笔记 作者 He YiJun – storysnail<at>gmail.com 团队 ls 版权 转载请保留本声明! 本文档包含的原创代码根据Ge ...

  3. linux -- 串口调试总结

    linux 串口输出调试 在某些情况下,需要同时对两台或多台Linux主机进行管理和操作.如果手头缺少足够多的键盘和显示器,那么通过一台机器的串口对其余主机进行控制不失为一种快捷.有效的方法. 下面就 ...

  4. Linux串口c_cc[VTIME]和c_cc[VMIN]属性设置的作用

    Linux串口c_cc[VTIME]和c_cc[VMIN]属性设置的作用 在串口编程模式下,open未设置O_NONBLOCK或O_NDELAY的情况下. c_cc[VTIME]和c_cc[VMIN] ...

  5. linux串口驱动分析——发送数据

    一.应用程序中write函数到底层驱动历程 和前文提到的一样,首先先注册串口,使用uart_register_driver函数,依次分别为tty_register_driver,cdev_init函数 ...

  6. linux串口驱动分析——打开设备

    串口驱动是由tty_driver架构实现的.一个应用程序中的函数要操作硬件,首先会经过tty,级级调用之后才会到达驱动之中.本文先介绍应用程序中打开设备的open函数的整个历程. 首先在串口初始化中会 ...

  7. Linux串口编程详解(转)

    串口本身,标准和硬件 † 串口是计算机上的串行通讯的物理接口.计算机历史上,串口曾经被广泛用于连接计算机和终端设备和各种外部设备.虽然以太网接口和USB接口也是以一个串行流进行数据传送的,但是串口连接 ...

  8. linux串口编程总结

    串口本身.标准和硬件 † 串口是计算机上的串行通讯的物理接口.计算机历史上,串口以前被广泛用于连接计算机和终端设备和各种外部设备.尽管以太网接口和USB接口也是以一个串行流进行数据传送的.可是串口连接 ...

  9. Chain TDNN/LSTM的拼帧索引、延时

    TDNN模型示例 TDNN拼帧: 层:(0,3) 层:(-9,0) 层:(0,3) 层:(-6,0) 层:(0,3) 层:(-3,0) 层:(0,3) 层:(-3,0)     输出依赖 帧,各层需要 ...

随机推荐

  1. 从QC到QA

    QC遇到了什么无法逾越的障碍 我们公司的主要业务是项目外包,一般的项目都在2-3个月的周期,采用瀑布模式.这种模式本身是相对简单,且十分成熟的模式.但是在实际的工作中,我们还是遇到了前所未有的挑战. ...

  2. POJ-1087 A Plug for UNIX (网络流)

    思路 电器数1 ~ 100,附带100种接口,注意题目:You notice that some of the devices use plugs for which there is no rece ...

  3. ubuntu资料

    1.VNC实现Windows远程访问Ubuntu 16.04(无需安装第三方桌面,直接使用自带远程工具) https://www.cnblogs.com/xuliangxing/p/7642650.h ...

  4. AP2800无法放出SSID?

    实际的无线网络中,有时候由于对无线设备的datasheet不是很了解,可能会以旧的知识去判断一些故障.在思科的较新的AP型号中:例如AP2800&AP3800等,有时候发现它们可正常的注册到W ...

  5. ASA密码恢复流程

    1.建立console连接2.重启启动安全设备 3.进入ROMMMON模式出现Use BREAK or ESC to interrupt boot字样时,按下ESC键进入ROMMON模式.4.设置RO ...

  6. 验证码 倒计时 vue 操作对象

    //html <input type="number" v-model="phoneNumber" placeholder="请输入手机号&qu ...

  7. mysql事务隔离分析

    首先说明下,这里主要内容为整理总结网络搜索的零散信息. 写在最前面,mysql事务是在Innodb引擎中得以实现的,如果这点不了解的话,请自行了解. 事务直接数据的可见性通过MVCC(多版本并发控制) ...

  8. Mac旧机「焕」新机过程记录

    一.首先我做了非硬件上的优化处理,在升级到10.14之前还是挺管用的.但是为了使用最新的iOS SDK,升级到10.14以后,已经不管用了. 1.设置->通用 将动画相关的选项去掉. 2.设置- ...

  9. scrapy 中 shell 出现 403 Forbiidden 解决方案

    版权声明本文来自:https://blog.csdn.net/qq_37462361/article/details/87860025 进入正题: 出现 403,表示网站拒绝提供服务 (因为很多网站都 ...

  10. (踩过的坑)使用Github Page搭建个人博客

    最近需要搭建一个网站,作为导航网址,但是自己的域名备案还要等上几天,就想着有没有别的办法来搭建一个公网可以访问的网站. Github Page的话是一个github个人主页,完全适合用来搭建普通网站. ...