Posts Tagged “ejemplo java”

Well here I am back from vacation. This Easter I spent a few wonderful days in Cáceres with my family (a lot of my mother’s family lives there). I had not been there for many years and I missed the drumsticks, the drunkness the family. :) The truth is that I really enjoyed it and I hope to go back soon.

Ok, let’s get on with it… today we are going to look at implementing a Web Server.

Sometimes it can be interesting to incorporate a little web server in our TC65 or MTX65 GPRS modems. It’s convenient to directly connect to the modem with a browser or any parameter. So today I’m putting up an example that I’ve made, it’s slightly longer than usual, it took up quite a lot of time and it implements this: a small and simple (very simple) Web Server. This example can also be used as a Socket Server example. I don’t remember which blog user it was but I told them that I would post an example or Socket Server in Java. So here it is.

The example here is part of one of my projects. I have taken many things from my project, including error control, to break it down a little bit and make it more understandable. I think that it’s enough. Anyway to remove the code there might be an instruction that’s not necessary, I haven’t looked at everything, I just checked that it worked properly.

Blog de Electrónica Avanzada

Alright, what does this example do? 

If you download and open the project you will see 3 files. To test out the example you just have to edit the main.java and change the connection parameters depending on your operator. If you use Movistar in Spain you can leave the code as it is and compile and run the project.

When you run the program you will see that the modem is connected to the GPRS and the obtained IP will come out through the standard output (normally ASC0). If I run it I get:

java-web-server1

After this, open the browser (I only tried with Microsoft Explorer, nothing else) and write something in the address bar like:

http://80.29.209.172/suma.html?a=10&b=20

You will get a response like this:

java-web-server2

This means that the modem has received the HTTP GET request, taken the values “a” and “b” from the parameters passed in the URL, added them together and returned them to a HTTP page with the sum. Obviously what this example does (GPRS calculator) is not used a lot and doesn’t make much sense, but I’m sure that this example will spark new ideas.

One more thing, I have put a variable called “numMaxClientesX” in the code. You can specify the number of concurrent requests that you may have in this. By default it’s “1” and you can increase it but remember that Siemens/Cinterion modems support up to 6 sockets and one of them already has the socket listener that listens in the TCP80 port (Http). Also, think about launching a Thread for each connection established.

The example is done using sockets via java classes. To whoever asked me which is better, if you manage the sockets with java classes or with AT commands as I have sometimes done before in this blog, I wouldn’t know what to say as they are more or less the same. The advantage of using java classes is that perhaps the application (socket server) is slightly simpler to implement and with AT commands you have a little more control over each of the socket’s status. They are subjective assessments as both methods work really well; sometimes I use one and sometimes the other.

I hope that you found it interesting, until next time. ;)

www.blogElectronica.com

Tags: , , , ,

Comments 12 Comments »

On several occasions I have been asked how to add more data to an existing file in the flash memory of our TC65 or XT65 modems (or MTX terminals from Matrix). So today I am going to post a small Java program to help those of you who don’t know how to do this. It’s very simple.

Basically you use the OutputStream object instead of the DataOutputStream (DataOutputStream comes from OutputStream) which I’m sure you saw in the post where the majority of the Java examples are.

Let’s look at a little example today. Let’s assume that we have a file in the modem’s file system called  ”A:/file.txt“ with the text “hola”. If you don’t have one, make one and put it in the modem.

Blog de Electrónica Avanzada

/* Create an object in the FileConnection class and specify the name of the file to open, which will be file.txt*/
FileConnection objFileConnection = (FileConnection)
Connector.open(“file:///a:/file.txt”);

/*Check the current file size*/
long fileSize=objFileConnection.fileSize();

/*Create an object in the OutputStream class. With this we can write data in the file and pass it off as a current file size parameter so that the file writing “pointer” is at the end of the file.*/
OutputStream objOutputStream = objFileConnection.openOutputStream(fileSize);

/*Write the text “Adios” inside the file that we have just opened (or created)*/
objOutputStream.write(“adios”.getBytes());

/*Send it writing everything that’s left in the buffer prior to closing it*/
objOutputStream.flush();

/*Close the object*/
objOutputStream.close();

/*Close the file*/
objFileConnection.close();

Once you run the application, if you look at the flie.txt contents you will see that the contents include “holaadios” i.e. it doesn’t overwrite the contents that’s already in the file, it adds the data.

To summarize, I will put up a few posts about two new terminal models, the MTX65+Gv3 (GPRS terminal modem with GPS) and the new MTX65-ULP (GPRS ultra low power modem). Firstly we will look at their features and then I will go into more detail about programming both devices, paying attention to the peculiarities of each one so that everyone will know exactly how to use them. I’m sure you’ll like it. This will be in a few days time.

I hope it’s been interesting for you. ;)

Tags: , , , , , , ,

Comments 7 Comments »

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.

RFC868

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.:

time-a.timefreq.bldrdoc.gov
time-b.timefreq.bldrdoc.gov
time-c.timefreq.bldrdoc.gov
utcnist.colorado.edu
time-nw.nist.gov
nist1.nyc.certifiedtime.com
nist1.dc.certifiedtime.com
nist1.sjc.certifiedtime.com
nist1.datum.com
ntp2.cmc.ec.gc.ca
ntps1-0.uni-erlangen.de
ntps1-1.uni-erlangen.de
ntps1-2.uni-erlangen.de
ntps1-0.cs.tu-berlin.de
time.ien.it
ptbtime1.ptb.de
ptbtime2.ptb.de

 

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
millisecondsFrom1970=secondsFrom1900-Long.parseLong(“2208988800″);

//As Java needs milliseconds (not seconds) to
//calculate the date from 1/1/1970 we multiply it by 1000
millisecondsFrom1970=millisecondsFrom1970*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 »

Today I am going to write a small post with a new Java example for Siemens modems, it follows on from the post with examples that I wrote a few days ago. I will be posting more examples from time to time that you can use. If you ever want to share any examples or ideas with me they will be gladly received. ;)

Programación java Siemens


FTP_EXAMPLE
 (Download)

Description: it’s a basic FTP application with Java for Siemens modems. It creates a FTP connection and it creates a file called “file.txt” in the remote server and it contains “123”. All you have to do it modify the code line where the login, password and FTP server are that you want to use.

Valid for modems: TC65, XT65, TC65T, MTX65 y MTX65+G

I want to comment on one detail from the examples that I posted the other day. I posted all of the examples in the format EXAMPLE_xxx. Well all of the examples work perfectly with the command AT^SJRA but if you put that name in the AT^SCFG command’s “Userware/Autostart/AppName” so that the application starts automatically, it will not work. Don’t write the underscore (_) in your .jar file’s name for autostarting.

Right that’s it for today, it’s my daughter’s birthday today and I want to set up the Wii that I bought her before she gets home. And no it’s not a boomerang gift, I’m more of a Playstation guy. :) I will automatically get myself a PS3 as a gift before the end of the year. See you next time.

Tags: , , , , , , ,

Comments 33 Comments »

Over the last few months I have been getting a number of programming examples for gprs Siemens modems (TC65 and XT65) and for terminals  (TC65T, MTX65 and MTX65+G) both distributed by Matrix in Spain.

Today it’s Saturday and I’m going to give you a set of examples from these modems so that they are there when you need them. The majority of them can be found in previous posts although there are a few new ones.

They are quick examples i.e. I don’t completely run all of the exceptions or anything but if you are just starting out with these modems, I think this may be a good guide. You will see that the vast majority of applications that you can get for real projects are based on small tasks, which is what the following examples show:

Programación java Siemens

Here we go; these are the examples that are in the blog:

EXAMPLE_HelloWorld (Download)

Description: All this basic application does is take information from the modem’s standard output. First you have to configure the standard output with the command AT^SCFG for example AT^SCF=”Userware/Stdout”,””,”ASC0″

Valid for modems: TC65, XT65, TC65T, MTX65 and MTX65+G

EXAMPLE_SMS (Download)

Description: Basically what this example does is send an SMS to a specific phone number every minute.

Valid for modems: TC65, XT65, TC65T, MTX65 and MTX65+G

EXAMPLE_Thread (Download)

Description: The program launches a thread, what this does is count up to 5 taking the result from the standard output. The program waits for the end of the thread before finishing.

Valid for modems: TC65, XT65, TC65T, MTX65 and MTX65+G

EXAMPLE_Timer (Download)

Description: This example creates a 5 second timer i.e. every 5 seconds it runs task, what it does is take data from the modem’s standard output.

Valid for modems: TC65, XT65, TC65T, MTX65 and MTX65+G

EXAMPLE_GPIO (Download)

Description: It shows how to configure a GPIO input and output with Java, e.g. changing the GPIO output status or how to read the input.

Valid for modems: TC65, XT65, TC65T, MTX65 and MTX65+G

EXAMPLE_TCPConnection (Download)

Description: This shows how to create a TCP/IP connection with the AT command class. The example creates a connection with a socket that connects to Google’s IP; it sends a frame through the socket and receives a string through the socket which shows the standard output.

Valid for modems: TC65, XT65, TC65T, MTX65 and MTX65+G

EXAMPLE_TCPConnection2 (Download)

Description: It shows how to create a TCP/IP connection using Java classes themselves. The example creates a connection with a socket that connects to Google’s IP. It sends a frame through the socket and receives a string through the socket which shows the standard output.

Valid for modems: TC65, XT65, TC65T, MTX65 and MTX65+G

EXAMPLE_Files (Download)

Description: This shows how to create a file in the modem’s flash memory, how to write data in it and read the data too.

Valid for modems: TC65, XT65, TC65T, MTX65 and MTX65+G

EXAMPLE_GPS (Download)

Description: This shows how to use GPS with the AT command class. The boot up program activates the GPS and it programs it to receive a URC with the position every 5 seconds. This position is stored in a variable and it also shows the standard output.

Valid for modems: XT65 and MTX65+G

EXAMPLE_GPS_JSR179 (Download)

Description:

This example briefly shows how to capture a GPS position without using the AT commands class i.e. using the JSR179’s Location class.

Valid for modems: XT65 and MTX65+G

EXAMPLE_HTTP (Download)

Description: This example creates a GPRS connection and uses the Java class HttpConnection that is downloaded and shown through Google homepage’s standard output

Valid for modems: TC65, XT65, TC65T, MTX65 and MTX65+G

EXAMPLE_SerialPort (Download)

Description: This shows how to use the modem’s serial port/s with Java. It opens the two serial ports ASC0 and ASC1 and whatever it receives at ASC0 at 115200 bauds is retransmitted by the ASC1 at 57600 and vice versa. It’s designed for the TC65 but it is used to see how to operate the XT65’s ASC0 port.

Valid for modems: TC65, TC65T and MTX65 (XT65 and MTX65+G only 1 serial port).

EXAMPLE_Watchdog (Download)

Description: This shows how to use the TC65v3’s new Watchdog. The program has up to 25000. After 10000 the program stops refreshing Watchdog so that you see how to reset it after 15 seconds.

Valid for modems: TC65, TC65T and MTX65 (with version 3.0 de firmware)

I hope that you find these examples useful. I will try to put more on soon related to low power modes and FTP.

So congratulations to those of you who are on holiday and to those who aren’t like me, cheer up we’re almost there! ;) See you next time!

Tags: , , , , , , ,

Comments 157 Comments »