modbus.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. nanoMODBUS - A compact MODBUS RTU/TCP C library for microcontrollers
  3. MIT License
  4. Copyright (c) 2024 Valerio De Benedetto (@debevv)
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in all
  12. copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. */
  21. /** @file */
  22. /*! \mainpage nanoMODBUS - A compact MODBUS RTU/TCP C library for microcontrollers
  23. * nanoMODBUS is a small C library that implements the Modbus protocol. It is especially useful in resource-constrained
  24. * system like microcontrollers.
  25. *
  26. * GtiHub: <a href="https://github.com/debevv/nanoMODBUS">https://github.com/debevv/nanoMODBUS</a>
  27. *
  28. * API reference: \link nanomodbus.h \endlink
  29. *
  30. */
  31. #ifndef NANOMODBUS_H
  32. #define NANOMODBUS_H
  33. #include <stdbool.h>
  34. #include <stdint.h>
  35. #include <string.h>
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. /**
  40. * nanoMODBUS errors.
  41. * Values <= 0 are library errors, > 0 are modbus exceptions.
  42. */
  43. typedef enum nmbs_error {
  44. // Library errors
  45. NMBS_ERROR_INVALID_REQUEST = -8, /**< Received invalid request from client */
  46. NMBS_ERROR_INVALID_UNIT_ID = -7, /**< Received invalid unit ID in response from server */
  47. NMBS_ERROR_INVALID_TCP_MBAP = -6, /**< Received invalid TCP MBAP */
  48. NMBS_ERROR_CRC = -5, /**< Received invalid CRC */
  49. NMBS_ERROR_TRANSPORT = -4, /**< Transport error */
  50. NMBS_ERROR_TIMEOUT = -3, /**< Read/write timeout occurred */
  51. NMBS_ERROR_INVALID_RESPONSE = -2, /**< Received invalid response from server */
  52. NMBS_ERROR_INVALID_ARGUMENT = -1, /**< Invalid argument provided */
  53. NMBS_ERROR_NONE = 0, /**< No error */
  54. // Modbus exceptions
  55. NMBS_EXCEPTION_ILLEGAL_FUNCTION = 1, /**< Modbus exception 1 */
  56. NMBS_EXCEPTION_ILLEGAL_DATA_ADDRESS = 2, /**< Modbus exception 2 */
  57. NMBS_EXCEPTION_ILLEGAL_DATA_VALUE = 3, /**< Modbus exception 3 */
  58. NMBS_EXCEPTION_SERVER_DEVICE_FAILURE = 4, /**< Modbus exception 4 */
  59. } nmbs_error;
  60. /**
  61. * Return whether the nmbs_error is a modbus exception
  62. * @e nmbs_error to check
  63. */
  64. #define nmbs_error_is_exception(e) ((e) > 0 && (e) < 5)
  65. /**
  66. * Bitfield consisting of 2000 coils/discrete inputs
  67. */
  68. typedef uint8_t nmbs_bitfield[250];
  69. /**
  70. * Bitfield consisting of 256 values
  71. */
  72. typedef uint8_t nmbs_bitfield_256[32];
  73. /**
  74. * Read a bit from the nmbs_bitfield bf at position b
  75. */
  76. #define nmbs_bitfield_read(bf, b) ((bool) ((bf)[(b) / 8] & (0x1 << ((b) % 8))))
  77. /**
  78. * Set a bit of the nmbs_bitfield bf at position b
  79. */
  80. #define nmbs_bitfield_set(bf, b) (((bf)[(b) / 8]) = (((bf)[(b) / 8]) | (0x1 << ((b) % 8))))
  81. /**
  82. * Reset a bit of the nmbs_bitfield bf at position b
  83. */
  84. #define nmbs_bitfield_unset(bf, b) (((bf)[(b) / 8]) = (((bf)[(b) / 8]) & ~(0x1 << ((b) % 8))))
  85. /**
  86. * Write value v to the nmbs_bitfield bf at position b
  87. */
  88. #define nmbs_bitfield_write(bf, b, v) \
  89. (((bf)[(b) / 8]) = ((v) ? (((bf)[(b) / 8]) | (0x1 << ((b) % 8))) : (((bf)[(b) / 8]) & ~(0x1 << ((b) % 8)))))
  90. /**
  91. * Reset (zero) the whole bitfield
  92. */
  93. #define nmbs_bitfield_reset(bf) memset(bf, 0, sizeof(bf))
  94. /**
  95. * Modbus transport type.
  96. */
  97. typedef enum nmbs_transport {
  98. NMBS_TRANSPORT_RTU = 1,
  99. NMBS_TRANSPORT_TCP = 2,
  100. } nmbs_transport;
  101. /**
  102. * nanoMODBUS platform configuration struct.
  103. * Passed to nmbs_server_create() and nmbs_client_create().
  104. *
  105. * read() and write() are the platform-specific methods that read/write data to/from a serial port or a TCP connection.
  106. *
  107. * Both methods should block until either:
  108. * - `count` bytes of data are read/written
  109. * - the byte timeout, with `byte_timeout_ms >= 0`, expires
  110. *
  111. * A value `< 0` for `byte_timeout_ms` means no timeout.
  112. *
  113. * Their return value should be the number of bytes actually read/written, or `< 0` in case of error.
  114. * A return value between `0` and `count - 1` will be treated as if a timeout occurred on the transport side. All other
  115. * values will be treated as transport errors.
  116. *
  117. * Additionally, an optional crc_calc() function can be defined to override the default nanoMODBUS CRC calculation function.
  118. *
  119. * These methods accept a pointer to arbitrary user-data, which is the arg member of this struct.
  120. * After the creation of an instance it can be changed with nmbs_set_platform_arg().
  121. */
  122. typedef struct nmbs_platform_conf {
  123. nmbs_transport transport; /*!< Transport type */
  124. int32_t (*read)(uint8_t* buf, uint16_t count, int32_t byte_timeout_ms,
  125. void* arg); /*!< Bytes read transport function pointer */
  126. int32_t (*write)(const uint8_t* buf, uint16_t count, int32_t byte_timeout_ms,
  127. void* arg); /*!< Bytes write transport function pointer */
  128. uint16_t (*crc_calc)(const uint8_t* data, uint32_t length,
  129. void* arg); /*!< CRC calculation function pointer. Optional */
  130. void* arg; /*!< User data, will be passed to functions above */
  131. uint32_t initialized; /*!< Reserved, workaround for older user code not calling nmbs_platform_conf_create() */
  132. } nmbs_platform_conf;
  133. /**
  134. * Modbus server request callbacks. Passed to nmbs_server_create().
  135. *
  136. * These methods accept a pointer to arbitrary user data, which is the arg member of the nmbs_platform_conf that was passed
  137. * to nmbs_server_create together with this struct.
  138. *
  139. * `unit_id` is the RTU unit ID of the request sender. It is always 0 on TCP.
  140. */
  141. typedef struct nmbs_callbacks {
  142. #ifndef NMBS_SERVER_DISABLED
  143. #ifndef NMBS_SERVER_READ_COILS_DISABLED
  144. nmbs_error (*read_coils)(uint16_t address, uint16_t quantity, nmbs_bitfield coils_out, uint8_t unit_id, void* arg);
  145. #endif
  146. #ifndef NMBS_SERVER_READ_DISCRETE_INPUTS_DISABLED
  147. nmbs_error (*read_discrete_inputs)(uint16_t address, uint16_t quantity, nmbs_bitfield inputs_out, uint8_t unit_id,
  148. void* arg);
  149. #endif
  150. #if !defined(NMBS_SERVER_READ_HOLDING_REGISTERS_DISABLED) || !defined(NMBS_SERVER_READ_WRITE_REGISTERS_DISABLED)
  151. nmbs_error (*read_holding_registers)(uint16_t address, uint16_t quantity, uint16_t* registers_out, uint8_t unit_id,
  152. void* arg);
  153. #endif
  154. #ifndef NMBS_SERVER_READ_INPUT_REGISTERS_DISABLED
  155. nmbs_error (*read_input_registers)(uint16_t address, uint16_t quantity, uint16_t* registers_out, uint8_t unit_id,
  156. void* arg);
  157. #endif
  158. #ifndef NMBS_SERVER_WRITE_SINGLE_COIL_DISABLED
  159. nmbs_error (*write_single_coil)(uint16_t address, bool value, uint8_t unit_id, void* arg);
  160. #endif
  161. #ifndef NMBS_SERVER_WRITE_SINGLE_REGISTER_DISABLED
  162. nmbs_error (*write_single_register)(uint16_t address, uint16_t value, uint8_t unit_id, void* arg);
  163. #endif
  164. #ifndef NMBS_SERVER_WRITE_MULTIPLE_COILS_DISABLED
  165. nmbs_error (*write_multiple_coils)(uint16_t address, uint16_t quantity, const nmbs_bitfield coils, uint8_t unit_id,
  166. void* arg);
  167. #endif
  168. #if !defined(NMBS_SERVER_WRITE_MULTIPLE_REGISTERS_DISABLED) || !defined(NMBS_SERVER_READ_WRITE_REGISTERS_DISABLED)
  169. nmbs_error (*write_multiple_registers)(uint16_t address, uint16_t quantity, const uint16_t* registers,
  170. uint8_t unit_id, void* arg);
  171. #endif
  172. #ifndef NMBS_SERVER_READ_FILE_RECORD_DISABLED
  173. nmbs_error (*read_file_record)(uint16_t file_number, uint16_t record_number, uint16_t* registers, uint16_t count,
  174. uint8_t unit_id, void* arg);
  175. #endif
  176. #ifndef NMBS_SERVER_WRITE_FILE_RECORD_DISABLED
  177. nmbs_error (*write_file_record)(uint16_t file_number, uint16_t record_number, const uint16_t* registers,
  178. uint16_t count, uint8_t unit_id, void* arg);
  179. #endif
  180. #ifndef NMBS_SERVER_READ_DEVICE_IDENTIFICATION_DISABLED
  181. #define NMBS_DEVICE_IDENTIFICATION_STRING_LENGTH 128
  182. nmbs_error (*read_device_identification)(uint8_t object_id, char buffer[NMBS_DEVICE_IDENTIFICATION_STRING_LENGTH]);
  183. nmbs_error (*read_device_identification_map)(nmbs_bitfield_256 map);
  184. #endif
  185. #endif
  186. void* arg; // User data, will be passed to functions above
  187. uint32_t initialized; // Reserved, workaround for older user code not calling nmbs_callbacks_create()
  188. } nmbs_callbacks;
  189. /**
  190. * nanoMODBUS client/server instance type. All struct members are to be considered private,
  191. * it is not advisable to read/write them directly.
  192. */
  193. typedef struct nmbs_t {
  194. struct {
  195. uint8_t buf[260];
  196. uint16_t buf_idx;
  197. uint8_t unit_id;
  198. uint8_t fc;
  199. uint16_t transaction_id;
  200. bool broadcast;
  201. bool ignored;
  202. } msg;
  203. nmbs_callbacks callbacks;
  204. int32_t byte_timeout_ms;
  205. int32_t read_timeout_ms;
  206. nmbs_platform_conf platform;
  207. uint8_t address_rtu;
  208. uint8_t dest_address_rtu;
  209. uint16_t current_tid;
  210. } nmbs_t;
  211. /**
  212. * Modbus broadcast address. Can be passed to nmbs_set_destination_rtu_address().
  213. */
  214. static const uint8_t NMBS_BROADCAST_ADDRESS = 0;
  215. /** Set the request/response timeout.
  216. * If the target instance is a server, sets the timeout of the nmbs_server_poll() function.
  217. * If the target instance is a client, sets the response timeout after sending a request. In case of timeout,
  218. * the called method will return NMBS_ERROR_TIMEOUT.
  219. * @param nmbs pointer to the nmbs_t instance
  220. * @param timeout_ms timeout in milliseconds. If < 0, the timeout is disabled.
  221. */
  222. void nmbs_set_read_timeout(nmbs_t* nmbs, int32_t timeout_ms);
  223. /** Set the timeout between the reception/transmission of two consecutive bytes.
  224. * @param nmbs pointer to the nmbs_t instance
  225. * @param timeout_ms timeout in milliseconds. If < 0, the timeout is disabled.
  226. */
  227. void nmbs_set_byte_timeout(nmbs_t* nmbs, int32_t timeout_ms);
  228. /** Create a new nmbs_platform_conf struct.
  229. * @param platform_conf pointer to the nmbs_platform_conf instance
  230. */
  231. void nmbs_platform_conf_create(nmbs_platform_conf* platform_conf);
  232. /** Set the pointer to user data argument passed to platform functions.
  233. * @param nmbs pointer to the nmbs_t instance
  234. * @param arg user data argument
  235. */
  236. void nmbs_set_platform_arg(nmbs_t* nmbs, void* arg);
  237. #ifndef NMBS_SERVER_DISABLED
  238. /** Create a new nmbs_callbacks struct.
  239. * @param callbacks pointer to the nmbs_callbacks instance
  240. */
  241. void nmbs_callbacks_create(nmbs_callbacks* callbacks);
  242. /** Create a new Modbus server.
  243. * @param nmbs pointer to the nmbs_t instance where the client will be created.
  244. * @param address_rtu RTU address of this server. Can be 0 if transport is not RTU.
  245. * @param platform_conf nmbs_platform_conf struct with platform configuration. It may be discarded after calling this method.
  246. * @param callbacks nmbs_callbacks struct with server request callbacks. It may be discarded after calling this method.
  247. *
  248. * @return NMBS_ERROR_NONE if successful, NMBS_ERROR_INVALID_ARGUMENT otherwise.
  249. */
  250. nmbs_error nmbs_server_create(nmbs_t* nmbs, uint8_t address_rtu, const nmbs_platform_conf* platform_conf,
  251. const nmbs_callbacks* callbacks);
  252. /** Handle incoming requests to the server.
  253. * This function should be called in a loop in order to serve any incoming request. Its maximum duration, in case of no
  254. * received request, is the value set with nmbs_set_read_timeout() (unless set to < 0).
  255. * @param nmbs pointer to the nmbs_t instance
  256. *
  257. * @return NMBS_ERROR_NONE if successful, other errors otherwise.
  258. */
  259. nmbs_error nmbs_server_poll(nmbs_t* nmbs);
  260. /** Set the pointer to user data argument passed to server request callbacks.
  261. * @param nmbs pointer to the nmbs_t instance
  262. * @param arg user data argument
  263. */
  264. void nmbs_set_callbacks_arg(nmbs_t* nmbs, void* arg);
  265. #endif
  266. #ifndef NMBS_CLIENT_DISABLED
  267. /** Create a new Modbus client.
  268. * @param nmbs pointer to the nmbs_t instance where the client will be created.
  269. * @param platform_conf nmbs_platform_conf struct with platform configuration. It may be discarded after calling this method.
  270. *
  271. * @return NMBS_ERROR_NONE if successful, NMBS_ERROR_INVALID_ARGUMENT otherwise.
  272. */
  273. nmbs_error nmbs_client_create(nmbs_t* nmbs, const nmbs_platform_conf* platform_conf);
  274. /** Set the recipient server address of the next request on RTU transport.
  275. * @param nmbs pointer to the nmbs_t instance
  276. * @param address server address
  277. */
  278. void nmbs_set_destination_rtu_address(nmbs_t* nmbs, uint8_t address);
  279. /** Send a FC 01 (0x01) Read Coils request
  280. * @param nmbs pointer to the nmbs_t instance
  281. * @param address starting address
  282. * @param quantity quantity of coils
  283. * @param coils_out nmbs_bitfield where the coils will be stored
  284. *
  285. * @return NMBS_ERROR_NONE if successful, other errors otherwise.
  286. */
  287. nmbs_error nmbs_read_coils(nmbs_t* nmbs, uint16_t address, uint16_t quantity, nmbs_bitfield coils_out);
  288. /** Send a FC 02 (0x02) Read Discrete Inputs request
  289. * @param nmbs pointer to the nmbs_t instance
  290. * @param address starting address
  291. * @param quantity quantity of inputs
  292. * @param inputs_out nmbs_bitfield where the discrete inputs will be stored
  293. *
  294. * @return NMBS_ERROR_NONE if successful, other errors otherwise.
  295. */
  296. nmbs_error nmbs_read_discrete_inputs(nmbs_t* nmbs, uint16_t address, uint16_t quantity, nmbs_bitfield inputs_out);
  297. /** Send a FC 03 (0x03) Read Holding Registers request
  298. * @param nmbs pointer to the nmbs_t instance
  299. * @param address starting address
  300. * @param quantity quantity of registers
  301. * @param registers_out array where the registers will be stored
  302. *
  303. * @return NMBS_ERROR_NONE if successful, other errors otherwise.
  304. */
  305. nmbs_error nmbs_read_holding_registers(nmbs_t* nmbs, uint16_t address, uint16_t quantity, uint16_t* registers_out);
  306. /** Send a FC 04 (0x04) Read Input Registers request
  307. * @param nmbs pointer to the nmbs_t instance
  308. * @param address starting address
  309. * @param quantity quantity of registers
  310. * @param registers_out array where the registers will be stored
  311. *
  312. * @return NMBS_ERROR_NONE if successful, other errors otherwise.
  313. */
  314. nmbs_error nmbs_read_input_registers(nmbs_t* nmbs, uint16_t address, uint16_t quantity, uint16_t* registers_out);
  315. /** Send a FC 05 (0x05) Write Single Coil request
  316. * @param nmbs pointer to the nmbs_t instance
  317. * @param address coil address
  318. * @param value coil value
  319. *
  320. * @return NMBS_ERROR_NONE if successful, other errors otherwise.
  321. */
  322. nmbs_error nmbs_write_single_coil(nmbs_t* nmbs, uint16_t address, bool value);
  323. /** Send a FC 06 (0x06) Write Single Register request
  324. * @param nmbs pointer to the nmbs_t instance
  325. * @param address register address
  326. * @param value register value
  327. *
  328. * @return NMBS_ERROR_NONE if successful, other errors otherwise.
  329. */
  330. nmbs_error nmbs_write_single_register(nmbs_t* nmbs, uint16_t address, uint16_t value);
  331. /** Send a FC 15 (0x0F) Write Multiple Coils
  332. * @param nmbs pointer to the nmbs_t instance
  333. * @param address starting address
  334. * @param quantity quantity of coils
  335. * @param coils bitfield of coils values
  336. *
  337. * @return NMBS_ERROR_NONE if successful, other errors otherwise.
  338. */
  339. nmbs_error nmbs_write_multiple_coils(nmbs_t* nmbs, uint16_t address, uint16_t quantity, const nmbs_bitfield coils);
  340. /** Send a FC 16 (0x10) Write Multiple Registers
  341. * @param nmbs pointer to the nmbs_t instance
  342. * @param address starting address
  343. * @param quantity quantity of registers
  344. * @param registers array of registers values
  345. *
  346. * @return NMBS_ERROR_NONE if successful, other errors otherwise.
  347. */
  348. nmbs_error nmbs_write_multiple_registers(nmbs_t* nmbs, uint16_t address, uint16_t quantity, const uint16_t* registers);
  349. /** Send a FC 20 (0x14) Read File Record
  350. * @param nmbs pointer to the nmbs_t instance
  351. * @param file_number file number (1 to 65535)
  352. * @param record_number record number from file (0000 to 9999)
  353. * @param registers array of registers to read
  354. * @param count count of registers
  355. *
  356. * @return NMBS_ERROR_NONE if successful, other errors otherwise.
  357. */
  358. nmbs_error nmbs_read_file_record(nmbs_t* nmbs, uint16_t file_number, uint16_t record_number, uint16_t* registers,
  359. uint16_t count);
  360. /** Send a FC 21 (0x15) Write File Record
  361. * @param nmbs pointer to the nmbs_t instance
  362. * @param file_number file number (1 to 65535)
  363. * @param record_number record number from file (0000 to 9999)
  364. * @param registers array of registers to write
  365. * @param count count of registers
  366. *
  367. * @return NMBS_ERROR_NONE if successful, other errors otherwise.
  368. */
  369. nmbs_error nmbs_write_file_record(nmbs_t* nmbs, uint16_t file_number, uint16_t record_number, const uint16_t* registers,
  370. uint16_t count);
  371. /** Send a FC 23 (0x17) Read Write Multiple registers
  372. * @param nmbs pointer to the nmbs_t instance
  373. * @param read_address starting read address
  374. * @param read_quantity quantity of registers to read
  375. * @param registers_out array where the read registers will be stored
  376. * @param write_address starting write address
  377. * @param write_quantity quantity of registers to write
  378. * @param registers array of registers values to write
  379. *
  380. * @return NMBS_ERROR_NONE if successful, other errors otherwise.
  381. */
  382. nmbs_error nmbs_read_write_registers(nmbs_t* nmbs, uint16_t read_address, uint16_t read_quantity,
  383. uint16_t* registers_out, uint16_t write_address, uint16_t write_quantity,
  384. const uint16_t* registers);
  385. /** Send a FC 43 / 14 (0x2B / 0x0E) Read Device Identification to read all Basic Object Id values (Read Device ID code 1)
  386. * @param nmbs pointer to the nmbs_t instance
  387. * @param vendor_name char array where the read VendorName value will be stored
  388. * @param product_code char array where the read ProductCode value will be stored
  389. * @param major_minor_revision char array where the read MajorMinorRevision value will be stored
  390. * @param buffers_length length of every char array
  391. *
  392. * @return NMBS_ERROR_NONE if successful, other errors otherwise.
  393. */
  394. nmbs_error nmbs_read_device_identification_basic(nmbs_t* nmbs, char* vendor_name, char* product_code,
  395. char* major_minor_revision, uint8_t buffers_length);
  396. /** Send a FC 43 / 14 (0x2B / 0x0E) Read Device Identification to read all Regular Object Id values (Read Device ID code 2)
  397. * @param nmbs pointer to the nmbs_t instance
  398. * @param vendor_url char array where the read VendorUrl value will be stored
  399. * @param product_name char array where the read ProductName value will be stored
  400. * @param model_name char array where the read ModelName value will be stored
  401. * @param user_application_name char array where the read UserApplicationName value will be stored
  402. * @param buffers_length length of every char array
  403. *
  404. * @return NMBS_ERROR_NONE if successful, other errors otherwise.
  405. */
  406. nmbs_error nmbs_read_device_identification_regular(nmbs_t* nmbs, char* vendor_url, char* product_name, char* model_name,
  407. char* user_application_name, uint8_t buffers_length);
  408. /** Send a FC 43 / 14 (0x2B / 0x0E) Read Device Identification to read all Extended Object Id values (Read Device ID code 3)
  409. * @param nmbs pointer to the nmbs_t instance
  410. * @param object_id_start Object Id to start reading from
  411. * @param ids array where the read Object Ids will be stored
  412. * @param buffers array of char arrays where the read values will be stored
  413. * @param ids_length length of the ids array and buffers array
  414. * @param buffer_length length of each char array
  415. * @param objects_count_out retrieved Object Ids count
  416. *
  417. * @return NMBS_ERROR_NONE if successful, NMBS_INVALID_ARGUMENT if buffers_count is less than retrieved Object Ids count,
  418. * other errors otherwise.
  419. */
  420. nmbs_error nmbs_read_device_identification_extended(nmbs_t* nmbs, uint8_t object_id_start, uint8_t* ids, char** buffers,
  421. uint8_t ids_length, uint8_t buffer_length,
  422. uint8_t* objects_count_out);
  423. /** Send a FC 43 / 14 (0x2B / 0x0E) Read Device Identification to retrieve a single Object Id value (Read Device ID code 4)
  424. * @param nmbs pointer to the nmbs_t instance
  425. * @param object_id requested Object Id
  426. * @param buffer char array where the resulting value will be stored
  427. * @param buffer_length length of the char array
  428. *
  429. * @return NMBS_ERROR_NONE if successful, other errors otherwise.
  430. */
  431. nmbs_error nmbs_read_device_identification(nmbs_t* nmbs, uint8_t object_id, char* buffer, uint8_t buffer_length);
  432. /** Send a raw Modbus PDU.
  433. * CRC on RTU will be calculated and sent by this function.
  434. * @param nmbs pointer to the nmbs_t instance
  435. * @param fc request function code
  436. * @param data request data. It's up to the caller to convert this data to network byte order
  437. * @param data_len length of the data parameter
  438. *
  439. * @return NMBS_ERROR_NONE if successful, other errors otherwise.
  440. */
  441. nmbs_error nmbs_send_raw_pdu(nmbs_t* nmbs, uint8_t fc, const uint8_t* data, uint16_t data_len);
  442. /** Receive a raw response Modbus PDU.
  443. * @param nmbs pointer to the nmbs_t instance
  444. * @param data_out response data. It's up to the caller to convert this data to host byte order. Can be NULL.
  445. * @param data_out_len number of bytes to receive
  446. *
  447. * @return NMBS_ERROR_NONE if successful, other errors otherwise.
  448. */
  449. nmbs_error nmbs_receive_raw_pdu_response(nmbs_t* nmbs, uint8_t* data_out, uint8_t data_out_len);
  450. #endif
  451. /** Calculate the Modbus CRC of some data.
  452. * @param data Data
  453. * @param length Length of the data
  454. */
  455. uint16_t nmbs_crc_calc(const uint8_t* data, uint32_t length, void* arg);
  456. #ifndef NMBS_STRERROR_DISABLED
  457. /** Convert a nmbs_error to string
  458. * @param error error to be converted
  459. *
  460. * @return string representation of the error
  461. */
  462. const char* nmbs_strerror(nmbs_error error);
  463. #endif
  464. #ifdef __cplusplus
  465. } // extern "C"
  466. #endif
  467. #endif //NANOMODBUS_H