0

I have declared a struct with some variables that right now are fixed, but one of the variables on the struct needs to be what the Arduino's serial port reads.

struct testing_var 
{
    const char *name;
    byte key[32];
    byte pt[16];
};

static testing_var const test1 = {
    .name        = "Speck-128-ECB",
    .key         = {0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
                    0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00},
    .pt   = {0x6c, 0x61, 0x76, 0x69, 0x75, 0x71, 0x65, 0x20,
                    0x74, 0x69, 0x20, 0x65, 0x64, 0x61, 0x6d, 0x20},
};

As you can see for how I have declared it, I'm trying to get a plaintext from the serial port and the Arduino should encrypt that. I have a function that does this encryption, but I can't quite figure out how to pass what the serial port reads to the struct.

void loop() { 

  if (Serial.available()>0) {
    var1 = Serial.read();
}

I need to pass this var1 as the variable pt in the struct. Any clues? I'm maybe missing something very obvious, but I don't usually work with structs.

EDIT: I'm trying to assign the value read by the serial port to the struct by creating another function that goes like this:

void setPlain(struct PlainText *myPlain, const char* thePlain){
    myPlain -> plaintext = the Plain;
   }

Then, in the loop, I use:

  if (Serial.available()>0) {
    var1 = Serial.read();
    setPlain(&PlainText, var1)
}

I'm getting an error using this function, I guess that it has something to do with the type of variable I'm trying to assign, these values are in HEX, not in char. But I'm not really sure it that's it or what other workaround should I use.

1
  • You need to design and implement a protocol to encapsulate the data.
    – Majenko
    Aug 29, 2017 at 17:37

1 Answer 1

1

You read the serial port one char at time, and assign it to test1.pt[i], where i is an index varying from 0 to 15.

And test1 cannot be const, because you are going to modify it with the serial data.

Here is a test code showing how to do it.

struct testing_var 
{
    const char *name;
    byte key[32];
    byte pt[16];
};

//  test1 cannot be 'const'
static testing_var test1 = {
    .name        = "Speck-128-ECB",
    .key         = {0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
                    0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00},
    .pt   = {0x6c, 0x61, 0x76, 0x69, 0x75, 0x71, 0x65, 0x20,
                    0x74, 0x69, 0x20, 0x65, 0x64, 0x61, 0x6d, 0x20},
};

void setup() {
  Serial.begin(9600);
  while(!Serial);
}

void loop() {
  Serial.println("Enter 16 chars:");
  //  Read 16 chars from Serial
  for (int i=0; i < 16;) {
    if (Serial.available()) {
      test1.pt[i++] = Serial.read();
    }
  }
  // Do something with the data, like print it.
  for (int i = 0; i < 16; i++) {
    Serial.print(char((test1.pt[i])));
  }
  Serial.println();
}
1
  • Note to LuzA, the reason LookAlterno's example limits the plaintext to 16 chars is because your declaration, by providing 16 bytes of data initially, only reserved 16 bytes of memory. Aug 29, 2017 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.