Trouble rading serial, converting to the right data and process it.

Hi everyone,

I'll try to explain my problem as thorough as possible and maybe I could get your help on some subjects.

So here's my code

int Mavail = 0;
int MreceiveByte = 0;
byte Mbyte[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
byte Mbytes;
byte Mbytess;

void setup() 
{ 
  Serial.begin(57600);           
  Serial1.begin(57600);             
} 


void loop() 
{ 
  if (Serial.available())
    {
      Serial1.write(Serial.read());
    }  
  if (Serial1.available() && Mavail==0 )
    { 
    
      ReadM(); 
    }
   else
  {
    
   Mavail = 0;
   Serial1.flush(); 
         
       
    }  
}  

void ReadM() 
{ 
 
      MreceiveByte = Serial1.read();    //read 1-st byte  
      if (MreceiveByte==97) 
      { 
        delay(1);
        Mbyte[1]=255;   //header
        Mbyte[2]=85;
        Mbyte[3]=23;
        Mbyte[4]=4; 
        Mbyte[5]=0;
        Mbyte[6]=33;
        Mbyte[7]=MreceiveByte; 
    
    
        for (int i=8; i <= 15; i++)
          {   
            Mbyte[i] = Serial1.read();    //read  8-15 bytes of data
            delay(5); 
            
            
          //Mbytes = Mbyte[3] + Mbyte[4] + Mbyte[5] + Mbyte[6] + Mbyte[7] + Mbyte[8] + Mbyte[9] + Mbyte[10] + Mbyte[11] + Mbyte[12] + Mbyte[13] + Mbyte[14] + Mbyte[15];  //summ of bytes to receive checksum

           }  

          
      SerialPrintKey();
        Mavail=1; 
      } 

} 

void SerialPrintKey()  //send command to serial port


{

           for (int x=1; x <= 15; x++){   
           Serial.print(Mbyte[x], HEX); //send command
           Serial.print(" ");
    }
    
           Mbytes = Mbyte[3] + Mbyte[4] + Mbyte[5] + Mbyte[6] + Mbyte[7] + Mbyte[8] + Mbyte[9] + Mbyte[10] + Mbyte[11] + Mbyte[12] + Mbyte[13] + Mbyte[14] + Mbyte[15]; //dirty way of byte summ
          if (Mbytes >= 256)  Mbytess = Mbytes - 512;
          if (Mbytes >= 512)  Mbytess = Mbytes - 768;
          if (Mbytes >= 768)  Mbytess = Mbytes - 1024;
          if (Mbytes >= 1024) Mbytess = Mbytes - 1280; 
         /* Mbyte[16] = (Mbytess * -1);
          */
          
           Serial.print(Mbyte[16], HEX);  //send checksum
           Serial.print(" ");

}

What I'm trying to achieve is to read serial1 (ASCII characters are being sent to Serial1) then process them as bytes, compose a message of 16 bytes so to send to Serial in HEX format.

As you can see first 7 bytes are already known (FF 55 1E 04 00 23) actually I converted them from HEX to DEC as arduino receives the next 8-15 bytes as DEC. After all bytes are received I need to calculate checksum and put it in byte 16.

For example arduino receives ASCII text: abcdefghi, converts?? it to DEC and puts this message in byte array. The problem is that it's not always 9 bytes it should receive, message can be: abc or adcd so message length is unknown. Also if I put message in array I should specify byte array length and if message shorter than 9 bytes last bytes show the number of 255 which is totally unnecessary. Also what I tried is to receive this message in String while calling String.length operator I can see message length but to process String as separate chars is a bit of pain. Of course it would be helpful if one could read serial buffer in different variables let's say message to String and to byte array. But it's not possible in Arduino, right?

Ok another problem is with checksum: Is it possible to store Ascii info in variable as HEX data and then calculate it in the right way?

Hopefully someone could give me pointers to proceed and give me some advice I'll be very grateful.

Thanks in advance.

Hex and decimal are just how we see data. The Arduino sees it all as binary.

Have a look at the examples in serial input basics. It includes a parse example which converts (for example) the ASCII "123" into the byte value 123.

...R

Hi Robin2,

I do understand that and thanks for this link it has some useful information.

Anyways what is most important to me is the solution to this problem:

For example arduino receives ASCII text: "abcdefghi" or "abcd" puts this message in byte array. The problem is that length would be always different but to put it in byte array I need to specify the length myself. How to determine the length of message in buffer before reading it to variables?

sammybrown:
For example arduino receives ASCII text: "abcdefghi" or "abcd" puts this message in byte array. The problem is that length would be always different but to put it in byte array I need to specify the length myself.

The code in serial input basics adds a '\0' character after the received data. That means the char array is a string and the normal string functions work. It think you check the length with strlen().

There is no need to know the length of the string as it is arriving - assuming there is a terminating character such as a linefeed.

...R

yes I already tried with String function as well but what I'm trying to achieve is a little bit trickier than it seems.

Let me say this way: Arduino receives a Message that consists of characters like "abcdefg......." with some terminating character like ">", it's all good but the length of this Message is unknown it can be 3 characters or 5,6,10 etc. it receives it within 5 or 10 minutes interval. This is the most important part as I don't want to store it in String because it's a complex routine to parse it to different chars or bytes. What interests me more is to store this Message into byte arrays to work with it's much more convenient at least for me but I need to know the length before I specify the correct size of byte array.

Ok I have an idea how it would be possible to do it though I'm missing some directions. Could it be possible to read this Message from buffer into String first then call String.length command and store it's value in variable (int = x, for example), after it's done initialize For int = x ++ and store Message from buffer to byte arrays with the right array size according to variable x? I know that buffer is erased after it has been read I tried a peek function but what it does it let's only one character to peek for that's not enough...

sammybrown:
Let me say this way: Arduino receives a Message that consists of characters like "abcdefg......." with some terminating character like ">", it's all good but the length of this Message is unknown it can be 3 characters or 5,6,10 etc. it receives it within 5 or 10 minutes interval.

I'm not sure I understand this.

Are you saying that several characters arrive (maybe as few as 3 or as many as 10) and that they arrive slowly spread over a 10 minute period between the first character and the terminating character.

OR

Are you saying that the characters arrive all at once, but perhaps with 10 minutes between different groups.

I had assumed you meant the latter and the code in serial input basics can handle that very easily.

I don't know why you think that parsing will present a problem.

...R