linux 串口 拼帧处理

时间:2023-03-09 00:21:22
linux 串口 拼帧处理

串口每次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 ;
}