Pesquisa personalizada

2008/07/04

J2ME: Using CommConnection at Linux with an USB Serial Port Adapter

I use an USB Serial Adapter with my Linux (Kubuntu - Hardy) notebook. I'm developing a midlet to run into an Automatic Vehicle Locator (aka AVL) hardware Linux based and with MIDP 2.0 CLDC 1.1 support. To comunicate with the AVL I can use a standard RS-232 port or Internet (via GPRS). To program the AVL I prefer to use the serial port to transfer the JAR to it, but to debug, I want to use my full IDE features (I use Netbeans with full Sun mobility pack installed).

The AVL has three serial ports:

  • /dev/ttyS0 - to the standard console;
  • /dev/ttyS1 - to communicate with its GPS module (standard NMEA 0183);
  • /dev/ttyS2 - to communicate with its GSM/GPRS module (to transfer data and send/receive AT Commands).

Instead always transfer the JAR file to the AVL (I'm using screen + zmodem to do it), I wanted to debug it in my notebook and only after test it locally is that I would turn it in the AVL. To do it I attached a GSM/GPRS modem via an USB serial adapter to my notebook and also a GPS with another serial adapter. In Ubuntu the two USB serial port adapters appears as:

  • /dev/ttyUSB0
  • /dev/ttyUSB1

However, after trying to open the serial port from the J2ME midlet running in the emulator, I got exceptions:

set attr failed(00000005): Input/output error
java.io.IOException: Input/output error
at com.sun.midp.io.j2me.comm.Protocol.connect(Protocol.java:285)
at com.sun.midp.io.ConnectionBaseAdapter.openPrim(ConnectionBaseAdapter.java:103)
at javax.microedition.io.Connector.openPrim(Connector.java:329)
at javax.microedition.io.Connector.open(Connector.java:222)
at javax.microedition.io.Connector.open(Connector.java:198)
at javax.microedition.io.Connector.open(Connector.java:180)
at net.marciowb.cldc.avl.comm.ATCommand.(ATCommand.java:38)
at net.marciowb.cldc.avl.vt850.Rastreador.openIO(Rastreador.java:123)
at net.marciowb.cldc.avl.vt850.Rastreador.(Rastreador.java:69)
at java.lang.Class.runCustomCode(+0)
at com.sun.midp.midlet.MIDletState.createMIDlet(+34)
at com.sun.midp.midlet.Selector.run(Selector.java:150)
Unable to create MIDlet net.marciowb.cldc.avl.vt850.Rastreador
java.lang.IllegalArgumentException: Missing device name
at com.sun.midp.io.j2me.comm.Protocol.connect(Protocol.java:247)
at com.sun.midp.io.ConnectionBaseAdapter.openPrim(ConnectionBaseAdapter.java:103)
at javax.microedition.io.Connector.openPrim(Connector.java:329)
at javax.microedition.io.Connector.open(Connector.java:222)
at javax.microedition.io.Connector.open(Connector.java:198)
at javax.microedition.io.Connector.open(Connector.java:180)
at net.marciowb.cldc.avl.comm.RS232.(RS232.java:40)
at net.marciowb.cldc.avl.vt850.Rastreador.(Rastreador.java:70)
at java.lang.Class.runCustomCode(+0)
at com.sun.midp.midlet.MIDletState.createMIDlet(+34)
at com.sun.midp.midlet.Selector.run(Selector.java:150)

It happens when I try:

String cnnGsmStr = "comm:COM1;baudrate=115200;bitsperchar=8;stopbits=1;parity=none;blocking=off";
CommConnection cnnGsm = (CommConnection)Connector.open(cnnGsmStr);

If I try:

String cnnGsmStr = "comm:/dev/ttyUSB0;baudrate=115200;bitsperchar=8;stopbits=1;parity=none;blocking=off";
CommConnection cnnGsm = (CommConnection)Connector.open(cnnGsmStr);

I get:

Unable to create MIDlet net.marciowb.cldc.avl.vt850.Rastreador
java.lang.IllegalArgumentException: Illegal device name
at com.sun.midp.io.j2me.comm.Protocol.connect(Protocol.java:258)
at com.sun.midp.io.ConnectionBaseAdapter.openPrim(ConnectionBaseAdapter.java:103)
at javax.microedition.io.Connector.openPrim(Connector.java:329)
at javax.microedition.io.Connector.open(Connector.java:222)
at javax.microedition.io.Connector.open(Connector.java:198)
at javax.microedition.io.Connector.open(Connector.java:180)
at net.marciowb.cldc.avl.comm.ATCommand.(ATCommand.java:38)
at net.marciowb.cldc.avl.vt850.Rastreador.openIO(Rastreador.java:123)
at net.marciowb.cldc.avl.vt850.Rastreador.(Rastreador.java:69)
at java.lang.Class.runCustomCode(+0)
at com.sun.midp.midlet.MIDletState.createMIDlet(+34)
at com.sun.midp.midlet.Selector.run(Selector.java:150)

After search, try, read, try, think, try, read, search, try and no success, I find in /WTK2.5.2/lib/system.config references to COM1 and COM2, but after modify for several times this values I saw that it doesn't apply to my question. So, after do a full find in WTK directory I found the /WTK2.5.2/bin/libzayit.so file that contains hard code references to /dev/ttyS0 and /dev/ttyS1 but don't have any reference to /dev/ttyUSB0 or /dev/ttyUSB1. Half problem solved!

Then to do my midlet works with the Sun Mobility Pack in my Linux machine with the USB serial port adapter, just I do:

su
rm /dev/ttyS[01]
ln -s /dev/ttyUSB0 /dev/ttyS0
ln -s /dev/ttyUSB1 /dev/ttyS1
chmod a+w /dev/ttyS[01]

Done! Now You can use:

CommConnection cnnGsm = (CommConnection)Connector.open("comm:COM1;baudrate=115200");

That it works!

Labels: , ,

0 Comments:

Post a Comment

<< Home