#ifndef __PLC_H__ #define __PLC_H__ #include #define PLC_AREA_I 0x81 #define PLC_AREA_Q 0x82 #define PLC_AREA_M 0x83 #define PLC_AREA_DB 0x84 #define PLC_MAX_BLOCK 256 typedef struct { char ip[64]; int port; int area; /* PLC_AREA_* */ int db; /* used when area == PLC_AREA_DB */ int read_start; /* first byte of the polled block (block-relative) */ int read_len; /* bytes per poll, <= PLC_MAX_BLOCK */ int poll_ms; int reconnect_ms; int tsap_local; /* e.g. 0x0100 */ int tsap_remote; /* e.g. 0x0102 (rack0 slot2), 0x0001 for S7-200 SMART */ } PlcConfig; /* Start background polling thread. 0 on success, -1 on error. */ int plc_init(const PlcConfig *cfg); /* Stop thread and close connection. */ void plc_shutdown(void); /* 1 when the last poll succeeded, 0 otherwise. */ int plc_is_connected(void); /* Read a single bit of the cached block. byte_idx is relative to read_start. * Returns 0/1 on success, -1 if the block is not (yet) available. */ int plc_get_bit(int byte_idx, int bit_idx); /* Copy one cached byte. Returns 0 on success, -1 if unavailable. */ int plc_get_byte(int byte_idx, uint8_t *out); /* Human readable last error (static buffer). */ const char *plc_last_error(void); #endif