Arduino sending data with checksum CRC16

I have removed as posted above in post #39. That was the problem. Thanks!
The thing why I have done that was, because I would see it comfortable on the serial monitor. For sending it is not needed anyway.

That is true! But here the same thing. For sending the CRC data between the Arduinos, it is not necessary to write HEX characters. For visualization yes, but when I receive the correct data on the other end, the CRC checksum is correct.

I have tested a little bit with the sending method with the checksum CRC16. It works, but if I send data from a joystick (during movement it sends for all positions a value) it is "slow" and does not move smoothly. That is because the received or sent values or both are too slow generated and transmitted/received.
Why is that a problem now and with normal serial connection it works properly? Is that because of the checksum that is generated for each value and needs too much time? Or is it another problem? The code is nearly the same as in post #39. I have only removed the serial output on the serial monitor. Any help is appreciated!

I strongly suggest that you post the latest version in a new post

Yes, of course! Sorry, I was at work. Now I am at home and will post the two actual codes:

Master sender:

/*Arduino 1 has the SC16IS750 connected on SDA/SCL
 *TX/RX of the SC16IS750 is connected to Arduino 2
 *VCC/GND
 *I2C of the SC16IS750 is connected to VCC
 *A0 and A1 of the SC16IS750 is connected as shown in the address table below
 */
 
#include <Wire.h>
#include <SC16IS750.h>

SC16IS750 i2cuart = SC16IS750(SC16IS750_PROTOCOL_I2C,SC16IS750_ADDRESS_AA);   /*Address table:  A1    A0    Address
                                                                                               VDD   VDD    0x90 => AA
                                                                                               VDD   GND    0x92 => AB
                                                                                               VDD   SCL    0x94 => AC
                                                                                               VDD   SDA    0x96 => AD
                                                                                               GND   VDD    0x98 => BA
                                                                                               GND   GND    0x9A => BB
                                                                                               GND   SCL    0x9C => BC
                                                                                               GND   SDA    0x9E => BD
                                                                                               SCL   VDD    0xA0 => CA
                                                                                               SCL   GND    0xA2 => CB
                                                                                               SCL   SCL    0xA4 => CC
                                                                                               SCL   SDA    0xA6 => CD
                                                                                               SDA   VDD    0xA8 => DA
                                                                                               SDA   GND    0xAA => DB
                                                                                               SDA   SCL    0xAC => DC
                                                                                               SDA   SDA    0xAE => DD
                                                                                               */

const byte bufferSize {10};
char serialBuffer[bufferSize+1] {""};
char incomingData[bufferSize] {""};
char value[bufferSize] {""};
char variable[bufferSize] {""};
uint16_t crc = 0;    //checksum crc
byte crcData[2] {""};        //create array for packet serial
byte crcDataRec[2] {""};        //create array for packet serial

int E = 0;
int F = 0;
int G = 0;
int H = 0;
int BUT = 0;

const byte DPangle = 158;

void setup()
{
  i2cuart.begin(38400);
  Serial.begin(38400);
}

void loop()
{
  readCommand(i2cuart);
  
  
  Serial.print("E = ");
  Serial.println(E);
  Serial.print("F = ");
  Serial.println(F);
  Serial.print("G = ");
  Serial.println(G);
  Serial.print("H = ");
  Serial.println(H);
  Serial.print("BUT = ");
  Serial.println(BUT);

  
  command("A01",i2cuart);
  command("B57",i2cuart);
  command("C"+String(DPangle),i2cuart);
  command("D122",i2cuart);
  command("@19",i2cuart);
 
}



void command(String value, Stream& stream) {
  memset(serialBuffer, 0, sizeof serialBuffer);
  sendToI2CUART(serialBuffer, value, stream);
}


void sendToI2CUART (char *const buffer, String command, Stream& stream)
{
  command.toCharArray(buffer, bufferSize);
  const size_t characters {strlen(buffer)};
  //Serial.print("laenge: "); Serial.println(characters);

  for(byte index = 0; index < characters; index++){            //create the checksum for the array data[i] with 0 <= i <= 3
        crc16(buffer[index]);                                  //call the function crc16
      }
      crc = getCRC();
      byte crcIndex = characters;
      byte crcIndex2 = crcIndex++;
      buffer[crcIndex] = crc >> 8;                         //store checksum in data array
      buffer[crcIndex2] = crc;
      crc = 0;
    
  for (byte i = 0; i < characters; i++){
      stream.write(buffer[i]);
      //Serial.print("gesendet: ");
      //Serial.println(buffer[i]);
  }
    stream.write('\0');
    stream.write(buffer[crcIndex]);
    //Serial.print("gesendet CRC: ");
    //Serial.println(buffer[crcIndex],HEX);
    stream.write(buffer[crcIndex2]);
    //Serial.print("gesendet CRC: ");
    //Serial.println(buffer[crcIndex2],HEX);
    stream.write('\n');

}


//Calculates CRC16 of nBytes of data[index] in byte array
void crc16(uint8_t data) {

  int i;
  crc = crc ^ ((uint16_t)data << 8);
  for (i=0; i<8; i++){
    if (crc & 0x8000){
      crc = (crc << 1) ^ 0x1021;
    }
    else{
      crc <<= 1;
    }
  }
}

uint16_t getCRC() {
  return crc;
}


void readCommand(Stream& stream){

  char var = 0;
  int val = 0;
  byte i = 0;
  byte j = 0;
  byte k = 0;
  byte state = 0;

  while(stream.available()) {
    char m = stream.read();
    if(m == '\0') {
      state = 1;
      i = 0;
    }

    if(m == '\n') {
      state = 2;
      i = 0;
    }

    switch(state){
      case 0: incomingData[i++] = m;
              //Serial.print("incomingData = ");
              //Serial.println(incomingData[i]);
              break;
      case 1: if(m != '\0'){
                crcData[i++] = m;
                //Serial.print("empfangen CRC: ");
                //Serial.println(crcData[i]);
              }
              else {
                break;
              }
              break;
      case 2: for(byte index = 0; index < strlen(incomingData); index++){            //create the checksum for the array data[i] with 0 <= i <= 3
                crc16(incomingData[index]);                                  //call the function crc16
              }
              crc = getCRC();
              crcDataRec[0] = crc >> 8;                         //store checksum in data array
              crcDataRec[1] = crc;
              crc = 0;

              //Serial.print("crcData[0] = ");
              //Serial.println(crcData[0],HEX);
              //Serial.print("crcDataRec[0] = ");
              //Serial.println(crcDataRec[0],HEX);
              //Serial.print("crcData[1] = ");
              //Serial.println(crcData[1],HEX);
              //Serial.print("crcDataRec[1] = ");
              //Serial.println(crcDataRec[1],HEX);

              if(crcData[0] == crcDataRec[0] && crcData[1] == crcDataRec[1]) {
                while(i <= strlen(incomingData) ) {
                  if(isdigit(incomingData[i]) == true) {
                    value[j] = incomingData[i];
                    j++;
                    i++;
                  }
                  else {
                    variable[k] = incomingData[i];
                    k++;
                    i++;
                  }
                }
                memset(incomingData, 0, sizeof incomingData);
                j = 0;
                k = 0;
                i = 0;
      
                val = atoi(value);  //making the integer part
                var = variable[0];

                memset(value, 0, sizeof value);
                memset(variable, 0, sizeof variable);

                switch(var) {
                  case 'E' : E = val;
                             break;
                  case 'F' : F = val;
                             break;
                  case 'G' : G = val;
                             break;
                  case 'H' : H = val;
                             break;
                  case '#' : BUT = val;
                             break;
                }
              }
              else if(crcData[0] != crcDataRec[0] || crcData[1] != crcDataRec[1]){
                memset(incomingData, 0, sizeof incomingData);
              }
              state = 0;
              var = 0;
              val = 0;
              memset(crcData, 0, sizeof crcData);
              memset(crcDataRec, 0, sizeof crcDataRec);
              break;
    }
  }
}

Slave receiver:

/*Arduino 2 has the SC16IS750 connected on TX/RX
 */

const byte bufferSize {10};
char serialBuffer[bufferSize+1] {""};
char incomingData[bufferSize] {""};
char value[bufferSize] {""};
char variable[bufferSize] {""};
uint16_t crc = 0;    //checksum crc
byte crcData[2] {""};
byte crcDataRec[2] {""};

int A = 0;
int B = 0;
int C = 0;
int D = 0;
int BUT = 0;

  
void setup()
{
  Serial.begin(38400);
  
}

void loop() {

  readCommand();

  
  Serial.print("A = ");
  Serial.println(A);
  Serial.print("B = ");
  Serial.println(B);
  Serial.print("C = ");
  Serial.println(C);
  Serial.print("D = ");
  Serial.println(D);
  Serial.print("BUT = ");
  Serial.println(BUT);


  command("E10");
  command("F5");
  command("G147");
  command("H251");
  command("#33");
  
}


void command(String value) {
  memset(serialBuffer, 0, sizeof serialBuffer);
  sendToUART(serialBuffer, value);
}


void sendToUART (char *const buffer, String command)
{
  command.toCharArray(buffer, bufferSize);
  const size_t characters {strlen(buffer)};
  //Serial.print("laenge: "); Serial.println(characters);

  for(byte index = 0; index < characters; index++){            //create the checksum for the array data[i] with 0 <= i <= 3
        crc16(buffer[index]);                                  //call the function crc16
      }
      crc = getCRC();
      byte crcIndex = characters;
      byte crcIndex2 = crcIndex++;
      buffer[crcIndex] = crc >> 8;                         //store checksum in data array
      buffer[crcIndex2] = crc;
      crc = 0;

  for (byte i = 0; i < characters; i++)
  {
    Serial.write(buffer[i]);
    //Serial.print("gesendet: ");
    //Serial.println(buffer[i]);
  }
  Serial.write('\0');
  Serial.write(buffer[crcIndex]);
  //Serial.print("gesendet CRC: ");
  //Serial.println(buffer[crcIndex],HEX);
  Serial.write(buffer[crcIndex2]);
  //Serial.print("gesendet CRC: ");
  //Serial.println(buffer[crcIndex2],HEX);
  Serial.write('\n');

}


//Calculates CRC16 of nBytes of data[index] in byte array
void crc16(uint8_t data) {

  int i;
  crc = crc ^ ((uint16_t)data << 8);
  for (i=0; i<8; i++){
    if (crc & 0x8000){
      crc = (crc << 1) ^ 0x1021;
    }
    else{
      crc <<= 1;
    }
  }
}

uint16_t getCRC() {
  return crc;
}


void readCommand(){

  char var = 0;
  int val = 0;
  byte i = 0;
  byte j = 0;
  byte k = 0;
  byte state = 0;
  
  while(Serial.available()) {
    char m = Serial.read();
    if(m == '\0') {
      state = 1;
      i = 0;
    }

    if(m == '\n') {
      state = 2;
      i = 0;
    }

    switch(state){
      case 0: incomingData[i++] = m;
              //Serial.print("incomingData = ");
              //Serial.println(incomingData[i]);
              break;
      case 1: if(m != '\0'){
                crcData[i++] = m;
                //Serial.print("empfangen CRC: ");
                //Serial.println(crcData[i]);
              }
              else {
                break;
              }
              break;
      case 2: for(byte index = 0; index < strlen(incomingData); index++){            //create the checksum for the array data[i] with 0 <= i <= 3
                crc16(incomingData[index]);                                  //call the function crc16
              }
              crc = getCRC();
              crcDataRec[0] = crc >> 8;                         //store checksum in data array
              crcDataRec[1] = crc;
              crc = 0;

              //Serial.print("crcData[0] = ");
              //Serial.println(crcData[0],HEX);
              //Serial.print("crcDataRec[0] = ");
              //Serial.println(crcDataRec[0],HEX);
              //Serial.print("crcData[1] = ");
              //Serial.println(crcData[1],HEX);
              //Serial.print("crcDataRec[1] = ");
              //Serial.println(crcDataRec[1],HEX);
  
              if(crcData[0] == crcDataRec[0] && crcData[1] == crcDataRec[1]) {
                while(i <= strlen(incomingData) ) {
                  if(isdigit(incomingData[i]) == true) {
                    value[j] = incomingData[i];
                    j++;
                    i++;
                  }
                  else {
                    variable[k] = incomingData[i];
                    k++;
                    i++;
                  }
                }
                memset(incomingData, 0, sizeof incomingData);
                j = 0;
                k = 0;
                i = 0;
      
                val = atoi(value);  //making the integer part
                var = variable[0];

                memset(value, 0, sizeof value);
                memset(variable, 0, sizeof variable);

                switch(var) {
                case 'A' : A = val;
                           break;
                case 'B' : B = val;
                           break;
                case 'C' : C = val;
                           break;
                case 'D' : D = val;
                           break;
                case '@' : BUT = val;
                           break;
                }
              }
              else if(crcData[0] != crcDataRec[0] || crcData[1] != crcDataRec[1]){
                memset(incomingData, 0, sizeof incomingData);
              }
              state = 0;
              memset(crcData, 0, sizeof crcData);
              memset(crcDataRec, 0, sizeof crcDataRec);
              break;
    }
  }
}
1 Like

No one? Should not it be slower as normal serial UART connection? Slows the String the whole transmission down or is this little String part ok? If not, could anybody tell/help me how to write it without a String, if that is the problem?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.