# WIKI : Sample apps with Modbus protocol over serial port

**URL:** <https://mangoh.discourse.group/t/wiki-sample-apps-with-modbus-protocol-over-serial-port/1670>\
**Category:** Uncategorized\
**Created:** [July 5, 2018, 1:53pm UTC](https://mangoh.discourse.group/t/wiki-sample-apps-with-modbus-protocol-over-serial-port/1670 "2018-07-05T13:53:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francis.duhaut](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/francis.duhaut/32/350_2.png) [@Francis.duhaut](https://mangoh.discourse.group/u/Francis.duhaut)\
**Post date:** [July 5, 2018, 1:53pm UTC](https://mangoh.discourse.group/t/wiki-sample-apps-with-modbus-protocol-over-serial-port/1670/1 "2018-07-05T13:53:23Z")

</div>

Hello all,

Just a message to share with you small application who use a FDTI USB serial port and modbus protocole.

This is a good base if you want to implement data access with serial port over modbus protocol.  
Only function 3 and 16 are implemented with CRC calculation but it’s easy to add more.

I use le\_fdMonitor\_Create() to pollin into a handler incomming modbus messages. Apps use a circular buffer to “eject” bad data and read only a good modbus frame. You will find example to read/write data to a C Struct over modbus.

This was tested with MAngoh red and WP76 / R9 and legato 18.05. Driver for FDTI USB serial port is loaded at apps startup.

[wiiq3Monitor.zip](https://mangoh.discourse.group/uploads/short-url/oOkjdIVogR4eCziWAkkXSkDrt5r.zip) (162.9 KB)

Example : connect a USB FDTI serial converter to main USB connector

start apps

open a terminal at 115200bps (I use Hercules terminal for ex)  
send this modbus frame :

{01}{03}{12}{4D}{00}{10}{D1}{69}  
-\> Slave address = 0x01 function 0x03 address 0x124D and read 16 registers (0x0010) CRC frame is 0xD169

-\> check the answer  
{01}{03}{20}{00}{00}{00}{00}{00}{00}{00}{00}{00}{00}{00}{00}{00}{00}{00}{00}{00}{00}{00}{00}{00}{00}{00}{00}{00}{00}{00}{00}{00}{00}{00}{00}{92}{7A}

-\> 0x01 slave address  
-\> 0x03 function 0x03  
-\> 0x20 is 16 registers of 2 bytes = 32 bytes  
-\> after is data mapped to the CStruct at 0x124D  
-\> 0x927A is the CRC

 ![image](https://us1.discourse-cdn.com/flex016/uploads/mangoh/original/1X/1959a538459b703eab562cb07a399417d97a6766.png)

Have a nice day.  
Francis.

---

<div class="post-metadata">

**Author:** ![Madan](https://avatars.discourse-cdn.com/v4/letter/m/6a8cbe/32.png) [@Madan](https://mangoh.discourse.group/u/Madan)\
**Post date:** [August 9, 2018, 5:51pm UTC](https://mangoh.discourse.group/t/wiki-sample-apps-with-modbus-protocol-over-serial-port/1670/2 "2018-08-09T17:51:08Z")

</div>

Hi

Probably this is the only example out there on Legato for serial port communications. I have used this code for basic serial port communication. Picked serial.c/.h and relevant configurations. Built and uploaded the code in to mangOH Red.

Application exits because it could not find comport. (error no 2). I have added the /dev/ttyHS0 and /dev/ttyHSL1 for R/W. But open port fails always. Tested on both ports.

Both ports are available in /dev folder. If the ports are already in use, I think the error no. should have been something like ‘access denied’. Actual plan is to use the console USB to push data into PC (testing phase only)

Is there anything I am missing?

Thanks

---

<div class="post-metadata">

**Author:** ![Francis.duhaut](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/francis.duhaut/32/350_2.png) [@Francis.duhaut](https://mangoh.discourse.group/u/Francis.duhaut)\
**Post date:** [August 9, 2018, 6:40pm UTC](https://mangoh.discourse.group/t/wiki-sample-apps-with-modbus-protocol-over-serial-port/1670/3 "2018-08-09T18:40:59Z")

</div>

Can try with unsandboxed apps?  
I use Ftdi usb serial converter without any issue.

My apps are unsandboxed.

Francis

---

<div class="post-metadata">

**Author:** ![Madan](https://avatars.discourse-cdn.com/v4/letter/m/6a8cbe/32.png) [@Madan](https://mangoh.discourse.group/u/Madan)\
**Post date:** [August 10, 2018, 5:30am UTC](https://mangoh.discourse.group/t/wiki-sample-apps-with-modbus-protocol-over-serial-port/1670/4 "2018-08-10T05:30:25Z")

</div>

Thank you Francis. I will try unsandboxing.

---

<div class="post-metadata">

**Author:** ![Madan](https://avatars.discourse-cdn.com/v4/letter/m/6a8cbe/32.png) [@Madan](https://mangoh.discourse.group/u/Madan)\
**Post date:** [August 10, 2018, 12:13pm UTC](https://mangoh.discourse.group/t/wiki-sample-apps-with-modbus-protocol-over-serial-port/1670/5 "2018-08-10T12:13:29Z")

</div>

Unsandboxing did the trick. It works now and I am able to get the Apps serial out on the console port.

Thanks Francis.

---

<div class="post-metadata">

**Author:** ![mpetersen](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/mpetersen/32/1177_2.png) [@mpetersen](https://mangoh.discourse.group/u/mpetersen)\
**Post date:** [August 21, 2020, 6:50pm UTC](https://mangoh.discourse.group/t/wiki-sample-apps-with-modbus-protocol-over-serial-port/1670/6 "2020-08-21T18:50:39Z")

</div>

Any updates you can share on your Modbus implementation?  
…would be most appreciated!  
Thanks
