- fixed logging of array objects

- added checksum to binprot
This commit is contained in:
2015-04-30 12:51:02 +02:00
parent 2c78f6b448
commit 8bef7c156f
2 changed files with 54 additions and 10 deletions

View File

@ -25,6 +25,7 @@ typedef struct {
ParData p; ParData p;
ArrayItem *items, *freeItems; ArrayItem *items, *freeItems;
char *saveFile; char *saveFile;
int logged;
} ArrayObj; } ArrayObj;
static ParClass arrayObjClass = { "array", sizeof(ArrayObj) }; static ParClass arrayObjClass = { "array", sizeof(ArrayObj) };
@ -280,6 +281,9 @@ static void ArrayObjParDef(void *object)
} }
} }
ParName(item->name); ParName(item->name);
if (!arr->logged) {
ParLogAs(NULL);
}
ParSave(2); ParSave(2);
ParAccess(usUser); ParAccess(usUser);
if (item->unit) { if (item->unit) {
@ -346,6 +350,11 @@ static int ArrayObjInit(SConnection * con, int argc, char *argv[],
creationCmd = Arg2Tcl(argc, argv, NULL, 0); creationCmd = Arg2Tcl(argc, argv, NULL, 0);
} }
arr = ParMake(con, argv[1], &arrayObjClass, ArrayObjParDef, creationCmd); 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->freeItems = NULL;
arr->items = NULL; arr->items = NULL;
return arr != NULL; return arr != NULL;

View File

@ -49,17 +49,18 @@
* *
* sct send 250 3 int2 10 2 crc / skip code skip float crc * 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 * modbus-crc: CRC-16-IBM, order: lo,hi
* keller-crc: CRC-16-IBM, order: hi,lo * keller-crc: CRC-16-IBM, order: hi,lo
* sycon-crc: sort of mod 256 checksum, byte stuffing included (no float allowed) * sycon-crc: sort of mod 256 checksum, byte stuffing included (no float allowed)
* and some others ...
*/ */
typedef enum {intType, hexType, floatType, typedef enum {intType, hexType, floatType,
skipType, codeType, crcType, dumpType, errorType} BinDataType; skipType, codeType, crcType, dumpType, errorType} BinDataType;
// dumpType, errorType must be the last items // dumpType, errorType must be the last items
typedef enum {modbusCrc, kellerCrc, rsportCrc, syconCrc} CrcAlgorithm; typedef enum {modbusCrc, kellerCrc, rsportCrc, chksumCrc, syconCrc} CrcAlgorithm;
typedef struct { typedef struct {
CrcAlgorithm crcAlgorithm; CrcAlgorithm crcAlgorithm;
@ -136,6 +137,25 @@ static int calc_crc8(char *inp, int inpLen)
return crc; 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) { int BinReadItem(Ascon *a) {
BinPrivate *p = a->private; BinPrivate *p = a->private;
@ -154,7 +174,11 @@ int BinReadItem(Ascon *a) {
if (strcasecmp(item, "crc") == 0) { if (strcasecmp(item, "crc") == 0) {
p->type = crcType; p->type = crcType;
if (p->crcAlgorithm == chksumCrc) {
p->expectedChars = 1;
} else {
p->expectedChars = 2; p->expectedChars = 2;
}
p->nextFmt += valen; p->nextFmt += valen;
return 1; return 1;
} else if (strncasecmp(item, "int", 3) == 0) { } else if (strncasecmp(item, "int", 3) == 0) {
@ -245,7 +269,7 @@ int BinHandler(Ascon *a) {
int size; int size;
int valen; int valen;
BinDataType type; BinDataType type;
long iValue; unsigned long uValue;
double fValue; double fValue;
BinPrivate *p = a->private; BinPrivate *p = a->private;
@ -285,6 +309,9 @@ int BinHandler(Ascon *a) {
crc = calc_crc8(GetCharArray(dyn), l); crc = calc_crc8(GetCharArray(dyn), l);
DynStringConcatChar(dyn, crc); DynStringConcatChar(dyn, crc);
break; break;
case chksumCrc:
DynStringConcatChar(dyn, calc_chksum(GetCharArray(dyn), l));
break;
} }
} else if (strncasecmp(item, "int", 3) == 0) { } else if (strncasecmp(item, "int", 3) == 0) {
sscanf(item + 3, "%d", &size); sscanf(item + 3, "%d", &size);
@ -296,16 +323,16 @@ int BinHandler(Ascon *a) {
} else { } else {
switch (type) { switch (type) {
case intType: case intType:
res = sscanf(item, "%ld", &iValue); res = sscanf(item, "%ld", &uValue);
if (res != 1) { if (res != 1) {
BinError(a, "invalid integer"); BinError(a, "invalid integer");
return 1; return 1;
} }
for (i = size - 1; i >= 0; i--) { for (i = size - 1; i >= 0; i--) {
if (i < sizeof data) { if (i < sizeof data) {
data[i] = iValue % 256; data[i] = uValue % 256;
} }
iValue /= 256; uValue /= 256;
} }
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
if (i >= sizeof data) { if (i >= sizeof data) {
@ -317,12 +344,12 @@ int BinHandler(Ascon *a) {
l += size; l += size;
break; break;
case hexType: case hexType:
res = sscanf(item, "%lx", &iValue); res = sscanf(item, "%lx", &uValue);
if (res != 1) { if (res != 1) {
BinError(a, "invalid hex. integer"); BinError(a, "invalid hex. integer");
return 1; return 1;
} }
DynStringConcatChar(dyn, (iValue & 255)); DynStringConcatChar(dyn, (uValue & 255));
break; break;
case floatType: case floatType:
res = sscanf(item, "%lf", &fValue); res = sscanf(item, "%lf", &fValue);
@ -519,6 +546,12 @@ int BinHandler(Ascon *a) {
if (calc_crc(str, l) != 0) { if (calc_crc(str, l) != 0) {
DynStringConcat(p->result, "badCRC "); 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) { } else if (p->crcAlgorithm == syconCrc) {
/* subtract CRC char (undo the addition) and subtract crc higher four bits */ /* 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; 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) { if (argc < 2) {
return 0; return 0;
@ -560,6 +593,8 @@ static int BinInit(Ascon * a, SConnection * con, int argc, char *argv[])
p->crcAlgorithm = syconCrc; p->crcAlgorithm = syconCrc;
} else if (strcasecmp(argv[2], "rsport-crc") == 0) { } else if (strcasecmp(argv[2], "rsport-crc") == 0) {
p->crcAlgorithm = rsportCrc; p->crcAlgorithm = rsportCrc;
} else if (strcasecmp(argv[2], "chksum-crc") == 0) {
p->crcAlgorithm = chksumCrc;
} else if (strcasecmp(argv[2], "modbus-crc") != 0) { } else if (strcasecmp(argv[2], "modbus-crc") != 0) {
SCPrintf(con, eError, "ERROR: unknown crc-algorithm %s", argv[2]); SCPrintf(con, eError, "ERROR: unknown crc-algorithm %s", argv[2]);
a->private = NULL; a->private = NULL;