Converting some serial data

Hello

I have a circuit that reads anything sent from one Arduino to another.
Any text sent to the serial port of the first IC, is read by the second Arduino over IR.

So any serial.print statement on the first Arduino, will result in the following code reading that transmitted data and displaying it in the the serial port of the second receiving Arduino...

For example: I might send

Serial.print(123);
Serial.print(23);
Serial.print(221);
Serial.print(28);

This then decodes that...

Serial.println(F("Awaiting IR commands"));

do {

 int n, i, ch ;
 
 n = IRSerial.available() ;
 if (n > 0) {
       i = n ;
   while (i--) {
     ch = IRSerial.read() ;
     Serial.print((char)ch) ;
   }
 }
   
} while (Reading>0);

This basically lists the text out identically on the receiving serial screen.

My question is how do I change this receiving code to convert these received chars into variables again?

I have played around and worked out that a received 48 is a zero, a received 49 is therefore a 1 etc.
Also, a received 13 is a carriage return, and a 10 is a received line feed (which I think means end of line).

Obviously, I think I need to use the 13 (carriage return) to determine the end of each piece of data.
But, everything I have tried fails.

Any assistance would be great

I have played around and worked out that a received 48 is a zero, a received 49 is therefore a 1 etc.
Also, a received 13 is a carriage return, and a 10 is a received line feed (which I think means end of line).

Welcome to the wonderful world of ASCII values

Have a look at Serial input basics - updated

@OP

1. Assume that you have executed the following codes at the Sender side:

Serial.print('<');  //Start Mark of the Total Message
Serial.print(123);   //1st item of message
Serial.print(',');      //, is item separator
Serial.print(23);     //2nd item of message
Serial.print(',')
Serial.print(221);
Serial.print(',')
Serial.print(28);
Serial.print('>')       //End Mark of message

2. Now, execute the following codes at the Receiver side.

Serial.println(F("Awaiting IR commands"));
int x1, x2, x3, x4; //to hold received 123, 23, 221, and 28
char myData[50] = "";

void loop()
{
  byte n = IRSerial.available();
  IRSerial.readBytesUntil('>', myData, 50); //looping code
  Serial.println(myData); //shows:<123,23,221,28  ( > is excluded)
  //----retrive 123-----
  myData[0] = 0x30;   //ASCII code of 0
  myData[4] = 0x00; //null byte
  int x1 = atoi(myData); // x1 = 123
  Serial.println(x1, DEC);  //shows: 123
  //----retrive 23, and the rest..............
}
/*
do
{
  int n, i, ch ;
  n = IRSerial.available() ;
  if (n > 0)
  {
    i = n ;
    while (i--)
    {
      ch = IRSerial.read() ;
      Serial.print((char)ch) ;
    }
  }
}
while (Reading > 0);
*/

3. Hope that above codes would be helpful if I have understood your post.

Thank you for taking the time to reply.

The only difference is, I don't have the '>' start and end markers. It's taking standard text from the debug screen.

Yes... I know the whole code depends on those markers!

I will play around with it, and see if I can adapt it to get what I need.
I am wondering if I can go with the fact that the data is always 3 digits, and use the '13' (carriage return) as an identifier to signal the end of that data.
It can still verify the data by the first 2 and last 2 data contents matching a known set of variables.

Thanks. I will have a play

Arduino_Steve:
The only difference is, I don't have the '>' start and end markers. It's taking standard text from the debug screen.

Telling us what you don't have is not very useful. Tell us what you DO have.

For example does each message end with a linefeed character? If so that is what the second example in Serial Input Basics is intended for.

If you are using a different end-marker then it should be straightforward to change the code to match.

...R

Arduino_Steve:
The only difference is, I don’t have the ‘>’ start and end markers. It’s taking standard text from the debug screen.

1. We may engage a little amount of time to study the following points why @Robin2 has cleverly designed his UART Transmission Frame as: <ASCIICodedDat1, data2, data3, …, datan> where the commas (,) are very important as inter data separators in addition to StartMark (<) and StopMark (>).

2. Let us take this specific example: <123,23,221,28>

3. If we wish to send the above string from the InputBox of Serial Monitor, we place the string in the InputBox and click on the Send Button. As a result, 15 UART Frames (one after another) go down from Serial Monitor to the UNO. At 9600 Bd, each frame (10-bit = 1-StartBit + 8-bit ASCII code for each character + 1-StopBit) takes about 1041 us to travel from Serial Monitor to Arduino.

4. At the UNO Receiver side, all these charcaters could be caught and stored in a ‘NULL-byte’ terminated character type array (say, char myData[50]) by executing the following codes:

void loop()
{
   byte n = Serial.available();
   if(n !=0)
   {
       Serial.readBytesUntil('>', myData, 50);  //incoming characters are taken until > is encountered or ..
       Serial.println(myData);                        //shows: <123,23,221,28    (> is not included in the array)
   }
}

5. Let us retrieve 123 from the array. How?
(1) Replace < of the array location myData[0] by the ASCII code of 0 (zero), which is 0x30. Replace first comma (,) by the ASCII Code of NULL-byte, which is 0x00. After that, let us execute the atoi(myData) instruction to get 123. Now look: if there would not be a comma over there, where would we get an empty space to insert a NULL-byte which is needed by the atoi() function? The codes are:

myData[0] - 0x30;
myData[4] = 0x00;
int x1 = atoi(myData);   //atoi() function takes myData[0], ..., myData[4] 
Serial.println(x1); //shows: 123

(2) Follow the above trick to get the next integer – 23.

OK. I have changed my transmission code to be identical to as GolamMostafa suggests (with < > and commas).

The received code is identical (copy/pasted) from the same post.
It’s reading the entire string correctly and printing the string correctly.

But its only serial printing a single digit for the first answer. Not sure why yet

Arduino_Steve:
But its only serial printing a single digit for the first answer. Not sure why yet

Post your entire program with code tags (</>). We will show the defect that you have in your program.

I changed the x1 to my variable, and made it a byte.. the value will never be higher than 255 (and to save code space). Doesn't fix the issue if I change it to an int anyway.

Just for your info, the whole string prints as:

15,255,1,1,1,0,0,0,2,3,1,2,11,1,2,1,0,0,0,18,254

First Serial.print does print the entire string correctly as above.

However, the second serial.print command only prints '5' ?
There are more statements to add to the retrieving loop, but I thought I would get it to retrieve the 15 first.
The last correct retrieval will also then exit the loop.

Serial.println(F("Awaiting IR commands"));

byte Start1=0;

char myData[150] = "";

 while (!IRSerial.available()>0){}                                                    //Wait for first IR data
 
   byte n = IRSerial.available();
   IRSerial.readBytesUntil('>', myData, 150);                                   //Get all the data between the identifiers

   Serial.println(myData);  

                    
 do { 
   
   myData[0] = 0x30;                         //ASCII code of 0
   myData[4] = 0x00;                         //null byte

   Start1 = atoi(myData);Serial.println(Start1, DEC);
 
   
 } while (All_IR==0);

Please, re-submit your sketch; there is no setup() and loop() function.

I have solved the issue. All working now.
Many thanks for your assistance and time