c++arduinoeepromradio-transmission

Reading a character and integer command from radio in order to execute a function


I am trying to create a code and loop that can read a character and integer from radio. This character is a command that will represent a packet recover command. This code is being performed in Arduino. The purpose of this code is to read a character command and an integer number between 0 and 512. This integer value represents a number that corresponds to a packet. The data is stored using an EEPROM. I will comment in the code what I am trying to achieve.

Assuming some arbitrary data is already on the EEPROM.

//*******Functions to access EEPROM********//

void I2CEEPROM_Write( unsigned int address, byte data )
{
  Wire.beginTransmission(EEPROM_ID);
  Wire.write((int)highByte(address) );
  Wire.write((int)lowByte(address) );
  Wire.write(data);
  Wire.endTransmission();
  delay(5); // wait for the I2C EEPROM to complete the write cycle
}

byte I2CEEPROM_Read(unsigned int address )
{
  byte data;
  Wire.beginTransmission(EEPROM_ID);
  Wire.write((int)highByte(address) );
  Wire.write((int)lowByte(address) );
  Wire.endTransmission();
  Wire.requestFrom(EEPROM_ID,(byte)1);
  while(Wire.available() == 0) // wait for data
    ;
  data = Wire.read();
  return data;
}      

 void sendPackets(uint32_t start, uint32_t ending, char message_type)
 {
     radio.begin(1200);
     uint32_t starting=start;
     int number_of_packets=ceil(((float)ending-start)/total_size);
     uint32_t last_byte=start;
 for (uint32_t j=starting; j<=start+(data_size*i)-1; j++)
{
  if (j>ending-1)
    break;
  Serial.print("Address: ");
  Serial.println(j);
  Serial.print("Value :");
  Serial.println(I2CEEPROM_Read(j));
  last_byte=j;
}

    starting=last_byte;

   delay(100);
   }
}

  //*********Main Function******//

 void setup()
  {
 Serial.begin(9600);
   Serial.setTimeout(100); //don't wait longer than 100ms for incoming data
  }

  void loop(void)
  {
    char cmd;
    int xyz;

 if (Serial.available())
 {
cmd = Serial.read();  
// ***I don't know how the if statement will be structured inside the bracket*****//    
if (cmd == 'C', xyz)
      {
      //**what do I write here such that I can find the packet number
     relating to the integer (xyz). The integer is the packet number I am
     looking for. The C command represents that I am trying to recover a 
     missing packet.**//

      }
else if (cmd == 'S')
      {
  Serial.println("incomplete");
      }
else if (cmd == 'V')
   {
  Serial.println("wrong");
}
  else if (cmd == 'T')
{
  Serial.println("correct");
}
else
{
  Serial.println("retry");
  }
 }
}

Solution

  • If I understand you correctly, you are attempting to read in commands from Serial, and if you encounter the 'C' command, you expect an integer value (0-512) to follow. You will need to read the integer value in. Here's one way:

    cmd = Serial.read();
    if(cmd == 'C')
    {
        int packetNum = Serial.parseInt();
        // Continue on with your packetNum
    }
    ...
    

    The Serial.parseInt() function will read in the ASCII-representation of numbers from the serial stream, and then it will attempt to parse them and return them as an integer value. The integer value is what you want to work with in your code.

    But be warned: If Serial.parseInt() is unable to parse an integer value from the serial stream, or times out waiting for it, it will return the value 0. You can test for that return value and act on it accordingly, but in your case the value of 0 is also a legitimate packet number. If possible, you may want to change your allowable packet numbers to be 1-513 so you can easily handle this error condition. (There are other ways to go about it too, but this would be an easy fix).

    For more information about Serial.parseInt(), see http://arduino.cc/en/Serial/ParseInt

    Also, as an aside, instead of using a bunch of if/else if/else if branches for each possible argument, you might instead like to use a switch statement. It accomplishes the same thing, but makes the code cleaner and more elegant.

    switch(cmd)
    {
        case 'C':
            int packetNum = Serial.parseInt();
            // Continue on with your packetNum
            break;
    
        case 'S':
            Serial.println("incomplete");
            break;
    
        case 'V':
            Serial.println("wrong");
            break;
    
        case 'T':
            Serial.println("correct");
            break;
    
        default:
            Serial.println("retry");
    }
    

    Learn about switch/case here: http://arduino.cc/en/Reference/SwitchCase