Okay, atm I have almost whole the program finished for reading in the values and printing out what needs to be visualized.
The only problem I still have is the checksum. This is the problem:
I have the string: $IIMWV,225.0,R,000.0,N,A*38
The 38 is the bitwise XOR function of every character between $ and *
I stored that solution into an int called CheckSum. If I print the the CheckSum like -> Serial.println(CheckSum, HEX);
I will get the 38.
The real problem is, how can I compare the 38 that I have like 2 characters from the string (With the atoi function I can make it into an int) with the CheckSum which is still an int (HEX 38 = int 56)? Because there isn't an atoh function (I think?), how is it possible to compare these?
I hope you understand what I mean.
The program: (probably not the best solution to everything, like the bitwise XOR. But it works ![]()
#include <string.h>
// Reading String
char InputChar = 0;
char AnemometerString[27];
int index = 0;
//Dividing String
char Delimiters[] = "$,*";
char *StringPart1;
char *StringPart2;
char *StringPart3;
char *StringPart4;
char *StringPart5;
char *StringPart6;
char *StringPart7;
int CheckSum = 0;
//Processing String Parts
float WindAngle = 0;
float WindSpeed = 0;
char *RelativeReference = "R";
char *True = "T";
char *Knots = "N";
char *KMH = "K";
char *MPH = "M";
char *Available = "A";
char *Alarm = "V";
void setup()
{
Serial2.begin(9600);
Serial.begin(9600);
Serial.println("Power on");
}
void loop()
{
if (Serial2.available() > 0) // If the serial input gets information => do the next
{
InputChar = Serial2.read();
AnemometerString[index] = InputChar;
index++;
AnemometerString[index] = '\0';
if ( InputChar == '\n')
{
AnemometerString[index] = '\0';
}
if (index == 27)
{
int CheckSum = AnemometerString[1] ^ AnemometerString[2] ^ AnemometerString[3] ^ AnemometerString[4] ^ AnemometerString[5] ^ AnemometerString[6] ^ AnemometerString[7] ^ AnemometerString[8] ^ AnemometerString[9] ^ AnemometerString[10] ^ AnemometerString[11] ^
AnemometerString[12] ^ AnemometerString[13] ^ AnemometerString[14] ^ AnemometerString[15] ^ AnemometerString[16] ^ AnemometerString[17] ^ AnemometerString[18] ^ AnemometerString[19] ^ AnemometerString[20] ^ AnemometerString[21] ^ AnemometerString[22] ^
AnemometerString[23];
Serial.print("The whole string: \t");
Serial.print(AnemometerString);
Serial.println("\n");
StringPart1 = strtok(AnemometerString, Delimiters);
StringPart2 = strtok(NULL, Delimiters);
StringPart3 = strtok(NULL, Delimiters);
StringPart4 = strtok(NULL, Delimiters);
StringPart5 = strtok(NULL, Delimiters);
StringPart6 = strtok(NULL, Delimiters);
StringPart7 = strtok(NULL, Delimiters);
int CheckSum2 = atoi(StringPart7);
if (CheckSum == CheckSum2)
{
if (strcmp (StringPart1, "IIMWV") == 0)
{
Serial.print("The start sentence is: \t");
Serial.println(StringPart1);
WindAngle = atof(StringPart2);
WindSpeed = atof(StringPart4);
if (strcmp(StringPart3, RelativeReference) == 0)
{
Serial.print("The windangle is: \t");
Serial.print(WindAngle);
Serial.println(" Relative");
}
else if (strcmp(StringPart3, True) == 0)
{
Serial.print("The windangle is: \t");
Serial.print(WindAngle);
Serial.println(" true");
}
else
{
Serial.println("Wrong windangle signal");
}
if (strcmp(StringPart5, Knots) == 0)
{
Serial.print("The windspeed is: \t");
Serial.print(WindSpeed);
Serial.println(" knots");
}
else if (strcmp(StringPart5, KMH) == 0)
{
Serial.print("The windspeed is: \t");
Serial.print(WindSpeed);
Serial.println(" km/h");
}
else if (strcmp(StringPart5, MPH) == 0)
{
Serial.print("The windspeed is: \t");
Serial.print(WindSpeed);
Serial.println(" mph");
}
else
{
Serial.println("Wrong windspeed signal");
}
if (strcmp(StringPart6, Available) == 0)
{
Serial.print("Total string: \t\t");
Serial.println("Available");
}
else if (strcmp(StringPart6, Alarm) == 0)
{
Serial.print("The total string: \t\t");
Serial.println("Alarm!");
}
else
{
Serial.println("Wrong status!");
}
Serial.print("The checksum is: \t");
Serial.println(StringPart7);
Serial.println("\n");
}
else
{
Serial.println("Error, wrong input device \n");
}
}
index = 0;
}
}
}