top of page

ARDUINO side

​

char startB      = 60;                          //required starting byte for messages
char endB        = 62;                          //required ending byte for messages
char stateB      = 64;                          //byte for outgoing messages to indicate outcome of message checking
char parameterError = 63;               //byte for outgoing message if no parameter was found
char responseB   = 36;                      //byte for outgoing messages to indicate the response value

int inByte       = 0;                           //used to check single bytes
char parameter   = 0;                    //parameter message refers to (a-z allowed)
int value        = 0;                            //new value for parameter
int check        = 0;                           //check parameter (0 = no message, 1 = correct message, 2+ = error found)

void setup() {
  // initialize serial:
  Serial.begin(19200);
}

void loop() {

                           
  //Checking a single <x99> message.
  check = 0;                                      //clear check state (no message received yet)
  while (Serial.available() > 0) {
    parameter = 0; value = 0;                     //resetting parameters
    
    inByte = Serial.read();                       //read a byte
    if (inByte != 60) {                           //check if the start character is found
      check = 2;                                  //do not break unless a message has been started ("<")
    }                                             //checking stops when "<" has been found OR queue is empty
    else {
      check = 1;                                  //message has started correctly
      parameter = Serial.read();                  //read the relevant parameter to change
      if (parameter+0 < 96 || parameter+0 > 122) {//check if  the parameter is between a-z
        check = 3;
        break; }
      
      value = Serial.parseInt();                  //read the value to give to the parameter
      
      inByte = Serial.read();                     //read a byte
      if (inByte != 62) {                         //check if an end character is found
        check = 5;
      }
      break;
    }
  }

  //Do something with the message:
  if (check > 1 && check < 3) {
    parameter = parameterError;
  }
 
  if (check > 0) {
    Serial.print(startB); Serial.print(parameter); Serial.print(value); Serial.print(responseB); Serial.print(123); Serial.print(stateB); Serial.print(check); Serial.print(endB);
    //Serial.println("");
  }
 
  delay(10);
}

RASPBERRY side

​

char startB      = 60;                          //required starting byte for messages
char endB        = 62;                          //required ending byte for messages
char stateB      = 64;                          //byte for outgoing messages to indicate outcome of message checking
char parameterError = 63;               //byte for outgoing message if no parameter was found
char responseB   = 36;                      //byte for outgoing messages to indicate the response value

int inByte       = 0;                           //used to check single bytes
char parameter   = 0;                    //parameter message refers to (a-z allowed)
int value        = 0;                            //new value for parameter
int check        = 0;                           //check parameter (0 = no message, 1 = correct message, 2+ = error found)

void setup() {
  // initialize serial:
  Serial.begin(19200);
}

void loop() {

                           
  //Checking a single <x99> message.
  check = 0;                                      //clear check state (no message received yet)
  while (Serial.available() > 0) {
    parameter = 0; value = 0;                     //resetting parameters
    
    inByte = Serial.read();                       //read a byte
    if (inByte != 60) {                           //check if the start character is found
      check = 2;                                  //do not break unless a message has been started ("<")
    }                                             //checking stops when "<" has been found OR queue is empty
    else {
      check = 1;                                  //message has started correctly
      parameter = Serial.read();                  //read the relevant parameter to change
      if (parameter+0 < 96 || parameter+0 > 122) {//check if  the parameter is between a-z
        check = 3;
        break; }
      
      value = Serial.parseInt();                  //read the value to give to the parameter
      
      inByte = Serial.read();                     //read a byte
      if (inByte != 62) {                         //check if an end character is found
        check = 5;
      }
      break;
    }
  }

  //Do something with the message:
  if (check > 1 && check < 3) {
    parameter = parameterError;
  }
 
  if (check > 0) {
    Serial.print(startB); Serial.print(parameter); Serial.print(value); Serial.print(responseB); Serial.print(123); Serial.print(stateB); Serial.print(check); Serial.print(endB);
    //Serial.println("");
  }
 
  delay(10);
}

“Scientists have calculated that the chance of anything so patently absurd actually existing are millions to one. But magicians have calculated that million-to-one chances crop up nine times out of ten”.
- Terry Pratchett, Mort

bottom of page