plc.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #ifndef __PLC_H__
  2. #define __PLC_H__
  3. #include <stdint.h>
  4. #define PLC_AREA_I 0x81
  5. #define PLC_AREA_Q 0x82
  6. #define PLC_AREA_M 0x83
  7. #define PLC_AREA_DB 0x84
  8. #define PLC_MAX_BLOCK 256
  9. typedef struct {
  10. char ip[64];
  11. int port;
  12. int area; /* PLC_AREA_* */
  13. int db; /* used when area == PLC_AREA_DB */
  14. int read_start; /* first byte of the polled block (block-relative) */
  15. int read_len; /* bytes per poll, <= PLC_MAX_BLOCK */
  16. int poll_ms;
  17. int reconnect_ms;
  18. int tsap_local; /* e.g. 0x0100 */
  19. int tsap_remote; /* e.g. 0x0102 (rack0 slot2), 0x0001 for S7-200 SMART */
  20. } PlcConfig;
  21. /* Start background polling thread. 0 on success, -1 on error. */
  22. int plc_init(const PlcConfig *cfg);
  23. /* Stop thread and close connection. */
  24. void plc_shutdown(void);
  25. /* 1 when the last poll succeeded, 0 otherwise. */
  26. int plc_is_connected(void);
  27. /* Read a single bit of the cached block. byte_idx is relative to read_start.
  28. * Returns 0/1 on success, -1 if the block is not (yet) available. */
  29. int plc_get_bit(int byte_idx, int bit_idx);
  30. /* Copy one cached byte. Returns 0 on success, -1 if unavailable. */
  31. int plc_get_byte(int byte_idx, uint8_t *out);
  32. /* Human readable last error (static buffer). */
  33. const char *plc_last_error(void);
  34. #endif