gpt4 book ai didi

c - UART 比较图表。比格尔骨

转载 作者:行者123 更新时间:2023-11-30 17:25:30 25 4
gpt4 key购买 nike

我在尝试将 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;
}

最佳答案

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

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

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

read(fd, &charRead, 1)

或者改变你的阅读逻辑,以最大限度地提高阅读量和数据处理量。我还建议在访问 stringRead 时添加边界检查。

关于c - UART 比较图表。比格尔骨,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27065930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com