一个嵌入式系统通常需要通过串口与其主控系统进行全双工通讯譬如一个流水线控制系统需要不断的接受从主控系统发送来的查询和控制信息并将执行结果或查询结果发送回主控系统本文介绍了一个简单的通过串口实现全双工通讯的Java类库该类库大大的简化了对串口进行操作的过程
本类库主要包括SerialBeanjava (与其他应用程序的接口) SerialBufferjava
(用来保存从串口所接收数据的缓沖区) ReadSerialjava (从串口读取数据的程序)
另外本类库还提供了一个例程SerialExamplejava 作为示范在下面的内容中将逐
一对这几个部分进行详细介绍
SerialBean
SerialBean是本类库与其他应用程序的接口该类库中定义了SerialBean的构造方法以及初始化串口从串口读取数据往串口写入数据以及关闭串口的函数具体介绍如下
public SerialBean(int PortID)
本函数构造一个指向特定串口的SerialBean该串口由参数PortID所指定
PortID = 表示COMPortID = 表示COM由此类推
public int Initialize()
本函数初始化所指定的串口并返回初始化结果如果初始化成功返回否则返回初始化的结果是该串口被SerialBean独占性使用其参数被设置为 N 如果串口被成功初始化则打开一个进程读取从串口传入的数据并将其保存在缓沖区中
public String ReadPort(int Length)
本函数从串口(缓沖区)中读取指定长度的一个字符串参数Length指定所返回字符串的长度
public void WritePort(String Msg)
本函数向串口发送一个字符串参数Msg是需要发送的字符串
public void ClosePort()
本函数停止串口检测进程并关闭串口
SerialBean的源代码如下
package serial;
import javaio*;
import javautil*;
import m*;
/**
*
* 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 for COM
* for COM 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 = ;
int InitFail = ;
try
{
portId = CommPortIdentifiergetPortIdentifier(PortName);
try
{
serialPort = (SerialPort)
portIdopen(Serial_Communication );
} 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 = serialPortgetInputStream();
out = serialPortgetOutputStream();
} catch (IOException e)
{
return InitFail;
}
//Initialize the communication parameters to none
try
{
serialPortsetSerialPortParams(
SerialPortDATABITS_
SerialPortSTOPBITS_
SerialPortPARITY_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);
RTstart();
// 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 = SBGetMsg(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 = ; 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是本类库中所定义的串口缓沖区,它定义了往该缓沖区中写入数据和从该缓沖区中读取数据所需要的函数。tw.WingWit.CoM
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 > Contentlength())
{
available = false;
while (available == false)
{
try
{
wait();
} catch (InterruptedException e) { }
}
}
CurrentMsg = Contentsubstring( LengthNeeded);
TempContent = Contentsubstring(LengthNeeded);
Content = TempContent;
LengthNeeded = ;
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 = ncat(dtoString());
if (LengthNeeded < Content.length())
{
available = true;
}
notifyAll();
}
}
3. ReadSerial
ReadSerial是一个进程,它不断的从指定的串口读取数据并将其存放到缓沖区中。
public ReadSerial(SerialBuffer SB, InputStream Port)
本函数构造一个ReadSerial进程,参数SB指定存放传入数据的缓沖区,参数Port指定从串口所接收的数据流。
public void run()
ReadSerial进程的主函数,它不断的从指定的串口读取数据并将其存放到缓沖区中。
Re