C UART比较图表。小猎犬骨

C UART比较图表。小猎犬骨,c,uart,beagleboneblack,C,Uart,Beagleboneblack,我在尝试将uart输入数据(来自GPS)与“$”进行比较以检测新包时遇到问题。我确信问题在于如何操作charRead变量。我尝试了一千种方法,但可能是因为我缺乏经验,我还没有找到问题所在。 代码一直在编译,数据一直在传输,但一旦我将代码加载到beaglebone中,它就会被堆叠起来,但它不会进入“if(charRead=='$')”中 提前谢谢你 #include <stdio.h> #include <stdlib.h> #include <stddef.h>

我在尝试将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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“有效载荷.h”
#定义插槽“/sys/devices/bone\u capemgr.9/SLOTS”
#定义CR0x0d
#定义空间0x20
#定义逗号0x2C
#定义最大尺寸100
无符号长时间_数据;
无符号整数按钮=45;
int i,z=0,j=0,值;
int rx_长度;
int main()
{
//使用termios的uart4配置
int-fd;
//无符号字符*mess;
无符号整数值=0;
gpio_导出(按钮);
//等待按钮按下
while(值!=1){
如果(z==0){
printf(“等待\n”);}
z++;
gpio_获取_值(按钮和值);}
//打开UART
//打开用于发送/接收的uart4,不控制设备
如果((fd=open(“/dev/ttyO4”,O|RDONLY | O|NOCTTY | O|unblock))<0){
printf(“无法打开uart4访问。\n”);
}
termios uart4;
cfsetospeed(&uart4,B9600);//设置速度
//设置uart4的属性
uart4.c_iflag=0;
uart4.c_of lag=0;
uart4.c_lflag=0;
tcsetattr(fd、TCSANOW和uart4);
//-----检查是否有任何接收字节-----
//从端口读取最多100个字符(如果有)
无符号字符stringRead[MAXSIZE];
无符号字符读取;
做{
如果(rx_长度=读取(fd,(void*)字符读取,最大大小)>0){
如果(charRead=='$'){
i=0;
stringRead[i]=charRead;//存储在字符的第一个位置-->$
做{
rx_length=read(fd,(void*)charRead,MAXSIZE);//读取下一位
如果((charRead!='\0')){
i++;
stringRead[i]=charRead;//写入stringRead
}
}while(charRead!=“CR”);//ASCII回车
stringRead[i+1]=字符读取;
printf(“%s”,stringRead);
}}
如果(rx_长度==0){
//没有数据
}
gpio_获取_值(按钮和值);
}while(值!=0);
gpio_未输出(按钮);
关闭(fd);
返回0;
}

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

read(fd, (void*)charRead, MAXSIZE)
您需要一次读取一个字符:

read(fd, &charRead, 1)
或者更改您的读取逻辑以最大化读取量和数据处理。我还建议在访问
stringRead

//时添加一个边界检查。下面应该处理GPS NMEA消息的读取并显示它
// 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;
}
//我没有运行这个程序,但编译成功了 //注: //1)“i”变量的处理 //2)读取GPS输入的调用 //3)错误情况的处理 //4)简单的逻辑流程 #包括 #包括 #包括 #包括 //^包含/ /这不是C++,所以这行不需要 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括“有效载荷.h” //#定义插槽“/sys/devices/bone_capemgr.9/SLOTS”//未使用,引发编译器警告 #定义CR(0x0D) //#定义空间(0x20)//未使用,引发编译器警告 //#定义逗号(0x2C)//未使用,引发编译器警告 #定义最大尺寸(100) #定义按钮_端口(45) //无符号长时间_数据;//未使用,将引发编译器警告 //int j=0;//未使用,将引发编译器警告 //int值=0;//未使用,将引发有关变量屏蔽的编译器警告 int main() { int i; int z=0;//用于控制执行流的标志 int rx_length;//从read()返回状态值 //使用termios的uart4配置 int fd;//文件描述符编号 //unsigned char*mess;//未使用,引发编译器警告 无符号整数值=0; gpio_导出(按钮_端口); //等待按钮按下 //在等待时燃烧大量CPU周期 while(0==值) { 如果(z==0) { printf(“等待”); z++;//停止重新进入此“if”块 } //建议使用nsleep()释放CPU gpio_获取_值(按钮_端口和值); }//结束时 //打开uart4进行接收 如果((fd=open(“/dev/ttyO4”,O|RDONLY | O|NOCTTY | O|unblock))<0) { perror(“打开/dev/tty04失败”); 出口(1); } //否则,打开成功 termios uart4; cfsetospeed(&uart4,B9600);//设置速度以匹配GPS输出 //设置uart4的属性
//注:最好读取当前termois值 //然后将它们修改为所需的状态 uart4.c_iflag=0; uart4.c_of lag=0; uart4.c_lflag=0; tcsetattr(fd、TCSANOW和uart4); //-----检查是否有任何接收字节----- //从端口读取最多100个字符(如果有) unsigned char stringRead[MAXSIZE];//将包含GPS NMEA消息 unsigned char charRead;//输入缓冲区 做{ 而(1) { rx_长度=读取(fd和charRead,1); 如果(0==rx_长度) {//由于fd设置为非阻塞,这将执行很多操作 ;//什么也不做,占用CPU周期 //建议使用nsleep()释放CPU } 如果(0>rx_len