gpt4 book ai didi

c - 与 RPi 的 RS-232 通信

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

这是此原始帖子的后续问题:

RaspberryPi RS-232 trouble

我根据接受的答案对代码进行了更改,终端现在已设置为阻止和规范输入。 read() 现在按预期工作,除了第一次读取(通常是一些额外的随机符号与一些好的数据混合)之外,我从激光测距仪获得了代表的单行数据这正是我所期望看到的。

但是,我现在遇到一个问题,如果读取次数过多,激光测距仪将进入非操作状态,停止通信,并且只能通过重新启动电源来重置。仅在设备上进行 4 次读取时就会发生这种情况,而我无法进行超过 8 次读取。读取是一次全部读取还是跨越多个程序启动都没有关系。

int main(){
int uart0_filestream = -1;
int loop, i, sentBytes;
int isError, rx_length;
unsigned char rx_buffer[256];

uart0_filestream = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
fcntl(uart0_filestream, F_SETFL, 0);

if(uart0_filestream == -1){
printf("ERROR: Unable to open UART\n");
checkError(errno);
exit(1);
}

struct termios options;

isError = tcgetattr(uart0_filestream, &options);

if(isError < 0){
printf("ERROR: tcgetattr failed.\n");
checkError(errno);
exit(1);
}

//set c ontrol options and baud
options.c_cflag |= (CLOCAL | CREAD);
cfsetispeed(&options, B19200);
cfsetospeed(&options, B19200);

//set 8 data bits, 1 stop bit, no parity
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;

//set input options
options.c_iflag |= (INPCK | ISTRIP); //strip parity
options.c_iflag &= ~(IXON | IXOFF | IXANY); //turn off software flow control

//set canonical input processing
options.c_lflag |= (ICANON | ECHO | ECHOE);

tcflush(uart0_filestream, TCIFLUSH);
isError = tcsetattr(uart0_filestream, TCSANOW, &options);

if(isError < 0){
printf("ERROR: tcsetattr failed.\n");
checkError(errno);
exit(1);
}

//read() successfully returns expected data, with the exception of the first
//read. Reading more than 6-8 times, regardless of same session or restarting
//program puts LRF in non-working state which requires a power cycle to fix
for(i=0; i<4;i++ ){
rx_length = read(uart0_filestream, (void*)rx_buffer, 255);

if(rx_length < 0){
printf("ERROR: read() failed.\n");
checkError(errno);
}

if(rx_length > 0){
printf("rx_lentgh = %i:\t ", rx_length);
for(loop=0; loop<rx_length; loop++){
printf("%c", rx_buffer[loop]);
}
printf("\n");
}
}

//writing once has no affect, sending command has no affect
//looping the write, eventually 'something' gets to the LRF
//but it puts it in the same state as when reading too many
//times and have to power cycle
for(i = 0; i<8; i++){
sentBytes = write(uart0_filestream, "o\n", 2);

if(sentBytes < 2){
printf("ERROR: write() failed.\n");
checkError(errno);
}
}

close(uart0_filestream);
}

我不相信它是测距仪,因为我可以运行 minicom,它会成功地连续显示输出。

正如我的代码注释中所指出的,我也无法向激光测距仪写入任何命令。每个命令由一个字符组成,后跟一个新行。一次写入没有影响,但循环多次可能会使设备处于与读取次数过多相同的状态。

我已阅读Posix Serial Programming Guide ,并尝试了不同的标志组合,我认为这些组合可能会影响阅读或写作,但没有成功。

最佳答案

[编辑]这个主要建议让我很尴尬 - 认为字符串是“o/n”。

发送的数据量少了1。

// sentBytes = write(uart0_filestream, "o\n", 2);
// if(sentBytes < 2){
sentBytes = write(uart0_filestream, "o\n", 3);
if(sentBytes < 3){
<小时/>

次要:不清楚为什么读取缓冲区减少 1(rx_buffer 的大小为 256)。 read() 不会附加 \0,此代码也不会。代码很好地将传入数据视为字节数组。虽然在 printf("%c"... 中,如果 !isgraph(rx_buffer[loop])` 可以做一些事情。

// rx_length = read(uart0_filestream, (void*)rx_buffer, 255)
rx_length = read(uart0_filestream, (void*)rx_buffer, sizeof rx_buffer)

关于c - 与 RPi 的 RS-232 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19187418/

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