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.