Library for Modbus TCP


Hi, I am currently working on a project that involves multiple modules that do remote data acquisition. Those modules contain 4 analog inputs that are read using a 4 channel 24bit ADC. Those values are then transmitted to a PIC18F, and I am hoping on using the Particle Photon to send those values over a wifi network to a master.
This whole infrastructure is going to be built using the ModBUS protocol, or more precisely ModBUS TCP.

The master will pool slaves for values, that the slaves will transmit over wifi to the master.

My question is, are there any existing libraries for modbus TCP using the Photon.
And more importantly, is the photon the right product for what I want to build.


@hugo, there is no existing ModBus TCP library that I know of for the Photon. However, if you can find a suitable Arduino library, we can look at porting it. The Photon is a perfect product for your application, supporting both standard (TCP, UDP HTTP and now HTTPS) and Particle cloud protocols. :slight_smile:


Is the photon like the ESB8266, meaning that the MCU will communicate via its hardware UART interface with the Photon, only providing the content of the packet and the connection information, meaning the stack is photon side and not MCU side?
If so, I found an arduino library that does ModBUS RTU that could be ported, meaning it would be basically a RTU to TCP converter, if I understand correctly?


Https??? Really?




@hugo, the Photon uses a Broadcom BCM43362 wifi chip along with an STM32F205 processor. Broadcom’s WICED library (wifi stack) runs on the Photon as a set of tasks on FreeRTOS along with Photon’s own code for cloud and wiring support. Though WICED is a closed library (for now), the Photon code is open source and provides substantial control over TCP/UDP communications. There is no UART interface to the Broadcom chip so the answer is basically no, the Photon is not like the ESB8266.


I have used the modbus tcp library for a couple projects from…


It would be amazing if particle could have this ported as it would greatly shrink my projects…as it is…an arduino, ethernet shield, relay shield, and wireless travel router, just to control two pwm pins and 4 relays. Awesome if we can replace with a particle photon and relay board and be able to communicate easily with industrial HMIs, PLCs, lab view, and modbus apps for Android and ios.





I noticed your indication that you folks could look at converting an existing library. I’m lead to believe the MgsModbus library works fine on Arduino. Any chance it would be considered for a port in the near future?

There appears to be working Modbus library for the Arduino at:



@ctmorrison, I started looking at that and got pulled onto something else. I’ll have to look again to see where I was! :flushed:


So…as I look further into Modbus TCP, I’m wondering if I can simply open a TCP connection to the destination IP at port 502 (using TCPClient). I could then simply write the string of characters required to read the register(s) I need from the target device. Then, simply sit there and read the bytes being returned from the Modbus device.

I hate to trivialize this, but I’m wondering if it could be this easy for a very simple application wherein I need to read 12 sets of readings from one device. Yes, I’m really trying desperately to keep this project as simple as possible and then extend as required.



It appears the answer is “Yes, I can do that.” I have a Photon talking Modbus TCP to a simulator on my MacBook. There’s lots more work to do just to finish the application I’m working on. I have to read a number of registers, check the return code, convert big-Endian to little-Endian floating points and such. After all of that, I need to push the data to Grovestreams (that’s the easy part).

I have never published a library on Github, but I’d like to share what I’m doing. I’ll need some help if/when I get that far.


Have you had success with Modbus TCP?
Did you find some library to work with Particle?

Thank a lot…


Hi @dmautomac, I could not find a library, but ended up writing some simple code to read the registers I needed. Part of the trick is the big-Endian to little-Endian conversion, but it’s pretty straight forward once you know it’s needed. Register addressing is a bit confusing, too, but no big deal. Let me know how I can help. If you need me to post some of my code (rough, but it works), please advise.


You had some code that has had success with this Modbus TCP? How did you part of addressing?



Hmmmm…perhaps I haven’t had enough coffee yet this morning. Let me try this. Yes, I had very good success with Modbus TCP. I could not find any available code, so I downloaded a description of the Modbus protocol and wrote my own code to talk to the device.

The device I needed to talk to is a YSI water quality analyzer. It holds sensor readings (12 of them) in Modbus registers. Each sensor requires 8 registers and each register requires 2 bytes. The whole register scheme is standard. In the case of the YSI, the registers hold sensor data, as I mentioned.

Two aspects (at least) make this a bit tricky: the data is stored as floating point numbers in big-Endian format, whereas the Photon is little-Endian. I believe (I don’t have the docs with me) this is the standard format for Modbus register data. The other aspect is the command to read the device is a bit tricky–it’s a 12 byte array that indicates the READ command, the starting address and how many registers to read.

Basically, I used the TCPclient class to instantiate an object I could use to open port 502 (the standard Modbus TCP port) to the YSI meter’s IP address. I then issued a command to request the desired number of bytes starting with register 1 (address 0). As the sensor readings come back, I convert them from big-Endian to little-Endian format using a union structure (dealer’s choice) and the build a string to push the data to the cloud service (Grovestreams in my case).

I hope this at least partially answers your question.

By the way, I found a free simulator (somewhere) which I used to debug the program, since there was only 1 YSI analyzer available and it was in production. The simulator was a huge benefit, since I knew exactly what data I should be reading.



It’s been some time since the last post, but I am wondering if you had any success implementing the Modbus TCP protocol via a TCP connection on the Photon?

I am currently working on a project where I need to access data from industrial temperature and humidity sensors, which use the Modbus TCP protocol. Any information or help is greatly appreciated!




Your post is very interesting. I was struggling with implementation of Modbus TCP on esp and photon…with no success. May i kindly ask you to share your code or library…using TCP Client and Photon

Kind regards


@teodorbobochikov, I’m happy to share my (crude) code, but will need a few days before I have sufficient free time. Please ping me if you don’t hear from me within a week. I may have forgotten.


Thank you very much for that. As advised i am gently ping you to send me the code:)