📄 test.java
字号:
//package comm;import java.io.*;import java.util.*;import gnu.io.*;import java.awt.*;import java.util.Properties;import javax.swing.*;import java.awt.event.*;import java.io.*;import java.util.*;import javax.swing.JFrame;import gnu.io.*;/** * Class declaration * * * @author * @version 09/03/20 */public class test extends JFrame implements ActionListener,SerialPortEventListener{ static Enumeration portList; static CommPortIdentifier portId; static String messageString; static SerialPort serialPort; static InputStream inputStream; static boolean outputBufferEmptyFlag = false; JScrollPane p1; //JTextField Text=new JTextField(10); JTextField port=new JTextField(10); JButton ok=new JButton("open"); JTextArea area=new JTextArea(15,10); //JLabel l1=new JLabel("Message:"); JLabel l2=new JLabel("port:"); JButton read=new JButton("read"); /** * Method declaration * * * @param args * * @see */ test() { setTitle("SerialCom"); setVisible(true); setLayout(null); p1=new JScrollPane(area); add(port);port.setBounds(120,30,150,25); add(l2);l2.setBounds(35,30,100,25); //add(Text);Text.setBounds(120,50,150,25); add(ok);ok.setBounds(300,30,80,23); add(read);read.setBounds(150, 80, 80, 25); add(p1);p1.setBounds(35,120,340,250); ok.addActionListener(this); read.addActionListener(this); setSize(400,400); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setResizable(true); Dimension screen=Toolkit.getDefaultToolkit().getScreenSize(); setLocation((screen.width)/4,(screen.height)/4); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ serialPort.close(); System.exit(0); } }); } public void actionPerformed(ActionEvent e) { if(e.getSource()==ok) { boolean portFound = false; String useport = port.getText(); area.append("choose port:"); area.append(useport); area.append("\n"); System.out.println("choose port:"+useport); String defaultPort = "/dev/"+useport; portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equalsIgnoreCase(defaultPort)) { area.append("Found port:"); area.append( defaultPort); area.append("\n"); System.out.println("Found port " + defaultPort); portFound = true; try { serialPort = (SerialPort) portId.open("test", 200000); } catch (PortInUseException ex) { area.append("Port in use!"); //area.append( defaultPort); area.append("\n"); System.out.println("Port in use."); continue; } try { serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException ex) {} //area.append(serialPort.getName()); area.append("\n"); //serialPort.close(); //System.exit(1); } } } if (!portFound) { area.append("port "); area.append(defaultPort); area.append("not found!"); area.append("\n"); System.out.println("port " + defaultPort + " not found."); } } else if(e.getSource()==read) { area.append("wait for msg:"); area.append("\n"); try { serialPort.addEventListener(this); } catch(TooManyListenersException a){} serialPort.notifyOnDataAvailable(true); try { inputStream=serialPort.getInputStream(); } catch(IOException a){} } } public void serialEvent(SerialPortEvent event) { switch(event.getEventType()) { case SerialPortEvent.BI: case SerialPortEvent.OE: case SerialPortEvent.FE: case SerialPortEvent.PE: case SerialPortEvent.CD: case SerialPortEvent.CTS: case SerialPortEvent.DSR: case SerialPortEvent.RI: case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case SerialPortEvent.DATA_AVAILABLE: byte[] readBuffer=new byte[200]; try { String fileName = null; while(inputStream.available()>0) { area.append("there are data available in the port"); area.append("\n"); int numBytes=inputStream.read(readBuffer); fileName = "/root/data.txt"; BufferedWriter bw = new BufferedWriter(new FileWriter(fileName)); try{ String str = new String(readBuffer); bw.write(str); //写入 bw.newLine(); } catch(IOException e){ } area.append(new String(readBuffer)); bw.close(); } } catch(IOException e){} break; } } public static void main(String[] args) { new test(); //if (args.length > 0) { //defaultPort = args[0]; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -