Reading serial and extract digits

Hi,
Im struggling with a problem, and hope someone can see what im doing wrong.
I have a unit sending out when reading with hyperterminal "$DF158,33,Q4,D"
And i try to read this and sort out the position digits "158" and resend as "%158", the 158 can be anything from 001 to 360, depending on input. My code so far is as below, but my output is really not good, just garbage, not as i wanted %XXX.
Hope anybody out here can se what im doing wrong.
Br
Patrik

void setup()
{
mySerial.begin(4800); //out
Serial.begin(4800); // in
}

void loop() {

//mySerial.print("%");

i = 0;
while (mySerial.available() > 0)
{
char charData = (char)mySerial.read(); //Read the buffer
data[i] = charData; //Fill data into array
i++;

Serial.print("%");
Serial.print(data[3]);
Serial.print(data[4]);
Serial.print(data[5]);
Serial.write( '\r' );

delay(10);
}

1 Like

Start by posting your code in a better way and posting all of it

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

1 Like

Making the input using hardware is better.

But you defined it in your mind correctly and at the time of execution you did the opposite.

Also software serial depends on the CPU being interrupt free.

Not Clear!

1. You have a UNO with which a HyperTerminal is connected via software UART Port named -- mySerial(SRX, STX). Is this correct? if so, what are the numerical values for SRX pin and STX pin?

2. You are receiving this string from HyperTerminal: $DF158,33,Q4,D. Is it correct?

3. Now. you want to catch 3-digit after $DF and then you want to send this string to Hyper Terminal: %158. Is it correct?

Is the heading always THREE digits?
If not, there’s more work to do.

1 Like

I understand it is an unknown device, which was reverse engineered using Hyperteminal:

1 Like

if the input is terminated with a known character such as a linefeed, Serial.readBytesUntil() can be used to read a complete line and sscanf() used to extract the numeric value from with the string

consider

char buf [80];

void loop (void)
{
    if (Serial.available())  {
        int n = Serial.readBytesUntil('\n', buf, sizeof(buf));
        buf [n] = '\0';

        int val;
        sscanf (buf, "$DF%d,", &val);

        Serial.print   ("%");
        Serial.println (val);
    }
}

// -----------------------------------------------------------------------------
void setup (void)
{
    Serial.begin (9600);
}

Hi,
Thanks for the answer, the Myserial is pin 2 and 3, the "$DF158,33,Q4,D" is the data from hyperterminal, i connected laptop to the unit to see what type of data that will come out and will be sent to the ARDUINO. So basically, what i try to do is to take this "$DF158,33,Q4,D" and extract the three digits after the "DF" letters, add a "%" and send it out again on another serial. But all i get is garbage on the output.

It is quite easy task. You should first found the combination of chars "$DF" and read number just after it.

The code may be something like:

char df_string[] = "$DF";
uint8_t index =0;
char data[4];
while (mySerial.available() > 0)
 {
   char charData = (char)mySerial.read(); 
   
   // search "$DF" in input data
   if (index <3) {
    if  (charData == df_string[index]) {index++;}
    else {index =0;}
    }
   
    // DF found, extract the number
    else if (index <6) 
       {
         if (charData >= '0' && charData <= '9') {
          data[index -3] = charData;
          index++;
          }
          else {index =0;}
          } 
          
     // number extracted, send it
    else if (index == 6)  {
          Serial.print("%");
          Serial.print(data[0]);
          Serial.print(data[1]);
          Serial.print(data[2]);
          Serial.write( '\r' );
          index =0;    // reset the counter, ready to next turn
          }
          
   }

Can you please, show the connection diagram between your unknown device and the PC?

Hi,

Here is a simple drawing,

Best regards

Patrik

Is your DF Unit connected with UNO? Can you see the string $DF158,33,Q4,D on the Serial Monitor of UNO? If yes, please post your sketch that you have uploaded/executed.

Note: If DF Unit is a RS232 device, then you cannot connect it directly with UNO risking your UNO to be fried. You need a RS232/TTL converter.

what are "pin1" and "pin2"?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.