How to use Modbus TCP in C++ with libmodbus in Visual Studio 2017
Вставка
- Опубліковано 5 лют 2025
- Putting this video out there in the rare chance this helps someone else.
This is a brute force, caveman-like method of including the library.
If anyone has a better method of using libmodbus in Windows Visual Studio, please comment below to commend below. Will be pleased to learn.
Note: I am aware that the library comes with a example on how to compile libmodbus on VS2008, but unfortunately it does not work for me.
Thanx man !!
This video helped a lot, now I am able to perform read/write operations..
You saved me, tnx bro !
Thanks for your clear and detailed tutorial. It really helps!!
weach functions displays an errno when a specific libmodbus functions fails to do its task so i waned to ask whether the string attached to that error is also shown in the output which describes what that errno means? if so how can i add that to the throw in exception handling? thank you.
Very helpful! Thank you!
Hi, I used your video to write code for connecting modbus mb3000 and I've managed to connect but I get error saying gateway is unavaliable. Do you have an idea on how to fix this problem?
I am using vpn connection and only this code to configure the data.
I did as shown in the video, but when I compile (minute 15:36) the error appears "undefined reference to `modbus_read_registers', undefined reference to `modbus_strerror', undefined reference to `modbus_new_tcp'...", how can I solve this problem ?
I'm using Dev C++ because my pc version doesn't work this visual studio
I would guess your code cannot find the modbus library you included. You may an issue with linking the library to your IDE. Unfortunately, I am not familiar with Dev C++ interface, so you have to look for other tutorials on the linking files within the same folder
Thanks a lot for the read function. I would like to ask if there is a function to write to registers through MODBUS as well. If so, how to do that? Thank you.
Also, is this code only viable for modbus-tcp or can we use it for communication done in modbus rtu as well?
Yes, i believe there are functions to read and write modbus registers. And to use for modbus rtu, use modbus_new_rtu() instead of modbus_new_tcp()
You may find documentation of the library's available functions at the main website. libmodbus.org/
This build is done using v3.1.7, so documentation is here libmodbus.org/olddocs/v3.1.7/libmodbus
Do consider supporting the project. :)
@@chlow1627 Thank you very much for the timely response. It was very helpful.
Will modbus_connect(mb) return -1 if there is a usb-rs-485 converter connected to a COM port but the converter itself is not connected with any device?
@@anshuman2952 Hi, sorry for late reply, on my code, it exits with an exception error. Haven't had time to really figured out how to resolve this
I understand. Thank you very much for trying for my sake.@@chlow1627
I want to execute a DLL made in cpp using libmodbus inbuilt api's and run that DLL in csharp so is that achievable? If so, then any tips on how I can make that work?
Unfortunately, I am not too familiar with DLL implementation.
I believe the repo owner already compiled some sample DLL. Prehaps you could look into repo for that.
It is definitely achievable. You just need to make a project in C/C++ that exposes some API, the project will be build as dll project and a C# wrapper over it.
Thanks !!! Quite useful ! !!!!
I know this is an old video, but i hope you can help.
I can't get it to run at 10:34 in the video. It gives me 8 errors of unresolved external symbols. I am running the latest version of Visual Studio Community 2022, that being version 17.5.1 and version 3.1.10 of the libmodbus. I would greatly appreciate it if you have any solution to this and are willing to help.
You are in luck. Maybe more details about the error message?
I happen to see this. If I recall correctly, I saw unresolved errors when I missed out certain linkages, so maybe step through the video again ?
Also, Did you include extern "c"?
@@chlow1627 I saw it again and figured it out :D
I got a problem that said something about the preprocessor definitions being undefined. On Google someone said that I could go to C/C++ settings -> Advanced and type disable the specific warning 4996 and it worked.
requesting you to share the code
It will not work. You really have to set these configurations on your computer
thank you!