作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在尝试将 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/
我正在尝试每 10 秒使用一次周期性任务来读取 ADC 引脚 ADC0 至 ADC5。为此,我使用 read() 读取 4 个字节。读取的值可以在 0 - 4095 之间变化(理论上)。然而,我似乎并
我正在尝试每 10 秒使用一次周期性任务来读取 ADC 引脚 ADC0 至 ADC5。为此,我使用 read() 读取 4 个字节。读取的值可以在 0 - 4095 之间变化(理论上)。然而,我似乎并
我是一名优秀的程序员,十分优秀!