Hi,
I have a issue where from my j2me application which runs through emulator could read from COM port but when i try to write something it is no sending any data.
The COM port listener code as follows am using rxtx lib which is listening on COM2 port
import gnu.io.*;
import java.io.*;
import java.util.*;
public class TestCOM2 implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId1;
InputStream inputStream;
OutputStream outputStream;
SerialPort serialPort1;
Thread readThread;
static String TimeStamp;
public static void main(String[] args) {
try {
portId1 = CommPortIdentifier.getPortIdentifier("COM2");
System.out.println("helell");
TestCOM2 r = new TestCOM2();
System.out.println("helell1111");
} catch (Exception e) {
TimeStamp = new java.util.Date().toString();
System.out.println(TimeStamp + ": COM1 " + portId1);
System.out.println(TimeStamp + ": msg1 - " + e);
}
};
public TestCOM2() {
try {
TimeStamp = new java.util.Date().toString();
serialPort1 = (SerialPort) portId1.open("ComControl", 2000);
System.out.println(TimeStamp + ": " + portId1.getName() + "opened for scanner input");
} catch (PortInUseException e) {}
try {
inputStream = serialPort1.getInputStream();
} catch (IOException e) {}
try {
serialPort1.addEventListener(this);
} catch (TooManyListenersException e) {}
serialPort1.notifyOnDataAvailable(true);
try {
serialPort1.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
try{
outputStream = serialPort1.getOutputStream();
outputStream.write("Hello FROM COM2 ".getBytes());
outputStream.close();
}catch(Exception ex){
}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
}
public void serialEvent(SerialPortEvent event) {
System.out.println(" Event rised"+event);
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
StringBuffer readBuffer = new StringBuffer();
int c;
try {
if(inputStream.available()>0){
DataInputStream dIs=new DataInputStream(inputStream);
byte[] b=new byte[inputStream.available()];
dIs.readFully(b);
readBuffer.append(new String(b));
//c=inputStream.read();
//readBuffer.append((char) c);
//System.out.println(" C is "+c);
}
String scannedInput = readBuffer.toString();
TimeStamp = new java.util.Date().toString();
System.out.println(TimeStamp + ": scanned inputreceived:" + scannedInput);
inputStream.close();
} catch (IOException e) {}
break;
}
}
}
My j2me application code as follows
....
....
comm = (CommConnection)Connector.open("comm:COM3");
os = comm.openOutputStream();
System.out.println(" got outputstream"+os);
os.write("Hello".getBytes());
System.out.println(" written response received for upload ");
os.flush();
os.close();
...
...
there is no exception at all.
If i run a hyperterminal on COM2 and mobile application sends data to COM3 it is working fine and getting shown in hyperterminal.
Please help me.
Thanks & Regards

Reply With Quote

