简体   繁体   中英

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) . 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" .

Instead of just printing this data out, I want to store it in the dataRead[] array and interpret it into float values. For instance, dataRead[2] to dataRead[8] encodes a float value for the "roll" as characters +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'.

I am not familiar with Dev C++ but in C++ you have the strtod function which converts string to double (float). 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". 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 . 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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