The back story. I am trying to get an emc32 to send a single byte (a number) to a MEGA using ReceiveEvent on the slave. Based on the number sent, when the emc than requests the data packet using Wire.requestFrom on the master and RequestData on the slave, the MEGA will respond with the corresponding packet. From all the debug print statments it looks like both programs are flowing correctly. The slave receives the single number and responds with the correct data packet.
Problem, the received data is totally wrong.
Developed the code using a single data packet (no ReceiveEvent). Works great. I than added the ReceiveEvent to tell the slave which data packet to send and the received data packet is garbage. Below are the code sinpits.
Question, Why is sending a byte before requesting the data packet cause the received data to be scrambled?
Thanks.
// Get the data from the MEGA. First send which dataPacket to get than
// request the data.
Serial.println("Sending which packet to get");
// Start transmiting, take control of the bus
Wire.beginTransmission(8);
WhichOne=1;
Serial.print("Requesting: ");
Serial.println(WhichOne);
Wire.printf("%c", WhichOne);
error = Wire.endTransmission(true);
Serial.printf("endTransmission: %u\n", error);
// End transmiting, free the bus
Serial.println("Requesting data");
byteCount = Wire.requestFrom(8, sizeof(dataPacket1)); // request data from slave device #8
byte *ptr = (byte*) &dataPacket1;
// check if the recieved data is correct. If it is processed
if ( byteCount != sizeof(dataPacket1)) {
Serial.print( "Received bad packet. Expected " );
Serial.print(sizeof(dataPacket1));
Serial.print( " bytes but got " );
Serial.println( byteCount );
}
else {
Serial.print("Size of dataPacket: ");
Serial.println(sizeof(dataPacket1));
for ( byte i = 0; i < sizeof(dataPacket1); ++i ) {
byte c = Wire.read(); // receive a byte as character
ptr[i] = c;
Serial.print(c,HEX);
Serial.print(" - ");
Serial.println(c,DEC);
}
}
Wire.beginTransmission(8);
WhichOne=2;
Serial.print("Requesting: ");
Serial.println(WhichOne);
Wire.printf("%c", WhichOne);
error = Wire.endTransmission(true);
Serial.printf("endTransmission: %u\n", error);
Serial.print("Requesting dataPacket: ");
Serial.println(WhichOne);
byteCount = Wire.requestFrom(8, sizeof(dataPacket2)); // request data from slave device #8
byte *ptr2 = (byte*) &dataPacket2;
// check if the recieved data is correct. If it is processed
if ( byteCount != sizeof(dataPacket2)) {
Serial.print( "Received bad packet. Expected " );
Serial.print(sizeof(dataPacket2));
Serial.print( " bytes but got " );
Serial.println( byteCount );
}
else {
Serial.print("Size of dataPacket: ");
Serial.println(sizeof(dataPacket2));
for ( byte i = 0; i < sizeof(dataPacket2); ++i ) {
byte c = Wire.read(); // receive a byte as character
ptr[i] = c;
}
}
This is on the slave.
void ReceiveEvent(int howMany)
{
Serial.println("received request for datapacketno"); // while(1 < Wire.available()) // loop through all but the last
Serial.print("howMany = ");
Serial.println(howMany);
dataPacketNo = Wire.read(); // receive byte as a character
Serial.print("Data Packet Rec: ");
Serial.println(dataPacketNo); // print the character
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}
// This is the event for when a request for data comes over the I2C bus, See setup()
// *************** Event handler for I2C. ********************
void RequestData() {
if (dataPacketNo == 1){
Serial.print("received request for data: ") ;
Serial.println(dataPacketNo);
Wire.write((byte*)&dataPacket1, sizeof(dataPacket1)); //send packet
Serial.println(dataPacket1.DiscreteState1,BIN);
Serial.print(sizeof(dataPacket1));
Serial.println(" Bytes sent");
}//
else if (dataPacketNo == 2){
Serial.print("received request for data: ") ;
Serial.println(dataPacketNo);
Wire.write((byte*)&dataPacket2, sizeof(dataPacket2)); //send packet
Serial.print(sizeof(dataPacket2));
Serial.println(" Bytes sent");
}
else
{
Serial.print("Unknown dataPacketNo: ");
Serial.println(dataPacketNo);}
}