Posts Tagged “hora”

I haven’t written on blogElectronica for nearly a month. For one reason or another it’s been hard to find any spare time in the past few weeks… I’ve had a lot of work, a personal project that has required more of my attention than usual, a small operation to remove some small spots from my eyelids (I’m sure someone asked me what had happened to my eyes the other day ;) ) and the last few days I have been unwell with the flu. From now on I hope the go back to writing an article every week or so.

Right let’s get started, this is an easy article to pick up the pace. We are going to look at an example of how to get the date/time in your TC65 Siemens/Cinterion modems (of course all this applies to other terminals like the MTX65MTX65+G and TC65T).

Occasionally we need to have the current date/time in our Java program for these modems. What for? For example it’s to save the time on a log along with some data or to do a task at a specific time etc. This means that there are multiple situations where we need to have the correct time. As I’m sure you know, the TC65 has an RTC, but unless we have a back-up battery for this RTC (the MTX65 has the footprint for it), after rebooting the computer the RTC won’t have the time.


So how do we get the date/time? 

Well there are several ways. If we are talking about a  GPS device like the XT65 (or MTX65+G) it’s a lot easier as it can be extracted from the position frame i.e. if we reed the position with AT^SGPSR=0 we will get the response:

  ^SGPSR: [GpsDate, GpsUTCTime, GpsLatitude, NS-Indicator, GpsLongitude, EW-Indicator, GpsAltitude, GpsSpeed, GpsCourse, GpsStatus]

where in GpdDate and GPSUTCTime we get the date and time respectively. But this doesn’t work will all modems like with the TC65, MTX65 and TC65T that have GPS. In fact even for the XT65/MTX65+G we can need to know the time even without GPS coverage.

What can be done then? 

Well a typical and simple way is to send itself an SMS message. I will be able to get the date/time at the end of the SMS after I have received it. Once I have got it from the SMS, I can establish the time in the modem with the command AT+CCLK. This method doesn’t give a very accurate time, as you can tell easily.

Without doubt the best way to get the date/time is using NTP (Network Time Protocol). NTP allows you to synchronize the clock with variable latency networks like the internet with very high precision. For this method UDP packets are used. However this protocol is quite complex and the vast majority of applications don’t need to have millisecond accuracy.

And how do you get the time with some precision in a simple way?

With Time Protocol (RFC868). It’s very, very simple. There are many time servers online that offer Time Protocol e.g.:


Very good, how does it work? 

Well basically these servers are listening to the TCP/UDP 37 port and the procedure is as follows (for TCP):

1.- Connect to the time server via the TCP37 port.
2.- After connecting, without us sending anything, the server sends us a 4-byte data package with the date/time (with the seconds passed from 00:00:00 of 1/1/1990).
3.- The server closes the socket.

It’s that easy.

Indeed it’s not as precise as using NTP, but the time from when the server sends the 4-byte data package until the time that we receive it can vary slightly. But as I said before, being one or two seconds out isn’t important for most of our applications

Java Example.

I’m posting  a Java example here for the MTX65/MTX65+G for those who need it. It does what I mentioned before. It connects to a time server, gets 4-bytes with the time and calculates the date/time. How easy is that?

The program is very simple, but just so nobody gets confused I will go over the following lines from the code:

//Calculate the seconds passed from 1/1/1900 secondsFrom1900=buffer[0]*16777216 + buffer[1]*65536 + buffer[2]*256 + buffer[3];

//Now calculate the milliseconds passed from 1/1/1970

//As Java needs milliseconds (not seconds) to
//calculate the date from 1/1/1970 we multiply it by 1000

//We get the current date/time (GMT)
date = new Date(millisecondsFrom1970);
System.out.println(“Date/Time:  ” + date.toString());

The protocol specified in the RFC868 indicates that the server has returned the seconds that have passed since 1/1/1900 at 00:00:00 and I store and calculate it in the secondsFrom1900 variable.

But Java, the constructor of the Date class, needs the milliseconds (milli, not seconds) passed from 1/1/1970.  This is why we firstly subtract the seconds passed from the year 1900 to today from the seconds passed from 1900 to 1970 (some 2208988800 seconds), obtaining the seconds passed from 1/1/1970 to today. After we multiply the value by 1000 as we need to work with milliseconds and not seconds. The rest of the program is very simple.

Well I hope that you found this article interesting, see you next time. Be good! ;)


Tags: , , , , , , , ,

Comments 15 Comments »