From 8bef7c156fc8c9a7af45b4f1d7da1937f1ffe2dd Mon Sep 17 00:00:00 2001 From: Markus Zolliker Date: Thu, 30 Apr 2015 12:51:02 +0200 Subject: [PATCH 1/2] - fixed logging of array objects - added checksum to binprot --- arrobj.c | 9 +++++++++ binprot.c | 55 +++++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/arrobj.c b/arrobj.c index 741521d..c3529a3 100644 --- a/arrobj.c +++ b/arrobj.c @@ -25,6 +25,7 @@ typedef struct { ParData p; ArrayItem *items, *freeItems; char *saveFile; + int logged; } ArrayObj; static ParClass arrayObjClass = { "array", sizeof(ArrayObj) }; @@ -280,6 +281,9 @@ static void ArrayObjParDef(void *object) } } ParName(item->name); + if (!arr->logged) { + ParLogAs(NULL); + } ParSave(2); ParAccess(usUser); if (item->unit) { @@ -346,6 +350,11 @@ static int ArrayObjInit(SConnection * con, int argc, char *argv[], creationCmd = Arg2Tcl(argc, argv, NULL, 0); } arr = ParMake(con, argv[1], &arrayObjClass, ArrayObjParDef, creationCmd); + if (argc > 2 && strcasecmp(argv[2], "logged") == 0) { + arr->logged = 1; + } else { + arr->logged = 0; + } arr->freeItems = NULL; arr->items = NULL; return arr != NULL; diff --git a/binprot.c b/binprot.c index f0ce52f..e38fa4e 100644 --- a/binprot.c +++ b/binprot.c @@ -49,17 +49,18 @@ * * sct send 250 3 int2 10 2 crc / skip code skip float crc * - * different crc's might be used (argv[2] opf BinInit): + * different crc's might be used (argv[2] of BinInit): * modbus-crc: CRC-16-IBM, order: lo,hi * keller-crc: CRC-16-IBM, order: hi,lo * sycon-crc: sort of mod 256 checksum, byte stuffing included (no float allowed) + * and some others ... */ typedef enum {intType, hexType, floatType, skipType, codeType, crcType, dumpType, errorType} BinDataType; // dumpType, errorType must be the last items -typedef enum {modbusCrc, kellerCrc, rsportCrc, syconCrc} CrcAlgorithm; +typedef enum {modbusCrc, kellerCrc, rsportCrc, chksumCrc, syconCrc} CrcAlgorithm; typedef struct { CrcAlgorithm crcAlgorithm; @@ -136,6 +137,25 @@ static int calc_crc8(char *inp, int inpLen) return crc; } +/*----------------------------------------------------------------------------*/ +static char calc_chksum(char *inp, int inpLen) +{ +/** simple check sum + * crc calculation: + * returns the 8bit checksum of a message + */ + + unsigned char crc = 0; + unsigned char data; + int n; + + while (inpLen--) { + crc += *inp; + inp++; + } + return crc; +} + /*----------------------------------------------------------------------------*/ int BinReadItem(Ascon *a) { BinPrivate *p = a->private; @@ -154,7 +174,11 @@ int BinReadItem(Ascon *a) { if (strcasecmp(item, "crc") == 0) { p->type = crcType; - p->expectedChars = 2; + if (p->crcAlgorithm == chksumCrc) { + p->expectedChars = 1; + } else { + p->expectedChars = 2; + } p->nextFmt += valen; return 1; } else if (strncasecmp(item, "int", 3) == 0) { @@ -245,7 +269,7 @@ int BinHandler(Ascon *a) { int size; int valen; BinDataType type; - long iValue; + unsigned long uValue; double fValue; BinPrivate *p = a->private; @@ -285,6 +309,9 @@ int BinHandler(Ascon *a) { crc = calc_crc8(GetCharArray(dyn), l); DynStringConcatChar(dyn, crc); break; + case chksumCrc: + DynStringConcatChar(dyn, calc_chksum(GetCharArray(dyn), l)); + break; } } else if (strncasecmp(item, "int", 3) == 0) { sscanf(item + 3, "%d", &size); @@ -296,16 +323,16 @@ int BinHandler(Ascon *a) { } else { switch (type) { case intType: - res = sscanf(item, "%ld", &iValue); + res = sscanf(item, "%ld", &uValue); if (res != 1) { BinError(a, "invalid integer"); return 1; } for (i = size - 1; i >= 0; i--) { if (i < sizeof data) { - data[i] = iValue % 256; + data[i] = uValue % 256; } - iValue /= 256; + uValue /= 256; } for (i = 0; i < size; i++) { if (i >= sizeof data) { @@ -317,12 +344,12 @@ int BinHandler(Ascon *a) { l += size; break; case hexType: - res = sscanf(item, "%lx", &iValue); + res = sscanf(item, "%lx", &uValue); if (res != 1) { BinError(a, "invalid hex. integer"); return 1; } - DynStringConcatChar(dyn, (iValue & 255)); + DynStringConcatChar(dyn, (uValue & 255)); break; case floatType: res = sscanf(item, "%lf", &fValue); @@ -519,6 +546,12 @@ int BinHandler(Ascon *a) { if (calc_crc(str, l) != 0) { DynStringConcat(p->result, "badCRC "); } + break; + case chksumCrc: + if (calc_chksum(str, l-1) != str[l-1]) { + DynStringConcat(p->result, "badCRC "); + } + break; } } else if (p->crcAlgorithm == syconCrc) { /* subtract CRC char (undo the addition) and subtract crc higher four bits */ @@ -544,7 +577,7 @@ static int BinInit(Ascon * a, SConnection * con, int argc, char *argv[]) { BinPrivate *p; -/* argv[2] may be modbus-crc (default), keller-crc, rsport-crc or sycon-crc */ +/* argv[2] may be modbus-crc (default), keller-crc, rsport-crc, chksum-crc or sycon-crc */ if (argc < 2) { return 0; @@ -560,6 +593,8 @@ static int BinInit(Ascon * a, SConnection * con, int argc, char *argv[]) p->crcAlgorithm = syconCrc; } else if (strcasecmp(argv[2], "rsport-crc") == 0) { p->crcAlgorithm = rsportCrc; + } else if (strcasecmp(argv[2], "chksum-crc") == 0) { + p->crcAlgorithm = chksumCrc; } else if (strcasecmp(argv[2], "modbus-crc") != 0) { SCPrintf(con, eError, "ERROR: unknown crc-algorithm %s", argv[2]); a->private = NULL; From 900e2771fc28fda5b3d63c1446dafdb486144d14 Mon Sep 17 00:00:00 2001 From: Markus Zolliker Date: Fri, 5 Jun 2015 09:36:17 +0200 Subject: [PATCH 2/2] - bug fix (illegal value should be printed) --- pardef.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pardef.c b/pardef.c index 187b56f..a89eff7 100644 --- a/pardef.c +++ b/pardef.c @@ -387,7 +387,7 @@ static int ParOutError(SConnection * con, ParData * o) o->name, ctx->thisPar); break; case ILLNUM: - SCPrintf(con, eError, "ERROR: illegal value", o->name, ctx->valueArg); + SCPrintf(con, eError, "ERROR: illegal value %s", ctx->valueArg); break; case ILLARGC: SCPrintf(con, eError, "ERROR: illegal number of arguments for %s %s",