- more AsconStatus states

- enhancements in binprot
- make Logger public (logger.h)
- added lscprot (50 ms write delay for lakeshore models)
- additonal bug fixes
This commit is contained in:
2016-05-13 16:32:18 +02:00
parent e476d19040
commit fdb443441a
10 changed files with 169 additions and 28 deletions

View File

@ -27,6 +27,7 @@
*
* and formatItem is one of the follwing
*
* a number (decimal format): returned byte must match (else error)
* skip skip one byte
* skip<n> skip <n> bytes
* code returned function code, when bit7 is set, the response is
@ -39,6 +40,7 @@
* and append it to the response
* float convert 4 bytes from ieee float
* and append the number to the response
* dump read all bytes in read buffer as hex (only for tests)
* crc check crc (if wrong, "badCRC" is added to the response)
*
* multiple items in the response are space separated
@ -49,16 +51,16 @@
*
* 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)
* chksum-crc: simple 8bit checksum
*/
typedef enum {intType, hexType, floatType,
skipType, codeType, crcType, dumpType, errorType} BinDataType;
// dumpType, errorType must be the last items
typedef enum {intType, hexType, floatType, skipType, codeType, checkType,
crcType, dumpType, endType, errorType} BinDataType;
// dumpType, endType, errorType must be the last items
typedef enum {modbusCrc, kellerCrc, rsportCrc, syconCrc, chksumCrc} CrcAlgorithm;
@ -164,7 +166,7 @@ int BinReadItem(Ascon *a) {
if (sscanf(p->nextFmt, "%30s%n", item, &valen) <= 0) {
if (p->type < dumpType) {
p->dumpFrom = GetDynStringLength(a->rdBuffer);
p->type= dumpType;
p->type = endType;
}
p->expectedChars = 999;
return 0;
@ -193,13 +195,22 @@ int BinReadItem(Ascon *a) {
p->type = hexType;
} else if (strcasecmp(item, "code") == 0) {
p->type = codeType;
} else if (strcasecmp(item, "dump") == 0) {
p->dumpFrom = GetDynStringLength(a->rdBuffer);
p->type = dumpType;
} else if (strncasecmp(item, "skip", 4) == 0) {
size = 1;
sscanf(item + 4, "%d", &size);
p->expectedChars = size;
p->type = skipType;
} else {
return -1;
} else {
p->iValue = 0;
if (1 == sscanf(item, "%ld", &p->iValue)) {
p->expectedChars = 1;
p->type = checkType;
} else {
return -1;
}
}
p->nextFmt += valen;
return 1;
@ -268,6 +279,7 @@ int BinHandler(Ascon *a) {
int valen;
BinDataType type;
long iValue;
unsigned long uValue;
double fValue;
BinPrivate *p = a->private;
@ -327,11 +339,12 @@ int BinHandler(Ascon *a) {
BinError(a, "invalid integer");
return 1;
}
uValue = iValue;
for (i = size - 1; i >= 0; i--) {
if (i < sizeof data) {
data[i] = iValue % 256;
data[i] = uValue % 256;
}
iValue /= 256;
uValue >>= 8;
}
for (i = 0; i < size; i++) {
if (i >= sizeof data) {
@ -411,7 +424,11 @@ int BinHandler(Ascon *a) {
snprintf(item, sizeof item, "%2.2x ", (str[i] & 255));
DynStringConcat(p->result, item);
}
if (p->type == errorType) {
if (p->type == endType && p->dumpFrom < l) {
DynStringCopy(a->errmsg, "BINERR: superflous chars ");
DynStringConcat(a->errmsg, GetCharArray(p->result));
a->state = AsconFailed;
} else if (p->type == errorType) {
DynStringConcat(a->errmsg, GetCharArray(p->result));
a->state = AsconFailed;
} else {
@ -492,6 +509,7 @@ int BinHandler(Ascon *a) {
DynStringCopy(p->result, item);
}
break;
case endType:
case dumpType:
break;
case skipType:
@ -500,7 +518,7 @@ int BinHandler(Ascon *a) {
case intType:
p->expectedChars--;
p->iValue = p->iValue * 256 + (a->lastChar & 255);
if (p->expectedChars <= 0) {
if (p->expectedChars <= 0) {
snprintf(item, sizeof item, "%ld ", p->iValue);
DynStringConcat(p->result, item);
}
@ -555,6 +573,16 @@ int BinHandler(Ascon *a) {
/* subtract CRC char (undo the addition) and subtract crc higher four bits */
p->chksum -= a->lastChar + (a->lastChar & 0x0f) * 16;
}
break;
case checkType:
p->expectedChars--;
if ((a->lastChar & 255) != p->iValue) {
DynStringCopy(a->errmsg, "BINERR: mismatch ");
p->type = errorType;
p->dumpFrom = 0;
/* skip to end */
p->nextFmt = "";
}
}
if (p->expectedChars <= 0) {
BinReadItem(a);