Hello,
I have tried to send an array with data and at the end a checksum. I really does not see where the issue is. I have tried to see through my coding, but I can not see the issue. Can someone help to find where the problem is?
I will get the data from the array, if I delete the condition in the if-statement in the readCommand() function.
Instead of this:
else if(m == '\n' && crcData[0] == crcDataRec[0] && crcData[1] == crcDataRec[1])
I use this:
else if(m == '\n')
and get the correct data.
But now, I will creat the checksum from the received data again and compare it with the received one. If that is true, the received data will be used. Any idea?
Here is the sender code:
/*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 the received checksum
byte crcDataRec[2]; //create array for the new calculated checksum from received data
int E = 0;
int F = 0;
int G = 0;
int H = 0;
int BUT = 0;
void setup()
{
i2cuart.begin(38400);
Serial.begin(38400);
}
void loop()
{
readCommand();
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("A0");
command("B55");
command("C180");
command("D125");
command("@12");
}
void command(String value) {
memset(serialBuffer, 0, sizeof serialBuffer);
sendToI2CUART(serialBuffer, value);
}
void sendToI2CUART (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;
for (byte i = 0; i < characters; i++)
{
i2cuart.write(buffer[i]);
Serial.print("gesendet: ");
Serial.println(buffer[i]);
}
i2cuart.write('\0');
i2cuart.write(buffer[crcIndex]);
Serial.println(buffer[crcIndex]);
i2cuart.write(buffer[crcIndex2]);
Serial.println(buffer[crcIndex2]);
i2cuart.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;
int val = 0;
static byte i = 0;
static byte j = 0;
static byte k = 0;
while(i2cuart.available()) {
char m = i2cuart.read();
if(m != '\0' && m != '\n') {
incomingData[i++] = m;
}
else if(m == '\0') {
crcData[0] = i2cuart.read();
crcData[1] = i2cuart.read();
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;
Serial.print("crcData[0] = ");
Serial.println(crcData[0]);
Serial.print("crcDataRec[0] = ");
Serial.println(crcDataRec[0]);
Serial.print("crcData[1] = ");
Serial.println(crcData[1]);
Serial.print("crcDataRec[1] = ");
Serial.println(crcDataRec[1]);
}
else if(m == '\n' && crcData[0] == crcDataRec[0] && crcData[1] == crcDataRec[1]) {
i = 0;
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;
}
}
}
}
Here is the receiver code:
/*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)};
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;
for (byte i = 0; i < characters; i++)
{
Serial.write(buffer[i]);
}
Serial.write('\0');
Serial.write(buffer[crcIndex]);
Serial.write(buffer[crcIndex2]);
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;
int val = 0;
static byte i = 0;
static byte j = 0;
static byte k = 0;
while(Serial.available()) {
char m = Serial.read();
if(m != '\0' && m != '\n') {
incomingData[i++] = m;
}
else if(m == '\0') {
crcData[0] = Serial.read();
crcData[1] = Serial.read();
for(byte index = 0; index < i++; 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;
Serial.print("crcData[0] = ");
Serial.println(crcData[0]);
Serial.print("crcDataRec[0] = ");
Serial.println(crcDataRec[0]);
Serial.print("crcData[1] = ");
Serial.println(crcData[1]);
Serial.print("crcDataRec[1] = ");
Serial.println(crcDataRec[1]);
}
else if(m == '\n' && crcData[0] == crcDataRec[0] && crcData[1] == crcDataRec[1]) {
//Serial.print("incomingData: ");
//Serial.println(incomingData);
i = 0;
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;
//Serial.print("value: ");
//Serial.println(value);
//Serial.print("variable: ");
//Serial.println(variable);
val = atoi(value); //making the integer part
var = variable[0];
//Serial.println(var);
//Serial.println(val);
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;
}
}
}
}