在j2me中使用commConneciton API进行串行通信 [英] Serial communication using commConneciton API in j2me

查看:58
本文介绍了在j2me中使用commConneciton API进行串行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为Centrino芯片开发了一个j2me应用程序.该芯片连接到一个EZ功率计.我想使用Modbus协议通过RS-232串行通信从EZ电表读取数据.

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.

我有一些代码可以发送Modbus请求以读取保持寄存器.

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

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

//在这里使用commconnection打开com端口

// 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();

//在这里创建modbus协议请求.我想读取保持寄存器地址1000,它是hava 2寄存器.

// 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);

我总是得到读取0字节的响应.我不知道怎么了如果我写错了代码,请指导我.

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

谢谢.

推荐答案

available()通常返回0(对于许多类型的流),因此它不是很有用.只需尝试inStream.read(buffer).

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

这篇关于在j2me中使用commConneciton API进行串行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆