2014-11-21 34 views
0

我試圖比較uart輸入數據(來自GPS)與'$'以檢測新套件時出現問題。我相信問題在於我如何操作charRead變量。我嘗試了一千種,但可能是因爲我的經驗不足,我還沒有弄清楚它是什麼問題。代碼編譯和數據一直到來,但是一旦我將代碼加載到beaglebone中,它會被堆疊,但它不會進入「if(charRead =='$')」。UART比較圖表。 Beaglebone

預先感謝您!

#include <stdio.h> 
#include <stdlib.h> 
#include <stddef.h> 
#include <time.h> 
#include <iostream> 
#include <termios.h> 
#include <fcntl.h> 
#include <unistd.h> 
#include <sys/types.h> 
#include <string.h> 
#include <ctype.h> 
#include <math.h> 
#include <limits.h> 
#include "Payload.h" 


#define SLOTS "/sys/devices/bone_capemgr.9/slots" 
#define CR   0x0d 
#define SPACE  0x20 
#define COMMA  0x2C 
#define MAXSIZE 100 
unsigned long time_data; 
unsigned int button = 45; 
int i,z =0, j=0, value; 
int rx_length; 


int main() 
{ 
    //uart4 configuration using termios 

    int fd; 
    //unsigned char *mess; 
    unsigned int value = 0; 

    gpio_export(button); 

    //Wait until the button is pushed 
     while (value != 1){ 
      if (z==0){ 
      printf("waiting\n");} 
        z++; 
      gpio_get_value(button, &value);} 



    //OPEN THE UART 
    //open uart4 for tx/rx, not controlling device 

     if((fd = open("/dev/ttyO4", O_RDONLY | O_NOCTTY|O_NONBLOCK)) < 0){ 
      printf("Unable to open uart4 access.\n"); 
      } 
     termios uart4; 
     cfsetospeed(&uart4, B9600); //Set the speed 

     //set attributes of uart4 
     uart4.c_iflag = 0; 
     uart4.c_oflag = 0; 
     uart4.c_lflag = 0; 
     tcsetattr(fd, TCSANOW, &uart4); 




     //----- CHECK FOR ANY RX BYTES ----- 
      // Read up to 100 characters from the port if they are there 

      unsigned char stringRead[MAXSIZE]; 
      unsigned char charRead; 


     do{ 

      if (rx_length = read(fd, (void*)charRead, MAXSIZE)>0){ 

       if (charRead =='$'){ 
       i=0; 
       stringRead[i] = charRead; //Store in the first position of the char --> $ 
       do { 
         rx_length = read(fd, (void*)charRead, MAXSIZE); //Read the next bit 
         if((charRead != '\0')) { 
         i++; 
         stringRead[i] = charRead; //write into stringRead 
          } 
       } while(charRead != 'CR'); //ASCII Carriage Return 
        stringRead[i+1] = charRead; 
        printf("%s", stringRead); 
       }} 
      if (rx_length==0){ 
       //No data 
      } 

     gpio_get_value(button, &value); 

     }while (value!=0); 

    gpio_unexport(button); 

close(fd); 
return 0; 
} 
+0

此行:if(rx_length =讀(FD,(無效*)charRead,MAXSIZE)> 0){需要另一組括號,並且指針接收存儲器需要修復,而你僅讀取單個字符如此:if((rx_length = read(fd,&charRead,1))> 0){因此比較是針對放入rx_length的值,並且有一個地方可以將字符讀入 – user3629249 2014-11-21 18:05:39

回答

0

要傳遞的charRead變量值的鑄件,而不是指向一個內存位置的功能read()預計void *

read(fd, (void*)charRead, MAXSIZE) 

您需要可以一次讀取一個字符:

read(fd, &charRead, 1) 

或更改你的閱讀邏輯最大限度地量讀取和數據處理。我還建議在訪問stringRead時添加邊界檢查。

0
// The following should handle the reading of a GPS NMEA message and display it 
// I have not run the program, but compiling it was successful 
// note: 
// 1) the handling of the 'i' variable 
// 2) the calls to reading the GPS input 
// 3) the handling of error conditions 
// 4) the simple logic flow 

#include <stdio.h> 
#include <stdlib.h> 
#include <stddef.h> 
#include <time.h> 
//#include <iostream> // this is not C++ so this line not needed 
#include <termios.h> 
#include <fcntl.h> 
#include <unistd.h> 
#include <sys/types.h> 
#include <string.h> 
#include <ctype.h> 
#include <math.h> 
#include <limits.h> 
#include "Payload.h" 


// #define SLOTS "/sys/devices/bone_capemgr.9/slots" // not used, raises compiler warning 
#define CR  (0x0D) 
// #define SPACE  (0x20) // not used, raises compiler warning 
// #define COMMA  (0x2C) // not used, raises compiler warning 
#define MAXSIZE (100) 

#define BUTTON_PORT (45) 

// unsigned long time_data; // not used, raises compiler warning 


// int j=0; // not used, raises compiler warning 
// int value = 0; // not used, raises compiler warning about variable masking 



int main() 
{ 
    int i; 
    int z = 0; // flag used to control execution flow 
    int rx_length; // return status value from read() 


    //uart4 configuration using termios 

    int fd; // file descriptor number 
    //unsigned char *mess; // not used, raises compiler warning 
    unsigned int value = 0; 

    gpio_export(BUTTON_PORT); 

    //Wait until the button is pushed 
    // burn mass CPU cycles, while waiting 
    while (0 == value) 
    { 
     if (z==0) 
     { 
      printf("waiting\n"); 
      z++; // to stop re-entry to this 'if' block 
     } 

     // suggest using nsleep() to free up CPU 
     gpio_get_value(BUTTON_PORT, &value); 
    } // end while 



    //open uart4 for rx 
    if((fd = open("/dev/ttyO4", O_RDONLY | O_NOCTTY|O_NONBLOCK)) < 0) 
    { 
     perror("open failed for /dev/tty04"); 
     exit(1); 
    } 

    // implied else, open successful 

    termios uart4; 
    cfsetospeed(&uart4, B9600); //Set the speed to match the GPS output 

    //set attributes of uart4 
    // Note: probably better to read the current termois values 
    // then modify them to the desired states 
    uart4.c_iflag = 0; 
    uart4.c_oflag = 0; 
    uart4.c_lflag = 0; 
    tcsetattr(fd, TCSANOW, &uart4); 




    //----- CHECK FOR ANY RX BYTES ----- 
    // Read up to 100 characters from the port if they are there 

    unsigned char stringRead[MAXSIZE]; // will contain a GPS NMEA message 
    unsigned char charRead; // input buffer 


    do{ 

     while(1) 
     { 
      rx_length = read(fd, &charRead, 1); 
      if(0 == rx_length) 
      { // this will execute a lot since fd set to non-blocking 
       ; // do nothing, while hogging CPU cycles 
        // suggest using nsleep() to free CPU 
      } 

      else if(0 > rx_length) 
      { 
       perror("read failed"); 
       exit(1); 
      } 

      else if (charRead =='$') 
      { 
       stringRead[0] = charRead; //Store first char of NMEA GPS message 
       i=1; // index for second char of NMEA message from GPS 
      } 

      else 
      { 
       stringRead[i] = charRead; //Store char 
       i++; // index for next char into stringRead buffer 

       if(MAXSIZE <= i) 
       { // then overrun input buffer 
        perror("read- overrun input buffer, GPS message too long"); 
        exit(2); 
       } 

       if(CR == charRead) //ASCII Carriage Return - end of message 
       { // then, got complete message 
        break; // exit read loop, so can process message 
       } 
      } // end if 
     } // end while 

     stringRead[i] = '\0'; // terminate string so it can be printed 
     printf("%s", stringRead); 

     // get button state via BUTTON_PORT(45) 
     gpio_get_value(BUTTON_PORT, &value); 

    } while (value!=0); // then read next gps message 

    gpio_unexport(BUTTON_PORT); 

    close(fd); 
    return 0; 
} 
+0

非常感謝!我試過了,但是它顯示「讀取失敗:資源暫時不可用」。我會明天再試。再次,非常感謝! – landondonovan 2014-11-21 19:46:07

+0

//注意:可能更好地閱讀當前的termois值。這是關鍵。閱讀termios值解決了問題。謝謝 – landondonovan 2015-01-12 20:17:04