?? readcomm.java
字號:
/****************************************** * 程序文件名稱:ReadComm.java * 功能:從串行口COM1中接收數(shù)據(jù) ******************************************/import java.awt.event.*;import java.io.*;import java.util.*;import javax.comm.*;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextArea;import javax.swing.JTextField;class R_Frame extends JFrame implements Runnable, ActionListener, SerialPortEventListener { /* 檢測系統(tǒng)中可用的通訊端口類 */ static CommPortIdentifier portId; /* Enumeration 為枚舉型類,在java.util中 */ static Enumeration portList; InputStream inputStream; /* 聲明RS-232串行端口的成員變量 */ SerialPort serialPort; /** 讀取串口線程 */ Thread readThread; /** 寫串口線程 */ Thread writeThread; JPanel panOut = new JPanel(); JPanel panIn = new JPanel(); JPanel panBtn = new JPanel(); String str = ""; JTextField txtInmessage = new JTextField("上面文本框顯示接收到的數(shù)據(jù)"); JTextArea txtArea_In = new JTextArea(); JTextField txtOutmessage = new JTextField("上面文本框顯示發(fā)送的數(shù)據(jù)"); JTextArea txtArea_Out = new JTextArea(); JButton btnOpen = new JButton("打開串口"); JButton btnClose = new JButton("關(guān)閉串口"); JButton btnSend = new JButton("發(fā)送數(shù)據(jù)"); //發(fā)送數(shù)據(jù)變量 byte data[] = new byte[10240]; OutputStream outputStream; /* 建立窗體 */ R_Frame() { this.getContentPane().setLayout(null); panBtn.setLayout(null); panBtn.setBounds(0,0,300,30); panBtn.add(btnOpen); panBtn.add(btnClose); panBtn.add(btnSend); btnOpen.setBounds(0, 0, 100, 30); btnClose.setBounds(100, 0, 100, 30); btnSend.setBounds(200, 0, 100, 30); this.getContentPane().add(panBtn); btnOpen.addActionListener(this); btnClose.addActionListener(this); btnSend.addActionListener(this); //發(fā)送區(qū)域 panOut.setLayout(null); panOut.setBounds(0,30,300,200); txtArea_Out.setBounds(0,0,300,180); txtOutmessage.setBounds(0,180,300,20); panOut.add(txtArea_Out); panOut.add(txtOutmessage); this.getLayeredPane().add(panOut); //接受區(qū)域 panIn.setLayout(null); panIn.setBounds(0,230,300,200); txtArea_In.setBounds(0,0,300,180); txtInmessage.setBounds(0,180,300,20); panIn.add(txtArea_In); panIn.add(txtInmessage); this.getLayeredPane().add(panIn); } // R_Frame() end /* 點擊按扭所觸發(fā)的事件:打開串口,并監(jiān)聽串口. */ public void actionPerformed(ActionEvent event) { if(event.getSource().equals(btnOpen)) { //讀取用戶配置端口名稱 String portName = ConfigFile.findProperty("CommPort"); /* 獲取系統(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(portName)) { try { serialPort = (SerialPort) portId.open("ReadComm", 2000); txtInmessage.setText("已打開端口COM1 ,正在接收數(shù)據(jù)..... "); } catch (PortInUseException e) { } /* 設(shè)置串口監(jiān)聽器 */ try { serialPort.addEventListener(this); } catch (TooManyListenersException e) { } /* 偵聽到串口有數(shù)據(jù),觸發(fā)串口事件 */ serialPort.notifyOnDataAvailable(true); /* 設(shè)置串口輸出流 */ try { outputStream = serialPort.getOutputStream(); } catch (IOException e) { } } // if end } // if end } // while end readThread = new Thread(this); readThread.start(); // 線程負(fù)責(zé)每接收一次數(shù)據(jù)休眠20秒鐘 } if(event.getSource().equals(btnClose)){ if(serialPort!=null) { serialPort.close(); txtInmessage.setText("端口已關(guān)閉"); } } if(event.getSource().equals(btnSend)){ /* 設(shè)置串口通訊參數(shù) */ try { serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) { } /* 從文本區(qū)按字節(jié)讀取數(shù)據(jù) */ data = txtArea_Out.getText().getBytes(); /* 發(fā)送數(shù)據(jù)流(將數(shù)組data[]中的數(shù)據(jù)發(fā)送出去) */ try { outputStream.write(data); } catch (IOException e) { } } } // 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ù)位、停止位、奇偶校驗 */ 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ū)中 */ txtArea_In.append(str + "\n"); } catch (IOException e) { } } // serialEvent() end} // 類R_Frame endpublic class ReadComm { public static void main(String args[]) { R_Frame frame = new R_Frame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); //設(shè)置窗口為固定大小 frame.setSize(300,460); frame.setLocation(200, 200); frame.setVisible(true); }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -