与RPi进行RS-232通信

时间:2013-10-04 17:34:30

标签: c linux serial-port hardware raspberry-pi

这是对原始帖子的跟进问题:

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,尝试了不同的旗帜组合,我认为这些旗帜可能会影响阅读或写作,但却没有成功。

1 个答案:

答案 0 :(得分:0)

[编辑]这个引导建议是我的尴尬 - 认为字符串是“o / n”。

发送的数据量为1。

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

轻微:不清楚为什么读取缓冲区减少1(sizeof rx_buffer为256)。 read()不会附加\0,也不会附加此代码。 Code很好地将传入数据视为一个字节数组。虽然在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)