make demo server interactive

master
Ondřej Hruška 5 months ago
parent ed4d54a510
commit c0fb0e8122
  1. 157
      src/main.c

@ -12,70 +12,143 @@ ModbusException_t startOfAccess(ModbusSlave_t *ms, ModbusFunction_t fcx, uint8_t
void endOfAccess(ModbusSlave_t *ms) { printf("End of access\n"); } void endOfAccess(ModbusSlave_t *ms) { printf("End of access\n"); }
typedef struct {
uint16_t num;
uint16_t value;
} register_slot_t;
typedef struct {
uint16_t num;
bool value;
} coil_slot_t;
//@formatter:off
static register_slot_t holdings[] = {
{1, 10},
{2, 20},
{3, 30},
{4, 40},
{5, 50},
{7, 70},
{0xffff, 0}
};
static register_slot_t inputs[] = {
{1, 11},
{2, 21},
{3, 31},
{4, 41},
{5, 51},
{7, 71},
{0xffff, 0}
};
static coil_slot_t coils[] = {
{1, 1},
{2, 0},
{3, 1},
{4, 0},
{5, 1},
{7, 1},
{0xffff, 0}
};
static coil_slot_t discretes[] = {
{1, 0},
{2, 1},
{3, 1},
{4, 1},
{5, 1},
{7, 0},
{0xffff, 0}
};
//@formatter:on
ModbusException_t readHolding(ModbusSlave_t *ms, uint16_t i, uint16_t *out) { ModbusException_t readHolding(ModbusSlave_t *ms, uint16_t i, uint16_t *out) {
printf("rh %d\n", i); printf("rh %d\n", i);
switch (i) {
case 107: const register_slot_t *p = holdings;
*out = 0xAE41; while (p->num != 0xffff) {
break; if (p->num == i) {
case 108: *out = p->value;
*out = 0x5652;
break;
case 109:
*out = 0x4340;
break;
default:
return MB_EXCEPTION_ILLEGAL_DATA_ADDRESS;
}
return 0; return 0;
}
p++;
}
return MB_EXCEPTION_ILLEGAL_DATA_ADDRESS;
} }
ModbusException_t readInput(ModbusSlave_t *ms, uint16_t i, uint16_t *out) { ModbusException_t readInput(ModbusSlave_t *ms, uint16_t i, uint16_t *out) {
printf("ri %d\n", i); printf("ri %d\n", i);
switch (i) {
case 8: const register_slot_t *p = inputs;
*out = 0xa; while (p->num != 0xffff) {
break; if (p->num == i) {
default: *out = p->value;
return MB_EXCEPTION_ILLEGAL_DATA_ADDRESS;
}
return 0; return 0;
}
p++;
}
return MB_EXCEPTION_ILLEGAL_DATA_ADDRESS;
} }
ModbusException_t readCoil(ModbusSlave_t *ms, uint16_t i, bool *out) { ModbusException_t readCoil(ModbusSlave_t *ms, uint16_t i, bool *out) {
uint8_t store[] = { const coil_slot_t *p = coils;
0b11001101, 0b01101011, 0b10110010, 0b00001110, 0b00011011, while (p->num != 0xffff) {
}; if (p->num == i) {
*out = p->value;
return 0;
}
p++;
}
if (i >= 19 && i <= 55) {
int pos = i - 19;
*out = 0 != (store[pos / 8] & (1 << (pos % 8)));
} else {
return MB_EXCEPTION_ILLEGAL_DATA_ADDRESS; return MB_EXCEPTION_ILLEGAL_DATA_ADDRESS;
}
return 0;
} }
ModbusException_t readDiscrete(ModbusSlave_t *ms, uint16_t i, bool *out) { ModbusException_t readDiscrete(ModbusSlave_t *ms, uint16_t i, bool *out) {
uint8_t store[] = { const coil_slot_t *p = discretes;
0b10101100, while (p->num != 0xffff) {
0b11011011, if (p->num == i) {
0b00110101, *out = p->value;
}; return 0;
}
p++;
}
if (i >= 196 && i <= 217) {
int pos = i - 196;
*out = 0 != (store[pos / 8] & (1 << (pos % 8)));
} else {
return MB_EXCEPTION_ILLEGAL_DATA_ADDRESS; return MB_EXCEPTION_ILLEGAL_DATA_ADDRESS;
}
return 0;
} }
ModbusException_t writeCoil(ModbusSlave_t *pSlave, uint16_t i, bool b) { ModbusException_t writeCoil(ModbusSlave_t *pSlave, uint16_t i, bool b) {
printf("Write coil %d <- %d\n", i, b); printf("Write coil %d <- %d\n", i, b);
coil_slot_t *p = coils;
while (p->num != 0) {
if (p->num == i) {
p->value = b;
return 0; return 0;
}
p++;
}
return MB_EXCEPTION_ILLEGAL_DATA_ADDRESS;
} }
ModbusException_t writeHolding(ModbusSlave_t *pSlave, uint16_t i, uint16_t i1) {
printf("Write reg %d <- %d\n", i, i1); ModbusException_t writeHolding(ModbusSlave_t *pSlave, uint16_t i, uint16_t val) {
printf("Write reg %d <- %d\n", i, val);
register_slot_t *p = holdings;
while (p->num != 0) {
if (p->num == i) {
p->value = val;
return 0; return 0;
}
p++;
}
return MB_EXCEPTION_ILLEGAL_DATA_ADDRESS;
} }
static ModbusSlave_t ms = { static ModbusSlave_t ms = {

Loading…
Cancel
Save