树莓派串口通信可以发数据,但不能读数据

在树莓派上写了一个串口程序,现在的问题是:树莓派可以向Pc机发数据,但pc机通过串口助手发给树莓派数据,树莓派接收不到,树莓派串口程序如下:

  1 #include<stdio.h>                                                           
  2 #include<stdlib.h>
  3 #include<string.h>
  4 #include<unistd.h>
  5 #include<errno.h>
  6 #include<fcntl.h>
  7 #include<termios.h>
  8 #include<iostream>
  9 
 10 using namespace std;
 11 int main()
 12 {
 13 
 14     int tty_fd;
 15     int ret;
 16     char r_buf[548]={0};
 17     char s_buf[548]={0};
 18     struct termios options;
 19     tty_fd=open("/dev/ttyAMA0",O_RDWR|O_NOCTTY);
 20     if(tty_fd<0){
 21         printf("open tty_fd failed:%s\n",strerror(errno));  
 22         return -1;
 23     }
 24     printf("open tty_fd sucessful!\n");
 25     memset(&options,0,sizeof(options));
 26     ret=tcgetattr(tty_fd,&options);
 27     if(ret!=0){
 28         printf("tcgetattr failed:%s\n",strerror(errno));
 29         close(tty_fd);
 30         return -1;
 31     }
 32 
 33     options.c_cflag|=(CLOCAL|CREAD);
 34     options.c_cflag&=~CSIZE;
 35     options.c_cflag|=CS8;
 36     options.c_cflag&=~PARENB;
 37    
 38     cfsetispeed(&options,B9600);
 39     cfsetospeed(&options,B9600);
 40 
 41     options.c_cflag&=~CSTOPB;
 42     options.c_cc[VTIME]=0;
 43     options.c_cc[VMIN]=0;
 44     tcflush(tty_fd,TCIFLUSH);
 45 
 46     if((tcsetattr(tty_fd,TCSANOW,&options))!=0){
 47         printf("tcsetattr failed:%s\n",strerror(errno));
 48         close(tty_fd);
 49         return -1;
 50     }
 51     
 52     while(1){
 53         ret=read(tty_fd,r_buf,sizeof(r_buf));
 54         //ret=write(tty_fd,"hello world",strlen("hello world"));
 55         printf("length:%d\n",ret);  
 56         if(ret>0){
 57             printf("recive data:%x\n",r_buf[0]);    
 58         }
 59         sleep(1);
 60     
 61     }
 62 
 63 
 64 
 65     close(tty_fd);
 66     return 0;
 67 }                                                                                         

但是在树莓派上使用cutecom串口助手是可以正常接收数据的

在pc机上发送数据,串口助手显示的是这样的:有一次发送的数据32,还有两次莫名奇妙收到的数据

 

你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答

本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。

​​​​因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。