一个嵌入式系统通常需要通过串口与其主控系统进行全双工通讯,譬如一个流水线控制系统需要不断的接受从主控系统发送来的查询和控制信息,并将执行结果或查询结果发送回主控系统。本文介绍了一个简单的通过串口实现全双工通讯的java类库,该类库大大的简化了对串口进行操作的过程。
本类库主要包括:serialbean.java (与其他应用程序的接口), serialbuffer.java(用来保存从串口所接收数据的缓冲区), readserial.java (从串口读取数据的程序)。另外本类库还提供了一个例程serialexample.java 作为示范。在下面的内容中将逐一对这几个部分进行详细介绍。
1. serialbean
serialbean是本类库与其他应用程序的接口。该类库中定义了serialbean的构造方法以及初始化串口,从串口读取数据,往串口写入数据以及关闭串口的函数。具体介绍如下:
public serialbean(int portid)
本函数构造一个指向特定串口的serialbean,该串口由参数portid所指定。portid = 1 表示com1,portid = 2 表示com2,由此类推。
public int initialize()
本函数初始化所指定的串口并返回初始化结果。如果初始化成功返回1,否则返回-1。初始化的结果是该串口被serialbean独占性使用,其参数被设置为9600, n, 8, 1。如果串口被成功初始化,则打开一个进程读取从串口传入的数据并将其保存在缓冲区中。
public string readport(int length)
本函数从串口(缓冲区)中读取指定长度的一个字符串。参数length指定所返回字符串的长度。
public void writeport(string msg)
本函数向串口发送一个字符串。参数msg是需要发送的字符串。
public void closeport()
本函数停止串口检测进程并关闭串口。
serialbean的源代码如下:
package serial;
import java.io.*;
import java.util.*;
import javax.comm.*;
/**
*
* this bean provides some basic functions to implement full dulplex
* information exchange through the srial port.
*
*/
public class serialbean
{
static string portname;
commportidentifier portid;
serialport serialport;
static outputstream out;
static inputstream in;
serialbuffer sb;
readserial rt;
/**
*
* constructor
*
* @param portid the id of the serial to be used. 1 for com1,
* 2 for com2, etc.
*
*/
public serialbean(int portid)
{
portname = "com" + portid;
}
/**
*
* this function initialize the serial port for communication. it starts a
* thread which consistently monitors the serial port. any signal captured
* from the serial port is stored into a buffer area.
*
*/
public int initialize()
{
int initsuccess = 1;
int initfail = -1;
try
{
portid = commportidentifier.getportidentifier(portname);
try
{
serialport = (serialport)
portid.open("serial_communication", 2000);
} catch (portinuseexception e)
{
return initfail;
}
//use inputstream in to read from the serial port, and outputstream
//out to write to the serial port.
try
{
in = serialport.getinputstream();
out = serialport.getoutputstream();
} catch (ioexception e)
{
return initfail;
}
//initialize the communication parameters to 9600, 8, 1, none.
try
{
serialport.setserialportparams(9600,
serialport.databits_8,
serialport.stopbits_1,
serialport.parity_none);
} catch (unsupportedcommoperationexception e)
{
return initfail;
}
} catch (nosuchportexception e)
{
return initfail;
}
// when successfully open the serial port, create a new serial buffer,
// then create a thread that consistently accepts incoming signals from
// the serial port. incoming signals are stored in the serial buffer.
sb = new serialbuffer();
rt = new readserial(sb, in);
rt.start();
// return success information
return initsuccess;
}
/**
*
* this function returns a string with a certain length from the incoming
* messages.
*
* @param length the length of the string to be returned.
*
*/
public string readport(int length)
{
string msg;
msg = sb.getmsg(length);
return msg;
}
/**
*
* this function sends a message through the serial port.
*
* @param msg the string to be sent.
*
*/
public void writeport(string msg)
{
int c;
try
{
for (int i = 0; i < msg.length(); i++)
out.write(msg.charat(i));
} catch (ioexception e) {}
}
/**
*
* this function closes the serial port in use.
*
*/
public void closeport()
{
rt.stop();
serialport.close();
}
}
2. serialbuffer
serialbuffer是本类库中所定义的串口缓冲区,它定义了往该缓冲区中写入数据和从该缓冲区中读取数据所需要的函数。
public synchronized string getmsg(int length)
本函数从串口(缓冲区)中读取指定长度的一个字符串。参数length指定所返回字符串的长度。
public synchronized void putchar(int c)
本函数望串口缓冲区中写入一个字符,参数c 是需要写入的字符。
在往缓冲区写入数据或者是从缓冲区读取数据的时候,必须保证数据的同步,因此getmsg和putchar函数均被声明为synchronized并在具体实现中采措施实现的数据的同步。
serialbuffer的源代码如下:
package serial;
/**
*
* this class implements the buffer area to store incoming data from the serial
* port.
*
*/
public class serialbuffer
{
private string content = "";
private string currentmsg, tempcontent;
private boolean available = false;
private int lengthneeded = 1;
/**
*
* this function returns a string with a certain length from the incoming
* messages.
*
* @param length the length of the string to be returned.
*
*/
public synchronized string getmsg(int length)
{
lengthneeded = length;
notifyall();
if (lengthneeded > content.length())
{
available = false;
while (available == false)
{
try
{
wait();
} catch (interruptedexception e) { }
}
}
currentmsg = content.substring(0, lengthneeded);
tempcontent = content.substring(lengthneeded);
content = tempcontent;
lengthneeded = 1;
notifyall();
return currentmsg;
}
/**
*
* this function stores a character captured from the serial port to the
* buffer area.
*
* @param t the char value of the character to be stored.
*
*/
public synchronized void putchar(int c)
{
character d = new character((char) c);
content = content.concat(d.tostring());
if (lengthneeded < content.length())
{
available = true;
}
notifyall();
}
}
3. readserial
readserial是一个进程,它不断的从指定的串口读取数据并将其存放到缓冲区中。
public readserial(serialbuffer sb, inputstream port)
本函数构造一个readserial进程,参数sb指定存放传入数据的缓冲区,参数port指定从串口所接收的数据流。
public void run()
readserial进程的主函数,它不断的从指定的串口读取数据并将其存放到缓冲区中。
readserial的源代码如下:
package serial;
import java.io.*;
/**
*
* this class reads message from the specific serial port and save
* the message to the serial buffer.
*
*/
public class readserial extends thread
{
private serialbuffer combuffer;
private inputstream comport;
/**
*
* constructor
*
* @param sb the buffer to save the incoming messages.
* @param port the inputstream from the specific serial port.
*
*/
public readserial(serialbuffer sb, inputstream port)
{
combuffer = sb;
comport = port;
}
public void run()
{
int c;
try
{
while (true)
{
c = comport.read();
combuffer.putchar(c);
}
} catch (ioexception e) {}
}
}
4. serialexample
serialexample是本类库所提供的一个例程。它所实现的功能是打开串口com1,对其进行初始化,从串口读取信息对其进行处理后将处理结果发送到串口。
import serial.*;
import java.io.*;
/**
*
* this is an example of how to use the serialbean. it opens com1 and reads
* six messages with different length form the serial port.
*
*/
class serialexample
{
public static void main(string[] args)
{
//to do: add your java codes here
serialbean sb = new serialbean(1);
string msg;
sb.initialize();
for (int i = 5; i <= 10; i++)
{
msg = sb.readport(i);
sb.writeport("reply: " + msg);
}
sb.closeport();
}
}
5. 编译与调试
本类库中使用了java communication api (javax.comm)。这是一个java扩展类库,并不包括在标准的java sdk当中。如果你尚未安装这个扩展类库的话,你应该从sun公司的java站点下载这个类库并将其安装在你的系统上。在所下载的包里面包括一个安装说明,如果你没有正确安装这个类库及其运行环境的话,运行这个程序的时候你会找不到串口。
正确安装java communication api并将上述程序编译通过以后,你可以按如下方法测试这个程序。如果你只有一台机器,你可以利用一条rs-232电缆将com1和com2连接起来,在com1上运行serialexample,在com2上运行windows提供的超级终端程序。如果你有两台机器的话,你可以利用一条rs-232电缆将两台机器的com1(或者是com2)连接起来,在一端运行例程,另外一端运行windows提供的超级终端程序。如果有必要的
话,可以对serialexample中所声明的串口进行相应改动。