📄 serialconnector.java
字号:
private void packetSizeTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_packetSizeTextFieldActionPerformed try{ maxMsgLength=(short)(Integer.parseInt(packetSizeTextField.getText()) & 0xff); }catch(Exception e){ this.packetSizeTextField.setText(Integer.toString(maxMsgLength)); } }//GEN-LAST:event_packetSizeTextFieldActionPerformed private void ssButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ssButtonActionPerformed if(this.ssButton.getText().equalsIgnoreCase("Start Port")){ prefs.put("ComPort",this.comPortTextField.getText()); prefs.put("ComSpeed",this.comSpeedTextField.getText()); prefs.put("IpAddress",this.ipAddressTextField.getText()); prefs.put("IpPort",this.ipPortTextField.getText()); prefs.put("LocalPort",this.lhPortTextField.getText()); prefs.put("GroupId",this.groupTextField.getText()); prefs.put("TossimIP",this.sTossimTextField.getText()); prefs.put("TossimSerialIP", this.sTossimSerialTextField.getText()); prefs.put("OtherString", this.sOtherTextField.getText()); String value = groupTextField.getText(); if(value.trim().toUpperCase().startsWith("0X")) group=Short.parseShort(value.trim().substring(2),16); else group=Short.parseShort(value.trim()); try{ String connection = ""; this.connectionRadioButtonCOM.setEnabled(false); this.connectionRadioButtonTossim.setEnabled(false); this.connectionRadioButtonIP.setEnabled(false); this.connectionRadioButtonTossimSerial.setEnabled(false); this.oldProtocolCheckBox.setEnabled(false); setRBstate(false); javax.swing.ButtonModel selected = this.buttonGroup1.getSelection(); if(this.connectionRadioButtonCOM.getModel() == selected) { if (oldProtocolCheckBox.isSelected()) connection += "old-"; connection += "serial@" + this.comPortTextField.getText().trim() + ":" + this.comSpeedTextField.getText().trim(); if (oldProtocolCheckBox.isSelected()) connection += "," + maxMsgLength; } if(this.connectionRadioButtonIP.getModel() == selected) { connection += "sf@" + this.ipAddressTextField.getText().trim() + ":" + this.ipPortTextField.getText().trim(); } if(this.connectionRadioButtonTossimSerial.getModel() == selected) { connection += "tossim-serial@" + this.sTossimSerialTextField.getText().trim(); } if(this.connectionRadioButtonTossim.getModel() == selected) { connection += "tossim-radio@" + this.sTossimTextField.getText().trim(); } if(this.connectionRadioButtonOther.getModel() == selected) { connection = sOtherTextField.getText().trim(); } System.out.println("Connecting to: " + connection); serialStub = BuildSource.makePacketSource(connection); serialStub.open(net.tinyos.util.PrintStreamMessenger.err); distributorThread = new SerialConnector.DistributorThread(); distributorThread.start(); packetReader = new SerialConnector.PacketReader(); packetReader.start(); this.ssButton.setText("Stop Port"); System.out.println("Connected."); }catch(Exception e) { System.err.println("ERROR: could not connect: " + e.toString()); this.connectionRadioButtonCOM.setEnabled(true); this.connectionRadioButtonTossim.setEnabled(true); this.connectionRadioButtonTossimSerial.setEnabled(true); this.connectionRadioButtonIP.setEnabled(true); this.oldProtocolCheckBox.setEnabled(true); javax.swing.ButtonModel selected = this.buttonGroup1.getSelection(); if(this.connectionRadioButtonCOM.getModel() == selected){ this.connectionRadioButtonTossim.doClick(); this.connectionRadioButtonIP.doClick() ; this.connectionRadioButtonTossimSerial.doClick() ; this.connectionRadioButtonOther.doClick(); this.connectionRadioButtonCOM.doClick(); } if(this.connectionRadioButtonTossim.getModel() == selected){ this.connectionRadioButtonCOM.doClick(); this.connectionRadioButtonTossimSerial.doClick() ; this.connectionRadioButtonIP.doClick() ; this.connectionRadioButtonOther.doClick(); this.connectionRadioButtonTossim.doClick(); } if(this.connectionRadioButtonIP.getModel() == selected){ this.connectionRadioButtonCOM.doClick(); this.connectionRadioButtonTossim.doClick(); this.connectionRadioButtonTossimSerial.doClick() ; this.connectionRadioButtonOther.doClick(); this.connectionRadioButtonIP.doClick() ; } if(this.connectionRadioButtonTossimSerial.getModel() == selected){ this.connectionRadioButtonCOM.doClick(); this.connectionRadioButtonTossim.doClick(); this.connectionRadioButtonIP.doClick() ; this.connectionRadioButtonOther.doClick(); this.connectionRadioButtonTossimSerial.doClick() ; } if(this.connectionRadioButtonOther.getModel() == selected){ this.connectionRadioButtonCOM.doClick(); this.connectionRadioButtonTossim.doClick(); this.connectionRadioButtonIP.doClick() ; this.connectionRadioButtonTossimSerial.doClick() ; this.connectionRadioButtonOther.doClick(); } this.ssButton.setText("Start Port"); } }else if(this.ssButton.getText().equalsIgnoreCase("Stop Port")){ try{ this.connectionRadioButtonCOM.setEnabled(true); this.connectionRadioButtonTossim.setEnabled(true); this.connectionRadioButtonIP.setEnabled(true); this.connectionRadioButtonTossimSerial.setEnabled(true); this.oldProtocolCheckBox.setEnabled(true); this.connectionRadioButtonOther.setEnabled(true); javax.swing.ButtonModel selected = this.buttonGroup1.getSelection(); if(this.connectionRadioButtonCOM.getModel() == selected){ this.connectionRadioButtonTossim.doClick(); this.connectionRadioButtonIP.doClick() ; this.connectionRadioButtonTossimSerial.doClick() ; this.connectionRadioButtonOther.doClick(); this.connectionRadioButtonCOM.doClick(); } if(this.connectionRadioButtonTossim.getModel() == selected){ this.connectionRadioButtonCOM.doClick(); this.connectionRadioButtonTossimSerial.doClick() ; this.connectionRadioButtonIP.doClick() ; this.connectionRadioButtonOther.doClick(); this.connectionRadioButtonTossim.doClick(); } if(this.connectionRadioButtonIP.getModel() == selected){ this.connectionRadioButtonCOM.doClick(); this.connectionRadioButtonTossim.doClick(); this.connectionRadioButtonTossimSerial.doClick() ; this.connectionRadioButtonOther.doClick(); this.connectionRadioButtonIP.doClick() ; } if(this.connectionRadioButtonTossimSerial.getModel() == selected){ this.connectionRadioButtonCOM.doClick(); this.connectionRadioButtonTossim.doClick(); this.connectionRadioButtonIP.doClick() ; this.connectionRadioButtonOther.doClick(); this.connectionRadioButtonTossimSerial.doClick() ; } if(this.connectionRadioButtonOther.getModel() == selected){ this.connectionRadioButtonCOM.doClick(); this.connectionRadioButtonTossim.doClick(); this.connectionRadioButtonIP.doClick() ; this.connectionRadioButtonTossimSerial.doClick() ; this.connectionRadioButtonOther.doClick(); } packetReader.stopRun(); distributorThread.end(); serialStub.close(); System.out.println("Connection closed."); this.ssButton.setText("Start Port"); }catch(Exception e) { System.err.println("Could not terminate connection to local port: " + e.toString()); } } }//GEN-LAST:event_ssButtonActionPerformed private void connectionRadioButtonIPItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_connectionRadioButtonIPItemStateChanged if (evt.getStateChange() == ItemEvent.SELECTED) { this.ipLabel.setEnabled(true); this.ipAddressTextField.setEnabled(true); this.ipPortTextField.setEnabled(true); this.portLabel.setEnabled(true); } else if(evt.getStateChange() == ItemEvent.DESELECTED) { this.ipLabel.setEnabled(false); this.ipAddressTextField.setEnabled(false); this.ipPortTextField.setEnabled(false); this.portLabel.setEnabled(false); } }//GEN-LAST:event_connectionRadioButtonIPItemStateChanged private void connectionRadioButtonCOMItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_connectionRadioButtonCOMItemStateChanged if (evt.getStateChange() == ItemEvent.SELECTED) { this.comPortTextField.setEnabled(true); this.comSpeedTextField.setEnabled(true); this.comPortLabel.setEnabled(true); this.speedLabel.setEnabled(true); } else if(evt.getStateChange() == ItemEvent.DESELECTED) { this.comPortTextField.setEnabled(false); this.comSpeedTextField.setEnabled(false); this.comPortLabel.setEnabled(false); this.speedLabel.setEnabled(false); } }//GEN-LAST:event_connectionRadioButtonCOMItemStateChanged // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel sComPortSpeedLabel; private javax.swing.JLabel speedLabel; private javax.swing.JButton ssButton; private javax.swing.JLabel lhPortLabel; private javax.swing.JPanel controlPanel; private javax.swing.JTextField comPortTextField; private javax.swing.JLabel ipLabel; private javax.swing.JLabel sTossimSerialLabel; private javax.swing.JLabel sOtherLabel; private javax.swing.JRadioButton connectionRadioButtonCOM; private javax.swing.JLabel sComPortLabel; private javax.swing.JRadioButton connectionRadioButtonTossim; private javax.swing.JRadioButton connectionRadioButtonTossimSerial; private javax.swing.JRadioButton connectionRadioButtonIP; private javax.swing.JRadioButton connectionRadioButtonOther; private javax.swing.JLabel sentLabel; private javax.swing.JTextField comSpeedTextField; private javax.swing.JPanel mainPanel; private javax.swing.JTextField sTossimTextField; private javax.swing.JTextField sTossimSerialTextField; private javax.swing.JTextField sOtherTextField; private javax.swing.JTextField receivedTextField; private javax.swing.JTextField lhPortTextField; private javax.swing.JTextField packetSizeTextField; private javax.swing.ButtonGroup buttonGroup1; private javax.swing.JSeparator jSeparator1; private javax.swing.JSeparator jSeparator2; private javax.swing.JTextField groupTextField; private javax.swing.JLabel packetSizeLabel; private javax.swing.JLabel receivedLabel; private javax.swing.JPanel messagePanel; private javax.swing.JLabel messageLabel; private javax.swing.JLabel portLabel; private javax.swing.JTextField ipAddressTextField; private javax.swing.JTextField sentTextField; private javax.swing.JLabel comPortLabel; private javax.swing.JTextField ipPortTextField; private javax.swing.JTextField sComPortSpeedTextField; private javax.swing.JLabel groupLabel; private javax.swing.JCheckBox oldProtocolCheckBox; // End of variables declaration//GEN-END:variables /*********************************Message sendIF******************************/ public void registerPacketListener(PacketListenerIF packetListener, int messageID){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -