|
| 1 | +# Instructions to use with Espressif IoT Development Framework (ESP-IDF) |
| 2 | + |
| 3 | +## Adding libmodbus as a component |
| 4 | + |
| 5 | +- Create a subdirectory at the top level of your ESP-IDF project where you will |
| 6 | +place this and create a component, ie. `libmodbus`. This directory will be |
| 7 | +referred as *component directory* further down in this text. |
| 8 | + |
| 9 | +- Download the latest version of libmodbus with the method of your choice and |
| 10 | +unpack it under component directory in a subdirectory `libmodbus` |
| 11 | + |
| 12 | +- Copy the files supplied in this documentation directory to the component directory, |
| 13 | +namely: |
| 14 | + - `CMakeLists.txt`: the CMake script that enumerates files and directories used |
| 15 | + in the build as well as defines needed dependencies |
| 16 | + - `component.mk`: the component build definition |
| 17 | + - `config.h`: the library configuration, especially tailored for ESP-IDF. This is |
| 18 | + usually generated with the autoconf tool, but this is not present in ESP-IDF and |
| 19 | + is therefore manually prepared and customized |
| 20 | + - `idf_component.yml`: the component description file |
| 21 | + |
| 22 | +- Add a reference from your main project in the project top level `CMakeLists.txt` to |
| 23 | + the newly added module with something like: `set(EXTRA_COMPONENT_DIRS libmodbus/)`. |
| 24 | + If you already have other components you may just add the reference to the newly |
| 25 | + added component. |
| 26 | + |
| 27 | +- As the ESP-IDF does not provide a `nanosleep` function in its SDK, you should add |
| 28 | + this in your project so you will be able to find it at linking time, for example: |
| 29 | + |
| 30 | +``` |
| 31 | +int nanosleep(const struct timespec *req, struct timespec *_Nullable rem) { |
| 32 | + return usleep(req->tv_sec*1000 + req->tv_nsec / 1000); |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +Now you are almost ready to use libmodbus in your project! |
| 37 | + |
| 38 | +If you desire to use the library for serial communication, you will need to do a few |
| 39 | +more hardware configuration steps before using the `modbus_new_rtu` call, namely: |
| 40 | + |
| 41 | +- Configure, if needed, any pins for the used uart via `uart_set_pin` |
| 42 | + |
| 43 | +- Install the uart driver via the `uart_driver_install` |
| 44 | + |
| 45 | +- Configure, if needed, the uart mode (ie. set it to half duplex) via `uart_set_mode` |
| 46 | + |
| 47 | +These configurations are not included in libmodbus as they are highly hardware specific |
| 48 | +and would require a heavy change in the library interface. |
| 49 | + |
| 50 | +## Other details using libmodbus with ESP-IDF |
| 51 | + |
| 52 | +- The serial driver is implemented using the `vfs` virtual filesystem component. This |
| 53 | + makes the changes needed for the library minimal, but may not be the most performant |
| 54 | + solution. |
| 55 | + |
| 56 | +- The serial name (first parameter to `modbus_new_rtu`) should be a string containing |
| 57 | + only the serial index (ie. `"1"` or `"2"`). |
| 58 | + |
| 59 | +- When using the TCP version be aware of the maximum number of sockets that can be |
| 60 | + open on the platform: this is by default 10 and can be possibly raised to 16 in |
| 61 | + a standard configuration. Please check the `LWIP_MAX_SOCKETS` configuration |
| 62 | + variable. |
| 63 | + |
0 commit comments