fins.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. #ifndef INCLUDED_FINSLIB_FINS_H
  2. #define INCLUDED_FINSLIB_FINS_H
  3. #if defined(_WIN32)
  4. #include <winsock2.h>
  5. #pragma comment(lib, "ws2_32.lib")
  6. #endif
  7. #include <stdint.h>
  8. #include <time.h>
  9. #if defined(_WIN32)
  10. #define inet_pton InetPton
  11. #else
  12. #define INVALID_SOCKET (-1)
  13. typedef int SOCKET;
  14. #define closesocket close
  15. #endif
  16. /********************************************************/
  17. /* */
  18. #define FINS_ICF 0 /* Information Control Field */
  19. #define FINS_RSV 1 /* Reserved */
  20. #define FINS_GCT 2 /* Gateway Counter. Init op 0x07 naar CPU v2.0 of 0x02 */
  21. #define FINS_DNA 3 /* Destination Network Address (0..127) 0 = lokaal */
  22. #define FINS_DA1 4 /* Destination Node Address (0..254) 0 = lokaal */
  23. #define FINS_DA2 5 /* Destination Unit Address (0..31) 0 = CPU unit */
  24. #define FINS_SNA 6 /* Source Network Address (0..127) 0 = lokaal */
  25. #define FINS_SA1 7 /* Source Node Address (0..254) 0 = intern in PLC */
  26. #define FINS_SA2 8 /* Source Unit Address (0..31) 0 = CPU unit */
  27. #define FINS_SID 9 /* Service ID, uniek nummer 0..FF per commando */
  28. #define FINS_MRC 10 /* Main Request Code */
  29. #define FINS_SRC 11 /* Sub Request Code */
  30. /* */
  31. /********************************************************/
  32. #define FINS_MRES 0
  33. #define FINS_SRES 1
  34. /********************************************************/
  35. /* */
  36. #define FINS_HEADER_LEN 12 /* Length of a FINS header */
  37. #define FINS_BODY_LEN 2000 /* Maximum length of a FINS body */
  38. #define FINS_MAX_TCP_HEADER 32 /* Maximum length of a FINS/TCP header */
  39. /* */
  40. /********************************************************/
  41. #define FINS_PORT_MIN 0
  42. #define FINS_PORT_RESERVED 1024
  43. #define FINS_PORT_MAX 65535
  44. #define FINS_TIMEOUT 60
  45. #define FINS_DEFAULT_PORT 9600 /* Default port for FINS TCP and UDP communications */
  46. /********************************************************/
  47. /* */
  48. #define FINS_MODE_UNKNOWN 0 /* PLC communication mode unknown */
  49. #define FINS_MODE_CV 1 /* PLC communicates like a CV PLC */
  50. #define FINS_MODE_CS 2 /* PLC communicates like a CS/CJ/CP PLC */
  51. /* */
  52. /********************************************************/
  53. #define FI_RD 0x01
  54. #define FI_WR 0x02
  55. #define FI_FILL 0x04
  56. #define FI_MRD 0x08
  57. #define FI_TRS 0x10
  58. #define FI_TRD 0x20
  59. #define FI_FRC 0x40
  60. #define FINS_MAX_READ_WORDS_SYSWAY 269
  61. #define FINS_MAX_READ_WORDS_ETHERNET 999
  62. #define FINS_MAX_READ_WORDS_CLINK 999
  63. #define FINS_MAX_READ_WORDS_SYSMAC_LINK 269
  64. #define FINS_MAX_READ_WORDS_DEVICENET 269
  65. #define FINS_MAX_WRITE_WORDS_SYSWAY 267
  66. #define FINS_MAX_WRITE_WORDS_ETHERNET 996
  67. #define FINS_MAX_WRITE_WORDS_CLINK 996
  68. #define FINS_MAX_WRITE_WORDS_SYSMAC_LINK 267
  69. #define FINS_MAX_WRITE_WORDS_DEVICENET 267
  70. /********************************************************/
  71. /* */
  72. #define FINS_COMM_TYPE_UNKNOWN 0x00 /* No communication protocol has been selected */
  73. #define FINS_COMM_TYPE_TCP 0x01 /* The communication protocol is FINS/TCP */
  74. #define FINS_COMM_TYPE_UDP 0x02 /* The communication protocol is FINS/UDP */
  75. /* */
  76. /********************************************************/
  77. /********************************************************/
  78. /* */
  79. #define FINS_RETVAL_ERRNO_BASE 0xC000 /* All higher error numbers are errno.h values */
  80. /* */
  81. #define FINS_RETVAL_SUCCESS 0x0000 /* Execution was successful */
  82. #define FINS_RETVAL_NOT_INITIALIZED 0x8001 /* The connection with the PLC was not initialized */
  83. #define FINS_RETVAL_NOT_CONNECTED 0x8002 /* There is no connection with the remote PLC */
  84. #define FINS_RETVAL_OUT_OF_MEMORY 0x8003 /* There was not enough free memory for the action */
  85. #define FINS_RETVAL_SUCCESS_LAST_DATA 0x8004 /* Execution successful and last data delivered */
  86. #define FINS_RETVAL_INVALID_IP_ADDRESS 0x8005 /* The IP address passed to inet_pton is invalid */
  87. #define FINS_RETVAL_MAX_ERROR_COUNT 0x8006 /* The connection was closed after reaching max errors */
  88. #define FINS_RETVAL_SYNC_ERROR 0x8007 /* Synchronization error. Some packets probably lost */
  89. /* */
  90. #define FINS_RETVAL_NO_READ_ADDRESS 0x8101 /* No read address in the remote PLC was specified */
  91. #define FINS_RETVAL_NO_WRITE_ADDRESS 0x8102 /* No write address in the remote PLC was specified */
  92. #define FINS_RETVAL_NO_DATA_BLOCK 0x8103 /* No local data memory block was provided */
  93. /* */
  94. #define FINS_RETVAL_INVALID_READ_ADDRESS 0x8201 /* An invalid read address string was specified */
  95. #define FINS_RETVAL_INVALID_WRITE_ADDRESS 0x8202 /* An invalid write address string was specified */
  96. /* */
  97. #define FINS_RETVAL_INVALID_READ_AREA 0x8301 /* No read area associated with the address */
  98. #define FINS_RETVAL_INVALID_WRITE_AREA 0x8302 /* No write area associated with the address */
  99. #define FINS_RETVAL_INVALID_FILL_AREA 0x8303 /* No fill area associated with the address */
  100. /* */
  101. #define FINS_RETVAL_INVALID_PARAMETER_AREA 0x8401 /* The parameter area is invalid */
  102. /* */
  103. #define FINS_RETVAL_INVALID_DATE 0x8501 /* The provided date is not valid */
  104. /* */
  105. #define FINS_RETVAL_INVALID_DISK 0x8601 /* An invalid disk was specified */
  106. #define FINS_RETVAL_INVALID_PATH 0x8602 /* An invalid path on a disk was specified */
  107. #define FINS_RETVAL_INVALID_FILENAME 0x8603 /* An invalid filename was specified */
  108. /* */
  109. #define FINS_RETVAL_NO_COMMAND 0x8701 /* No command specified when executing a function */
  110. #define FINS_RETVAL_NO_COMMAND_LENGTH 0x8702 /* No command length specified when executing a function*/
  111. #define FINS_RETVAL_BODY_TOO_SHORT 0x8703 /* Command body length too short */
  112. #define FINS_RETVAL_BODY_TOO_LONG 0x8704 /* The FINS body is longer than allowed */
  113. #define FINS_RETVAL_HEADER_SEND_ERROR 0x8705 /* Error sending complete header */
  114. #define FINS_RETVAL_COMMAND_SEND_ERROR 0x8706 /* Error sending complete command */
  115. #define FINS_RETVAL_RESPONSE_INCOMPLETE 0x8707 /* Response frame is shorter than expected */
  116. #define FINS_RETVAL_ILLEGAL_FINS_COMMAND 0x870B /* Illegal FINS command */
  117. #define FINS_RETVAL_RESPONSE_HEADER_INCOMPLETE 0x870C /* The received response header is incomplete */
  118. #define FINS_RETVAL_INVALID_FORCE_COMMAND 0x870D /* An invalid FORCE mode was specified */
  119. /* */
  120. #define FINS_RETVAL_TRY_LATER 0x8801 /* Please try again later */
  121. /* */
  122. #define FINS_RETVAL_CLOSED_BY_REMOTE 0x8900 /* TCP connection closed by remote node without error */
  123. #define FINS_RETVAL_NO_FINS_HEADER 0x8901 /* First 4 characters of TCP header are not "FINS" */
  124. #define FINS_RETVAL_DATA_LENGTH_TOO_LONG 0x8902 /* TCP connection data length too long */
  125. #define FINS_RETVAL_COMMAND_NOT_SUPPORTED 0x8903 /* TCP connection command not supported */
  126. #define FINS_RETVAL_ALL_CONNECTIONS_IN_USE 0x8904 /* All TCP connections are in use */
  127. #define FINS_RETVAL_NODE_ALREADY_CONNECTED 0x8905 /* Node is already connected */
  128. #define FINS_RETVAL_NODE_IP_PROTECTED 0x8906 /* IP address of client not in allowed IP adres list */
  129. #define FINS_RETVAL_CLIENT_NODE_OUT_OF_RANGE 0x8907 /* TCP the client node address is out of range */
  130. #define FINS_RETVAL_SAME_NODE_ADDRESS 0x8908 /* TCP client and server have the same node address */
  131. #define FINS_RETVAL_NO_NODE_ADDRESS_AVAILABLE 0x8909 /* TCP connection no node address available */
  132. /* */
  133. #define FINS_RETVAL_WSA_UNRECOGNIZED_ERROR 0x8A00 /* Windows WSA returned an unrecognized error code */
  134. #define FINS_RETVAL_WSA_NOT_INITIALIZED 0x8A01 /* Windows WSA was not properly initialized */
  135. #define FINS_RETVAL_WSA_E_NET_DOWN 0x8A02 /* Windows WSA the network subsystem or provided failed */
  136. #define FINS_RETVAL_WSA_E_AF_NO_SUPPORT 0x8A03 /* Windows WSA the address familiy is not supported */
  137. #define FINS_RETVAL_WSA_E_IN_PROGRESS 0x8A04 /* Windows WSA a blocking socket 1.1 call is in progres */
  138. #define FINS_RETVAL_WSA_E_MFILE 0x8A05 /* Windows WSA no more socket descriptors available */
  139. #define FINS_RETVAL_WSA_E_INVAL 0x8A06 /* Windows WSA Invalid argument supplied */
  140. #define FINS_RETVAL_WSA_E_INVALID_PROVIDER 0x8A07 /* Windows WSA Server provider function invalid */
  141. #define FINS_RETVAL_WSA_E_INVALID_PROCTABLE 0x8A08 /* Windows WSA Invalid procedure table */
  142. #define FINS_RETVAL_WSA_E_NOBUFS 0x8A09 /* Windows WSA No buffer space available */
  143. #define FINS_RETVAL_WSA_E_PROTO_NO_SUPPORT 0x8A0A /* Windows WSA the protocol is not supported */
  144. #define FINS_RETVAL_WSA_E_PROTO_TYPE 0x8A0B /* Windows WSA Wrong protocol type for this socket */
  145. #define FINS_RETVAL_WSA_E_PROVIDER_FAILED_INIT 0x8A0C /* Windows WSA Provider failed initialization */
  146. #define FINS_RETVAL_WSA_E_SOCKT_NO_SUPPORT 0x8A0D /* Windows WSA The specified socket type not supported */
  147. #define FINS_RETVAL_WSA_SYS_NOT_READY 0x8A0E /* Windows WSA The network subsystem is not ready */
  148. #define FINS_RETVAL_WSA_VER_NOT_SUPPORTED 0x8A0F /* Windows WSA The socket version is not supported */
  149. #define FINS_RETVAL_WSA_E_PROC_LIM 0x8A10 /* Windows WSA Process number limit reached */
  150. #define FINS_RETVAL_WSA_E_FAULT 0x8A11 /* Windows WSA The parameter is not valid */
  151. #define FINS_RETVAL_WSA_E_NET_RESET 0x8A12 /* Windows WSA Connection timeout during Keep Alive */
  152. #define FINS_RETVAL_WSA_E_NO_PROTO_OPT 0x8A13 /* Windows WSA Unsupported option for socket */
  153. #define FINS_RETVAL_WSA_E_NOT_CONN 0x8A14 /* Windows WSA Connection reset during Keep Alive */
  154. #define FINS_RETVAL_WSA_E_NOT_SOCK 0x8A15 /* Windows WSA The descriptor is not a socket */
  155. #define FINS_RETVAL_WSA_E_ACCES 0x8A16 /* Windows WSA Socket access violation */
  156. #define FINS_RETVAL_WSA_E_ADDR_IN_USE 0x8A17 /* Windows WSA The address is already in use */
  157. #define FINS_RETVAL_WSA_E_ADDR_NOT_AVAIL 0x8A18 /* Windows WSA The address is not available */
  158. #define FINS_RETVAL_WSA_E_INTR 0x8A19 /* Windows WSA The blocking 1.1 call was cancelled */
  159. #define FINS_RETVAL_WSA_E_ALREADY 0x8A1A /* Windows WSA Non blocking call already in progress */
  160. #define FINS_RETVAL_WSA_E_CONN_REFUSED 0x8A1B /* Windows WSA The connection was refused */
  161. #define FINS_RETVAL_WSA_E_IS_CONN 0x8A1C /* Windows WSA Socket is already connected */
  162. #define FINS_RETVAL_WSA_E_NET_UNREACH 0x8A1D /* Windows WSA Network is unreacheable */
  163. #define FINS_RETVAL_WSA_E_HOST_UNREACH 0x8A1E /* Windows WSA Host is unreacheable */
  164. #define FINS_RETVAL_WSA_E_TIMED_OUT 0x8A1F /* Windows WSA The connection timed out */
  165. #define FINS_RETVAL_WSA_E_WOULD_BLOCK 0x8A20 /* Windows WSA Non-blocking connection would block */
  166. /* */
  167. #define FINS_RETVAL_CANCELED 0x0001 /* End code 0x0001 The service was canceled */
  168. /* */
  169. #define FINS_RETVAL_LOCAL_NODE_NOT_IN_NETWORK 0x0101 /* End code 0x0101 Local node is not in network */
  170. #define FINS_RETVAL_LOCAL_TOKEN_TIMEOUT 0x0102 /* End code 0x0102 Local node token timeout */
  171. #define FINS_RETVAL_LOCAL_RETRIES_FAILED 0x0103 /* End code 0x0103 Local node retries failed */
  172. #define FINS_RETVAL_LOCAL_TOO_MANY_SEND_FRAMES 0x0104 /* End code 0x0104 Local node too many send frames */
  173. #define FINS_RETVAL_LOCAL_ADDRESS_RANGE_ERROR 0x0105 /* End code 0x0105 Local node address range error */
  174. #define FINS_RETVAL_LOCAL_ADDRESS_DUPLICATION 0x0106 /* End code 0x0106 Local node address duplication */
  175. /* */
  176. #define FINS_RETVAL_DEST_NOT_IN_NETWORK 0x0201 /* End code 0x0201 Destination is not in network */
  177. #define FINS_RETVAL_DEST_UNIT_MISSING 0x0202 /* End code 0x0202 Destination unit missing */
  178. #define FINS_RETVAL_DEST_THIRD_NODE_MISSING 0x0203 /* End code 0x0203 Destination third node missing */
  179. #define FINS_RETVAL_DEST_NODE_BUSY 0x0204 /* End code 0x0204 Destination node is busy */
  180. #define FINS_RETVAL_DEST_TIMEOUT 0x0205 /* End code 0x0205 Destination response timed out */
  181. /* */
  182. #define FINS_RETVAL_CONTR_COMM_ERROR 0x0301 /* End code 0x0301 Communications controller error */
  183. #define FINS_RETVAL_CONTR_CPU_UNIT_ERROR 0x0302 /* End code 0x0302 CPU Unit error */
  184. #define FINS_RETVAL_CONTR_BOARD_ERROR 0x0303 /* End code 0x0303 Controller board error */
  185. #define FINS_RETVAL_CONTR_UNIT_NUMBER_ERROR 0x0304 /* End code 0x0304 Unit number error */
  186. /* */
  187. #define FINS_RETVAL_UNSUPPORTED_COMMAND 0x0401 /* End code 0x0401 Undefined command */
  188. #define FINS_RETVAL_UNSUPPORTED_VERSION 0x0402 /* End code 0x0402 Not supported by model/version */
  189. /* */
  190. #define FINS_RETVAL_ROUTING_ADDRESS_ERROR 0x0501 /* End code 0x0501 Routing destination address error */
  191. #define FINS_RETVAL_ROUTING_NO_TABLES 0x0502 /* End code 0x0502 No routing tables available */
  192. #define FINS_RETVAL_ROUTING_TABLE_ERROR 0x0503 /* End code 0x0503 Routing table error */
  193. #define FINS_RETVAL_ROUTING_TOO_MANY_RELAYS 0x0504 /* End code 0x0504 Too many relays */
  194. /* */
  195. #define FINS_RETVAL_COMMAND_TOO_LONG 0x1001 /* End code 0x1001 Command too long */
  196. #define FINS_RETVAL_COMMAND_TOO_SHORT 0x1002 /* End code 0x1002 Command too short */
  197. #define FINS_RETVAL_COMMAND_ELEMENT_MISMATCH 0x1003 /* End code 0x1003 Elements/data don't match */
  198. #define FINS_RETVAL_COMMAND_FORMAT_ERROR 0x1004 /* End code 0x1004 Command format error */
  199. #define FINS_RETVAL_COMMAND_HEADER_ERROR 0x1005 /* End code 0x1005 Command header error */
  200. /* */
  201. #define FINS_RETVAL_PARAM_AREA_MISSING 0x1101 /* End code 0x1101 Parameter area classification missing*/
  202. #define FINS_RETVAL_PARAM_ACCESS_SIZE_ERROR 0x1102 /* End code 0x1102 Parameter access size wrong */
  203. #define FINS_RETVAL_PARAM_START_ADDRESS_ERROR 0x1103 /* End code 0x1103 Start address out of range */
  204. #define FINS_RETVAL_PARAM_END_ADDRESS_ERROR 0x1104 /* End code 0x1104 End address out of range */
  205. #define FINS_RETVAL_PARAM_PROGRAM_MISSING 0x1106 /* End code 0x1106 Program number is missing */
  206. #define FINS_RETVAL_PARAM_RELATIONAL_ERROR 0x1109 /* End code 0x1109 Parameter relational error */
  207. #define FINS_RETVAL_PARAM_DUPLICATE_ACCESS 0x110A /* End code 0x110A Duplicate data access */
  208. #define FINS_RETVAL_PARAM_RESPONSE_TOO_LONG 0x110B /* End code 0x110B Response too long */
  209. #define FINS_RETVAL_PARAM_PARAMETER_ERROR 0x110C /* End code 0x110C Parameter error */
  210. /* */
  211. #define FINS_RETVAL_RD_ERR_PROTECTED 0x2002 /* End code 0x2002 The program area is protected */
  212. #define FINS_RETVAL_RD_ERR_TABLE_MISSING 0x2003 /* End code 0x2003 The table is not existing */
  213. #define FINS_RETVAL_RD_ERR_DATA_MISSING 0x2004 /* End code 0x2004 The search data does not exist */
  214. #define FINS_RETVAL_RD_ERR_PROGRAM_MISSING 0x2005 /* End code 0x2005 The program does not exist */
  215. #define FINS_RETVAL_RD_ERR_FILE_MISSING 0x2006 /* End code 0x2006 The file does not exist */
  216. #define FINS_RETVAL_RD_ERR_DATA_MISMATCH 0x2007 /* End code 0x2007 Data comparison failed */
  217. /* */
  218. #define FINS_RETVAL_WR_ERR_READ_ONLY 0x2101 /* End code 0x2101 The specified area is read-only */
  219. #define FINS_RETVAL_WR_ERR_PROTECTED 0x2102 /* End code 0x2102 The program area is protected */
  220. #define FINS_RETVAL_WR_ERR_CANNOT_REGISTER 0x2103 /* End code 0x2103 Cannot register file */
  221. #define FINS_RETVAL_WR_ERR_PROGRAM_MISSING 0x2105 /* End code 0x2105 Program number is not valid */
  222. #define FINS_RETVAL_WR_ERR_FILE_MISSING 0x2106 /* End code 0x2106 File does not exist */
  223. #define FINS_RETVAL_WR_ERR_FILE_EXISTS 0x2107 /* End code 0x2107 The file already exists */
  224. #define FINS_RETVAL_WR_ERR_CANNOT_CHANGE 0x2108 /* End code 0x2108 Cannot change the data */
  225. /* */
  226. #define FINS_RETVAL_MODE_NOT_DURING_EXECUTION 0x2201 /* End code 0x2201 Not possible during execution */
  227. #define FINS_RETVAL_MODE_NOT_DURING_RUN 0x2202 /* End code 0x2202 Not possible while running */
  228. #define FINS_RETVAL_MODE_IS_PROGRAM 0x2203 /* End code 0x2203 Not possible in program mode */
  229. #define FINS_RETVAL_MODE_IS_DEBUG 0x2204 /* End code 0x2204 Not possible in debug mode */
  230. #define FINS_RETVAL_MODE_IS_MONITOR 0x2205 /* End code 0x2205 Not possible in monitor mode */
  231. #define FINS_RETVAL_MODE_IS_RUN 0x2206 /* End code 0x2206 Not possible in run mode */
  232. #define FINS_RETVAL_MODE_NODE_NOT_POLLING 0x2207 /* End code 0x2207 Specified node not in polling mode */
  233. #define FINS_RETVAL_MODE_NO_STEP 0x2208 /* End code 0x2208 Step cannot be executed */
  234. /* */
  235. #define FINS_RETVAL_DEVICE_FILE_MISSING 0x2301 /* End code 0x2301 File device missing */
  236. #define FINS_RETVAL_DEVICE_MEMORY_MISSING 0x2302 /* End code 0x2302 There is no file memory */
  237. #define FINS_RETVAL_DEVICE_CLOCK_MISSING 0x2303 /* End code 0x2303 There is no clock */
  238. /* */
  239. #define FINS_RETVAL_DATALINK_TABLE_MISSING 0x2401 /* End code 0x2401 Data link table missing or corrupt */
  240. /* */
  241. #define FINS_RETVAL_UNIT_MEMORY_CONTENT_ERROR 0x2502 /* End code 0x2502 Memory content error */
  242. #define FINS_RETVAL_UNIT_IO_SETTING_ERROR 0x2503 /* End code 0x2503 I/O setting error */
  243. #define FINS_RETVAL_UNIT_TOO_MANY_IO_POINTS 0x2504 /* End code 0x2504 Too many I/O points registered */
  244. #define FINS_RETVAL_UNIT_CPU_BUS_ERROR 0x2505 /* End code 0x2505 CPU bus line error */
  245. #define FINS_RETVAL_UNIT_IO_DUPLICATION 0x2506 /* End code 0x2506 Duplicate I/O address */
  246. #define FINS_RETVAL_UNIT_IO_BUS_ERROR 0x2507 /* End code 0x2507 I/O bus error */
  247. #define FINS_RETVAL_UNIT_SYSMAC_BUS2_ERROR 0x2509 /* End code 0x2509 Sysmac bus/2 error */
  248. #define FINS_RETVAL_UNIT_CPU_BUS_UNIT_ERROR 0x250A /* End code 0x250A CPU bus unit error */
  249. #define FINS_RETVAL_UNIT_SYSMAC_BUS_DUPLICATION 0x250D /* End code 0x250D Same word is used more than once */
  250. #define FINS_RETVAL_UNIT_MEMORY_ERROR 0x250F /* End code 0x250F Memory error in internal memory */
  251. #define FINS_RETVAL_UNIT_SYSMAC_BUS_TERMINATOR 0x2510 /* End code 0x2510 Sysmac bus terminator missing */
  252. /* */
  253. #define FINS_RETVAL_COMMAND_NO_PROTECTION 0x2601 /* End code 0x2601 The specified area is not protected */
  254. #define FINS_RETVAL_COMMAND_WRONG_PASSWORD 0x2602 /* End code 0x2602 Wrong password specified */
  255. #define FINS_RETVAL_COMMAND_PROTECTED 0x2604 /* End code 0x2604 The specified area is protected */
  256. #define FINS_RETVAL_COMMAND_SERVICE_EXECUTING 0x2605 /* End code 0x2605 Service is already executing */
  257. #define FINS_RETVAL_COMMAND_SERVICE_STOPPED 0x2606 /* End code 0x2606 The service is stopped */
  258. #define FINS_RETVAL_COMMAND_NO_EXECUTION_RIGHT 0x2607 /* End code 0x2607 No execution right */
  259. #define FINS_RETVAL_COMMAND_SETTINGS_INCOMPLETE 0x2608 /* End code 0x2608 The settings are not complete */
  260. #define FINS_RETVAL_COMMAND_ITEMS_NOT_SET 0x2609 /* End code 0x2609 Necessary items not set */
  261. #define FINS_RETVAL_COMMAND_ALREADY_DEFINED 0x260A /* End code 0x260A Number already defined */
  262. #define FINS_RETVAL_COMMAND_ERROR_WONT_CLEAR 0x260B /* End code 0x260B Error will not clear */
  263. /* */
  264. #define FINS_RETVAL_ACCESS_NO_RIGHTS 0x3001 /* End code 0x3001 No access right */
  265. /* */
  266. #define FINS_RETVAL_ABORTED 0x4001 /* End code 0x3001 Service aborted */
  267. /* */
  268. /********************************************************/
  269. #pragma pack(push,1)
  270. struct fins_command_tp
  271. {
  272. unsigned char header[FINS_HEADER_LEN];
  273. unsigned char body[FINS_BODY_LEN];
  274. };
  275. #pragma pack(pop)
  276. /********************************************************/
  277. struct fins_area_tp
  278. {
  279. /* */
  280. int plc_mode; /* CS/CJ or CV mode communication */
  281. const char* name; /* Text string with the area short code */
  282. int bits; /* Number of bits in the data */
  283. int length; /* Number of bytes per element */
  284. uint8_t area; /* Area code */
  285. uint32_t low_id; /* Lowest identificator */
  286. uint32_t high_id; /* Highest identificator */
  287. uint32_t low_addr; /* Lowest memory address */
  288. uint32_t high_addr; /* Highest memory address */
  289. int32_t access; /* Read or Read/Write access */
  290. bool force; /* Force status used */
  291. }; /* */
  292. /********************************************************/
  293. struct fins_sys_tp
  294. {
  295. char address[128];
  296. uint16_t port;
  297. SOCKET sockfd;
  298. time_t timeout;
  299. int error_count;
  300. int error_max;
  301. int last_error;
  302. bool error_changed;
  303. uint8_t local_net;
  304. uint8_t local_node;
  305. uint8_t local_unit;
  306. uint8_t remote_net;
  307. uint8_t remote_node;
  308. uint8_t remote_unit;
  309. uint8_t sid;
  310. uint8_t comm_type;
  311. char model[21];
  312. char version[21];
  313. int plc_mode;
  314. };
  315. struct fins_cpudata_tp
  316. {
  317. char model[21];
  318. char version[21];
  319. unsigned char system_block[40];
  320. int largest_em_bank;
  321. int program_area_size;
  322. int iom_size;
  323. int number_of_dm_words;
  324. int timer_counter_size;
  325. int em_non_file_memory_size;
  326. int memory_card_size;
  327. int num_sysmac_bus_masters;
  328. int num_racks;
  329. uint16_t bus_unit_id[16];
  330. bool bus_unit_present[16];
  331. bool dip_switch[8];
  332. uint8_t memory_card_type;
  333. };
  334. struct fins_address_tp
  335. {
  336. char name[4];
  337. uint32_t main_address;
  338. uint32_t sub_address;
  339. };
  340. int finslib_cpu_unit_data_read(struct fins_sys_tp* sys, struct fins_cpudata_tp* cpudata);
  341. //断开连接
  342. //传入参数为FINS设备指针
  343. void finslib_disconnect(struct fins_sys_tp* sys);
  344. //读取N个bit位
  345. //传入参数为FINS设备指针,起始地址的char指针,格式类似“CIO100.1”,存储结果的bool值区域指针,需要读取的位数
  346. int finslib_memory_area_read_bit(struct fins_sys_tp* sys, const char* start, bool* data, size_t num_bits);
  347. //读取N个int16值
  348. //传入参数为FINS设备指针,起始地址的char指针,格式类似“CIO100”,存储结果的int16_t值区域指针,需要读取的int16_t的个数
  349. int finslib_memory_area_read_int16(struct fins_sys_tp* sys, const char* start, int16_t* data, size_t num_int16);
  350. //读取N个uint16值
  351. //传入参数为FINS设备指针,起始地址的char指针,格式类似“CIO100”,存储结果的uint16_t值区域指针,需要读取的uint16_t的个数
  352. int finslib_memory_area_read_uint16(struct fins_sys_tp* sys, const char* start, uint16_t* data, size_t num_uint16);
  353. //写入N个bit位
  354. //传入参数为FINS设备指针,起始地址的char指针,格式类似“CIO100.1”,要写入的数据所在的内存指针,需要写入的bit值个数
  355. int finslib_memory_area_write_bit(struct fins_sys_tp* sys, const char* start, const bool* data, size_t num_bit);
  356. //写入N个int16值
  357. //传入参数为FINS设备指针,起始地址的char指针,格式类似“CIO100”,要写入的数据所在的内存指针,需要写入的int16_t的个数
  358. int finslib_memory_area_write_int16(struct fins_sys_tp* sys, const char* start, const int16_t* data, size_t num_int16);
  359. //写入N个uint16值
  360. //传入参数为FINS设备指针,起始地址的char指针,格式类似“CIO100”,要写入的数据所在的内存指针,需要写入的uint16_t的个数
  361. int finslib_memory_area_write_uint16(struct fins_sys_tp* sys, const char* start, const uint16_t* data,
  362. size_t num_uint16);
  363. void finslib_milli_second_sleep(int msec);
  364. time_t finslib_monotonic_sec_timer(void);
  365. //连接PLC,简单模式
  366. //传入FINS设备指针,远程设备IP,端口号,远程设备节点号,错误代码指针和最大错误数量(若错误数超过该值则会返回达到错误上限的错误)
  367. struct fins_sys_tp* finslib_tcp_connect(struct fins_sys_tp* sys, const char* address, uint16_t port, uint8_t remote_node, int* error_val, int error_max);
  368. //连接PLC,正常模式
  369. //传入FINS设备指针,远程设备IP,端口号,本地网络号,本地节点号,本地单元号,远程设备网络号,远程设备节点号,远程设备单元号,错误代码指针和最大错误数(若错误数超过该值则会返回达到错误上限的错误)
  370. struct fins_sys_tp* finslib_tcp_connect(struct fins_sys_tp* sys, const char* address, uint16_t port, uint8_t local_net,
  371. uint8_t local_node, uint8_t local_unit, uint8_t remote_net, uint8_t remote_node,
  372. uint8_t remote_unit, int* error_val, int error_max);
  373. int XX_finslib_communicate(struct fins_sys_tp* sys, struct fins_command_tp* command, size_t* bodylen);
  374. bool XX_finslib_decode_address(const char* str, struct fins_address_tp* address);
  375. void XX_finslib_init_command(struct fins_sys_tp* sys, struct fins_command_tp* command, uint8_t mrc, uint8_t src);
  376. const struct fins_area_tp* XX_finslib_search_area(struct fins_sys_tp* sys, const struct fins_address_tp* address,
  377. int bits, uint32_t access, bool force);
  378. int XX_finslib_wsa_errorcode_to_fins_retval(int errorcode);
  379. #endif