This isnt a help request, just opened to share with fellow Home Assistant travellers some hands on notes from recent experience. I trust others will also be free to share the answers to challenges they faced so others can learn too. One thing about modbus is that while the functionality is available in HA, theres not so much experience shared because modbus hasnt been so popular. If a reader can quickly find just one solution to a problem here - that makes it worthwhile
Below is a list of things that had to be learnt for a successful modbus implementation.
Modbus? Its an automation protocol introduced for equipment communication in the late 1970’s. the protocol has only been updated a few times with the current RS485 protocol developed in the 80’s. Very popular in the manufacturing community because its comparatively easy to learn and use. You can Google this and read to your hearts content!.
RS485 is the protocol over which modbus us transmitted. Communication is over a single twisted pair. its easy to find such wire - ethernet cat 5/5e/6 all have 4 twisted pairs inside.
In layman’s terms? RS485 is the roadway on which vehicles travel. modbus tcp is the car/truck/bus whatever you fancy moving along that roadway. The names sometimes get used interchangeably - its OK. We know what your talking about!
It is important to use twisted pair (UTP - Universal Twisted Pair) cable as this helps block EMC interference from other devices. Avoid running your cable too close to VFD/VSD inverters for that reason.
If you have multiple modbus devices to connect, all can be connected together in one network, but the cable must daisy chain from one device to the next… NO junction boxes where the cable turns into a birds nest with dodgy connectors and branch-offs going in different directions. Don’t allow the cable to be pinched in any way - eg garage floor, pull garbage bin over cable - such a simple event may cause a short effectively blocking comms. Damage like that - maybe even during install - leaves no evident witness and could result in an hours/days/weeks long headache to solve. Failure to daisy chain will result in a non working network. Keep the cable twisted as close to the actual terminal as you can - obvious be rational here - don’t drive yourself crazy. On daisy chain terminals ensure both cables are stripped and captured correctly in the terminal. Remember, this is a low voltage protocol, and while miswiring is unlikely to damage hardware as RS485 is usually optocoupled, by the same token, it doesn’t take much of a wiring fault to stop the network As per the next paragraph, don’t forget your resistor at each end of the wiring. There’s some discussion that short runs don’t need resistors. Maybe, maybe not, but at the cost of 1/50th of a cafe coffee or less, its worth including.
The protocol was designed to include a 1/4 watt 120ohm resister at each end of your cable run. This pulls the twisted pair voltage back to 0V between data signals, it stops the signal rebound from the end of the cable run and silences out potential EMC inputs.
Modbus RTU - this is the comms protocol direct from a computer serial port to connected RS485 devices. You will need to specify baud rate, bits, stop bits and parity in your yaml. if your computer does not have a serial port - USB-RS485 serial adapters are readily available. Alternatively - and especially if the device is some distance from your computer you might consider switching to modbus tcp, and install a modbus tcp–>rtu gateway device close to your device. The link to that gateway can be via Wi-Fi if required.
modbus tcp - this is comms over a LAN network (or WAN if you’re tricky). sometimes this will plug into a device, or more frequently you might install a ethernet (or wifi) to RS485 gateway.
RS485 gateway These devices can be configured many ways. i configured mine to accept ethernet - wifi in station mode and convert the modbus tcp to modbus rtu - either serial port or two screw terminals. On the ethernet side of my device i configured a static IP on the home wifi network. static is safest because HA yaml includes that IP. On RS485 side of the configuration webpage you add your RS485 network baud rate, bits, stop bits and parity. you have to have the same settings on every RS485 device on your twisted pair network. My settings were 9600, 8, 1, and no parity. This was common to all the modbus relays, temp sensors AI AO and DI devices. so i adopted that rate for everything.
Programming of your modbus devices. this is where you may go prematurely grey, pull hair out or something even worse. its painstaking, frustrating, one wrong step and you reset the device and start from scratch.
Device ID - this is the “slave number” of your device on the RS485 twisted pair. NOTE the gateway has no setting for this because its number is 0. you can only have one gateway on the network. keep your eyes open. on rare occasions you may have to code that 0 in other device settings. I did not have to do this in yaml. every device obviously must have a different slave address. you can number these between 1 and 254. The addresses can be included in any order on your twisted pair daisy chain. No requirement to be consecutive. You cannot have more than 32 devices on a single twisted pair network - check on your gateway. Some gateways are less than 32 depending upon specs.
FYI - the RS485 portion of the gateway is referred to as the “master”, the ethernet/wifi portion is a “server”, and your computer is of course a “client” of said server.
PREP FOR PROGRAMMING RS485 DEVICES
In preparation, download Modbus Poll. Its free - so long as you dont mind it closing on you every 10 mins. It not an expensive software, and a purchase saves increasing frustration at the 10 minute lockout. Believe me, its probably the most productive spend you will make in a long time. And beyond the initial config, modbus poll can be used to check network operation before the network is handed to HA purvey. (Disclaimer: I am not $ponsored for this or any other recommendation in this how to…!!
)
Next, regardless of your intended final network, in 99% of cases you WILL need a serial port for initial config. Obtain something suitable - the USB ones are easy to use, attach to your computer to find the COM port number its using (for Windows) Start>Control Panel> Device Manager>expand the section LPT & COM. Could be any number of ports present, but likely yours is a somewhere between COM1 and COM10 inclusive. There should be names beside the COM number - which you should recognize from what you bought. If not see which COM disappears when you disconnect yours.
You need a 2 wire RS485 adapter hand off from your serial with a clear plus or minus to show polarity. connect about 500mm/20" of UTP with the solid colour in positive (+) and the striped-with-white in negative (-). On the other end of the wire strip about 8mm on each core and bend the exposed wire in a tight U shape. this makes an easy to connect wire that can be used over and over to configure your devices.
You got a 4 wire RS485 version? Easily fixed… Check the instructions or google if necessary - “4 wire to 2 wire RS485 conversion”. The fix is usually a jumper wire between 2 terminals. In preparation for your next step, in the two terminals spare, connect about 500mm/20" of UTP with the solid colour in positive (+) and the striped-with-white in negative (-). On the other end of the wire strip about 8mm on each core and bend the exposed wire in a tight U shape. this makes an easy to connect wire that can be used over and over to configure your devices.
Then… Plan your network addresses. record the new addresses handy to each device - you might somehow attach a tag or label so everything you need is available right there - most importantly the new slave number.
Also a good idea to record every device on a spreadsheet. Unique_ID, master, slave addr, manufacturer/model. these as a minimum… remember, 2 years after your setup is complete you wont remember the exact details - well most of us wont remember… Obviously this is even more critical if you are installing as a business!
Get ready the modbus tables for your device. If not in the box, download them. You cant guess these they can be different even within a brand from model to model. Often a google search “[brand] [model number] modbus table download” will find the table quickly - but double check the result was the correct one for your device.
Learning you need to do before you launch into RS485 device configuration
i will cover the 6 normal registers on modbus devices later after this section on programming. You do however need to have a grasp of those principles before
- Look in the tables for default address and the default baud rate etc of the device. Note all details.
- Look in the table for the change of slave address method and the codes to use. You will need to make the necessary adjustments to the code for the new address in your network
- Connect the RS485 terminals on the device to your serial adapter. There are several methods of marking RS485
Whether your twisted pair network connects to a serial port on your computer - or to a gateway, the same RS485 principals apply.
When communicating though, we need to get to the network using the correct method. modbus rtu is straightforward. Choose the correct COM port to use, select modbus rtu as comms, add the baud rate, bits etc. save and connect. your software speaks modbus rtu direct. You will also need to enter the slave number to reach the correct device on the twisted pair network.
modbus tcp is just as easy. input IP address of the gateway, select modbus tcp, add the slave address.
So - you have reached your device on the new network. now you need to configure access.
Im sorry - cant be finished today, but on it. Your comments on direction very appreciated.
1 post - 1 participant
Read full topic