A recent course requires writing a serial communication tool for upper computers. I wrote a simple serial communication tool with a graphical interface based on Java. The process will be described in detail below for your reference^_^
one:
First of all, you need to download an additional jar package that supports Java serial port communication operation. Since java.comm is relatively old and does not support 64-bit systems, it is recommended that Rxtx jar package (both 32-bit/64-bit support).
Official download address: http://fizzed.com/oss/rxtx-for-java (Note: FQ may be required to download)
Children's shoes that cannot be FQ can be downloaded here:
http://xiazai.VeVB.COM/201612/yuanma/javamfzrxtx(VeVB.COM).rar (32-bit)
http://xiazai.VeVB.COM/201612/yuanma/javamfzrxtx(VeVB.COM).rar (64-bit)
two:
Download the unzipped jar package and introduce it under Java Build Path:
capture
Note: If a java.lang.UnsatisfiedLinkError error is thrown during operation, please copy the two files of rxtxParallel.dll and rxtxSerial.dll in the rxtx decompression package to the C:/Windows/System32 directory to resolve the error.
three:
Regarding the use of this jar package, I wrote a SerialTool.java class, which provides various simple services for serial port communication. The code is as follows (note that this class is located in the serialPort package):
package serialPort;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.ArrayList;import java.util.Enumeration;import java.util.TooManyListenersException;import gnu.io.CommPort;import gnu.io.CommPortIdentifier;import gnu.io.NoSuchPortException;import gnu.io.PortInUseException;import gnu.io.SerialPort;import gnu.io.SerialPortEventListener;import gnu.io.UnsupportedCommOperationException;import serialException.*;/** * Serial Port service class, providing services such as opening and closing the serial port, reading and sending serial port data (using a singleton design mode) * @author zhong * */public class SerialTool { private static SerialTool serialTool = null; static { //Initialize a SerialTool object when this class is loaded by ClassLoader if (serialTool == null) { serialTool = new SerialTool(); } } //The constructor of the private SerialTool class is not allowed to generate SerialTool objects private SerialTool() {} /** * Get the SerialTool object that provides the service* @return serialTool */ public static SerialTool getSerialTool() { if (serialTool == null) { serialTool = new SerialTool(); } return serialTool; } /** * Find all available ports* @return List of available port names*/ public static final ArrayList<String> findPort() { //Get all currently available serial ports Enumeration<CommPortIdentifier> portList = CommPortIdentifier.getPortIdentifiers(); ArrayList<String> portNameList = new ArrayList<>(); //Add available serial port name to List and return the List while (portList.hasMoreElements()) { String portName = portList.nextElement().getName(); portNameList.add(portName); } return portNameList; } /** * Open serial port* @param portName Port name* @param baudrate Baudrate* @return SerialPort object* @throws SerialPortParameterFailure Failed to set the serial port parameters* @throws NotASerialPort The port pointing to the device is not the serial port type* @throws NoSuchPort There is no serial port device corresponding to this port* @throws PortInUse The port has been occupied*/ public static final SerialPort openPort(String portName, int baudrate) throws SerialPortParameterFailure, NotASerialPort, NoSuchPort, PortInUse { try { //Identify the port by the port name CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName); //Open the port and give the port name and a timeout (timeout time of opening operation) CommPort commPort = portIdentifier.open(portName, 2000); //Defend whether it is the serial port if (commPort instanceof SerialPort) { SerialPort serialPort = (SerialPort) commPort; try { //Set the baud rate and other parameters of the serial port serialPort.setSerialPortParams(baudrate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) { throw new SerialPortParameterFailure(); } //System.out.println("Open " + portName + " sucessfully !"); return serialPort; } else { //Not serial port throw new NotASerialPort(); } } catch (NoSuchPortException e1) { throw new NoSuchPort(); } catch (PortInUseException e2) { throw new PortInUse(); } } /** * Close the serial port* @param serialport The serial port object to be closed*/ public static void closePort(SerialPort serialPort) { if (serialPort != null) { serialPort.close(); serialPort = null; } } /** * Send data to the serial port* @param serialPort Serialport object* @param order Data to be sent* @throws SendDataToSerialPortFailure Failed to send data to the serial port* @throws SerialPortOutputStreamCloseFailure Close the output stream error of the serial port object*/ public static void sendToPort(SerialPort serialPort, byte[] order) throws SendDataToSerialPortFailure, SerialPortOutputStreamCloseFailure { OutputStream out = null; try { out = serialPort.getOutputStream(); out.write(order); out.flush(); } catch (IOException e) { throw new SendDataToSerialPortFailure(); } finally { try { if (out != null) { out.close(); out = null; } } catch (IOException e) { throw new SerialPortOutputStreamCloseFailure(); } } } /** * Read data from the serial port* @param serialPort The SerialPort object with a connection has been established* @return The data read* @throws ReadDataFromSerialPortFailure An error occurred while reading data from the serial port* @throws SerialPortInputStreamCloseFailure Close serial port object input stream error*/ public static byte[] readFromPort(SerialPort serialPort) throws ReadDataFromSerialPortFailure, SerialPortInputStreamCloseFailure { InputStream in = null; byte[] bytes = null; try { in = serialPort.getInputStream(); int bufflenth = in.available(); //Get the data length in the buffer while (bufflenth != 0) { bytes = new byte[bufflenth]; //Initialize the byte array to the length of data in buffer in.read(bytes); bufflenth = in.available(); } } catch (IOException e) { throw new ReadDataFromSerialPortFailure(); } finally { try { if (in != null) { in.close(); in = null; } } catch(IOException e) { throw new SerialPortInputStreamCloseFailure(); } } return bytes; } /** * Add listener* @param port serial port object* @param listener Serial port listener* @throws TooManyListeners There are too many listening class objects*/ public static void addListener(SerialPort port, SerialPortEventListener listener) throws TooManyListeners { try { //Add listener port.addEventListener(listener); //Set to wake up the listening receiving thread when data arrives port.notifyOnDataAvailable(true); //Set to wake up the interrupt thread when communication is interrupted port.notifyOnBreakInterrupt(true); } catch (TooManyListenersException e) { throw new TooManyListeners(); } } }Note: The throw exceptions in this method are all my custom exceptions. The reason for this is to facilitate corresponding processing in the main program. Here is a description of one of the exceptions:
(Note that all my custom Exceptions are placed in the serialException package)
package serialException;public class SerialPortParameterFailure extends Exception { /** * */ private static final long serialVersionUID = 1L; public SerialPortParameterFailure() {} @Override public String toString() { return "Set serial port parameters failed! Opening the serial port operation is not completed!"; } }I have rewritten its toString() method for each custom Exception class, so that the main program can print the corresponding error message after catching the Exception.
There is also a class in the serialException package that extracts the error information in the received Exception object and converts it into a string and returns it. The code is as follows:
package serialException;import java.io.IOException;import java.io.PrintWriter;import java.io.StringWriter;/** * Responsible for extracting and converting the error message in the passed Exception into a string; * @author zhong * */public class ExceptionWriter { /** * Encapsulate the error message in Exception into a string and return the string* @param e contains the error Exception * @return Error message string */ public static String getErrorInfoFromException(Exception e) { StringWriter sw = null; PrintWriter pw = null; try { sw = new StringWriter(); pw = new PrintWriter(sw); e.printStackTrace(pw); return "/r/n" + sw.toString() + "/r/n"; } catch (Exception e2) { return "An error message was not obtained, please check and try again!"; } finally { try { if (pw != null) { pw.close(); } if (sw != null) { sw.close(); } } catch (IOException e1) { e1.printStackTrace(); } } }}Four:
The use of the main program class, Client.java contains the program's entry address (main method). Its function is to display a welcome interface and call the DataView.java class for actual serial port data display.
Client.java code is as follows:
package serialPort;import java.awt.Color;import java.awt.FlowLayout;import java.awt.Font;import java.awt.Frame;import java.awt.Graphics;import java.awt.GridLayout;import java.awt.Image;import java.awt.Label;import java.awt.Panel;import java.awt.Toolkit;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JOptionPane;import serialException.ExceptionWriter;/** * Main program* @author zhong * */public class Client extends Frame{ /** * */ private static final long serialVersionUID = 1L; /** * Program interface width*/ public static final int WIDTH = 800; /** * Program interface height*/ public static final int HEIGHT = 620; /** * The location of the program interface (horizontal coordinates) */ public static final int LOC_X = 200; /** * The location of the program interface (horizontal coordinates) */ public static final int LOC_Y = 70; Color color = Color.WHITE; Image offScreen = null; // Used for double buffering// Set the icon of the window (here I customized the icon icon of the Windows window, because I really think which small coffee icon is not good-looking = =) Toolkit toolKit = getToolkit(); Image icon = toolKit.getImage(Client.class.getResource("computer.png")); //Hold other classes DataView dataview = new DataView(this); //Main interface class (displaying the monitoring data main panel) /** * Main method* @param args // */ public static void main(String[] args) { new Client().launchFrame(); } /** * Show main interface*/ public void launchFrame() { this.setBounds(LOC_X, LOC_Y, WIDTH, HEIGHT); //Set the location where the program appears on the desktop this.setTitle("CDIO Project"); //Set the program title this.setIconImage(icon); this.setBackground(Color.white); //Set the background color this.addWindowListener(new WindowAdapter() { //Add listening to window status public void windowClosing(WindowEvent arg0) { //System.exit(0); //Exit the program} }); this.addKeyListener(new KeyMonitor()); //Add the keyboard listener this.setResizable(false); //The window size cannot be changed this.setVisible(true); //The display window new Thread(new RepaintThread()).start(); //The repaint thread} /** * Draw various component elements of the program interface*/ public void paint(Graphics g) { Color c = g.getColor(); g.setFont(new Font("Microsoft Yahei", Font.BOLD, 40)); g.setColor(Color.black); g.drawString("Welcome to use the real-time monitoring system of the upper computer", 45, 190); g.setFont(new Font("Microsoft Yahei", Font.ITALIC, 26)); g.setColor(Color.BLACK); g.drawString("Version: 1.0 Powered By: ZhongLei", 280, 260); g.setFont(new Font("Microsoft Yahei", Font.BOLD, 30)); g.setColor(color); g.drawString("―--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- = Color.white; } /** * Double buffering method repaints each element component of the interface*/ public void update(Graphics g) { if (offScreen == null) offScreen = this.createImage(WIDTH, HEIGHT); Graphics gOffScreen = offScreen.getGraphics(); Color c = gOffScreen.getColor(); gOffScreen.setColor(Color.white); gOffScreen.fillRect(0, 0, WIDTH, HEIGHT); //Repaint the background canvas this.paint(gOffScreen); //Re-draw the interface element gOffScreen.setColor(c); g.drawImage(offScreen, 0, 0, null); // "spatch" the newly drawn canvas on the original canvas} /* * Inner class implements monitoring of keyboard events*/ private class KeyMonitor extends KeyAdapter { public void keyReleased(KeyEvent e) { int keyCode = e.getKeyCode(); if (keyCode == KeyEvent.VK_ENTER) { // When the monitor hears that the user hits the keyboard enter key, execute the following operation setVisible(false); //Hide the welcome interface dataview.setVisible(true); //Show the monitoring interface dataview.dataFrame(); //Initialize the monitoring interface} } } /* * Repaint the thread (repaint every 250 milliseconds) */ private class RepaintThread implements Runnable { public void run() { while(true) { repaint(); try { Thread.sleep(250); } catch (InterruptedException e) { //Create a Dialog when a repaint thread throws an exception and displays the exception details String err = ExceptionWriter.getErrorInfoFromException(e); JOptionPane.showMessageDialog(null, err, "Error", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } } } } }Running screenshot:
Note: The bottom "click Enter key to enter the main interface" in the actual operation process has a flash effect (it is achieved by re-drawing the interface every once in a while, allowing this sentence to appear repeatedly in white and black colors). The double buffering method is conducive to solving the problem of flickering the interface when re-drawing (if you do not use the double buffering method, it is equivalent to drawing new things on the old interface bit by bit every time you re-draw, and double buffering is essentially by first drawing a new interface in memory, and then directly covering the old interface with the new interface at once)
The DataView.java code is as follows: (This class is used to display serial port data in real time)
Simple explanation:
The hardware device sends data to the computer through the serial port every once in a while. After the serial port tool is successfully connected to the hardware device and adds a monitoring, it will parse the data and update the interface every time the data is received;
Your requirements are likely to be different from mine when using it. This class is for reference only. In actual use, you may need to recreate the data display interface and data analysis method.
package serialPort;import java.awt.Button;import java.awt.Choice;import java.awt.Color;import java.awt.Font;import java.awt.Frame;import java.awt.Graphics;import java.awt.Image;import java.awt.Label;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.util.List;import java.util.TooManyListenersException;import javax.swing.JOptionPane;import gnu.io.SerialPort;import gnu.io.SerialPortEvent;import gnu.io.SerialPortEventListener;import serialException.*;/** * Monitoring data display class* @author Zhong * */public class DataView extends Frame { /** * */ private static final long serialVersionUID = 1L; Client client = null; private List<String> commList = null; //Save available port numbers private SerialPort serialPort = null; //Save serial port object private Font font = new Font("Microsoft Yahei", Font.BOLD, 25); private Label tem = new Label("No data yet", Label.CENTER); //Temperature private Label hum = new Label("No data yet", Label.CENTER); //Humidity private Label pa = new Label("No data yet", Label.CENTER); //Pressure private Label rain = new Label("No data yet", Label.CENTER); //Rainfall private Label win_sp = new Label("No data yet", Label.CENTER); //Wind speed private Label win_dir = new Label("No data yet", Label.CENTER); //Wind direction private Choice commChoice = new Choice(); //Serial port selection (drop-down box) private Choice bpsChoice = new Choice(); //Bad rate selection private Button openSerialButton = new Button("Open serial port"); Image offScreen = null; //Canvas when repainting//Set the icon of window Toolkit toolKit = getToolkit(); Image icon = toolKit.getImage(DataView.class.getResource("computer.png")); /** * Class constructor* @param client */ public DataView(Client client) { this.client = client; commList = SerialTool.findPort(); //Scan the valid serial port once when the program is initialized} /** * The main menu window displays; * Add Label, button, drop-down bar and related event listening; */ public void dataFrame() { this.setBounds(client.LOC_X, client.LOC_Y, client.WIDTH, client.HEIGHT); this.setTitle("CDIO Project"); this.setIconImage(icon); this.setBackground(Color.white); this.setLayout(null); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent arg0) { if (serialPort != null) { //Close the serial port when the program exits and releases the resource SerialTool.closePort(serialPort); } System.exit(0); } }); tem.setBounds(140, 103, 225, 50); tem.setBackground(Color.black); tem.setFont(font); tem.setForeground(Color.white); add(tem); hum.setBounds(520, 103, 225, 50); hum.setBackground(Color.black); hum.setFont(font); hum.setForeground(Color.white); add(hum); pa.setBounds(140, 193, 225, 50); pa.setBackground(Color.black); pa.setFont(font); pa.setForeground(Color.white); add(pa); rain.setBounds(520, 193, 225, 50); rain.setBackground(Color.black); rain.setFont(font); rain.setForeground(Color.white); add(rain); win_sp.setBounds(140, 283, 225, 50); win_sp.setBackground(Color.black); win_sp.setFont(font); win_sp.setForeground(Color.white); add(win_sp); win_dir.setBounds(520, 283, 225, 50); win_dir.setBackground(Color.black); win_dir.setFont(font); win_dir.setForeground(Color.white); add(win_dir); //Add serial port selection option commChoice.setBounds(160, 397, 200, 200); //Check whether there is a serial port available. If (commList == null || commList.size()<1) { JOptionPane.showMessageDialog(null, "No valid serial port was found! ", "Error", JOptionPane.INFORMATION_MESSAGE); } else { for (String s : commList) { commChoice.add(s); } } add(commChoice); //Add baud rate option bpsChoice.setBounds(526, 396, 200, 200); bpsChoice.add("1200"); bpsChoice.add("2400"); bpsChoice.add("4800"); bpsChoice.add("9600"); bpsChoice.add("14400"); bpsChoice.add("19200"); bpsChoice.add("115200"); add(bpsChoice); //Add the open serial port button openSerialButton.setBounds(250, 490, 300, 50); openSerialButton.setBackground(Color.lightGray); openSerialButton.setFont(new Font("Microsoft Yahei", Font.BOLD, 20)); openSerialButton.setForeground(Color.darkGray); add(openSerialButton); //Add event listening for the open serial port button openSerialButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //Get the serial port name String commName = commChoice.getSelectedItem(); //Get the baud rate String bpsStr = bpsChoice.getSelectedItem(); //Check whether the serial port name is correct if (commName == null || commName.equals("")) { JOptionPane.showMessageDialog(null, "No valid serial port was found!", "Error", JOptionPane.INFORMATION_MESSAGE); } else { //Check whether the baud rate is correct if (bpsStr == null || bpsStr.equals("")) { JOptionPane.showMessageDialog(null, "Bad rate is obtained error! ", "Error", JOptionPane.INFORMATION_MESSAGE); } else { // When both the serial port name and baud rate are obtained correctly int bps = Integer.parseInt(bpsStr); try { //Get the serial port object with the specified port name and baud rate serialPort = SerialTool.openPort(commName, bps); //Add the listener SerialTool.addListener(serialPort, new SerialListener()); //The monitor is prompted for successful monitoring JOptionPane.showMessageDialog(null, "The monitor is successful, the monitoring data will be displayed later!", "Prompt", JOptionPane.INFORMATION_MESSAGE); } catch (SerialPortParameterFailure | NotASerialPort | NoSuchPort | PortInUse | TooManyListeners e1) { //When an error occurs, use a Dialog to prompt the specific error message JOptionPane.showMessageDialog(null, e1, "Error", JOptionPane.INFORMATION_MESSAGE); } } } } } }); this.setResizable(false); new Thread(new RepaintThread()).start(); //Start the repaint thread} /** * Draw the main interface component element*/ public void paint(Graphics g) { Color c = g.getColor(); g.setColor(Color.black); g.setFont(new Font("Microsoft Yahei", Font.BOLD, 25)); g.drawString("Temperature: ", 45, 130); g.setColor(Color.black); g.setFont(new Font("Microsoft Yahei", Font.BOLD, 25)); g.drawString("Humidity: ", 425, 130); g.setColor(Color.black); g.setFont(new Font("Microsoft Yahei", Font.BOLD, 25)); g.drawString("Pressure: ", 45, 220); g.setColor(Color.black); g.setFont(new Font("Microsoft Yahei", Font.BOLD, 25)); g.drawString("Rain: ", 425, 220); g.setColor(Color.black); g.setFont(new Font("Microsoft Yahei", Font.BOLD, 25)); g.drawString("Wind speed: ", 45, 310); g.setColor(Color.black); g.setFont(new Font("Microsoft Yahei", Font.BOLD, 25)); g.drawString("Wind direction: ", 425, 310); g.setColor(Color.gray); g.setFont(new Font("Microsoft Yahei", Font.BOLD, 20)); g.drawString("Serial port selection: ", 45, 410); g.setColor(Color.gray); g.setFont(new Font("Microsoft Yahei", Font.BOLD, 20)); g.drawString("Bad Rate: ", 425, 410); } /** * Double buffering method repaints each element component of the interface*/ public void update(Graphics g) { if (offScreen == null) offScreen = this.createImage(Client.WIDTH, Client.HEIGHT); Graphics gOffScreen = offScreen.getGraphics(); Color c = gOffScreen.getColor(); gOffScreen.setColor(Color.white); gOffScreen.fillRect(0, 0, Client.WIDTH, Client.HEIGHT); //Repaint the background canvas this.paint(gOffScreen); //Repaint the interface element gOffScreen.setColor(c); g.drawImage(offScreen, 0, 0, null); //"Spray" the newly drawn canvas on the original canvas} /* * Repaint thread (repaint every 30 milliseconds) */ private class RepaintThread implements Runnable { public void run() { while(true) { //Call the repaint method repaint(); //Scan the available serial port commList = SerialTool.findPort(); if (commList != null && commList.size()>0) { //Add the newly scanned available serial port for (String s: commList) { //Does the serial port name already exist? The initial default is not existed (if it exists in commList but does not exist in commChoice, it will be newly added) boolean commExist = false; for (int i=0; i<commChoice.getItemCount(); i++) { if (s.equals(commChoice.getItem(i))) { //The currently scanned serial port name already exists in the initial scan commExist = true; break; } } if (commExist) { //The currently scanned serial port name already exists in the initial scan, and directly enters the next loop continue; } else { //If it does not exist, add the new serial port name to the available serial port drop-down list commChoice.add(s); } } //Remove the already unavailable serial port for (int i=0; i<commChoice.getItemCount(); i++) { // Whether the serial port has expired, the initial default is invalid (exist in commChoice but does not exist in commList, it has expired) boolean commNotExist = true; for (String s : commList) { if (s.equals(commChoice.getItem(i))) { commNotExist = false; break; } } if (commNotExist) { //System.out.println("remove" + commChoice.getItem(i)); commChoice.remove(i); } else { continue; } } } else { //If the scanned commList is empty, remove all existing serial ports commChoice.removeAll(); } try { Thread.sleep(30); } catch (InterruptedException e) { String err = ExceptionWriter.getErrorInfoFromException(e); JOptionPane.showMessageDialog(null, err, "Error", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } } } } } /** * Create a serial listening class in the form of an internal class* @author zhong * */ private class SerialListener implements SerialPortEventListener { /** * Handle monitored serial port events*/ public void serialEvent(SerialPortEvent serialPortEvent) { switch (serialPortEvent.getEventType()) { case SerialPortEvent.BI: // 10 Communication interrupt JOptionPane.showMessageDialog(null, "Communication interrupt with serial device", "Error", JOptionPane.INFORMATION_MESSAGE); break; case SerialPortEvent.OE: // 7 Overflow (overflow) error case SerialPortEvent.FE: // 9 Frame error case SerialPortEvent.PE: // 8 Parity error case SerialPortEvent.CD: // 6 Carrier detection case SerialPortEvent.CTS: // 3 Clear data to be sent case SerialPortEvent.DSR: // 4 The data to be sent case is ready SerialPortEvent.RI: // 5 Ringing indicates case SerialPortEvent.OUTPUT_BUFFER_EMPTY: // 2 The output buffer has been cleared break; case SerialPortEvent.DATA_AVAILABLE: // 1 Available data exists on the serial port//System.out.println("found data"); byte[] data = null; try { if (serialPort == null) { JOptionPane.showMessageDialog(null, "The serial port object is empty! Monitoring failed! ", "Error", JOptionPane.INFORMATION_MESSAGE); } else { data = SerialTool.readFromPort(serialPort); //Read data and store it in byte array//System.out.println(new String(data)); //Customize the parsing process, you can parse the data after receiving the data according to your own needs during the actual use process if (data == null || data.length < 1) { //Check whether the data is read correctly JOptionPane.showMessageDialog(null, "No valid data was obtained during the reading of the data! Please check the device or program!", "Error", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } else { String dataOriginal = new String(data); //Convert byte array data to a string that saves the original data String dataValid = ""; //Valid data (used to save the original data string and remove the string after the beginning * number) String[] elements = null; //Used to save the string array obtained after splitting the original string by spaces//Parse the data if (dataOriginal.charAt(0) == '*') { //When the first character of the data is the * number, it means that the data reception is completed, and the parsing dataValid = dataOriginal.substring(1); elements = dataValid.split(" "); if (elements == null || elements.length < 1) { //Check whether the data is parsed correctly JOptionPane.showMessageDialog(null, "The data parsing process has an error, please check the device or program!", "Error", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } else { try { //Update the interface Label value/*for (int i=0; i<elements.length; i++) { System.out.println(elements[i]); }*/ //System.out.println("win_dir: " + elements[5]); tem.setText(elements[0] + " ℃"); hum.setText(elements[1] + " %"); pa.setText(elements[2] + " hPa"); rain.setText(elements[3] + " mm"); win_sp.setText(elements[4] + " m/s"); win_dir.setText(elements[5] + " °"); } catch (ArrayIndexOutOfBoundsException e) { JOptionPane.showMessageDialog(null, "The data analysis process has an error, and the update interface data has failed! Please check the device or program! ", "Error", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } } } } } } } catch (ReadDataFromSerialPortFailure | SerialPortInputStreamCloseFailure e) { JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.INFORMATION_MESSAGE); System.exit(0); //Exit the system after the error message is displayed when a read error occurs} break; } } } }Running screenshot:
Download the entire project source code: http://xiazai.VeVB.COM/201612/yuanma/javaserialMonitor(VeVB.COM).rar
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.