×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    12

    Writing to COM port not working but reading is working

    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

  2. #2
    Registered User
    Join Date
    Nov 2009
    Posts
    12

    Re: Writing to COM port not working but reading is working

    Hi,

    I need help on this urgently plz anybody look into this.

    Thanks

Similar Threads

  1. Reading the USB port
    By runfastman in forum Mobile Java General
    Replies: 0
    Last Post: 2009-01-30, 20:16
  2. Reading and writing from file in j2me????
    By amitsamel in forum Mobile Java General
    Replies: 1
    Last Post: 2007-11-15, 18:44
  3. Reading from comm port
    By StasV in forum Symbian
    Replies: 4
    Last Post: 2007-08-22, 09:48
  4. reading and writing buffer contects
    By hm36 in forum Symbian
    Replies: 9
    Last Post: 2007-07-06, 07:40
  5. BUG? Writing to serial port hangs
    By jprosti in forum PersonalJava
    Replies: 1
    Last Post: 2002-11-28, 11:45

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×