Modbus Slave

The Modbus Protocol in a shell

Modbus is communications  protocol originally used for Modicon PLCs but now is used by many manufacturers.  Some SCADA systems use it to talk to remote sensors and controllers. On Ebay you can find Modbus sensors and controller by add Modbus to the search.

 

Modbus is a Master/Slave protocol. It has simple commands to read/write I/O and registers.

Originally it could only have 32 slaves but that has expanded to 255.  Master sends a packet of bytes containg a header, data, and CRC.  The header has Station Address, command, starting address, ending address, etc.   Data is handled in bytes but treated as 16 bit words because "holding Registers" in the PLC were 16 bit.  So only 127 bytes of data can be in a packet/message. CRC is calculated from the header and data bytes but the CRC is not recursively included. The hardest part of writing the Modbus slave was the CRC16.  

 

Modbus RTU is tricky knowing when the end of the packet is received. There is no CR or LF or EOT.  So program has a timer and assumes all the bytes are being sent one after another.  When the timer/counter times out, it sets the frame complete flag.  The frame is analyzed first of all if it is my address?  Then if it is command 3 or 16.  Finally if the CRC is good. There could be many other checks in this section, ie. register range, register address.

The Story

Last year I was doing some research and i wanted to record the Temperature and humidity (DHT22) in 22 compartments.

This was in a factory with lots of steel and concrete and noise spread over a 500 ft radius. Wifi didn't look real feasible.

So I opted for a wired network, RS485 and modbus protocol. There are modbus simulators that you can check the slave with from a PC. http://www.modbustools.com/modbus_poll.html .

 

I actually used php to write a program to gather the data into a MySQL database every 5 minutes. Never got implemented yet at the factory but I do have 7 stations running full time at home .

These pictures are part of a year long project that is working on the bench but never got implemented at the factory because of the physical constraint of drilling holes through concrete walls 20 ft in air.

 

 

 

 

 

 

 

 

 

 

The Sourcecode is published in the Great Cow BASIC Forum here. I have been using Great Cow BASIC to write code  for over 10 years now.

Why Great Cow BASIC?

Great Cow BASIC is real enabler. I am always amazed at the power of a few lines of code.

Over the last 10 years it has compiled the code for a random morsecode generator, barndoor mount for tracking stars,  tv tuner module controller, IR curtain for vending, Clocks and a sideral clock, blinking bar graphs, 7seg displays, used for training for PICs, gps reader, video on screen display using a max chip, talking counter, and a fake 555 timer chip.

Last couple years GCB has become a magnitude more powerful. Interrupts are easier. Timers are less cryptic. So many new libraries. Assembly can be included easily.  New chips are being accommodated as they come out.

Not basic at all!

 

Some Impressions about the Projects:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Author: mmotte in the Forum