send time stamp with Modbus


Thread Starter


How to send time stamp on MODBUS (RS485)? Is better to use datatype integer for each parameter (likes seconds, minutes & Hours) or combine seconds, minutes & Hours in 32-bit float. If it is in 32-bit float, then how to convert time into 32-bit float?
There is no set way to send date/time values with modbus. I have seen a few different methods.

The first is the most basic by using six registers for six 16-bit integers representing year, month, day, hour, minute, second.

The next method uses two 32-bit integers, one for date (as days since Jan1,1900) and one for time (as milliseconds since midnight).

I've also seen a third method using one 32-bit floating point value where the integer portion represents the date and the fraction represents the time. This is how date/time is represented in an Excel Spreadsheet. The problem with this method is the resolution of a 32-bit float is about 7 digits. Since a 2010 date uses 5 digits, this leaves only two for the time, giving a resolution of about 5 minutes. Therefore, in my opinion, if you want to represent date time with a single floating point value, you should use a 64-bit float (a block of 4 registers).

Enron Modbus sends date and time values using two 32-bit floats. They are sent as whole numbers like 122810.00 for Dec 28, 2010 and 141634.00 for 2:16:34 PM

Since there is no defined format for date and time in the modbus spec (that I know of), you can use one of these or create your own. As long the client and server (master and slave) are both set to use the same method, you are free to do it how you like.
This is not a question which is possible to answer without knowing what is encoding and decoding the time stamp at each end. You need to take leap years and "leap seconds" (which are added to years occasionally) into account, and you need to know that both ends are using the same algorithm.

The POSIX (or unix) time stamp is the most common format for time stamps. It is often represented as a floating point number to give you milli-seconds, but if you don't need milli-seconds then an integer would work just fine. However, a 32 bit integer will cover just 1970 to 2038 (which is why unix-like systems now use 64 bit integers for their clocks).

If your software at both ends doesn't already support time stamps in this format, then you will need to do all these calculations yourself. It's not a trivial exercise if you really do need accuracy to the second. There is lots of source code to do this that you can find on the Internet.

*However*, there are many other possible time stamp formats. One or more of these may be in common use in your industry, or be required by some other piece of software that you are using.

The above is assuming that you need a "general purpose time stamp". This is all I can assume however, as you have posted a very generic question. If you don't need a general purpose time stamp but can use one with more limited capabilities, then there are other things which can be done.

John Waalkes

Not to sound flippant, but I would think that it would be the one that your Modbus device expected it to be in.

Or are you creating your own Modbus enabled device? (which would be cool!)

As for float, that would be a bad choice as your variable type, since your value will get shoehorned into the variable type that it has to fit in (meaning that you will lose some accuracy).

So I would go with a byte each for seconds, minutes, hours, days & months, and a 16 bit word for years (that should be enough, it will outlast the Mayans in any case).

You can do ticks or seconds from midnight (if you are building your own device), but float is a poor choice in any case.

But, if you are working with a "store-bought" device, then you are pretty much locked into what the vendor has set up (hopefully it was a sane decision on the manufacturers part).

Good luck!