add timeout processing and allow modbus device id to 39

r3023 | dcl | 2010-09-14 11:23:02 +1000 (Tue, 14 Sep 2010) | 1 line
This commit is contained in:
Douglas Clowes
2010-09-14 11:23:02 +10:00
parent 4ac338f4af
commit 8263b6916c

View File

@@ -29,7 +29,7 @@ int ModbusWriteStart(Ascon *a) {
buff = GetCharArray(a->wrBuffer);
dbgprintf("modbus-wr:%s\n", buff);
dev = strtoul(buff, &endp, 10);
if (endp == buff || dev < 1 || dev > 9) {
if (endp == buff || dev < 1 || dev > 39) {
dbgprintf("modbus-er: Bad device id: %d from %s\n", dev, buff);
a->state = AsconIdle;
AsconError(a, "Bad device id", 0);
@@ -61,7 +61,7 @@ int ModbusWriteStart(Ascon *a) {
buff = endp + 1;
unsigned int val;
val = strtoul(buff, &endp, 10);
if (endp == buff || val < 0 || val > 65535) {
if (endp == buff || val > 65535) {
dbgprintf("modbus-er: Bad value: %d from %s\n", val, buff);
a->state = AsconIdle;
AsconError(a, "Bad value", 0);
@@ -94,6 +94,8 @@ int ModbusReading(Ascon *a) {
ret = AsconReadChar(a->fd, &chr);
while (ret > 0) {
a->start = DoubleTime();
DynStringConcatChar(a->rdBuffer, chr);
cp = (unsigned char*) GetCharArray(a->rdBuffer);
blen = GetDynStringLength(a->rdBuffer);
@@ -130,7 +132,21 @@ int ModbusReading(Ascon *a) {
}
ret = AsconReadChar(a->fd, &chr);
}
return 1;
if (ret < 0) {
AsconError(a, "AsconReadChar failed:", errno);
return 0;
}
if (a->state == AsconReadDone) {
DynStringConcatChar(a->rdBuffer, '\0');
} else {
if (a->timeout > 0) {
if (DoubleTime() - a->start > a->timeout) {
AsconError(a, "read timeout", 0);
a->state = AsconTimeout;
}
}
}
return 0;
}
/** @brief Modbus TCP protocol handler.
@@ -145,12 +161,19 @@ int ModbusProtHandler(Ascon *a) {
ret = ModbusWriteStart(a);
return ret;
break;
case AsconReadStart:
a->start = DoubleTime();
ret = AsconStdHandler(a);
return ret;
break;
case AsconReading:
ret = ModbusReading(a);
return ret;
break;
default:
return AsconStdHandler(a);
ret = AsconStdHandler(a);
return ret;
break;
}
return 1;
}