【发布时间】:2013-10-03 13:22:40
【问题描述】:
我正在使用我的 Pi 上的 RS-232 线路与激光测距仪进行通信。我已经使用minicom以19200的波特率测试了两者之间的通信(因为这是LRF的波特率并且无法更改),并且工作正常。尽管向 LRF 写下任何命令(由单个字符组成并按“回车”)可能需要多次尝试才能生效,但双向通信效果很好。
但是,当我开始用 C 代码编程以使用 RS-232 进行读写时,它只工作了一半。这是我正在使用的代码:
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
//SETUP UART0
int main(){
int uart0_filestream = -1;
int loop;
int i;
int isError=1, rx_length;
unsigned char rx_buffer[256];
useconds_t micro=3000;
uart0_filestream = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
if(uart0_filestream == -1)
printf("ERROR: Unable to open UART\n\n");
else
printf("UART open\n\n");
struct termios options;
tcgetattr(uart0_filestream, &options);
options.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
options.c_iflag = IGNPAR | ICRNL;
options.c_oflag = 0;
options.c_lflag = 0;
tcflush(uart0_filestream, TCIFLUSH);
tcsetattr(uart0_filestream, TCSANOW, &options);
unsigned char tx_buffer[20];
unsigned char *p_tx_buffer;
p_tx_buffer = &tx_buffer[0];
*p_tx_buffer++ = 'o';
*p_tx_buffer++ = '\n';
/*
if(uart0_filestream != -1){
for(i = 0; i<100; i++){
int count = write(uart0_filestream, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0]));
if(count < 0)
printf("\n\nERROR: No bytes written\n\n");
else
printf("%i bytes written: %s\n", (p_tx_buffer - &tx_buffer[0]), tx_buffer);
}
}
*/
if(uart0_filestream != -1){
for(i=0; i<50; ){
rx_length = read(uart0_filestream, (void*)rx_buffer, 255);
if(rx_length > 0){
printf("rx_lentgh = %i:\t ", rx_length);
for(loop=0; loop<30; loop++){
//check for NULL and new line for easier readability
if(rx_buffer[loop] == NULL)
rx_buffer[loop] = '$';
if(rx_buffer[loop] == '\n')
rx_buffer[loop] = '%';
printf("%c", rx_buffer[loop]);
}
printf("\n");
i++;
}
}
}
close(uart0_filestream);
}
当我尝试从设备读取数据时,它总是返回错误。我开始循环查看是否不断阅读会产生不同的结果。在 100 次尝试中,通常是 4-5 次返回数据,其余所有 [i]rx_length[/i] 都返回 -1。返回的数据应如下所示:
计数:0000
其中数字取决于 LRF 测量的距离。但相反,我得到这样的输出:
rx_lentgh = 16: jRþ$COUNTS:0000%$$$$$$$$$$$$
rx_lentgh = 8: %$COUNTSTS:0000%$$$$$$$$$$$$
rx_lentgh = 16: :0142%%$COUNTS:0$$$$$$$$$$$$
rx_lentgh = 8: 000%%$COCOUNTS:0$$$$$$$$$$$$
rx_lentgh = 16: UNTS:0142%%$COUN$$$$$$$$$$$$
rx_lentgh = 24: TS:0142%%$COUNTS:0000%%$$$$$
rx_lentgh = 8:计数:0%$COUNTS:0000%%$$$$$
rx_lentgh = 16: 142%%$COUNTS:000:0000%%$$$$$
rx_lentgh = 16: 0%%$COUNTS:0142%:0000%%$$$$$
rx_lentgh = 8: %$COUNTSTS:0142%:0000%%$$$$$
rx_lentgh = 8: :0000%%$TS:0142%:0000%%$$$$$
rx_lentgh = 8: 计数:0TS:0142%:0000%%$$$$$
rx_lentgh = 24: 142%%$COUNTS:0142%%$COUN$$$$
rx_lentgh = 8: TS:0000%UNTS:0142%%$COUN$$$$
rx_lentgh = 16: %$COUNTS:0000%%$2%%$COUN$$$$
rx_lentgh = 8:计数:0:0000%%$2%%$COUN$$$$
rx_lentgh = 16: 142%%$COUNTS:0002%%$COUN$$$$
rx_lentgh = 8: 0%%$COUNT:0002%%$COUN$$$$
rx_lentgh = 16: TS:0142%%$COUNTS2%%$COUN$$$$
rx_lentgh = 8: :0000%%$%$COUNTS2%%$COUN$$$$
rx_lentgh = 16:计数:0142%%$CO2%%$COUN$$$$
rx_lentgh = 8: UNTS:000142%%$CO2%%$COUN$$$$
rx_lentgh = 24: 0%%$COUNTS:0142%%$COUNTS$$$$
rx_lentgh = 16: :0000%%$COUNTS:0%$COUNTS$$$$
rx_lentgh = 24: 142%%$COUNTS:0142%%$COUN$$$$
rx_lentgh = 8: TS:0000%UNTS:0142%%$COUN$$$$
rx_lentgh = 16: %$COUNTS:0142%%$2%%$COUN$$$$
**The above is edited in my code for readability. A NULL character is replaced with '$' and a '\n' is replaced with '%'
您可以看到,每次它获取数据时,它至少会获得部分好读,有时甚至是整件事。但是里面有很多垃圾。您可以在我的代码中看到,我过滤掉了所有返回错误的读取。可能需要超过 1000 次读取才能获得这么多“好”读取。我真的认为这与时间有关,但即使是时间,我不应该仍然得到 [i]some[/i] 数据吗?
写作也有同样的问题。一次写入什么也不做。循环写入代码 100 次可能最终会将代码下载到 LRF,但在运行该代码后 LRF 几乎根本不起作用,我没有切断电源让它工作并在 minicom 中查看数据再次。
LRF 可以以 200Hz 或 10Hz 的频率发送数据包,具体取决于模式。上面检索到的所有数据都是通过 LRF 以 200Hz 发送数据包完成的。
任何帮助都将不胜感激!在我的其他课程和工作之间,我已经为此工作了几个星期。
【问题讨论】:
-
在开始怀疑软件之前,您检查过物理连接吗?可能是某个地方的连接不好,从而扰乱了您的通信。
-
嗯,我一直使用终端应用程序 minicom 进行良好的沟通。我可以运行 minicom,查看良好的数据,运行我的好程序并获得垃圾。然后再次运行 minicom 并获得良好的数据。所以我真的不认为这是联系。
标签: c serial-port hardware raspberry-pi