dbtype=gpsd,creatorID=6657;
static int dbType = 0x67707364; // gpsd
static int dbCreator = 0x36363537; // '6657'
Database db = new Database(dbType, dbCreator, Database.READONLY);
......
由于不涉及到对数据库的写操作,所以这里以只读的方式打开Palm内部数据库。
(2)打开Palm串行通讯端口,构造serialPort对象,使用serialPort.open方法打开串口。
Protocol serialPort = new Protocol();
serialPort.open
("0;baudrate=9600;bitsperchar=8;stopbits=1;parity=none;
autorts=off;autocts=off;blocking=off",1, true);
......
(3)发送数据,通过serialPort对象的openOutputStream()方法,获取OutputStream数据流,并赋给os对象,代码为OutputStream os = serialPort.openOutputStream()。然后,读取Palm内部数据库gpsd的每条记录内容,将内容按字节通过串行通讯传送到PC,并通过os.write(),将数据写入串口。主要代码为:
byte [] rnl={13,10};
for (int i=0;i<db.getNumberOfRecords();i++)
{
Graphics.getGraphics().drawString("send record No."+i,30,100);
os.write(db.getRecord(i));
os.write(rnl);
}
os.flush();
......
如果串行通讯数据传输完毕,关闭串行通讯端口和Palm内部数据库。代码如下:
System.out.println("Send finished!"); os.close(); System.out.println("connection closed."); ......
将此程序编译、安全校验、封装成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)
可以将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可以接收到串行通讯接收来的数据。
串行通讯往往是双方面的,在系统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串行通讯程序开发速度,是一条比较科学的串行通讯调试方法。
本文地址:http://com.8s8s.com/it/it32719.htm