📄 serialdemo.java
字号:
java.properties style. */ private void writeFile(String path) { Properties newProps; FileOutputStream fileOut = null; newProps = new Properties(); newProps.put("portName", parameters.getPortName()); newProps.put("baudRate", parameters.getBaudRateString()); newProps.put("flowControlIn", parameters.getFlowControlInString()); newProps.put("flowControlOut", parameters.getFlowControlOutString()); newProps.put("parity", parameters.getParityString()); newProps.put("databits", parameters.getDatabitsString()); newProps.put("stopbits", parameters.getStopbitsString()); try { fileOut = new FileOutputStream(path); } catch (IOException e) { System.out.println("Could not open file for writiing"); } newProps.save(fileOut, "Serial Demo poperties"); try { fileOut.close(); } catch (IOException e) { System.out.println("Could not close file for writiing"); } } /** Cleanly shuts down the applicaion. first closes any open ports and cleans up, then exits. */ private void shutdown() { connection.closeConnection(); System.exit(1); } /** Finds configuration file in arguments and creates a properties object from that file. */ private void parseArgs(String[] args) { if (args.length < 1) { return; } File f = new File(args[0]); if (!f.exists()) { f = new File(System.getProperty("user.dir") + System.getProperty("path.separator") + args[0]); } if (f.exists()) { try { FileInputStream fis = new FileInputStream(f); props = new Properties(); props.load(fis); fis.close(); loadParams(); } catch (IOException e) { } } } /** Set the parameters object to the settings in the properties object. */ private void loadParams() { parameters.setPortName(props.getProperty("portName")); parameters.setBaudRate(props.getProperty("baudRate")); parameters.setFlowControlIn(props.getProperty("flowControlIn")); parameters.setFlowControlOut(props.getProperty("flowControlOut")); parameters.setParity(props.getProperty("parity")); parameters.setDatabits(props.getProperty("databits")); parameters.setStopbits(props.getProperty("stopbits")); setConfigurationPanel(); } /** GUI element that holds the user changable elements for connection configuration. */ class ConfigurationPanel extends Panel implements ItemListener { private Frame parent; private Label portNameLabel; private Choice portChoice; private Label baudLabel; private Choice baudChoice; private Label flowControlInLabel; private Choice flowChoiceIn; private Label flowControlOutLabel; private Choice flowChoiceOut; private Label databitsLabel; private Choice databitsChoice; private Label stopbitsLabel; private Choice stopbitsChoice; private Label parityLabel; private Choice parityChoice; /** Creates and initilizes the configuration panel. The initial settings are from the parameters object. */ public ConfigurationPanel(Frame parent) { this.parent = parent; setLayout(new GridLayout(4, 4)); portNameLabel = new Label("Port Name:", Label.LEFT); add(portNameLabel); portChoice = new Choice(); portChoice.addItemListener(this); add(portChoice); listPortChoices(); portChoice.select(parameters.getPortName()); baudLabel = new Label("Baud Rate:", Label.LEFT); add(baudLabel); baudChoice = new Choice(); baudChoice.addItem("300"); baudChoice.addItem("2400"); baudChoice.addItem("9600"); baudChoice.addItem("14400"); baudChoice.addItem("28800"); baudChoice.addItem("38400"); baudChoice.addItem("57600"); baudChoice.addItem("152000"); baudChoice.select(Integer.toString(parameters.getBaudRate())); baudChoice.addItemListener(this); add(baudChoice); flowControlInLabel = new Label("Flow Control In:", Label.LEFT); add(flowControlInLabel); flowChoiceIn = new Choice(); flowChoiceIn.addItem("None"); flowChoiceIn.addItem("Xon/Xoff In"); flowChoiceIn.addItem("RTS/CTS In"); flowChoiceIn.select(parameters.getFlowControlInString()); flowChoiceIn.addItemListener(this); add(flowChoiceIn); flowControlOutLabel = new Label("Flow Control Out:", Label.LEFT); add(flowControlOutLabel); flowChoiceOut = new Choice(); flowChoiceOut.addItem("None"); flowChoiceOut.addItem("Xon/Xoff Out"); flowChoiceOut.addItem("RTS/CTS Out"); flowChoiceOut.select(parameters.getFlowControlOutString()); flowChoiceOut.addItemListener(this); add(flowChoiceOut); databitsLabel = new Label("Data Bits:", Label.LEFT); add(databitsLabel); databitsChoice = new Choice(); databitsChoice.addItem("5"); databitsChoice.addItem("6"); databitsChoice.addItem("7"); databitsChoice.addItem("8"); databitsChoice.select(parameters.getDatabitsString()); databitsChoice.addItemListener(this); add(databitsChoice); stopbitsLabel = new Label("Stop Bits:", Label.LEFT); add(stopbitsLabel); stopbitsChoice = new Choice(); stopbitsChoice.addItem("1"); stopbitsChoice.addItem("1.5"); stopbitsChoice.addItem("2"); stopbitsChoice.select(parameters.getStopbitsString()); stopbitsChoice.addItemListener(this); add(stopbitsChoice); parityLabel = new Label("Parity:", Label.LEFT); add(parityLabel); parityChoice = new Choice(); parityChoice.addItem("None"); parityChoice.addItem("Even"); parityChoice.addItem("Odd"); parityChoice.select("None"); parityChoice.select(parameters.getParityString()); parityChoice.addItemListener(this); add(parityChoice); } /** Sets the configuration panel to the settings in the parameters object. */ public void setConfigurationPanel() { portChoice.select(parameters.getPortName()); baudChoice.select(parameters.getBaudRateString()); flowChoiceIn.select(parameters.getFlowControlInString()); flowChoiceOut.select(parameters.getFlowControlOutString()); databitsChoice.select(parameters.getDatabitsString()); stopbitsChoice.select(parameters.getStopbitsString()); parityChoice.select(parameters.getParityString()); } /** Sets the parameters object to the settings in the configuration panel. */ public void setParameters() { parameters.setPortName(portChoice.getSelectedItem()); parameters.setBaudRate(baudChoice.getSelectedItem()); parameters.setFlowControlIn(flowChoiceIn.getSelectedItem()); parameters.setFlowControlOut(flowChoiceOut.getSelectedItem()); parameters.setDatabits(databitsChoice.getSelectedItem()); parameters.setStopbits(stopbitsChoice.getSelectedItem()); parameters.setParity(parityChoice.getSelectedItem()); } /** Sets the elements for the portChoice from the ports available on the system. Uses an emuneration of comm ports returned by CommPortIdentifier.getPortIdentifiers(), then sets the current choice to a mathing element in the parameters object. */ void listPortChoices() { CommPortIdentifier portId; Enumeration en = CommPortIdentifier.getPortIdentifiers(); // iterate through the ports. while (en.hasMoreElements()) { portId = (CommPortIdentifier) en.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { portChoice.addItem(portId.getName()); } } portChoice.select(parameters.getPortName()); } /** Event handler for changes in the current selection of the Choices. If a port is open the port can not be changed. If the choice is unsupported on the platform then the user will be notified and the settings will revert to their pre-selection state. */ public void itemStateChanged(ItemEvent e) { // Check if port is open. if (connection.isOpen()) { // If port is open do not allow port to change. if (e.getItemSelectable() == portChoice) { // Alert user. AlertDialog ad = new AlertDialog(parent, "Port Open!", "Port can not", "be changed", "while a port is open."); // Return configurationPanel to pre-choice settings. setConfigurationPanel(); return; } // Set the parameters from the choice panel. setParameters(); try { // Attempt to change the settings on an open port. connection.setConnectionParameters(); } catch (SerialConnectionException ex) { // If setting can not be changed, alert user, return to // pre-choice settings. AlertDialog ad = new AlertDialog(parent, "Unsupported Configuration!", "Configuration Parameter unsupported,", "select new value.", "Returning to previous configuration."); setConfigurationPanel(); } } else { // Since port is not open just set the parameter object. setParameters(); } } } /** Handles closing down system. Allows application to be closed with window close box. */ class CloseHandler extends WindowAdapter { SerialDemo sd; public CloseHandler(SerialDemo sd) { this.sd = sd; } public void windowClosing(WindowEvent e) { sd.shutdown(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -