Hello people,
I have the followong code:
#include <AltSoftSerial.h>
#include <FileIO.h>
#include <Process.h>
AltSoftSerial altSerial;
// Incoming data
char sReceived[19]; // The valid string can contain 18 bytes and a terminating null
void setup() {
Bridge.begin(); // Initialize the Bridge
Serial.begin(9600); // Initialize the Serial
FileSystem.begin();
// Wait until a Serial Monitor is connected.
while(!Serial);
altSerial.begin(4800);
}
void loop(){
char cByte = -1;
char cStatus = -1;
String sHexChar = "";
if (altSerial.available()) {
// Create a string array with the complete data
cByte = altSerial.read();
AddToReceived(cByte);
// See if the string matches a valid string (yet)
if ( MatchPattern() ) {
cStatus = getStatus();
sHexChar = CharToHex(cStatus);
Serial.println("Status Hex = " + sHexChar );
Serial.println("Status Chr = " + CharToHex(cStatus) );
// Do something with status 00
// if (cStatus = '\0' )
// if (CharToHex(cStatus) = "00" ){
if (sHexChar = "00" ){
Serial.println("-----> Status is '00'");
StatusMessage(cStatus);
}
// Do something with status 21
// if (cStatus = '\21')
// if (CharToHex(cStatus) = "21"){
if (sHexChar = "21"){
Serial.println("-----> Status is '21'");
StatusMessage(cStatus);
}
}
}
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
void StatusMessage(char cChar){
Serial.println("Status is: '" + (String)cChar + "'"); // For now, just print it
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
char getStatus(){
return sReceived[12];
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
void AddToReceived(char cByte)
{ // sReceived is a global variable
// We add information ad the end, discarding each first character when we reached a full buffer
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Example sReceived = [ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][01][02][03]
// becomes sReceived = [ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][01][02][03][04]
// and
// Example sReceived = [01][02][03][04][05][06][07][08][09][0a][0b][0c][0d][0e][0f][10][11][12][13]
// becomes sReceived = [02][03][04][05][06][07][08][09][0a][0b][0c][0d][0e][0f][10][11][12][13][14]
// Shift all characters one position to the left and add the new character to the new position
for (int i=0; i <= 17; i++){
sReceived[i] = sReceived[i+1];
}
sReceived[18] = cByte;
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
String CharToHex (char cByte)
{ String sHex = "";
// Get the character received and return the same information converted to it's hexadecimal form.
sHex = "0" +String(cByte,HEX); // Put a zero in front of each value, making '0' into '00' and '11' into '011' and 'ffa3' into '0ffa3'
sHex = sHex.substring(sHex.length()-2); // Get the last two characters, leaving '00' at '00' and turning '011' to '11' and even '0ffa3' into 'a3'
return sHex;
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
boolean MatchPattern()
{
if (sReceived[17] == '\3') // STX byte (Start of transmission)
if (sReceived[ 1] == '\2') // ETX byte (End of transmission)
return true; // Yup.... This is the one!
// No match.... So return false.
return false;
}
The part that is giving me headaches is this:
// Do something with status 00
// if (cStatus = '\0' )
// if (CharToHex(cStatus) = "00" ){
if (sHexChar = "00" ){
Serial.println("-----> Status is '00'");
StatusMessage(cStatus);
}
// Do something with status 21
// if (cStatus = '\21')
// if (CharToHex(cStatus) = "21"){
if (sHexChar = "21"){
Serial.println("-----> Status is '21'");
StatusMessage(cStatus);
}
Why?
Well.... see my output in the Serial Monitor:
Status Hex = 00
Status Chr = 00
-----> Status is '00'
[StatusMessage] Status is: ' '
-----> Status is '21'
[StatusMessage] Status is: ' '
I have tries every form of comparing I can imagine.
(Some of them are still in the comments)
Both if-statements will produce results and I don't get it.
How come?
Do I test the wrong variables?
Do I need an other way of comparing?
I just don't see the problem, and being more and more creative did not help me.
Please tell me what is wrong with my ways of comparing