|
|
|
|
@ -5,6 +5,9 @@ |
|
|
|
|
|
|
|
|
|
#include "modbus.h" |
|
|
|
|
|
|
|
|
|
#define PORT 5002 |
|
|
|
|
#define MTU_SIZE 1024 |
|
|
|
|
|
|
|
|
|
ModbusException_t startOfAccess(ModbusSlave_t *ms, ModbusFunction_t fcx, uint8_t slave_id) { |
|
|
|
|
printf("Start of access: fc%02d, slave %d\n", fcx, slave_id); |
|
|
|
|
return 0; |
|
|
|
|
@ -22,45 +25,26 @@ typedef struct { |
|
|
|
|
bool value; |
|
|
|
|
} coil_slot_t; |
|
|
|
|
|
|
|
|
|
// pattern indicating end of the register array
|
|
|
|
|
#define ENDSLOT {0xffff, 0} |
|
|
|
|
|
|
|
|
|
//@formatter:off
|
|
|
|
|
static register_slot_t holdings[] = { |
|
|
|
|
{1, 10}, |
|
|
|
|
{2, 20}, |
|
|
|
|
{3, 30}, |
|
|
|
|
{4, 40}, |
|
|
|
|
{5, 50}, |
|
|
|
|
{7, 70}, |
|
|
|
|
{0xffff, 0} |
|
|
|
|
{2, 15}, |
|
|
|
|
ENDSLOT |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
static register_slot_t inputs[] = { |
|
|
|
|
{1, 11}, |
|
|
|
|
{2, 21}, |
|
|
|
|
{3, 31}, |
|
|
|
|
{4, 41}, |
|
|
|
|
{5, 51}, |
|
|
|
|
{7, 71}, |
|
|
|
|
{0xffff, 0} |
|
|
|
|
{103, 67}, |
|
|
|
|
ENDSLOT |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
static coil_slot_t coils[] = { |
|
|
|
|
{1, 1}, |
|
|
|
|
{2, 0}, |
|
|
|
|
{3, 1}, |
|
|
|
|
{4, 0}, |
|
|
|
|
{5, 1}, |
|
|
|
|
{7, 1}, |
|
|
|
|
{0xffff, 0} |
|
|
|
|
ENDSLOT |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
static coil_slot_t discretes[] = { |
|
|
|
|
{1, 0}, |
|
|
|
|
{2, 1}, |
|
|
|
|
{3, 1}, |
|
|
|
|
{4, 1}, |
|
|
|
|
{5, 1}, |
|
|
|
|
{7, 0}, |
|
|
|
|
{0xffff, 0} |
|
|
|
|
ENDSLOT |
|
|
|
|
}; |
|
|
|
|
//@formatter:on
|
|
|
|
|
|
|
|
|
|
@ -184,15 +168,26 @@ int msg_received_fce(int fd, const simple_msg_t *msg) { |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// need a wrapper to adapt the return type to atexit()
|
|
|
|
|
void atexitcb() { simple_tcp_server_stop(); } |
|
|
|
|
|
|
|
|
|
int main() { |
|
|
|
|
// ModbusError_t e = mb_handleRequest(&ms, buf, pldlen, buf2, buflen, &respsize);
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) { |
|
|
|
|
atexit(atexitcb); |
|
|
|
|
|
|
|
|
|
printf("Starting TCP server\n"); |
|
|
|
|
simple_tcp_server_start(5002, 1024, msg_received_fce, NULL, NULL); |
|
|
|
|
int port = PORT; // default port
|
|
|
|
|
|
|
|
|
|
if (argc > 1) { |
|
|
|
|
// this is unsafe
|
|
|
|
|
port = atoi(argv[1]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (port <= 0 || port > 65535) { |
|
|
|
|
printf("Invalid port: %d\n", PORT); |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
printf("Starting TCP server on port %d\n", port); |
|
|
|
|
simple_tcp_server_start(port, MTU_SIZE, msg_received_fce, NULL, NULL); |
|
|
|
|
|
|
|
|
|
printf("Thread created, waiting\n"); |
|
|
|
|
while (!simple_tcp_server_ended()) { |
|
|
|
|
|