{"id":18039305,"url":"https://github.com/ceremcem/modbus_lib","last_synced_at":"2025-03-27T10:32:09.457Z","repository":{"id":40692725,"uuid":"372823423","full_name":"ceremcem/modbus_lib","owner":"ceremcem","description":"Lightweight and easy to merge Modbus RTU Slave library for microcontrollers","archived":false,"fork":false,"pushed_at":"2023-01-19T17:19:22.000Z","size":29,"stargazers_count":14,"open_issues_count":4,"forks_count":7,"subscribers_count":3,"default_branch":"master","last_synced_at":"2023-08-29T13:13:38.403Z","etag":null,"topics":["c","modbus-rtu","modbus-slave","portable-library","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/ceremcem.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-06-01T12:33:00.000Z","updated_at":"2023-03-29T14:55:51.000Z","dependencies_parsed_at":"2023-02-11T12:05:29.065Z","dependency_job_id":null,"html_url":"https://github.com/ceremcem/modbus_lib","commit_stats":null,"previous_names":[],"tags_count":0,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ceremcem%2Fmodbus_lib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ceremcem%2Fmodbus_lib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ceremcem%2Fmodbus_lib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ceremcem%2Fmodbus_lib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ceremcem","download_url":"https://codeload.github.com/ceremcem/modbus_lib/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222235885,"owners_count":16953366,"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":["c","modbus-rtu","modbus-slave","portable-library","stm32"],"created_at":"2024-10-30T14:09:05.456Z","updated_at":"2024-10-30T14:09:06.151Z","avatar_url":"https://github.com/ceremcem.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Description \n\nEasy to use/port Modbus RTU slave library, written microcontrollers in mind. \n\n# Implemented Functions \n\n* Function 03: Read Holding Registers\n* Function 06: Write Single Register\n\nPlanned: \n\n* Function 16: Write Multiple Registers\n\n# Usage \n\n1. Include the header and source file paths within your toolchain (Makefile or your IDE):\n\n```Makefile\nC_SOURCES =  \\\n    ... \\\n    modbus_lib/modbus_lib.c \\\n    modbus_lib/modbus_crc.c\n\nC_INCLUDES =  \\\n    ... \\\n    -Imodbus_lib\n```\n\n2. Include the header file within your `main.c`: \n\n```c\n#include \"modbus_lib.h\"\n```\n\n3. Implement the following functions in your application: \n\n* Read handler:\n\n```c\nuint16_t modbus_lib_read_handler(uint16_t la){ // la: logical_address\n    switch(la){\n        case 40001:\n            return something; \n        case 40002:\n            return something_else;\n        case 40003: \n            if (some_error){\n                return modbus_lib_send_error(MBUS_RESPONSE_SERVICE_DEVICE_FAILURE);\n            } else {\n                return some_other_value;\n            }\n        default:\n            return modbus_lib_send_error(MBUS_RESPONSE_ILLEGAL_DATA_ADDRESS); \n    }\n}\n\n```\n\n* Write handler: \n\n```c\nuint16_t modbus_lib_write_handler(uint16_t la, uint16_t value){\n    if ( la \u003e= 40001 \u0026\u0026 la \u003c= 40013 ){\n        my_buffer_reg_4xxxx[la-40001] = value;\n    }\n    if( la \u003e 40013 \u0026\u0026 la \u003c 40018){\n        if(some_error_during_writing_to_eeprom){\n            return \tMBUS_RESPONSE_SERVICE_DEVICE_FAILURE;\n        }\n    }\n    if (la == 40018 ){\n        output = (value \u003e\u003e 8) | (value \u003c\u003c 8);\n    }\n    \n    if (la \u003e 40018){\n\t    return MBUS_RESPONSE_ILLEGAL_DATA_ADDRESS;\n    }\n    return MBUS_RESPONSE_OK; // data is successfully written\n}\n```\n\n\n* Place the data receive function inside your incoming stream handler:\n\n```c\nvoid USART2_dataHandler(void)\n{\n  uint8_t buff[1]; \n  HAL_UART_Receive (\u0026huart2, buff, 1, 400);  \n  modbus_lib_append_data(buff[0]); // append byte-by-byte\n}\n```\n\n* Mark the end of incoming telegram after a timeout: \n\n```c\nvoid USART2_idleHandler(void)\n{\n    modbus_lib_end_of_telegram();\n}\n```\n\n* Implement the `modbus_lib_transport_write` function: \n\n```c\nint modbus_lib_transport_write(uint8_t* buffer, uint16_t length){\n    HAL_UART_Transmit(\u0026huart2, buffer, length, 1000);\n    return 0; \n}\n```\n\n4. Make Modbus slave configuration and initialize in your `main()` function: \n\n```c\nModbusConfig_t modbus_cfg = {\n    .address = 1\n}; \n\nmodbus_lib_init(\u0026modbus_cfg);\n```\n\n5. Debug: \n\n* Verify that `uint8_t g_modbus_lib_received_telegram[]` is filled with received telegram. \n* Verify that `uint8_t g_modbus_lib_received_length` has correct value. \n* Examine `uint8_t outgoing_telegram[]` contents for outgoing telegram (use `uint16_t oindex` for length) inside `modbus_lib.c`.\n\n# Example \n\nSTM32F407 Discovery board example is available [here](https://github.com/ceremcem/modbus_example). \n\n# Address Indexes\n\nFirst Modbus register address is 40001.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fceremcem%2Fmodbus_lib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fceremcem%2Fmodbus_lib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fceremcem%2Fmodbus_lib/lists"}