linux下串口程序,接收到的数据不正常!!!!

chart_abc 2014-08-07 09:53:04
自己做得板子上的mpu9150传感器不停的向mcu发送数据,然后再通过UART传给beaglebone black,但是运行串口程序只能接收到一组数据,然后就没有数据了,复位的话,有时候会有一段时间的数据正常,有时候一点反应也没有,依旧没有数据。数据还有可能不完整。是哪里出问题了呢?
我的数据是这样的:
Start...
Open...
Reading...
res=22 buf= 0.057 -9.835 -1.216

res=1 buf=

res=1 buf=

res=1 buf=

res=1 buf=
……
res=12 buf=.888 0.043

res=1 buf=

res=22 buf= 0.063 -9.866 0.016

res=1 buf=

res=22 buf= 0.067 -9.902 0.069

res=1 buf=

res=22 buf= 0.021 -9.967 0.454

res=1 buf=

res=22 buf= 0.015 -9.859 0.360

res=1 buf=

res=22 buf= 0.022 -9.853 0.384

res=1 buf=
……
我的代码:
#include <sys/types.h>  

#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#define BAUDRATE B9600
#define UART_DEVICE "/dev/ttyO1"

#define FALSE -1
#define TRUE 0
////////////////////////////////////////////////////////////////////////////////
/**
*@brief 设置串口通信速率
*@param fd 类型 int 打开串口的文件句柄
*@param speed 类型 int 串口速度
*@return void
*/
int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300,
B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {115200, 38400, 19200, 9600, 4800, 2400, 1200, 300,
115200, 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);
}
}
}
////////////////////////////////////////////////////////////////////////////////
/**
*@brief 设置串口数据位,停止位和效验位
*@param fd 类型 int 打开的串口文件句柄
*@param databits 类型 int 数据位 取值 为 7 或者8
*@param stopbits 类型 int 停止位 取值为 1 或者2
*@param parity 类型 int 效验类型 取值为N,E,O,,S
*/
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 */
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; /* 设置超时15 seconds*/
options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 1");
return (FALSE);
}
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
options.c_oflag &= ~OPOST; /*Output*/
return (TRUE);
}
////////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{

int fd, c=0, res;

char buf[24] ;

printf("Start...\n");
fd = open(UART_DEVICE, O_RDWR);

if (fd < 0) {
perror(UART_DEVICE);
exit(1);
}

printf("Open...\n");
set_speed(fd,9600);
if (set_Parity(fd,8,1,'N') == FALSE) {
printf("Set Parity Error\n");
exit (0);
}

printf("Reading...\n");
while(1) {

res = read(fd, buf, 23) ;

if(res==0)
continue;
buf[res]=0;

printf("res=%d buf=%s\n",res,buf);


// if (buf[0] == 0x0d)
// printf("\n");

// if (buf[0] == '@') break;
}

printf("Close...\n");
close(fd);

return 0;
}
...全文
398 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
junjie9002 2014-08-10
  • 打赏
  • 举报
回复
1 在main函数中,每次read()之前,都要清空buf, memset(buf,0,sizeof buf),不仍然会导致一直往里面写,会导致溢出。 2 while(1)循环速度比较快,可能串口的数据没有那么快,因该加个延时sleep()。
wzhdnefu 2014-08-10
  • 打赏
  • 举报
回复
先用PC的串口调试工具发数据,用你写的这个程序接收,确定一下是否接收正常,如果正常则表示程序应该问题不大(不一定绝对没有问题),然后再排查其他部分。
ma100 2014-08-09
  • 打赏
  • 举报
回复
用select 试试看
zhxianbin 2014-08-07
  • 打赏
  • 举报
回复
首先测试 mcu 发送是否正确,将 MCU 连电脑,使用串口助手看

23,118

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧