espressif / esp-modbus

ESP-Modbus - the officially suppported library for Modbus protocol (serial RS485 + TCP over WiFi or Ethernet).
Apache License 2.0
117 stars 55 forks source link

How to modify the eMBFuncReadHoldingRegister function (IDFGH-11595) #43

Closed iamugly closed 11 months ago

iamugly commented 11 months ago

Because of business need, I need to modify the eMBFuncReadHoldingRegister this function is implemented, in this function you need to call I write other components of the library, but how to compile all not modified by, and be submitted to the following error:

[1/3] espressif/esp-modbus (1.0.12) [2/3] espressif/mdns (1.2.2) [3/3] idf (5.0.0) CMake Error at E:/ESP-IDF/Espressif/frameworks/esp-idf-v5.0/tools/cmake/build.cmake:519 (message): ERROR: Some components (espressif/esp-modbus) in the "managed_components" directory were modified on the disk since the last run of the CMake. Content of this directory is managed automatically.

If you want to keep the changes, you can move the directory with the component to the "components"directory of your project.

I.E. for "espressif__esp-modbus" run:

mv C:\Users\Administrator\Desktop\modbusTCP\WifiLogger\managed_components\espressifesp-modbus C:\Users\Administrator\Desktop\modbusTCP\WifiLogger\components\espressifesp-modbus

Or, if you want to discard the changes remove the ".component_hash" file from the component's directory.

I.E. for "espressif__esp-modbus" run:

rm C:\Users\Administrator\Desktop\modbusTCP\WifiLogger\managed_components\espressif__esp-modbus.component_hash

Call Stack (most recent call first): E:/ESP-IDF/Espressif/frameworks/esp-idf-v5.0/tools/cmake/project.cmake:440 (idf_build_process) CMakeLists.txt:11 (project)

-- Configuring incomplete, errors occurred! See also "C:/Users/Administrator/Desktop/modbusTCP/WifiLogger/build/CMakeFiles/CMakeOutput.log".

alisitsyn commented 11 months ago

@iamugly ,

if you need to override some functionality of the esp-modbus component, there are several ways to do this.

  1. The simple way to change some functionality of the library is to move the C:\Users\Administrator\Desktop\modbusTCP\WifiLogger\managed_components\espressif__esp-modbus folder into C:\Users\Administrator\Desktop\modbusTCP\WifiLogger\components\espressif__esp-modbus. This is exactly what is stated in your message. In this case you can update the files components\espressif__esp-modbus folder to override the component downloaded by package manager.
  2. Other way is to create new component\patched_esp_modbus and modify its CMakeFiles.txt to be able to override one file from patched component and other files from official component. See patched component example here. Add your other component dependencies into requirements list of patched esp-modbus component if you need it.
  3. If you need to override just some of symbols from the library to your implementation you can use the --wrap= linker approach for this.
#add this to your main component CMakeLists.txt file ....
set(WRAP_FUNCTIONS
    eMBFuncWriteHoldingRegister # This function will be overridden with your implementation below
    # add other functions from the esp-modbus to override in your code 
)

foreach(wrap ${WRAP_FUNCTIONS})
    target_link_libraries(${COMPONENT_LIB} PUBLIC "-Wl,--undefined=${wrap}")
    target_link_libraries(${COMPONENT_LIB} PUBLIC "-Wl,--wrap=${wrap}")
    #target_link_libraries(${COMPONENT_LIB} INTERFACE "-u __wrap_${wrap}")
endforeach()

your_implementation_file.c:

eMBException __wrap_eMBFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
{
    // Do what you need here, because this body will be executed instead of original function
}

your_implementation_file.h:

eMBException __wrap_eMBFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
extern eMBException __real_eMBFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen );

Note: do not forget to add the above files into sources and include list of your project CMakeLists.txt. See the documentation

  1. Other ways also possible, but the simplest ones are above.
iamugly commented 11 months ago

@iamugly ,

if you need to override some functionality of the esp-modbus component, there are several ways to do this.

  1. The simple way to change some functionality of the library is to move the C:\Users\Administrator\Desktop\modbusTCP\WifiLogger\managed_components\espressif__esp-modbus folder into C:\Users\Administrator\Desktop\modbusTCP\WifiLogger\components\espressif__esp-modbus. This is exactly what is stated in your message. In this case you can update the files components\espressif__esp-modbus folder to override the component downloaded by package manager.
  2. Other way is to create new component\patched_esp_modbus and modify its CMakeFiles.txt to be able to override one file from patched component and other files from official component. See patched component example here. Add your other component dependencies into requirements list of patched esp-modbus component if you need it.
  3. If you need to override just some of symbols from the library to your implementation you can use the --wrap= linker approach for this.
#add this to your main component CMakeLists.txt file ....
set(WRAP_FUNCTIONS
    eMBFuncWriteHoldingRegister # This function will be overridden with your implementation below
    # add other functions from the esp-modbus to override in your code 
)

foreach(wrap ${WRAP_FUNCTIONS})
    target_link_libraries(${COMPONENT_LIB} PUBLIC "-Wl,--undefined=${wrap}")
    target_link_libraries(${COMPONENT_LIB} PUBLIC "-Wl,--wrap=${wrap}")
    #target_link_libraries(${COMPONENT_LIB} INTERFACE "-u __wrap_${wrap}")
endforeach()

your_implementation_file.c:

eMBException __wrap_eMBFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
{
    // Do what you need here, because this body will be executed instead of original function
}

your_implementation_file.h:

eMBException __wrap_eMBFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
extern eMBException __real_eMBFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen );

Note: do not forget to add the above files into sources and include list of your project CMakeLists.txt. See the documentation

  1. Other ways also possible, but the simplest ones are above.

Thank you very much for your answer!

alisitsyn commented 11 months ago

@iamugly ,

Thank you for update. I will close the issue as solved then. Feel free to reopen if you need more help.