0

我正在尝试创建一个可以从收音机读取字符和整数的代码和循环。该字符是表示数据包恢复命令的命令。此代码正在 Arduino 中执行。这段代码的目的是读取一个字符命令和一个 0 到 512 之间的整数。这个整数值代表一个数字,对应于一个数据包。数据使用 EEPROM 存储。我将在代码中评论我想要实现的目标。

假设一些任意数据已经在 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");
  }
 }
}
4

1 回答 1

1

如果我对您的理解正确,您正在尝试从 Serial 中读取命令,并且如果您遇到“C”命令,您希望后面跟着一个整数值 (0-512)。您将需要读取整数值。这是一种方法:

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

Serial.parseInt() 函数将从串行流中读取数字的 ASCII 表示形式,然后尝试解析它们并将它们作为整数值返回。整数值是您要在代码中使用的值。

但请注意:如果 Serial.parseInt() 无法从串行流中解析整数值,或者等待它超时,它将返回值 0。您可以测试该返回值并相应地对其进行操作,但是在您的情况下,值 0 也是合法的数据包编号。如果可能,您可能希望将允许的数据包编号更改为 1-513,以便轻松处理此错误情况。(还有其他方法可以解决这个问题,但这很容易解决)。

有关 Serial.parseInt() 的更多信息,请参阅http://arduino.cc/en/Serial/ParseInt

另外,顺便说一句,您可能更喜欢使用 switch 语句,而不是为每个可能的参数使用一堆 if/else if/else if 分支。它完成了同样的事情,但使代码更简洁、更优雅。

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");
}

在此处了解开关/案例:http: //arduino.cc/en/Reference/SwitchCase

于 2015-03-11T02:22:34.727 回答