modbus.h 21 KB

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