Experiment Modbus communication - Page 1
Forum section: electronics / general

author: Kittu20
author_rank: Regular Contributor
timestamp: November 08, 2023, 05:02:17 pm
content: Hello Everyone,I'm interested in learning the Modbus communication protocol by conducting practical experiments. To get started, I'd like to know which parts would you recommended for Modbus communication.I currently have a PIC18F45K80 microcontroller and two pairs of TTL to RS485 converters. Can I use these RS485 converters for Modbus communication, or do you recommend other components?

author: dferyance
author_rank: Regular Contributor
timestamp: November 09, 2023, 12:51:43 am
content: I suggest you have a modbus device you trust to communicate with. If, instead, you run your own code on both sides, you run the risk of inventing your own protocol.Modbus doesn't only run over RS485. Most modbus devices I worked with were modbus over TCP/IP or tunneled over a different protocol. But yeah, get your hands on something that talks modbus first.Modbus is very simple; often too simple for its use. Really the tricky parts of it is all the different ways the different manufacturers make up for modbus's limitations. Different encoding schemes, extensions, binary blobs and so on. But if you are using it to talk to actual coils that won't be an issue.

author: boz
author_rank: Regular Contributor
timestamp: November 09, 2023, 05:45:33 am
content: Assuming you are just testing locally then any bog standard RS422 or RS468 transceiver will be fine. MODBUS over serial (which is what RS485/422 uses) is the last of the really easy protocols IMHO and the official document is the last of the easy-to-understand ones =>https://www.modbus.org/docs/PI_MBUS_300.pdfYou only need a couple of hundred lines of C to implement a system which will process the function 0x03/0x04 read registers and the 0x06 or 0x10 write registers. I have got the XC8 code for an 18F26K40 Modbus slave I wrote for a customer project which has been working reliably on tens of thousands of devices I can redact out the customer parts if you get stuck PM meIf you ever go commercial or are talking to external lines then I recommend you implement an isolated transceiver, that is the biggest cause of failures I see on RS422/485

author: boz
author_rank: Regular Contributor
timestamp: November 09, 2023, 06:06:20 am
content: Also, plenty or Arduino samples and libraries, might be a better way to go as you are likely to get more beginner friendly help than the PIC

author: Siwastaja
author_rank: Super Contributor
timestamp: November 09, 2023, 09:33:07 am
content: That microcontroller is fine.Modbus itself is very simple, the real challenge is to interface with all sorts of real-world devices, decipher their documentation, find how they abuse the MODBUS protocol. You do this on device-by-device basis.What are you actually doing, i.e., what modbus devices you have in mind?

author: Kittu20
author_rank: Regular Contributor
timestamp: November 10, 2023, 12:12:32 am
content: Quote from: Siwastaja on November 09, 2023, 09:33:07 amI want to get hands on experience with modbus protocol. Currently I have PIC18F45K80, Two RS485 and one ESP32 I am looking for advice which hardware set up required to perform experiment to get full knowledge about modbus.

author: woofy
author_rank: Frequent Contributor
timestamp: November 10, 2023, 09:02:47 am
content: Quote from: dferyance on November 09, 2023, 12:51:43 amAbsolutely agree with that. Don't do both ends of the link.Either get yourself a modbus module and use your Pic to talk to it., or download one of the modbus test programs to your pc, and have that talk to your Pic.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: November 15, 2023, 05:13:28 pm
content: The RS485 part of modbus is also called "Modbus RTU"I had a short look at aliexpress, and you can get lots of gadgets that work with modbus, From temperature sensors 4-20mA outputs or inputs, to relay boards, and prices start at about EUR5. Sigrok/Pulseview can also decode Modbus RTU and for such low speed applications it works just fine with an EUR10 or less Logic Analyser hardware. (But do add an RS485 receiver to get proper logic levels that is compatible with your LA hardware.The combination of using a pre-made library with Sigrok / Pulseview probably gets you close to the Modbus standard, but getting some pre-made devices still seems good advice. Besides, with Ali prices, you can probably get a few which have your interest cheaper then you can build them yourself.Also, don't forget the FX1N, FX2N or FX3N PLC boards. They start around EUR25, and for that price you get a PCB with an STM32 (or clone) a bunch of hardened I/O, a pretty decent power supply (SMPS and filtering) and most also have an RS485 transceiver. The only downside of the PLC board I bought is that it's RS485 transceiver is an SN75176 without any extra protection, and this old transceiver is quite fragile. These PLC boars are excellent microcontroller experimentation boards. Quite often they also have holes for a programming header, but adding a few bodge wires (you only need GND and two others for STM32) is also a possibility.

author: John B
author_rank: Frequent Contributor
timestamp: November 15, 2023, 08:36:23 pm
content: The arduino libraries are fairly limited in what they can do, but they cover the basic functions, ie reading/writing coils, registers, singular or sequential. But some other ones like the mask write function are missing, and further advanced functions are not covered at all.I also ran into some weird issues on an arduino nano, despite the compiler showing sufficient memory and ram, where coil and register memory addresses were being overwritten by other functions, so you may be limited in sketch complexity.There are more comprehensive libraries like PyMODBUS for python, but then you're moving away from microcontrollers to more advanced devices that are going to running a full linux OS.

author: 5U4GB
author_rank: Super Contributor
timestamp: November 16, 2023, 07:51:42 am
content: Quote from: dferyance on November 09, 2023, 12:51:43 amFor a test system, anything running Linux with libmodbus and a USB<->RS485 converter makes for an ideal development environment. For general debugging there's also theCAS Modbus Scanner, although I've never played with that.

author: Siwastaja
author_rank: Super Contributor
timestamp: November 16, 2023, 10:21:48 am
content: libmodbus makes sense because it hides both MODBUS TCP and RTU behind the same interface, takes care of socket management / serial/termios stuff, if you do all of this yourself it's easily 500LoC. Doing UART on linux is much harder than doing UART on any MCU.OTOH, when we are talking about microcontrollers and RTU only, then I question the sensibility of using a library, unless it happens to have exactly the interface you need, and happens to be ported for the microcontroller you need, exactly the way you need (interrupts / DMA / etc. which does not interfere with the rest of your application design). But very likely it is just easier to implement it ad-hoc since it's not much more than writing a few header bytes to a buffer and calculating a CRC.

author: gmb42
author_rank: Frequent Contributor
timestamp: November 16, 2023, 10:46:14 am
content: Quote from: Doctorandus_P on November 15, 2023, 05:13:28 pmNot really, there are 2 base Modbus protocols, RTU which is binary and ASCII which is .. ASCII. Either can run over whatever transmission link you like.

author: Siwastaja
author_rank: Super Contributor
timestamp: November 16, 2023, 11:08:25 am
content: Modbus ASCII would be quite rare to see in actual use, while Modbus RTU and TCP are popular, and they arenearlyidentical in data frames - modbus TCP has two extra fields and does not use CRC field because TCP itself is error-free.

author: 5U4GB
author_rank: Super Contributor
timestamp: November 17, 2023, 11:30:41 am
content: Quote from: Siwastaja on November 16, 2023, 10:21:48 amThere's a bigger reason for using it than merely convenience, it hides all of the quirks and oddities and non-portabilities and strange corner cases and who-knows-what else so you can focus on dealing with the target device, not debugging odd boundary conditions in the comms. libmodbus is several thousand lines of code, the coremodbus.calone is around 2,000 lines (have a look at it to get an idea of the level of complexity required for a full implementation). The fact that libmodbus just works for most cases is because the devs spent a huge amount of time making sure that it just works. Every time I hook up some weirdo new device to it I'm astounded that it really does just work.

author: Siwastaja
author_rank: Super Contributor
timestamp: November 17, 2023, 12:45:51 pm
content: Quote from: 5U4GB on November 17, 2023, 11:30:41 amExcept that the exact same point can be made the other way around. Modbus devices notoriously implement the protocol in whatever way they feel like. The risk of using a library is that it does not work with the device without modification into internals.Modbus indeed is full of quirks. One of the simpler one is the drug-induced definition of register addresses, the original standard defines that "on paper" specified addresses are off by one from the "on wire" addresses (I forget which way, demonstrating how cumbersome such feature is). If everybody always followed strictly the standard, this would be just a small inconvenience, but because they don't, you have two types of addresses, and actually since you can't assume, off-by-one can happen in either direction. Therefore register address 12345 can actually mean 12344, 12345 or 12346. Without a library, you obviously choose which convention you follow (I follow the "on-wire" notation, even when it is nonstandard!), which instantly limits the choices from 3 to 2, and if the device specification happens to make a note whether the addresses are on-wire or on-paper, that's instantly down to 1! With libmodbus, you have to read through the documentation, so have one layer of uncertainty more. And becausethey actually don't tell you(see example:https://libmodbus.org/reference/modbus_write_register/), you are on your own.So eventually, if all youreallyneeded to do is to read holding registers, you would have copy-pasted the C language CRC check function directly from the official standard document, and written the 5 remaining lines that set the n_registers etc. accordingly, like this (actual code from a random project of mine:)Code:static uint8_t rtu_read_req;rtu_read_req = slave_addr;rtu_read_req = func_code;rtu_read_req = (reg_addr&0xff00)>>8;rtu_read_req = (reg_addr&0x00ff)>>0;rtu_read_req = 0;rtu_read_req = n_regs;uint16_t crc = crc16(rtu_read_req, 6);rtu_read_req = (crc&0x00ff)>>0;rtu_read_req = (crc&0xff00)>>8;rs485_start_write_read(rtu_read_req, buf, sizeof rtu_read_req, rx_buf_len_needed, modbus_rs485_inst, modbus_baud, modbus_mode);OK, it was more than 5 lines, sorry 'bout that.But be my guest and by all mean port libmodbus to a microcontroller, or fix a random arduino project which does not work, so that you can use a third-party library where library is not needed, the pinnacle of cargo cult engineering. In real world, libraries carry overhead, which is why they are most useful when they do complicated things which can be abstracted to simple and intuitive interfaces. And 97% of the complexity of libmodbus is in managing serial devices (maybe through termios, interface of which sucks) and TCP sockets; something you don't have on a microcontroller at all in form compatible with libmodbus, so you would be redoing most of it anyway.Quote from: 5U4GB on November 17, 2023, 11:30:41 amNice, but devices which implement it fully are rare. Probably some Modicon/now Schneider automation does, and of course any project actually using libmodbus would then be mostly compatible with libmodbus.I have lately written drivers for various, mostly Chinese, solar PV inverters and none of them implement anything beyond bare minimum modbus, and worse, they use weird non-standard conventions. If you are a slave, you may want to implement a bit more to be compatible with masters too clever, but especially if you are a master, you can implement bare minimum that gets the job done.Modbus is a low-quality spec, meaning it's followed ad-hoc in whatever way. It needs to be approached in a practical instead of perfectionist way.

author: Perkele
author_rank: Regular Contributor
timestamp: November 18, 2023, 09:05:40 pm
content: Quote from: Siwastaja on November 16, 2023, 11:08:25 amASCII is still used. It saves the day in cases when equipment is nitpicky about 1.5 or 3.5 RTU character timings.Or when cabling is not up to specification.Or with some RS-485 converters that have a poor implementation of RX/TX switching via DTR or RTS signal.It is used enough that I had to fix several bugs in libmodbus implementation of ASCII to get it to work.

author: Perkele
author_rank: Regular Contributor
timestamp: November 18, 2023, 09:14:01 pm
content: Quote from: 5U4GB on November 16, 2023, 07:51:42 amAnd use it only on Linux. Windows implementation of RTU is broken, there is data overrun problem when reading the data from serial port.The project itself seems to be in maintenance mode, there is no mention of adding support for Modbus function codes codes 20 and 21.

author: peter-h
author_rank: Super Contributor
timestamp: November 19, 2023, 09:30:46 am
content: IME all modbus slaves implement only the subset they need. Why should they implement registers they don't use?Also AFAICT the vast majority of modbus applications involve a SCADA master (basically a PC running some sort of SCADA package) which has comprehensive scripting facilities for packing/unpacking data into the 16 bit modbus registers.Yes modbus is old (the register paradigm is for 16 bit analog values, 1-bit signals, and relay coils, so eg text strings get loaded into the 16 bits regs, sometimes packed 2 at a time, sometimes not, and floating point values again have more variations) but it works and anybody can use it. In the real world, smart people tend to keep stuff simple because they know complexity will bite you in the bum within months of the original system builder leaving the companyAlternatives to modbus were always at least as horrible. I once wrote a converter between Eurotherm Bisync and Modbus RTU, in this devicehttps://www.kksystems.com/programmable-protocol-converters/kd485-prog-70.htmlPicking through the various edge cases was horrible.The 3.5 character period packet delimiter is implemented only sometimes. Probably the majority of devices do not. On a PC it is hard to implement because you don't have a decent real time OS - you need a state machine with fairly precise 1ms timing to do it. And that is at 9600 baud; at higher speeds it becomes impossible, and if running over TCP it is harder; there are solutions but they limit your options.

author: Siwastaja
author_rank: Super Contributor
timestamp: November 19, 2023, 10:13:14 am
content: Quote from: peter-h on November 19, 2023, 09:30:46 amYeah. But remember this is the minimum time specification. If you are a master, you can just sleep like a second (modbus usually isn't used where high data throughputs are needed). If you are a slave, and the master violated the spec and sent the next poll too early, and you still received it correctly (your slave id, CRC OK), then why not reply to it instead of throwing "you violated the spec" temper tantrum. Simple.QuoteYes. Stuff like libmodbus are actually toy projects; they are incapable of guaranteeing successful communication because they work with limitations of weird and unsuitable interfaces like termios, and buffered IO. libmodbus works very well in practice given these limitations, but it's a poor reference when we discuss doing this stuff directly on microcontrollers, which is both easier and more robust.

author: gerber
author_rank: Newbie
timestamp: November 19, 2023, 01:17:21 pm
content: For a proper working master for PC I can recommendModbus Poll. It can be used time limited without license, but one license is only $130 and gives perpetual updates. If you're developing a Modbus device, this is a really nice tool. It supports RTU/ASCII/TCP, both base 0 and base 1 addressing, has real time charting, csv logging, traffic monitoring etc. Support for commands 01, 02, 03, 04, 05, 06, 08, 0B, 0E, 0F, 11, 16, 17 and 2BDisclaimer: No affiliation with them other than being a satisfied customer.Apparently they have a slave device as well, but I have not used it.I also suggest getting a proper serial terminal, my fieldbus guru SW guy recommended Docklight to me when we started working toghether, there might be others but I like that one (that's also available for use both free and behind license)

author: peter-h
author_rank: Super Contributor
timestamp: November 19, 2023, 10:21:10 pm
content: QuoteWell, yes, but there can be issues.One thing is that typically the master is polling say 10 slaves, and it needs to get around them within some time limit. This is actually the main reason in practice for high baud rates e.g. 115k. But it can't just wait for 1 second between polls.Another thing is that some slaves will speculatively receive and decode every packet they see travelling on the 485 bus. This is especially in older products where you were running at say 16MHz and the code was written in C++With a float multiply taking 2ms... The idea is that you start compiling the response into a buffer and when the CRC on the poll verifies, you can return it right away. Remember that the address byte means precisely nothing unless the CRC is valid! With totally garbage data the address byte will be valid once in every 256 packets! In fact a 16 bit CRC will be valid once in every 64k garbage packets and for fun I once experimentally verified this in a token ring LAN I built around an 85C30, SDLC and MIL-STD-1553 physical busBut this slave is probably running a 1kHz timer tick andwillbe checking the 3.5 char gaps. So the master must not insert say 5ms gaps into the poll, either.