WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章手机编程PlamPalm J2ME串行通讯程序编写调试

Palm J2ME串行通讯程序编写调试
作者:李鲁群  来源:赛迪网  发布时间:2005-12-21 3:44:00
将Palm模拟器的串行通讯端口映射为PC的Com1通讯端口,使用Windows自带的终端仿真程序模拟PC来完成串行通信。启动Windows自带的终端仿真程序,设置其使用端口为Com2,设定串行通讯速率为9600波特、数据位为8位、无奇偶校验、停止位为1位,然后使用Null Modem串行通讯电缆将PC的Com1和Com2端口物理连接。

运行上述Palm程序可以发现,Palm模拟器通过串行通讯将内部数据传送到Windows自带的终端仿真程序的对话窗口中了。由于该对话窗口只能保存500行的对话内容,所以如果大于500行,可以选择Windows超级终端的“传送”菜单,选择捕获文本,将对话内容直接保存到一个标准的文本文件中。

2. PC端串行程序(见图6)



 

图6 PC断程序UML状态图



可以将PC端串行程序看为类似超级终端的应用程序。PC端串行程序完成的工作主要有,按照设定的传输速率(必须与Palm端的串行通讯参数一致)打开串行通讯端口,然后启动线程,并且监听串行通讯端口数据。接收到数据后,将数据显示。具体程序步骤如下:

(1)安装好Sun Java 串行通讯SDK类库,在程序中引入相应的类,代码为import javax.comm.*。然后采用循环枚举的方法,判断读取PC机上的串行通讯口的状态(此处通讯口是Com2),代码如下:

static Enumeration portList;
portList = CommPortIdentifier .getPortIdentifiers();
while (portList.hasMoreElements()) {
 portId = (CommPortIdentifier) portList.nextElement();
 if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
  if (portId.getName().equals("COM2")) {
   Reader reader = new Reader();
  }
.....


(2)打开串口,获取串行通讯数据流,代码如下:

public Reader() {
 try {
  serialPort = (SerialPort) portId.open("Li", 2000);
 } catch (PortInUseException e) {}
 try {
  inputStream = serialPort.getInputStream();
 } catch (IOException e) {}


(3)在serialPortEventListener事件中,判断是否串口有数据到来,如果有就将数据显示出来,代码如下:

public void serialEvent(SerialPortEvent 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:
  byte[] readBuffer = new byte[200];
  try {
   while (inputStream.available() > 0) {
    int numBytes = inputStream.read(readBuffer);
   }
   System.out.print(new String(readBuffer));
  } catch (IOException e) {}
  break;
.....


为了调试微机的串行通讯程序,我们使用Windows自带的终端仿真程序调试。这一次,我们使用Windows终端仿真程序模拟Palm串行通讯端。设置终端仿真程序使用端口为Com1,设定串行通讯速率为9600波特、数据位为8位、无奇偶校验、停止位为1位,然后使用Null Modem串行通讯电缆将PC计算机的Com1和Com2端口物理连接。选择Windows终端仿真程序菜单“传送发送文本文件”,将模拟数据文本文件发送。

运行上述PC程序,启动Windows终端仿真程序,可以发现PC可以接收到串行通讯接收来的数据。


Palm显示PC端数据系统



串行通讯往往是双方面的,在系统1中,我们介绍了Palm向PC计算机通过串行通讯发送数据的系统程序。这一次,我们将介绍PC计算机通过串行通讯向Palm发送数据的案例。

假设系统2内容为将PC文件e:\gps.txt,按照串行通讯9600波特速率、数据位为8位、无奇偶校验、停止位为1位,通过串行通讯将内容传送到Palm,并显示数据。这样,系统2也涉及到Palm端程序和PC计算机端程序。读者可以参照上述Windows终端仿真程序,调试Palm和PC双方程序。以下仅介绍Palm和PC两端的串行通讯程序。

1. Palm端程序

(1)打开串行端口,代码如下:

.....
Protocol serialPort = new Protocol();
serialPort.open("0;baudrate=9600;bitsperchar=8;stopbits=1;parity=none;
autorts=off;autocts=off;
blocking=off",1, true);
System.out.println("...opened");
....


(2)通过serialPort获取数据输入流,按设定的读取数据缓冲区大小读取数据,代码如下:

InputStream is = serialPort.openInputStream();
System.out.println("reading response...");
...
int len = is.read(readBuffer);
System.out.println("read: " + len +" bytes");


(3)将读取来的数据转换成字符串,然后显示,代码如下:

String s=new String(readBuffer, 0, len);
System.out.println("[" + s + "]");
....


(4)通讯结束后,关闭数据流及串口,代码如下:

is.close();
serialPort.close();
System.out.println("connection closed.");
} catch (Exception ioe) {
System.out.println("IO Ex: "+ioe.toString());
} 
.....


2. PC主要通讯程序

(1)打开串行通讯口,代码如下:

...
try {
 serialPort = (SerialPort)
 portId.open("SimpleWriteApp", 2000);
} catch (PortInUseException e) {System.out.println(e);}
....


(2)获取串行通讯口数据流,代码如下:

try {
 outputStream = serialPort.getOutputStream();
} catch (IOException e) {System.out.println(e);}


(3)打开gps.txt文本文件,读取文本文件中的每一行,将数据写入串行通信获取的输出流,代码如下:

try {
 //outputStream.write(messageString.getBytes());
 RandomAccessFile rf=new RandomAccessFile("e:\\gps.txt","rw");
 String ss;
 while ((ss=rf.readLine())!=null)
 {
  outputStream.write(ss.getBytes());
  System.out.println(ss);
 }
....
}


(4)通讯结束后,关闭数据流,代码为:

rf.close();
.....


从总体来讲,J2ME串行通讯程序的编写与调试需要许多技巧,合理应用Windows仿真终端提高编写与调试Palm串行通讯程序开发速度,是一条比较科学的串行通讯调试方法。

上一页  [1] [2] 

[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码