0

我想使用 C 编程语言和 GCC 编译器通过串行端口将数据从 Arduino 微控制器读取到运行 Mac OS X 的 PC。

我的数据格式是A xxxx xxxx xxxx xxxx xxxx xxxx B,A是数据的开头,B是数据的结尾,传感器之间有4个空格(“”)。xxxx 数据在 0-1023 之间变化。

我正在尝试这段代码:

#include<stdio.h>   /* Standard input/output definitions */
#include<stdlib.h>
#include<string.h>  /* String function definitions */
#include<unistd.h>  /* UNIX standard function definitions */
#include<fcntl.h>   /* File control definitions */
#include<errno.h>   /* Error number definitions */
#include<termios.h> /* POSIX terminal control definitions */
#include<string.h> 
#include<unistd.h>

char *buf;
int fd; /* File descriptor for the port */
int i,n;
char *sensor1, *sensor2, *sensor3, *sensor4, *sensor5, *sensor6,*header, *footer;

int open_port(void)
{
    fd = open("/dev/tty.usbmodem1d11", O_RDWR | O_NOCTTY | O_NDELAY);      

if (fd == -1)     {
    perror("cannot open");
}
else 
    fcntl(fd, F_SETFL, 0);
struct termios options;
tcgetattr(fd, &options);
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag |= (CLOCAL | CREAD);
tcsetattr(fd, TCSANOW, &options);
options.c_cflag &= ~CSIZE; 
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
//    options.c_cflag |= (IXON | IXOFF | IXANY); // xon & xoff on
return (fd);
}

int main(int argc, char**argv) {
    buf=malloc(4095);
    open_port();
    free(buf);
    while(1){ 
        read(fd,buf,90);          
         printf("%s\n",buf);  
    }
    close(fd);
}

但是结果并不一致,我的意思是数据的长度不一样:

1023 1023 1023 1023 1023 乙

一种

10233 023 1023 1023 乙

1023 1023 1023 1023 1023 乙

3 023 1023 1023 乙

1023 1023 1023 1023 1023 乙

一种

10233 023 1023 1023 乙

1023 1023 1023 1023 1023 万能

有什么建议么?

4

1 回答 1

2

检查您从每个读取调用中得到的结果:

int nbytes;

while(1) { 
    nbytes = read(fd,buf,90);
    if( nbytes > 0 ) {
        buf[nbytes] = 0;
        printf( "Read %2d bytes: '%s'\n", nbytes, buf );
    }
}

您需要将结果收集到缓冲区中,并根据设备的格式规范对其进行解析。你不能只是假设每次read通话都会给你一个完整的记录。

要收集结果,您需要跟踪您已经阅读的内容 - 像这样......

int nbytes, nparsed;
int npos = 0;

while(1) { 
    nbytes = read(fd, &buf[npos], 90-npos);
    if( nbytes > 0 )
    {
        npos += nbytes;

        // Parse a line.  If successful, move remainder of line to
        // start of buffer and continue...
        nparsed = parse_line(buf, npos);
        if( nparsed > 0 ) {
            memmove( buf, &buf[npos], npos-nparsed );
            npos -= nparsed;
        }
    }
}

取决于您的应用程序。它可能比这更简单。

于 2012-11-26T02:25:48.503 回答