?? readcomm.java
字號(hào):
/******************************************
* 程序文件名稱:ReadComm.java
* 功能:從串行口COM1中接收數(shù)據(jù)
******************************************/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.comm.*;
class R_Frame extends Frame implements Runnable,ActionListener,SerialPortEventListener
{
/* 檢測系統(tǒng)中可用的通訊端口類 */
static CommPortIdentifier portId;
/* Enumeration 為枚舉型類,在java.util中 */
static Enumeration portList;
InputStream inputStream;
/* 聲明RS-232串行端口的成員變量 */
SerialPort serialPort;
Thread readThread;
String str="";
TextField out_message=new TextField("上面文本框顯示接收到的數(shù)據(jù)");
TextArea in_message=new TextArea();
Button btnOpen=new Button("打開串口");
/*建立窗體*/
R_Frame()
{
super("串口接收數(shù)據(jù)");
setSize(200,200);
setVisible(true);
btnOpen.addActionListener(this);
add(out_message,"South");
add(in_message,"Center");
add(btnOpen,"North");
} //R_Frame() end
/*點(diǎn)擊按扭所觸發(fā)的事件:打開串口,并監(jiān)聽串口. */
public void actionPerformed(ActionEvent event)
{
/*獲取系統(tǒng)中所有的通訊端口 */
portList=CommPortIdentifier.getPortIdentifiers();
/* 用循環(huán)結(jié)構(gòu)找出串口 */
while (portList.hasMoreElements()){
/*強(qiáng)制轉(zhuǎn)換為通訊端口類型*/
portId=(CommPortIdentifier)portList.nextElement();
if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
if (portId.getName().equals("COM1")) {
try {
serialPort = (SerialPort) portId.open("ReadComm", 2000);
out_message.setText("已打開端口COM1 ,正在接收數(shù)據(jù)..... ");
}
catch (PortInUseException e) { }
/*設(shè)置串口監(jiān)聽器*/
try {
serialPort.addEventListener(this);
}
catch (TooManyListenersException e) { }
/* 偵聽到串口有數(shù)據(jù),觸發(fā)串口事件*/
serialPort.notifyOnDataAvailable(true);
} //if end
} //if end
} //while end
readThread = new Thread(this);
readThread.start();//線程負(fù)責(zé)每接收一次數(shù)據(jù)休眠20秒鐘
} //actionPerformed() end
/*接收數(shù)據(jù)后休眠20秒鐘*/
public void run() {
try {
Thread.sleep(20000);
}
catch (InterruptedException e) { }
} //run() end
/*串口監(jiān)聽器觸發(fā)的事件,設(shè)置串口通訊參數(shù),讀取數(shù)據(jù)并寫到文本區(qū)中*/
public void serialEvent(SerialPortEvent event) {
/*設(shè)置串口通訊參數(shù):波特率、數(shù)據(jù)位、停止位、奇偶校驗(yàn)*/
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e) { }
byte[] readBuffer = new byte[20];
try {
inputStream = serialPort.getInputStream();
}
catch (IOException e) {}
try {
/* 從線路上讀取數(shù)據(jù)流 */
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
} //while end
str=new String(readBuffer);
/*接收到的數(shù)據(jù)存放到文本區(qū)中*/
in_message.append(str+"\n");
}
catch (IOException e) { }
} //serialEvent() end
} //類R_Frame end
public class ReadComm
{
public static void main(String args[])
{
/* 實(shí)例化接收串口數(shù)據(jù)的窗體類 */
R_Frame R_win=new R_Frame();
/* 定義窗體適配器的關(guān)閉按鈕功能 */
R_win.addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0); }
});
R_win.pack();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -