- fixed logging of array objects
- added checksum to binprot
This commit is contained in:
9
arrobj.c
9
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;
|
||||
|
53
binprot.c
53
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;
|
||||
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;
|
||||
|
Reference in New Issue
Block a user