{"id":13812505,"url":"https://github.com/debevv/nanoMODBUS","last_synced_at":"2025-05-14T22:30:44.754Z","repository":{"id":38022255,"uuid":"449109849","full_name":"debevv/nanoMODBUS","owner":"debevv","description":"A compact MODBUS RTU/TCP C library for embedded/microcontrollers","archived":false,"fork":false,"pushed_at":"2025-05-04T23:59:35.000Z","size":27153,"stargazers_count":493,"open_issues_count":18,"forks_count":109,"subscribers_count":21,"default_branch":"master","last_synced_at":"2025-05-13T20:47:55.753Z","etag":null,"topics":["arduino","c","embedded","embedded-c","embedded-systems","microcontroller","microcontrollers","modbus","modbus-library","modbus-rtu","modbus-tcp","stm32"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/debevv.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-01-18T02:08:39.000Z","updated_at":"2025-05-12T23:29:47.000Z","dependencies_parsed_at":"2023-02-13T02:31:03.056Z","dependency_job_id":"5669127f-4077-4c5a-9b7c-5997e5d2fdd2","html_url":"https://github.com/debevv/nanoMODBUS","commit_stats":null,"previous_names":[],"tags_count":37,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/debevv%2FnanoMODBUS","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/debevv%2FnanoMODBUS/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/debevv%2FnanoMODBUS/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/debevv%2FnanoMODBUS/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/debevv","download_url":"https://codeload.github.com/debevv/nanoMODBUS/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254239453,"owners_count":22037713,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arduino","c","embedded","embedded-c","embedded-systems","microcontroller","microcontrollers","modbus","modbus-library","modbus-rtu","modbus-tcp","stm32"],"created_at":"2024-08-04T04:00:52.690Z","updated_at":"2025-05-14T22:30:44.700Z","avatar_url":"https://github.com/debevv.png","language":"C","funding_links":["https://storage.ko-fi.com/cdn/brandasset/logo_white_stroke.png'","https://ko-fi.com/B0B2LK779"],"categories":["Protocols"],"sub_categories":["Flash Memory"],"readme":"# nanoMODBUS - A compact MODBUS RTU/TCP C library for embedded/microcontrollers\n\n**If you found this library useful, buy me a coffee on**\n[\u003cimg src='https://storage.ko-fi.com/cdn/brandasset/logo_white_stroke.png' width='80'\u003e](https://ko-fi.com/B0B2LK779)\n\nnanoMODBUS is a small C library that implements the Modbus protocol. It is especially useful in embedded and\nresource-constrained systems like microcontrollers.  \nIts main features are:\n\n- Compact size\n    - Only ~2000 lines of code\n    - Client and server code can be disabled, if not needed\n- No dynamic memory allocations\n- Transports:\n    - RTU\n    - TCP\n- Roles:\n    - Client\n    - Server\n- Function codes:\n    - 01 (0x01) Read Coils\n    - 02 (0x02) Read Discrete Inputs\n    - 03 (0x03) Read Holding Registers\n    - 04 (0x04) Read Input Registers\n    - 05 (0x05) Write Single Coil\n    - 06 (0x06) Write Single Register\n    - 15 (0x0F) Write Multiple Coils\n    - 16 (0x10) Write Multiple registers\n    - 20 (0x14) Read File Record\n    - 21 (0x15) Write File Record\n    - 23 (0x17) Read/Write Multiple registers\n    - 43/14 (0x2B/0x0E) Read Device Identification\n- Platform-agnostic\n    - Requires only C99 and its standard library\n    - Data transport read/write functions are implemented by the user\n- User-definable CRC function for better performance\n- Broadcast requests and responses\n\n## At a glance\n\n```C\n#include \u003cstdio.h\u003e\n\n#include \"nanomodbus.h\"\n#include \"my_platform_stuff.h\"\n\nint main(int argc, char* argv[]) {\n    // Set up the TCP connection\n    void* conn = my_connect_tcp(argv[1], argv[2]);\n    if (!conn) {\n        fprintf(stderr, \"Error connecting to server\\n\");\n        return 1;\n    }\n\n    // my_transport_read() and my_transport_write() are implemented by the user \n    nmbs_platform_conf platform_conf;\n    nmbs_platform_conf_create(\u0026platform_conf);\n    platform_conf.transport = NMBS_TRANSPORT_TCP;\n    platform_conf.read = my_transport_read;\n    platform_conf.write = my_transport_write;\n    platform_conf.arg = conn;    // Passing our TCP connection handle to the read/write functions\n\n    // Create the modbus client\n    nmbs_t nmbs;\n    nmbs_error err = nmbs_client_create(\u0026nmbs, \u0026platform_conf);\n    if (err != NMBS_ERROR_NONE) {\n        fprintf(stderr, \"Error creating modbus client\\n\");\n        return 1;\n    }\n\n    // Set only the response timeout. Byte timeout will be handled by the TCP connection\n    nmbs_set_read_timeout(\u0026nmbs, 1000);\n\n    // Write 2 holding registers at address 26\n    uint16_t w_regs[2] = {123, 124};\n    err = nmbs_write_multiple_registers(\u0026nmbs, 26, 2, w_regs);\n    if (err != NMBS_ERROR_NONE) {\n        fprintf(stderr, \"Error writing register at address 26 - %s\", nmbs_strerror(err));\n        return 1;\n    }\n\n    // Read 2 holding registers from address 26\n    uint16_t r_regs[2];\n    err = nmbs_read_holding_registers(\u0026nmbs, 26, 2, r_regs);\n    if (err != NMBS_ERROR_NONE) {\n        fprintf(stderr, \"Error reading 2 holding registers at address 26 - %s\\n\", nmbs_strerror(err));\n        return 1;\n    }\n    \n    // Close the TCP connection\n    my_disconnect(conn);\n    \n    return 0;\n}\n```\n\n## Installation\n\n### Manual\n\nJust copy `nanomodbus.c` and `nanomodbus.h` inside your application codebase.\n\n### CMake project\n\nnanomodbus supports library linking by using CMake.\n\n```cmake\nFetchContent_Declare(\n        nanomodbus\n        GIT_REPOSITORY https://github.com/debevv/nanoMODBUS\n        GIT_TAG master # or the version you want\n        GIT_SHALLOW TRUE\n)\n\nFetchContent_MakeAvailable(nanomodbus)\n\n...\n\nadd_executable(your_program source_codes)\ntarget_link_libraries(your_program nanomodbus)\n```\n\n## API reference\n\nAPI reference is available in the repository's [GitHub Pages](https://debevv.github.io/nanoMODBUS/nanomodbus_8h.html).\n\n## Platform functions\n\nnanoMODBUS requires the implementation of 2 platform-specific functions, defined as function pointers when creating a\nclient/server instance.\n\n### Transport read/write\n\n```C\nint32_t read(uint8_t* buf, uint16_t count, int32_t byte_timeout_ms, void* arg);\nint32_t write(const uint8_t* buf, uint16_t count, int32_t byte_timeout_ms, void* arg);\n```\n\nThese are your platform-specific functions that read/write data to/from a serial port or a TCP connection.  \nBoth methods should block until either:\n\n- `count` bytes of data are read/written\n- the byte timeout, with `byte_timeout_ms \u003e= 0`, expires\n\nA value `\u003c 0` for `byte_timeout_ms` means infinite timeout.  \nWith a value `== 0` for `byte_timeout_ms`, the method should read/write once in a non-blocking fashion and return\nimmediately.\n\nTheir return value should be the number of bytes actually read/written, or `\u003c 0` in case of error.  \nA return value between `0` and `count - 1` will be treated as if a timeout occurred on the transport side. All other\nvalues will be treated as transport errors.\n\n### Callbacks and platform functions arguments\n\nServer callbacks and platform functions can access arbitrary user data through their `void* arg` argument. The argument\nis useful, for example, to pass the connection a function should operate on.  \nTheir initial values can be set via the `nmbs_set_callbacks_arg` and `nmbs_set_platform_arg` API methods.\n\n## Tests and examples\n\nTests and examples can be built and run on Linux with CMake:\n\n```sh\nmkdir build \u0026\u0026 cd build\ncmake ..\nmake\n```\n\nPlease refer to `examples/arduino/README.md` for more info about building and running Arduino examples.\n\n## Misc\n\n- To reduce code size, you can define the following `#define`s:\n    - `NMBS_CLIENT_DISABLED` to disable all client code\n    - `NMBS_SERVER_DISABLED` to disable all server code\n    - To disable individual server callbacks, define the following:\n        - `NMBS_SERVER_READ_COILS_DISABLED`\n        - `NMBS_SERVER_READ_DISCRETE_INPUTS_DISABLED`\n        - `NMBS_SERVER_READ_HOLDING_REGISTERS_DISABLED`\n        - `NMBS_SERVER_READ_INPUT_REGISTERS_DISABLED`\n        - `NMBS_SERVER_WRITE_SINGLE_COIL_DISABLED`\n        - `NMBS_SERVER_WRITE_SINGLE_REGISTER_DISABLED`\n        - `NMBS_SERVER_WRITE_MULTIPLE_COILS_DISABLED`\n        - `NMBS_SERVER_WRITE_MULTIPLE_REGISTERS_DISABLED`\n        - `NMBS_SERVER_READ_FILE_RECORD_DISABLED`\n        - `NMBS_SERVER_WRITE_FILE_RECORD_DISABLED`\n        - `NMBS_SERVER_READ_WRITE_REGISTERS_DISABLED`\n        - `NMBS_SERVER_READ_DEVICE_IDENTIFICATION_DISABLED`\n    - `NMBS_STRERROR_DISABLED` to disable the code that converts `nmbs_error`s to strings\n- Debug prints about received and sent messages can be enabled by defining `NMBS_DEBUG`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdebevv%2FnanoMODBUS","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdebevv%2FnanoMODBUS","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdebevv%2FnanoMODBUS/lists"}