UART比较图表。 Beaglebone

时间:2014-11-21 16:26:06

标签: c uart beagleboneblack

我在尝试将uart输入数据(来自GPS)与' $'进行比较时遇到问题。为了检测新的包裹。我确信问题在于我如何操作charRead变量。我尝试了一千件事,但可能是因为我缺乏经验,我还没弄清楚这是什么问题。 代码编译并且数据一直在进行,但是一旦我将代码加载到beaglebone中,它就会被堆叠,但它并没有输入" if(charRead ==' $ ')"

提前谢谢!

#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;
}

2 个答案:

答案 0 :(得分:0)

您正在传递变量值charRead的强制转换,而不是指向内存位置的指针,因为函数read()期望void *

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

您需要一次读取一个字符:

read(fd, &charRead, 1)

或者更改您的阅读逻辑以最大化读取和数据处理量。我还建议在访问stringRead时添加边界检查。

答案 1 :(得分: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;
}