2016-12-26 1 views
0

Mon beagleboneblack (appelons-le "BBB") exécutez 3.8.13-bone47, et j'ai été hanté par un bug pendant 2 semaines.J'ai eu un gros problème avec BeagleboneBlack Uart

Mon problème est: quand mon BBB obtient une chaîne, mon BBB enverra la même chaîne à un autre terminal. Par exemple: Mon ordinateur portable avec un pont Uart2USB a envoyé une chaîne "asd", puis BBB aura "asd" .Mais pendant ce temps, BBB enverrait "asd" à mon ordinateur portable.Et tout le module Uart sur BBB (Uart1 , Uart2, Uart4) a fait la même chose. Par ailleurs, j'ai essayé d'éviter ce bug en utilisant deux Uart (un pour TX, un autre pour TX). Heureusement, je l'ai fait, mais je veux toujours savoir la raison et la solution.

Voici mon uart.c, uart.h et test.c:

/*====================Uart.c================*/ 
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <termios.h> 
#include <errno.h> 
#include <sys/time.h> 
#include <string.h> 
#include "Uart.h" 

#define TRUE 1 
#define FALSE -1 

int fd_uart; 

void set_speed(void) 
{ 
     int i; 
     int status; 
     int speed_arr = B38400; 

     struct termios Opt; 
     tcgetattr(fd_uart,&Opt); 

     tcflush(fd_uart, TCIOFLUSH); 

     cfsetispeed(&Opt, speed_arr); 
     cfsetospeed(&Opt, speed_arr); 

     status = tcsetattr(fd_uart, TCSANOW, &Opt); 

     if(status != 0) 
       perror("tcsetattr fd1"); 
} 

int set_Parity(void) 
{ 
     struct termios options; 
     if(tcgetattr(fd_uart,&options)!= 0) 
     { 
      perror("SetupSerial 1"); 
      return(FALSE); 
     } 

     options.c_cflag &= ~CSIZE; 

     options.c_cflag |= CS8; 

     options.c_cflag &= ~PARENB; /* Clear parity enable */ 
     options.c_iflag &= ~INPCK;  /* Enable parity checking */ 
     options.c_iflag &= ~(ICRNL|IGNCR); 
     options.c_lflag &= ~(ICANON); 

     options.c_cflag &= ~CSTOPB; 

     options.c_iflag |= INPCK; 

     options.c_cc[VTIME] = 150; // 15 seconds 
     options.c_cc[VMIN] = 0; 

     tcflush(fd_uart,TCIFLUSH); /* Update the options and do it NOW */ 

     if(tcsetattr(fd_uart,TCSANOW,&options) != 0) 
     { 
       perror("SetupSerial 3"); 
       return (FALSE); 
     } 
     return (TRUE); 
} 

void initUart(int argc, char **argv) 
{ 
     char devname_head[10] = "/dev/"; 
     char dev_name[20]; 

     if(argc < 2) 
     { 
       printf("Please input './test_uart ttyOx'\n"); 
       exit(1); 
     } else { 

       strcpy(dev_name, devname_head); 
       strcat(dev_name, argv[1]); 

       fd_uart = open(dev_name, O_RDWR); 
       if(fd_uart < 0) 
       { 
         perror("error to open /dev/ttyOx\n"); 
         exit(1); 
       } else if (fd_uart == 0) { 
         printf("Can't Open Serial Port!\n"); 
         exit(0); 
       } else { 
         //Setup 
         set_speed();     //baud:38400 
         if (set_Parity() == FALSE)  //8,1,n 
         { 
           printf("Set Parity Error\n"); 
           exit(1); 
         } 
       } 
     } 
} 

void writeUartString(char *buf) 
{ 
     if (write(fd_uart,buf,strlen(buf)) < 0) 
     { 
       printf("write error\n"); 
     } 
} 

void writeUartMsg(char *buf,int num) 
{ 
     if (write(fd_uart,buf,num) < 0) 
     { 
       printf("write error\n"); 
     } 
} 

int readUart(char *buf) 
{ 
     int num = 0, i = 0; 
     num = read(fd_uart,buf,sizeof(buf)); 
     if(num < 0){ 
       printf("read error\n"); 
     } 
     return num; 
} 

void closeUart(void) 
{ 
     close(fd_uart); 
} 
/*======================================================*/ 

/*==================Uart.h===================*/ 
#ifndef UART_H 
#define UART_H 

void set_speed(void); 
int set_Parity(void); 
void initUart(int argc, char **argv); 
void writeUartString(char *buf); 
void writeUartMsg(char *buf,int num); 
void closeUart(void); 

extern int fd_uart; 

#endif 
/*=======================================*/ 

/*================test.c=================*/ 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include "Uart.h" 

int main(int argc, char **argv) 
{ 
     int i = 0,temp = 0; 
     char buf[64] = {0}; 
     initUart(argc,argv); 
     while(1){ 
       writeUartString("waiting\n"); 
       printf("waiting\n"); 
       memset(buf,0,64); 
       temp = readUart(buf); 
       printf("readUart's return value = %d\n",temp); 
       if (temp > 0){ 
         printf("get something\n"); 
         printf("%s\n",buf); 
       } 
       sleep(1); 
     } 
     return 0; 
} 
/*====================================*/ 

Est-ce que quelqu'un appelle aussi cette situation?

Répondre

0

Vous avez laissé enableed écho avec

options.c_lflag &= ~(ICANON); 

changer, au moins, à

options.c_lflag &= ~(ICANON | ECHO); 

Avec vous reste à leur état d'origine tous les drapeaux, plutôt, sauf que ICANON.