利用java实现串口全双工通讯 (投稿)
qingye jiang (john)
smth id: qyjohn
e-mail : qjiang@tsinghua.edu
一个嵌入式系统通常需要通过串口与其主控系统进行全双工通讯,譬如一个流水线
控制系统需要不断的接受从主控系统发送来的查询和控制信息,并将执行结果或查
询结果发送回主控系统。本文介绍了一个简单的通过串口实现全双工通讯的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 seria