站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2ME 专题Palm J2ME串行通讯程序编写调试

Palm J2ME串行通讯程序编写调试

摘要: Palm J2ME串行通讯程序编写调试
<table width=700 border=0> <tr> <td align=middle>Palm J2ME串行通讯程序编写调试 </td></tr> <tr> <td height=3></td> </tr> <tr> <td align=middle>作者:李鲁群 来源:赛迪网 </td></tr> <tr> <td height=10> </td></tr> <tr> <td>Palm是3Com公司的产品,其操作系统PalmOS是一种32位的嵌入式操作系统。Palm硬件提供了标准串行通讯接口(RS232接口)和红外线传输接口。利用它可以方便地与其它外部设备通讯、传输数据。目前3Com公司与Sun公司积极合作,已经提供了非常完善的嵌入式Java(J2ME),并提供了CLDC1.03和Palm相应的配置。基于PalmOS平台的Java虚拟机为KVM,此虚拟机中已经提供了完善的对Palm串行通讯口读写的SDK程序开发包。利用该开发包可以方便地开发Palm与外围设备的串行通讯的应用程序。

串行通讯历来是计算机软件界经久不衰的应用与研究内容之一。计算机串行通讯系统因为利用的外围硬件少,通常传输距离为15米,如果配合光电转换器,可将传输距离延长至若干公里。如果通讯双方使用Modem,再配合电话线,就可以使在任意位置的通讯双方实现串行通讯。串行通讯被广泛地应用在工业数据采集、IC卡读写等领域。由于Palm程序的编写与调试步骤与台式PC机不同,所以一些与Palm串行通讯的设备必须拿到室外才能正常使用。如GPS的接收机、GPS的全球定位系统、GPS的接收设备等,它们必须在室外,并且必须在较开阔的位置才能接收到足够的卫星数目,GPS接收机才能解算出当前的位置。然后,需要通过GPS接收机的串行通讯口将解算出的结果按照NMEA0183协议将数据发送出去,并实现定位。这就使得Palm串行通讯程序调试起来非常复杂。

笔者从事J2ME嵌入式程序开发1年有余,找到了一套较高效的编写和调试Palm J2ME嵌入式程序的方法,以下将详细介绍。

Palm串行通讯应用程序的开发步骤


通常Palm应用程序的开发是利用台式PC编写,编写后编译成Palm应用程序,然后将其导入到台式PC的Palm模拟器中运行调试。程序调试完成后,通过台式PC与Palm的同步软件(HotSync)导入到真正的Palm硬件,从而实现应用程序在Palm硬件上的运行(见图1)。



图1 Palm应用程序开发步骤UML活动图


Palm串行通讯的硬件环境


由于Palm串行通讯涉及到对硬件的操作,所以单纯地利用Palm模拟器无法调试Palm串行通讯程序,必须采用软件与硬件相结合的方法才可以。方法如下:

首先将Palm模拟器的串行通讯口映射到台式PC的物理串行通讯口。具体操作为鼠标右击Palm模拟器,选择Setting,然后选择Properties,再选择Serial Port,最后选择Com1。注意此串行通讯口设备必须未被其它设备使用。随后使用9针的D型串行通讯接口将PC物理通讯端口与Palm相通讯的设备连接一起。

当在同一台PC上完成Palm模拟器与PC串行通讯时,需要制作一根Null Modem串行通讯电缆,详细制作可参照相关串行通讯资料。

串行通讯辅助调试软件


终端仿真程序是Windows 9x/NT/2000自带的应用软件,是功能强大的串行通讯辅助调试软件,利用它可以方便地完成PC计算机之间的串行通讯。我们在此介绍终端仿真程序,并不是单纯地为了介绍Windows系统自带的软件,而主要是为了调试串行通讯程序。

终端仿真程序位于Windows工具“开始-程序-附件-通讯-超级终端”。如果系统中尚未安装超级终端程序,请选择“控制面板-添加删除程序”,选择Windows,选择通讯,选中超级终端复选项,然后按照提示插入Windows 9x/NT/2000系统安装光盘,完成终端仿真程序的安装。

启动超级终端程序Hpertrm.exe,选定串行通讯端口(Com1或Com2),然后设定好通讯速率与校验位。例如串行通讯速率为9600波特,数据位为8位,无奇偶校验,停止位为1位(见图2、图3)。



图2 超级终端串行通讯端口设定




图3 超级终端串行通讯速率设定


Palm J2ME串行通讯程序设计举例


一般来说,串行通讯的双方都必须遵守相同的通讯数据格式,编写各自的通讯程序。由于通讯程序的编写涉及到通讯的双方,所以任意一方通讯程序的错误都将导致通讯的失败,也将给编写和调试通讯带来许多困难。笔者通过反复试验发现,利用Windows终端仿真通讯程序模拟通讯双方中一方,然后调试通讯双方中另一方与Windows终端仿真通讯,当通讯两方都与Windows终端仿真调试成功后,即可实现它们之间的串行通讯了。这样可以最大限度地减少串行通讯程序的调试工作量,提高程序开发速度(见图4)。假设目前要设计两个串行通讯系统,分别是PC机显示Palm数据系统和Palm显示PC端数据系统,它们的具体实现如下:



图4 串行通讯UML关系图


PC机显示Palm数据系统


将Palm的数据库内容通过串行通讯,按照串行通讯9600波特速率、数据位为8位、无奇偶校验、停止位为1位传送到PC计算机,并显示。此系统涉及到Palm端程序和PC计算机端程序。

1. Palm端程序(见图5)



图5 Palm端程序UML状态图


Palm端程序主要分三步:

(1)打开Palm内部数据库,设Palm内部的数据库的标识为:

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=550 align=center borderColorLight=black border=1> <tr> <td class=code bgColor=#e6e6e6>
dbtype=gpsd,creatorID=6657;

static int dbType = 0x67707364; //  gpsd

static int dbCreator = 0x36363537; // '6657'

Database db = new Database(dbType, dbCreator, Database.READONLY);

......
</td></tr></table>


由于不涉及到对数据库的写操作,所以这里以只读的方式打开Palm内部数据库。

(2)打开Palm串行通讯端口,构造serialPort对象,使用serialPort.open方法打开串口。

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=550 align=center borderColorLight=black border=1> <tr> <td class=code bgColor=#e6e6e6>
Protocol serialPort = new Protocol();

serialPort.open

("0;baudrate=9600;bitsperchar=8;stopbits=1;parity=none;

autorts=off;autocts=off;blocking=off",1, true);

......
</td></tr></table>


(3)发送数据,通过serialPort对象的openOutputStream()方法,获取OutputStream数据流,并赋给os对象,代码为OutputStream os = serialPort.openOutputStream()。然后,读取Palm内部数据库gpsd的每条记录内容,将内容按字节通过串行通讯传送到PC,并通过os.write(),将数据写入串口。主要代码为:

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=550 align=center borderColorLight=black border=1> <tr> <td class=code bgColor=#e6e6e6>
byte [] rnl={13,10};

for (int i=0;i
</td></tr></table>


如果串行通讯数据传输完毕,关闭串行通讯端口和Palm内部数据库。代码如下:

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=550 align=center borderColorLight=black border=1> <tr> <td class=code bgColor=#e6e6e6>
System.out.println("Send finished!");

os.close();

System.out.println("connection closed.");

......
</td></tr></table>



将此程序编译、安全校验、封装成Palm格式的Prc应用程序,然后传送到Palm模拟器上。将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),代码如下:

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=550 align=center borderColorLight=black border=1> <tr> <td class=code bgColor=#e6e6e6>

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

  }

.....
</td></tr></table>

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

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=550 align=center borderColorLight=black border=1> <tr> <td class=code bgColor=#e6e6e6>
public Reader() {

 try {

  serialPort = (SerialPort) portId.open("Li", 2000);

 } catch (PortInUseException e) {}

 try {

  inputStream = serialPort.getInputStream();

 } catch (IOException e) {}
</td></tr></table>


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

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=550 align=center borderColorLight=black border=1> <tr> <td class=code bgColor=#e6e6e6>
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;

.....
</td></tr></table>


为了调试微机的串行通讯程序,我们使用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)打开串行端口,代码如下:

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=550 align=center borderColorLight=black border=1> <tr> <td class=code bgColor=#e6e6e6>

.....

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

....
</td></tr></table>

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

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=550 align=center borderColorLight=black border=1> <tr> <td class=code bgColor=#e6e6e6>
InputStream is = serialPort.openInputStream();

System.out.println("reading response...");

...

int len = is.read(readBuffer);

System.out.println("read: " + len +" bytes");
</td></tr></table>


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

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=550 align=center borderColorLight=black border=1> <tr> <td class=code bgColor=#e6e6e6>
String s=new String(readBuffer, 0, len);

System.out.println("[" + s + "]");

....
</td></tr></table>


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

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=550 align=center borderColorLight=black border=1> <tr> <td class=code bgColor=#e6e6e6>
is.close();

serialPort.close();

System.out.println("connection closed.");

} catch (Exception ioe) {

System.out.println("IO Ex: "+ioe.toString());

}

.....
</td></tr></table>


2. PC主要通讯程序

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

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=550 align=center borderColorLight=black border=1> <tr> <td class=code bgColor=#e6e6e6>
...

try {

 serialPort = (SerialPort)

 portId.open("SimpleWriteApp", 2000);

} catch (PortInUseException e) {System.out.println(e);}

....
</td></tr></table>


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

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=550 align=center borderColorLight=black border=1> <tr> <td class=code bgColor=#e6e6e6>
try {

 outputStream = serialPort.getOutputStream();

} catch (IOException e) {System.out.println(e);}
</td></tr></table>


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

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=550 align=center borderColorLight=black border=1> <tr> <td class=code bgColor=#e6e6e6>
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);

 }

....

}
</td></tr></table>


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

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=550 align=center borderColorLight=black border=1> <tr> <td class=code bgColor=#e6e6e6>
rf.close();

.....
</td></tr></table>


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

</td></tr></table>

↑返回目录
前一篇: 保护您的 J2ME/MIDP 应用程序
后一篇: 编写Palm J2ME红外线“聊天”程序