Serial communication using commConneciton API in j2me

你离开我真会死。 提交于 2019-12-11 12:40:22

问题


I have develop one j2me application for Centrino chip. That chip connected to one EZ power meter. I want to read data from EZ meter over serial communication RS-232 using Modbus protocol.

I have some code for send Modbus request for read Holding Register.

 //declare variable here
 CommConnection  commConn;
 InputStream     inStream;
 OutputStream    outStream;

// here open com port using commconnection

String strCOM = "comm:COM1;baudrate=9600;bitsperchar=8;stopbits=1;parity=even;blocking=on;autocts=off;autorts=off";
commConn = (CommConnection)Connector.open(strCOM);
inStream  = commConn.openInputStream();
outStream = commConn.openOutputStream();

// here create modbus protocol request. I want read holding register address 1000 it's hava 2 register.

    byte[] buffer = new byte[100];

    byte[] frame1 = new byte[] {(byte) 0x01, (byte) 0x03, (byte) 0x03, (byte) 0xE8, (byte) 0x00, (byte) 0x02, (byte) 0x34, (byte) 0xBA};
    outStream.write(frame1,0,frame1.length);//write(frame1);
    outStream.flush();
    int available1 = inStream.available();
    System.out.println("inStream Available : "+inStream.available());
    readBytes = inStream.read(buffer,0,available1);

    System.out.println("Read Integer : "+readBytes +" and Bytes size : "+buffer.length);

I always got response 0 bytes read. I don't know what is wrong. Please guide me if I write wrong code.

Thanks in advance.


回答1:


available() often returns 0 (for many types of streams) and so it is not very useful. Try just inStream.read(buffer).



来源:https://stackoverflow.com/questions/23775187/serial-communication-using-commconneciton-api-in-j2me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!