本文介绍了读串口数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Mac OS中的C串行端口数据读取。串口的配置是在单独的Arduino IDE完成。

我能够读取部分数据,但随后的印刷数据重复本身,如下图所示开始读取零。如果我删除O_NONBLOCK,程序只是挂起。我能做些什么来解决这个问题?其次,由于我在数据正在读在for循环中,如何确保读取速率与波特率对应?

看数据:
296 310 0

320 295 311

320 295 311

9 296 311

320 295 311

320 295 311

9 296 311

...

0 0 0

0 0 0

0 0 0
等等。

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&;&fcntl.h GT;INT缓冲器[300];INT主(INT ARGC,为const char * argv的[])
{    //打开串口
    INT口;
    端口=打开(/开发/ tty.usbmodem1411,O_RDONLY | O_NONBLOCK);
    如果(端口== -1)
    {
        的printf(无法打开串口\\ n);
        返回1;
    }    //实例文件
    FILE *文件= fdopen(端口,R);
    如果(文件== NULL)
    {
        的printf(无法实例化档案\\ n);
        关闭(端口);
        返回2;
    }    对于(INT J = 0; J< 200; J ++)
    {        的fscanf(文件,%D%d个,&放大器;缓冲液[3 * I],&放大器;缓冲液[3 * I + 1],&放大器;缓冲液[3 * I + 2]);
        的printf(%d个%D \\ n,缓冲[3 * I],缓冲[3 * I + 1],缓冲[3 * I + 2]);        I =(I + 1)%100;        usleep(10000);    }    FCLOSE(文件);
    关闭(端口);    返回0;}


解决方案

有关克里斯斯特拉顿:

 如果(与fgets(温度,sizeof的(临时)文件)!= NULL)
{
     sscanf的(温度,%D%d个\\ n,&安培;缓冲区[3 * I],和放大器;缓冲区[3 * I + 1],&安培;缓冲区[3 * I + 2]);
     的printf(%d个%D \\ n,缓冲[3 * I],缓冲[3 * I + 1],缓冲[3 * I + 2]);
}

不过,回到我原来的问题,即使我使用的fscanf和printf,我获取数据,然后我只看过零。我想这可能是更多的是串口通讯问题?

I am trying to read in serial port data in C on Mac OS. Configuration of serial port is done in separate Arduino IDE.

I am able to read partial data, but then the printed data repeats itself and starts reading zeros as seen below. If I remove O_NONBLOCK, the program just hangs. What can I do to fix this problem? Secondly, given that I am reading in data in a for loop, how do I make sure the read rate corresponds with the baud rate?

Seen data:296 310 0

320 295 311

320 295 311

9 296 311

320 295 311

320 295 311

9 296 311

...

0 0 0

0 0 0

0 0 0etc.

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

int buffer[300];

int main(int argc, const char* argv[])
{

    // open serial port
    int port;
    port = open("/dev/tty.usbmodem1411", O_RDONLY | O_NONBLOCK);
    if (port == -1)
    {
        printf("Unable to open serial port.\n");
        return 1;
    }

    // instantiate file
    FILE* file = fdopen(port, "r");
    if (file == NULL)
    {
        printf("Unable to instantiate file.\n");
        close(port);
        return 2;
    }

    for (int j = 0; j < 200; j++)
    {

        fscanf(file, "%d %d %d", &buffer[3*i], &buffer[3*i+1], &buffer[3*i+2]);
        printf("%d %d %d\n", buffer[3*i], buffer[3*i+1], buffer[3*i+2]);

        i = (i + 1) % 100;

        usleep(10000);

    }

    fclose(file);
    close(port);

    return 0;

}
解决方案

For Chris Stratton:

if (fgets(temp, sizeof(temp), file) != NULL)
{
     sscanf(temp, "%d %d %d\n", &buffer[3*i], &buffer[3*i+1], &buffer[3*i+2]);
     printf("%d %d %d\n", buffer[3*i], buffer[3*i+1], buffer[3*i+2]);
}

However, going back to my original question, even if I'm using fscanf and printf, I obtain data, and then I only read zeros indefinitely. I'm thinking this might be more of a serial port communication issue?

这篇关于读串口数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 20:17