- improvements in protocols and drivers
This commit is contained in:
42
binprot.c
42
binprot.c
@@ -59,7 +59,7 @@ typedef enum {intType, hexType, floatType,
|
||||
skipType, codeType, crcType, dumpType, errorType} BinDataType;
|
||||
// dumpType, errorType must be the last items
|
||||
|
||||
typedef enum {modbusCrc, kellerCrc, syconCrc} CrcAlgorithm;
|
||||
typedef enum {modbusCrc, kellerCrc, rsportCrc, syconCrc} CrcAlgorithm;
|
||||
|
||||
typedef struct {
|
||||
CrcAlgorithm crcAlgorithm;
|
||||
@@ -104,6 +104,37 @@ static int calc_crc(char *inp, int inpLen)
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static int calc_crc8(char *inp, int inpLen)
|
||||
{
|
||||
/** CRC-8 (x8+x5+x4+1) Algorithm
|
||||
* crc calculation:
|
||||
* returns the 8bit CRC value of a message
|
||||
* crc check:
|
||||
* returns 0 when the crc appended to the message is correct
|
||||
*/
|
||||
|
||||
unsigned char crc = 0;
|
||||
unsigned char data;
|
||||
int n;
|
||||
|
||||
while (inpLen--) {
|
||||
data = *(unsigned char *) inp;
|
||||
for (n = 0; n < 8; n++) {
|
||||
if ((crc ^ data) & 1) {
|
||||
crc ^= 0x18;
|
||||
crc >>= 1;
|
||||
crc |= 0x80;
|
||||
} else {
|
||||
crc >>= 1;
|
||||
crc &= 0x7f;
|
||||
}
|
||||
data >>= 1;
|
||||
}
|
||||
inp++;
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
int BinReadItem(Ascon *a) {
|
||||
@@ -250,6 +281,10 @@ int BinHandler(Ascon *a) {
|
||||
DynStringConcatChar(dyn, crc % 256);
|
||||
DynStringConcatChar(dyn, crc / 256);
|
||||
break;
|
||||
case rsportCrc:
|
||||
crc = calc_crc8(GetCharArray(dyn), l);
|
||||
DynStringConcatChar(dyn, crc);
|
||||
break;
|
||||
}
|
||||
} else if (strncasecmp(item, "int", 3) == 0) {
|
||||
sscanf(item + 3, "%d", &size);
|
||||
@@ -475,6 +510,7 @@ int BinHandler(Ascon *a) {
|
||||
str[l-1] = i;
|
||||
/* fall through */
|
||||
case modbusCrc:
|
||||
case rsportCrc:
|
||||
if (calc_crc(str, l) != 0) {
|
||||
DynStringConcat(p->result, "badCRC ");
|
||||
}
|
||||
@@ -503,7 +539,7 @@ static int BinInit(Ascon * a, SConnection * con, int argc, char *argv[])
|
||||
{
|
||||
BinPrivate *p;
|
||||
|
||||
/* argv[2] may be modbus-crc (default), keller-crc or sycon-crc */
|
||||
/* argv[2] may be modbus-crc (default), keller-crc, rsport-crc or sycon-crc */
|
||||
|
||||
if (argc < 2) {
|
||||
return 0;
|
||||
@@ -517,6 +553,8 @@ static int BinInit(Ascon * a, SConnection * con, int argc, char *argv[])
|
||||
p->crcAlgorithm = kellerCrc;
|
||||
} else if (strcasecmp(argv[2], "sycon-crc") == 0) {
|
||||
p->crcAlgorithm = syconCrc;
|
||||
} else if (strcasecmp(argv[2], "rsport-crc") == 0) {
|
||||
p->crcAlgorithm = rsportCrc;
|
||||
} else if (strcasecmp(argv[2], "modbus-crc") != 0) {
|
||||
SCPrintf(con, eError, "ERROR: unknown crc-algorithm %s", argv[2]);
|
||||
a->private = NULL;
|
||||
|
||||
Reference in New Issue
Block a user