我正在尝试创建一个可以从收音机读取字符和整数的代码和循环。该字符是表示数据包恢复命令的命令。此代码正在 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");
}
}
}