View Full Version : libmodbus patches for MODBUS/RTU over TS-XUART

May 18th, 2012, 01:47 AM
I've added native support for using libmodbus via xuartctl TCP serial port servers. Since TS supports the usage of XUART serial ports as network services, this allows one to create apps using libmodbus and the MODBUS/RTU protocol that are hosted on the local or a remote machine but use RS485 or RS232 serial ports on a SBC anywhere on the internet or local network.

Real serial port hardware is increasing becoming a rare feature on modern computers and often times USB to Serial converters are the only option. Unfortunately, in my experience, these USB to serial converters seem to always have sub-standard device drivers that often cause the host machines to crash or the USB device to lock up until you unplug/replug it in. Not only that, their performance is often quite poor when it comes to protocols like MODBUS that have a lot of request/responses due to lack of a real discrete IRQ signal on USB. The default software "xuartctl" on most of our XUART capable SBCs basically allows the SBC to function like a serial to ethernet converter out of the box. These network "exported" serial ports can be connected to by any other machine on the network that knows its IP address and used just as conveniently as a local serial port on any computer on the network.

To use MODBUS/RTU over real serial drivers in the past you've had to start out your C application something like this:

modbus_t *ctx;
ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);

This has not been changed in the patch, but now that function is smart about being passed a device name matching "HOSTNAME_OR_IPADDRESS:PORT", such that the following will also work to (e.g.) connect to an XUART port being served up at on TCP port 7350:

modbus_t *ctx;
ctx = modbus_new_rtu("", 115200, 'N', 8, 1);

The rest of the interaction with libmodbus is exactly the same. It should be noted that the patch makes use of a native "xuartctl" binary installed somewhere in the $PATH. "xuartctl" compiles on just about any POSIX system out there, Mac, Windows, Linux, Solaris, etc and its source is available here: http://ftp.embeddedarm.com/ftp/ts-arm-sbc/ts-7500-linux/sources/ It is installed by default on all SBC's with XUARTs.

Note that this has nothing to do with MODBUS/TCP which is another protocol which is supported by much fewer MODBUS devices out there that happen to have an ethernet port instead of RS485 serial.

I'm not sure if this will be integrated in future official distributions of libmodbus as that is up to the original author, but the patches are being submitted freely with the same open source GPL license as the original libmodbus. To find out more about libmodbus, please visit its website at http://libmodbus.org

To find out more about XUARTs and the userspace driver "xuartctl", please visit the wiki documentation at http://wiki.embeddedarm.com/wiki/Xuartctl

Grant Peacock
May 20th, 2012, 08:46 PM
For anyone who is not familiar with XUART hardware and MODBUS/RTU, I'd like to further explain the performance advantage of this patch.

MODBUS/RTU has strict timing on its serial frames. Each frame of typically 8 or more characters is not permitted to have a gap in it of 3.5 character times. If the transmitting device is running an RTOS, this is not a problem, but it is difficult for Linux to make this sort of guarantee. The faster the baud rate, the more this is an issue. At 9600 baud, any modern device can keep up, but at 115200 or 250000 baud, on an embedded system that is busy with other processing tasks, malformed MODBUS frames can become a concern. Any time the frame is transmitted with a gap, the client software will have to time out after waiting for a response and then retry. If this scenario is prevented, a maximum latency is established for MODBUS transactions.

The XUART hardware can transmit serial frames of up to 256 characters that are 100% guaranteed to be contiguous with no gaps. The libmodbus patch takes advantage of this capability to guarantee MODBUS compliance.

TS is also developing a series of affordable MODBUS/RTU slaves that can operate at up to 2.5 megabaud. The TS-7520-BOX and other MODBUS-capable SBCs will work with these devices as well as any MODBUS compliant legacy products. This makes it easy to use MODBUS as a high-performance IO expander to build a control system that is low cost, reliable, rugged, and physically distributed around a large area or building.

June 2nd, 2012, 09:28 PM
For the modbus, can we use libmodbus library directly and communicate through modbus command?

June 6th, 2012, 01:54 AM
For the modbus, can we use libmodbus library directly and communicate through modbus command?
Yes. libmodbus creates several useful C routines for querying modbus registers, setting coils, etc without having to actually construct modbus packets and compute your own CRCs

June 6th, 2012, 02:11 PM
Thank you first. Now my problem is that when ts4200 act as a medium to send raw data received from a master and send this raw data to a slave. I saw libmodbus has modbus_send_raw_request function can be used to send raw command, but when ts4200 received the reply from the slave, how does it send this raw reply data to master. Is there any function just like modbus_send_raw_request to send raw reply data?

Thank you in advance.

June 8th, 2012, 06:39 PM
If there is a way to find out the file descriptor of the serial port, you could simply use the write() sys call.

I'm not a libmodbus expert, but you may want to ask on that project's mailing list: libmodbus@googlegroups.com

Grant Peacock
June 12th, 2012, 04:05 AM
Hi Daniel,
The TS-4200 does not use XUARTs. I am unsure how well the TS-4200 will perform as a modbus device. There is no reason you can't install and use libmodbus on it.

It sounds like you are using the 4200 as a modbus server (slave) instead of a client (master) which is fine. I think you will be able to construct the responses you need using modbus_reply() although I'm not sure if that function has all the flexibility you need. As Jesse points out, modbus is not that complicated of a protocol and you can build the packets yourself and write() them to the serial port if libmodbus can't do what you want.

The serial port implementation in the TS-4200's Atmel CPU is a little different from what you may be used to on other TS products or Linux devices. The way it works is by using a DMA channel to move bytes to or from a UART register. The idea is that this is simpler than the one interrupt per character load suffered on many CPUs. While one would think that using DMA to provide a steady stream of characters would be a good way to send out modbus serial packets without gaps, our first tests have shown that this doesn't work as expected. At 19200 I assume it will work, but I believe we saw some issues at 115200 baud. I don't have any data in front of me however.

I'm sure the 4200 can be made to act as a reliable modbus server, but it may require Linux kernel development or verilog development.

September 4th, 2012, 11:52 PM
I am plan to use modbus on ts4200&ts8160 ttyS5, is it possible? If it is, how?


Grant Peacock
September 5th, 2012, 12:04 AM
Are you building a modbus server (slave) or client (master)? What baud rate will you be using?

MODBUS is a slippery protocol that is implemented a little differently be different devices. So, yes, you can easily create a modbus device, but you implement it the easy way you may find that some devices work with it and some don't.

If you must use ttyS5 (or any Atmel UART) then the worst case might be that someone has to write a modbus-specific driver to replace or improve the stock UART driver. But at low baud rates there shouldn't be any need for something like this.

October 5th, 2012, 11:12 AM
MODBUS is a slippery protocol that is implemented a little differently be different devices. So, yes, you can easily create a modbus device, but you implement it the easy way you may find that some devices work with it and some don't.