简体   繁体   English

如何将从串行端口读取的字符存储到缓冲区中进行处理?

[英]How to store chars read from serial port into buffer for processing?

Right now I have some code that is printing out data read from a serial port with putch(out) . 现在,我有一些代码可以打印出通过putch(out)从串行端口读取的数据。 But I need to store it into an array and process it to get floating point values out. 但是我需要将其存储到数组中并对其进行处理以获取浮点值。

Here is my code just for talking to the serial port, with the calculations omitted: 这是我的代码,仅用于与串行端口通信,而省略了计算:

#include <bios.h>
#include <conio.h>

#define COM1       0
#define DATA_READY 0x100

// set the baud rate, parity bit, data width...
#define SETTINGS ( 0xE0 | 0x03 | 0x00 | 0x00)

int main(void)
{
    int in, out, status,i;
    char dataRead[21];
    float roll, pitch;
    bioscom(0, SETTINGS, COM1); /*initialize the port*/
    clrscr();
    cprintf("Data sent to you:  ");    

    while (1) 
    { 
        status = bioscom(3, 0, COM1); // reading the data here
        if (status & DATA_READY)
            if ((out = bioscom(2, 0, COM1) & 0 7F) != 0) 
                putch(out);                // printing read value

        if (kbhit( ))    // If Esc is hit. it breaks and exit.
        { 
            if ((in = getch( )) == ‘ 1B’)   //  1B = Esc
            DONE = TRUE; 
            bioscom(1, in, COM1);  // data write. I am not making use of it.
        } 
    }
    return 0; 
}   

The incoming data encodes the roll and pitch, as something like "R:+XXX.XX P:-YYY.YY\\r\\n" . 输入的数据将滚动和倾斜编码为"R:+XXX.XX P:-YYY.YY\\r\\n"

Instead of just printing this data out, I want to store it in the dataRead[] array and interpret it into float values. 我想将其存储在dataRead[]数组中,并将其解释为浮点值,而不是仅打印此数据。 For instance, dataRead[2] to dataRead[8] encodes a float value for the "roll" as characters +XXX.XX . 例如, dataRead[2]dataRead[8]将“ roll”的浮点值编码为字符+XXX.XX

How do I store these characters in the array and get the floating point number from it? 如何将这些字符存储在数组中并从中获取浮点数?

If you could write down some code for serial port which does exactly what i want then it would be really helpful. 如果您可以为串行端口写下一些符合我想要的代码,那将非常有帮助。 Please make sure it is written in 'C'. 请确保将其写为“ C”。

I am not familiar with Dev C++ but in C++ you have the strtod function which converts string to double (float). 我对Dev C ++不熟悉,但是在C ++中,您具有将字符串转换为double(浮点型)的strtod函数。 You need to: 你需要:

  1. collect all characters from the serial port in one text line 在一个文本行中从串行端口收集所有字符

  2. parse the line 解析线

First is easy, just wait until reach "\\n". 首先很容易,只要等到到达“ \\ n”。 Something like: 就像是:

 char line [MAX_LINE];
 if(out == '\n')
   parse_line(line. ....)
 else
   line[n_readed++] = out;

The second part is more tricky. 第二部分比较棘手。 You can use some text processing library for parsing or write your own parse function using strtod . 您可以使用一些文本处理库进行解析,也可以使用strtod编写自己的解析函数。 Since your case is simple I would rather do the second. 由于您的情况很简单,因此我宁愿做第二个。 Here is an example of the parse function which reads your text lines: 这是解析函数的示例,该函数读取您的文本行:

 const char* line = "R:+123.56P:-767.77\r\n";

 // return true on success
 bool parse_line(const char* line, double* R, double* P)
 {
    const char* p = line;

    if(*p++ != 'R')
      return false;

    if(*p++ != ':')
      return false;

    if(*p == '+') // + sign is optional
      p++ ;

    char* pEnd;
    *R = strtod (p, &pEnd);

    if(pEnd == p)
      return false;

    p = pEnd;

    if(*p++ != 'P')
      return false;

    if(*p++ != ':')
      return false;

    *P = strtod (p, &pEnd);

    if(pEnd == p)
      return false;

    return true;
}

int main()
{
  double R,P;
  if(!parse_line(line, &R, &P))
     printf("error\n");
  else
     printf("sucessfully readed: %f\t%f\n", R,P);

  return 0;
}

You need to declare two float arrays and to fill them with the parsed float values when the parsing function returns true. 您需要声明两个float数组,并在解析函数返回true时用解析的float值填充它们。 Otherwise there have been some error, probably because of damaged data. 否则,可能是由于数据损坏而导致的一些错误。 Also you can change all doubles with floats. 您也可以使用浮点数更改所有双打。

I hope this to help you. 希望对您有所帮助。

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

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