一個嵌入式系統通常需要通過串口與其主控系統進行全雙工通訊
本類庫主要包括
(用來保存從串口所接收數據的緩沖區)
另外本類庫還提供了一個例程SerialExample
一對這幾個部分進行詳細介紹
SerialBean是本類庫與其他應用程序的接口
public SerialBean(int PortID)
本函數構造一個指向特定串口的SerialBean
PortID =
public int Initialize()
本函數初始化所指定的串口並返回初始化結果
public String ReadPort(int Length)
本函數從串口(緩沖區)中讀取指定長度的一個字符串
public void WritePort(String Msg)
本函數向串口發送一個字符串
public void ClosePort()
本函數停止串口檢測進程並關閉串口
SerialBean的源代碼如下
package serial;
import java
import java
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
*
*
*/
public SerialBean(int PortID)
{
PortName =
}
/**
*
* This function initialize the serial port for communication
* thread which consistently monitors the serial port
* from the serial port is stored into a buffer area
*
*/
public int Initialize()
{
int InitSuccess =
int InitFail =
try
{
portId = CommPortIdentifier
try
{
serialPort = (SerialPort)
portId
} catch (PortInUseException e)
{
return InitFail;
}
//Use InputStream in to read from the serial port
//out to write to the serial port
try
{
in = serialPort
out = serialPort
} catch (IOException e)
{
return InitFail;
}
//Initialize the communication parameters to
try
{
serialPort
SerialPort
SerialPort
SerialPort
} catch (UnsupportedCommOperationException e)
{
return InitFail;
}
} catch (NoSuchPortException e)
{
return InitFail;
}
// when successfully open the serial port
// then create a thread that consistently accepts incoming signals from
// the serial port
SB = new SerialBuffer();
RT = new ReadSerial(SB
RT
// 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
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 =
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 > Content
{
available = false;
while (available == false)
{
try
{
wait();
} catch (InterruptedException e) { }
}
}
CurrentMsg = Content
TempContent = Content
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(d
if (LengthNeeded < Content.length())
{
available = true;
}
notifyAll();
}
}
3. ReadSerial
ReadSerial是一個進程,它不斷的從指定的串口讀取數據並將其存放到緩沖區中。
public ReadSerial(SerialBuffer SB, InputStream Port)
本函數構造一個ReadSerial進程,參數SB指定存放傳入數據的緩沖區,參數Port指定從串口所接收的數據流。
public void run()
ReadSerial進程的主函數,它不斷的從指定的串口讀取數據並將其存放到緩沖區中。
Re
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19261.html