Skip to content

Commit

Permalink
Correct missing #, Also add CRC calculate function and correct one of
Browse files Browse the repository at this point in the history
the overloaded methods
  • Loading branch information
peterj43 committed Jul 12, 2021
1 parent a49cc13 commit 96feec0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
44 changes: 34 additions & 10 deletions src/SDI12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,8 @@ void SDI12::sendResponse(FlashString resp) {
}
setState(SDI12_LISTENING); // return to listening state
}
ifdef USE_CRC

#ifdef USE_CRC
#define POLY 0xa001
String SDI12::addCRCResponse(String &resp) {
char crcStr[3] = {0};
Expand All @@ -857,12 +858,12 @@ String SDI12::addCRCResponse(String &resp) {
for(int i = 0; i < resp.length(); i++) {
crc ^= (uint16_t)resp[i]; //Set the CRC equal to the exclusive OR of the character and itself
for (int j = 0; j < 8; j++){ //count = 1 to 8
if (crc & 0x0001){ //if the least significant bit of the CRC is one
crc >>= 1; //right shift the CRC one bit
crc ^= POLY; //set CRC equal to the exclusive OR of POLY and itself
if (crc & 0x0001){ //if the least significant bit of the CRC is one
crc >>= 1; //right shift the CRC one bit
crc ^= POLY; //set CRC equal to the exclusive OR of POLY and itself
}
else {
crc >>= 1; //right shift the CRC one bit
crc >>= 1; //right shift the CRC one bit
}
}
}
Expand All @@ -873,15 +874,15 @@ String SDI12::addCRCResponse(String &resp) {
}

char * SDI12::addCRCResponse(char *resp) {
char *crcStr = "\0";
uint16_t crc = 0;
char *crcStr[3] = {0};
uint16_t crc = 0;

for(int i = 0; i < strlen(resp); i++) {
crc ^= (uint16_t)resp[i]; //Set the CRC equal to the exclusive OR of the character and itself
for (int j = 0; j < 8; j++){ //count = 1 to 8
if (crc & 0x0001){ //if the least significant bit of the CRC is one
crc >>= 1; //right shift the CRC one bit
crc ^= POLY; //set CRC equal to the exclusive OR of POLY and itself
if (crc & 0x0001){ //if the least significant bit of the CRC is one
crc >>= 1; //right shift the CRC one bit
crc ^= POLY; //set CRC equal to the exclusive OR of POLY and itself
}
else {
crc >>= 1; //right shift the CRC one bit
Expand Down Expand Up @@ -923,6 +924,29 @@ String SDI12::addCRCResponse(FlashString resp) {
crcStr[2] = (char)( 0x0040 | (crc & 0x003F));
return (outResp + String(crcStr[0]) + String(crcStr[1]) + String(crcStr[2]));
}

String SDI12::calculateCRC(String &resp){
char crcStr[3] = {0};
uint16_t crc = 0;

for(int i = 0; i < resp.length(); i++) {
crc ^= (uint16_t)resp[i]; //Set the CRC equal to the exclusive OR of the character and itself
for (int j = 0; j < 8; j++){ //count = 1 to 8
if (crc & 0x0001){ //if the least significant bit of the CRC is one
crc >>= 1; //right shift the CRC one bit
crc ^= POLY; //set CRC equal to the exclusive OR of POLY and itself
}
else {
crc >>= 1; //right shift the CRC one bit
}
}
}
crcStr[0] = (char)( 0x0040 | (crc >> 12));
crcStr[1] = (char)( 0x0040 | ((crc >> 6) & 0x003F));
crcStr[2] = (char)( 0x0040 | (crc & 0x003F));
return (String(crcStr[0]) + String(crcStr[1]) + String(crcStr[2]));
}

#endif //USE_CRC


Expand Down
5 changes: 5 additions & 0 deletions src/SDI12.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

typedef const __FlashStringHelper *FlashString;


//#define USE_CRC

#define NO_IGNORE_CHAR '\x01' // a char not found in a valid ASCII numeric field
#define SDI12_BUFFER_SIZE 81 // <address> is a single character
// +<values> has a maximum value of 75 characters.
Expand All @@ -65,6 +68,7 @@ enum LookaheadMode
SKIP_NONE, // Nothing is skipped, and the stream is not touched unless the first waiting character is valid.
SKIP_WHITESPACE // Only tabs, spaces, line feeds & carriage returns are skipped.
};

#define READTIME sdi12timer.SDI12TimerRead()
#else
#define READTIME TCNTX
Expand Down Expand Up @@ -134,6 +138,7 @@ class SDI12 : public Stream
String addCRCResponse(String &resp); // Add CRC to the resp string (for slave use)
char * addCRCResponse( char *resp); // Add CRC to the resp string (for slave use)
String addCRCResponse(FlashString resp); // Add CRC to the resp string (for slave use)
String calculateCRC(String &resp); // Calculate the CRC for a response
#endif

int available(); // returns the number of bytes available in buffer
Expand Down

0 comments on commit 96feec0

Please sign in to comment.