本函數初始化所指定的串口并返回初始化結果。如果初始化成功返回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(); }
}
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)
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) {}
}
}
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中所聲明的串口進行相應改動。