//
// Warning: Not working yet !
//
// Special Wokwi settings:
// Use this in the Chrome browser.
// The Serial input is from a real serial port on the computer !
// For example the real device or a real Arduino board.
// The Serial output is redirected to the simulated Serial Monitor
// I tried to move everything to 115200, but that dit not work either.
//
// MysterySerialReceiver.ino (this sketch)
// https://wokwi.com/arduino/projects/310261647915614785
// MysterySerialSender.ino
// https://wokwi.com/arduino/projects/310250258016764481
//
//
// Receive packages of 8 byte
// 0x75, 0x60, 0x??, 0x??, 0x??, 0x??, 0x??, checksum
// The checksum is a 2's complement ?
// Set _SERIAL_ to the used serial bus.
#define _SERIAL_ Serial
byte buffer[8];
int index = 0;
void setup()
{
_SERIAL_.begin( 9600); // 9600, 8N1
}
void loop()
{
if( _SERIAL_.available() > 0)
{
buffer[index] = (byte) Serial.read();
Serial.print( buffer[index], HEX);
index++;
if( index == 8) // received eight bytes ?
{
if( buffer[0] == 0x75 && buffer[1] == 0x60 && checksum( buffer, sizeof(buffer) == 0))
{
// The data is valid
Serial.println( "New Data ");
for( int i=0; i<8; i++)
{
Serial.print( "0x");
if( buffer[i] < 0x10)
Serial.print( "0");
Serial.print( buffer[i], HEX);
Serial.print( ", ");
}
// Create the counter from the MysterySerialSender.ino sketch.
uint32_t counter;
counter = (uint32_t) buffer[5];
counter |= ((uint32_t) buffer[4]) << 8;
counter |= ((uint32_t) buffer[3]) << 16;
counter |= ((uint32_t) buffer[2]) << 24;
Serial.print( " (");
Serial.print( counter);
Serial.print( ")");
Serial.println();
index = 0; // clear the buffer, use the buffer from the beginning
}
else
{
// Shift everything one location to the left and check the data
// the next time that the loop() runs and something is received.
for( int i=0; i<7; i++)
buffer[i] = buffer[i+1];
index = 7; // there is new free spot at the end of the buffer
}
}
}
}
byte checksum( byte data[], int len)
{
byte total;
for( int i=0; i<len; i++)
{
total += data[i];
}
byte chk = (byte) (256 - (int) total);
return( chk);
}