简体   繁体   English

从串口读取

[英]read from serial port

This is the code given by http://www.gravitech.us/7segmentshield.html . 这是http://www.gravitech.us/7segmentshield.html给出的代码。

void SerialMonitorPrint (byte Temperature_H, int Decimal, bool IsPositive)
{
Serial.print("The temperature is ");
if (!IsPositive)
{
  Serial.print("-");
}
Serial.print(Temperature_H, DEC);
Serial.print(".");
Serial.print(Decimal, DEC);
Serial.print(" degree C");
Serial.print("\n\n");
}

But when I try to read data from serial port, I found that I read data character by character. 但是,当我尝试从串行端口读取数据时,发现逐个字符地读取数据。

UPDATE 更新

while(1)
{
    char buffer[100];
    int chars_read = read(fd, &buffer, sizeof(buffer));
    buffer[chars_read] = '\0';
    printf("%s", buffer);
}

So how can I read line by line? 那么如何逐行阅读?

You can't guarantee that a single call to read will give you exactly one line of text. 你不能保证一个调用read会给你的文字恰好一行。 You need to buffer the data. 您需要缓冲数据。 The easiest way to do this is to read exactly one character at a time, and stop when you reach a newline character. 最简单的方法是一次只读取一个字符,并在到达换行符时停止。 If you want to read as many characters as possible each time, the buffering code becomes more complicated. 如果您想每次读取尽可能多的字符,则缓冲代码将变得更加复杂。

Try this to start: 试试这个开始:

char buffer[100] = {0};
int pos = 0;

while( pos < 99 ) {
    read(fd, buffer+pos, 1);           // Note you should be checking the result
    if( buffer[pos] == '\n' ) break;
    pos++;
}

// Normally you would null-terminate string, but noticed I initialised the
// buffer to all zeroes at the beginning.  So this is not strictly necessary.
// However, in this case it will remove the newline character.
buffer[pos] = 0;

The while loop does not necessarily read character by character, but it might return you one character at a time for each read based on the serial port device and the rate of transmission. while循环不一定要逐个字符地读取字符,但是根据串行端口设备和传输速率,每次读取可能一次返回一个字符。

I've made some changes to fix few bugs in your while loop: 我进行了一些更改以修复while循环中的一些错误:

while(1)
{
    char buffer[100];
    ssize_t length = read(fd, &buffer, sizeof(buffer));
    if (length == -1)
    {
        printf("Error reading from serial port\n");
        break;
    }
    else if (length == 0)
    {
        printf("No more data\n");
        break;
    }
    else
    {
        buffer[length] = '\0'
        printf("%s", buffer);
    }
}

List of changes : 变更清单

  • Check the return value from read 检查read的返回值
  • I'm assuming when read fails or returns 0, it means no more data to read and breaks the while loop execution. 我假设当读取失败或返回0时,这意味着没有更多数据可读取并中断了while循环执行。 Modify this behavior as per your needs. 根据您的需要修改此行为。
  • Append a '\\0' character before printing, otherwise printf would be printing garbage values in the buffer. 在打印之前添加'\\0'字符,否则printf将在缓冲区中打印垃圾值。

Comments: 评论:

  • Do not worry about lines, the read should return a \\n character in the buffer which printf would interpret as a newline when you print it out. 不用担心行,读取应该在缓冲区中返回\\n字符,当您将其打印出来时, printf会将其解释为换行符。
  • If you're actually only interested in grabbing a line and storing it somewhere, you need to read and append to another buffer until you get a \\n in the buffer, and also need to handle multiple \\n within the same buffer implying multiple lines. 如果您实际上只想抓取一行并将其存储在某处,则需要读取并追加到另一个缓冲区,直到在缓冲区中获得\\n为止,并且还需要在同一缓冲区中处理多个\\n ,这意味着多行。

Probably you have to implement that by yourself. 可能您必须自己实施。 Just keep reading character by character until you reached '\\n', and return what you have read. 只需逐个字符地阅读,直到到达“ \\ n”,然后返回已阅读的内容即可。

I didn't check, but I suspect that's how 'readline' is usually implemented. 我没有检查,但是我怀疑通常是这样实施“ readline”的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM