package MaceWindu; import gnu.io.*; import java.awt.event.ItemEvent; import java.awt.event.KeyEvent; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.util.Enumeration; import java.util.TooManyListenersException; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; import javax.swing.UIManager; public class MaceWindu extends javax.swing.JFrame { SerialPort COM; OutputStream ComOut; InputStream ComInputStream; BufferedReader COMReader; String PreviousPortname; String PortName; String TxString; String RecievedString; String ConvertedStr; String PreviousWriteText; String LookAndFeelString; byte[] buffer = new byte[1024]; byte[] ConvertedByte; byte[] RecievedDataBs; char ConvertedChar; char PressedKeyChar; int PressedKeyCode; int ConvertedInt; int RecievedData; int Recieve; int TxData; int UnsignedTxData; int BaudRate=0; int DataBits=8; int StopBits=1; int Parity=0; int HandShake=0; boolean ButtonState = false; boolean PressedKeyCheck = false; public MaceWindu() { //アンチエイリアスを有効化させるやつ // でも,なんか効かないみたいwwww System.setProperty("awt.useSystemAAFontSettings","on"); System.setProperty("swing.aatext", "true"); ChangeLookAndFeel(); initComponents(); AvailableComPort(); ReadListenerEnable(); //ラジオボタンをボタンブループに追加する時はここに書かないとなんかエラー出るww ReadFormatSelectMenuButtonGroup.add(ReadStrSelectMenuButton); ReadFormatSelectMenuButtonGroup.add(ReadBinSelectMenuButton); ReadFormatSelectMenuButtonGroup.add(ReadOctSelectMenuButton); ReadFormatSelectMenuButtonGroup.add(ReadDecSelectMenuButton); ReadFormatSelectMenuButtonGroup.add(ReadHexSelectMenuButton); ReadModeSelectMenuButtonGroup.add(ReadSelectMenuButton); ReadModeSelectMenuButtonGroup.add(ReadLineSelectMenuButton); WriteFormatSelectMenuButtonGroup.add(WriteStrSelectMenuButton); WriteFormatSelectMenuButtonGroup.add(WriteBinSelectMenuButton); WriteFormatSelectMenuButtonGroup.add(WriteOctSelectMenuButton); WriteFormatSelectMenuButtonGroup.add(WriteDecSelectMenuButton); WriteFormatSelectMenuButtonGroup.add(WriteHexSelectMenuButton); WriteTriggerSelectMenuButtonGroup.add(PressReturnKeySelectMenuButton); WriteTriggerSelectMenuButtonGroup.add(PressAnyKeySelectMenuButton); ChangeLookAndFeelMenuButtonGroup.add(LafMetalSelectMenuButton); ChangeLookAndFeelMenuButtonGroup.add(LafMotifSelectMenuButton); ChangeLookAndFeelMenuButtonGroup.add(LafWindowsSelectMenuButton); ChangeLookAndFeelMenuButtonGroup.add(LafWindowsClassicSelectMenuButton); ChangeLookAndFeelMenuButtonGroup.add(LafMacSelectMenuButton); ChangeLookAndFeelMenuButtonGroup.add(LafGTKSelectMenuButton); ChangeLookAndFeelMenuButtonGroup.add(LafNimbusSelectMenuButton); ReadSelectMenuButton.setSelected(true); ReadStrSelectMenuButton.setSelected(true); WriteStrSelectMenuButton.setSelected(true); PressReturnKeySelectMenuButton.setSelected(true); LafMetalSelectMenuButton.setSelected(true); MsgLabel.setText(null); } private void ChangeLookAndFeel(){ /* if (LafMetalSelectMenuButton.isSelected() == true){ LookAndFeelString = "javax.swing.plaf.metal.MetalLookAndFeel"; } if (LafMotifSelectMenuButton.isSelected() == true){ LookAndFeelString = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"; } if (LafWindowsSelectMenuButton.isSelected() == true){ LookAndFeelString = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; } if (LafWindowsClassicSelectMenuButton.isSelected() == true){ LookAndFeelString = "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"; } if (LafMacSelectMenuButton.isSelected() == true){ LookAndFeelString = "com.sun.java.swing.plaf.mac.MacLookAndFeel"; } if (LafGTKSelectMenuButton.isSelected() == true){ LookAndFeelString = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"; } if (LafNimbusSelectMenuButton.isSelected() == true){ LookAndFeelString = "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"; } */ try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); //UIManager.setLookAndFeel(LookAndFeelString); //SwingUtilities.updateComponentTreeUI(this); } catch(Exception e) { MsgLabel.setText("Some Expention While Change LookAndFeel"); } } //文字列(string)を整数(int)にする関数 //IntValueはエラーの時に返すやつ。だから,実行するときにエラーで返してほしいやつ指定する private int ConvertStr2Int(String Strvalue, int Intvalue){ try { return Integer.parseInt(Strvalue); } catch (NumberFormatException e) { return Intvalue; } } private byte [] toByte(int IntData){ byte[] ByteData = new byte[4]; ByteData[0] = (byte)(0x000000ff & IntData); ByteData[1] = (byte)(0x0000ff00 & IntData); ByteData[2] = (byte)(0x00ff0000 & IntData); ByteData[3] = (byte)(0xff000000 & IntData); return ByteData; } private void AvailableComPort(){ PortsComboBox.removeAllItems(); PortsComboBox.addItem("-Ports-"); CommPortIdentifier portID = null; Enumeration portEnum = CommPortIdentifier.getPortIdentifiers(); if (portEnum.hasMoreElements()){ while (portEnum.hasMoreElements()){ CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement(); PortsComboBox.addItem(currPortId.getName()); } PortsComboBox.setSelectedIndex(0); }else{ JOptionPane.showMessageDialog(rootPane, "There Is No Available COM Port"); } } private void OpenComPort(String port){ try { CommPortIdentifier comID = CommPortIdentifier.getPortIdentifier(port); CommPort commPort = comID.open("Com", 2000); //引数はstring アプリケーション名 int タイムアウト時間 らしい (アプリケーション名は何にしてもおk!!ww) COM = (SerialPort)commPort; COM.setSerialPortParams(BaudRate, DataBits, StopBits, Parity); //COM.setSerialPortParams(2400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); COM.setFlowControlMode(HandShake); MsgLabel.setText(PortName + " Opened Successfully"); ComboBoxEnSelect(true); ReadListenerEnable(); ReadEnable(); WriteEnable(); } catch (Exception e) { MsgLabel.setText("Failed to Open " + PortName); ComboBoxEnSelect(false); } } private void CloseComPort(){ try { COM.close(); } catch(Exception e) { MsgLabel.setText("Failed To Close " + PreviousPortname); } } private void ComboBoxEnSelect(boolean i){ if (i == true){ BaudRateComboBox.setEnabled(true); DataBitsComboBox.setEnabled(true); StopBitsComboBox.setEnabled(true); ParityComboBox.setEnabled(true); HandShakeComboBox.setEnabled(true); }else{ BaudRateComboBox.setEnabled(false); DataBitsComboBox.setEnabled(false); StopBitsComboBox.setEnabled(false); ParityComboBox.setEnabled(false); HandShakeComboBox.setEnabled(false); } } public void WriteEnable(){ try { ComOut = COM.getOutputStream(); } catch (Exception e) { MsgLabel.setText("Error_OutputStream"); } } public void ReadEnable(){ try { COM.enableReceiveThreshold(1); ComInputStream = COM.getInputStream(); COMReader = new BufferedReader(new InputStreamReader(COM.getInputStream())); } catch (Exception e) { MsgLabel.setText("Exception In ReadEnable()"); } } private void Read(){ try{ if (ReadStrSelectMenuButton.isSelected() == true){ /*while (COMReader.ready()){ try { RecievedData = COMReader.read(); ConvertedChar = (char) RecievedData; ConvertedStr = String.valueOf(ConvertedChar); RecievedDataTextArea.append(ConvertedStr); } catch (Exception e) { MsgLabel.setText("An Error Occured At Read()"); } }*/ try { RecievedData = ComInputStream.read(); ConvertedChar = (char) RecievedData; ConvertedStr = String.valueOf(ConvertedChar); RecievedDataTextArea.append(ConvertedStr); } catch (Exception e) { } RecievedDataTextArea.setCaretPosition(RecievedDataTextArea.getText().length()); }else{ //while (ComInputStream.read() != -1) { //なんかよう分からんけど, != -1 にすると2回送信しないと受信しくれないwww //だからとりあえず,whileは外してtry-catch で Exception掴んでる try { RecievedData = ComInputStream.read(); if (ReadBinSelectMenuButton.isSelected() == true){ ConvertedStr = Integer.toBinaryString(RecievedData); //8桁になるように0を先頭に追加してくコード if (Fix8BitSelectMenuCheckBox.isSelected() == true){ while (ConvertedStr.length() <= 7){ ConvertedStr = "0" + ConvertedStr; } } if (AddFormatRepresentationSelectMenuCheckBox.isSelected() == true){ ConvertedStr = "0b" + ConvertedStr; } RecievedDataTextArea.append(ConvertedStr + " "); } if (ReadOctSelectMenuButton.isSelected() == true){ ConvertedStr = Integer.toOctalString(RecievedData); if (AddFormatRepresentationSelectMenuCheckBox.isSelected() == true){ ConvertedStr = "0" + ConvertedStr; } RecievedDataTextArea.append(ConvertedStr + " "); } if (ReadDecSelectMenuButton.isSelected() == true){ RecievedDataTextArea.append(String.valueOf(RecievedData) + " "); } if (ReadHexSelectMenuButton.isSelected() == true){ ConvertedStr = Integer.toHexString(RecievedData); if (FixCapitalLetterSelectMenuCheckBox.isSelected() == true){ ConvertedStr = ConvertedStr.toUpperCase(); } //2桁になるように0を先頭に追加してくコード if (Fix8BitSelectMenuCheckBox.isSelected() == true){ while (ConvertedStr.length() == 1){ ConvertedStr = "0" + ConvertedStr; } } if (AddFormatRepresentationSelectMenuCheckBox.isSelected() == true){ ConvertedStr = "0x" + ConvertedStr; } RecievedDataTextArea.append(ConvertedStr + " "); } } catch (Exception e) { MsgLabel.setText("An Error Occured At Read()"); } RecievedDataTextArea.setCaretPosition(RecievedDataTextArea.getText().length()); //} } } catch (Exception e){ } } private void ReadLine(){ try { while(COMReader.ready()){ RecievedString = COMReader.readLine(); RecievedDataTextArea.append(RecievedString); RecievedDataTextArea.setCaretPosition(RecievedDataTextArea.getText().length()); //DataLabel.setText(RecievedString); //System.out.println(RecievedString); } } catch (IOException e) { MsgLabel.setText("An Error Occured At ReadLine()"); } //JOptionPane.showMessageDialog(rootPane, "受信成功!"); //MsgLabel.setText("Data Received Successfully!!"); } private void ReadListenerEnable(){ try { COM.addEventListener(new SerialPortlistener()); COM.notifyOnDataAvailable(true); COM.enableReceiveThreshold(1); } catch (TooManyListenersException e) { MsgLabel.setText("Error : TooManyListenersException"); } catch(Exception e){ MsgLabel.setText("Error Occured While ReadListenerEnable"); } } @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { ReadModeSelectMenuButtonGroup = new javax.swing.ButtonGroup(); ReadFormatSelectMenuButtonGroup = new javax.swing.ButtonGroup(); WriteFormatSelectMenuButtonGroup = new javax.swing.ButtonGroup(); WriteTriggerSelectMenuButtonGroup = new javax.swing.ButtonGroup(); ChangeLookAndFeelMenuButtonGroup = new javax.swing.ButtonGroup(); jPanel1 = new javax.swing.JPanel(); jScrollPane1 = new javax.swing.JScrollPane(); RecievedDataTextArea = new javax.swing.JTextArea(); TxButton = new javax.swing.JButton(); MsgLabel = new javax.swing.JLabel(); PortsComboBox = new javax.swing.JComboBox(); BaudRateComboBox = new javax.swing.JComboBox(); DataBitsComboBox = new javax.swing.JComboBox(); StopBitsComboBox = new javax.swing.JComboBox(); ParityComboBox = new javax.swing.JComboBox(); HandShakeComboBox = new javax.swing.JComboBox(); WriteTextField = new javax.swing.JTextField(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); WritePreferenceMenu = new javax.swing.JMenu(); jMenu4 = new javax.swing.JMenu(); PressReturnKeySelectMenuButton = new javax.swing.JRadioButtonMenuItem(); PressAnyKeySelectMenuButton = new javax.swing.JRadioButtonMenuItem(); jMenu2 = new javax.swing.JMenu(); WriteStrSelectMenuButton = new javax.swing.JRadioButtonMenuItem(); WriteBinSelectMenuButton = new javax.swing.JRadioButtonMenuItem(); WriteOctSelectMenuButton = new javax.swing.JRadioButtonMenuItem(); WriteDecSelectMenuButton = new javax.swing.JRadioButtonMenuItem(); WriteHexSelectMenuButton = new javax.swing.JRadioButtonMenuItem(); AddRSelectMenuCheckBox = new javax.swing.JCheckBoxMenuItem(); AddNSelectMenuCheckBox = new javax.swing.JCheckBoxMenuItem(); ReadPreferenceMenu = new javax.swing.JMenu(); jMenu3 = new javax.swing.JMenu(); ReadSelectMenuButton = new javax.swing.JRadioButtonMenuItem(); ReadLineSelectMenuButton = new javax.swing.JRadioButtonMenuItem(); ReadFormatSelectMenu = new javax.swing.JMenu(); ReadStrSelectMenuButton = new javax.swing.JRadioButtonMenuItem(); ReadBinSelectMenuButton = new javax.swing.JRadioButtonMenuItem(); ReadOctSelectMenuButton = new javax.swing.JRadioButtonMenuItem(); ReadDecSelectMenuButton = new javax.swing.JRadioButtonMenuItem(); ReadHexSelectMenuButton = new javax.swing.JRadioButtonMenuItem(); Fix8BitSelectMenuCheckBox = new javax.swing.JCheckBoxMenuItem(); FixCapitalLetterSelectMenuCheckBox = new javax.swing.JCheckBoxMenuItem(); AddFormatRepresentationSelectMenuCheckBox = new javax.swing.JCheckBoxMenuItem(); jMenu5 = new javax.swing.JMenu(); jMenu6 = new javax.swing.JMenu(); LafMetalSelectMenuButton = new javax.swing.JRadioButtonMenuItem(); LafMotifSelectMenuButton = new javax.swing.JRadioButtonMenuItem(); LafWindowsSelectMenuButton = new javax.swing.JRadioButtonMenuItem(); LafWindowsClassicSelectMenuButton = new javax.swing.JRadioButtonMenuItem(); LafMacSelectMenuButton = new javax.swing.JRadioButtonMenuItem(); LafGTKSelectMenuButton = new javax.swing.JRadioButtonMenuItem(); LafNimbusSelectMenuButton = new javax.swing.JRadioButtonMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("MaceWindu"); setResizable(false); jPanel1.setMaximumSize(new java.awt.Dimension(50000, 50000)); jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); RecievedDataTextArea.setEditable(false); RecievedDataTextArea.setColumns(20); RecievedDataTextArea.setLineWrap(true); RecievedDataTextArea.setRows(5); RecievedDataTextArea.setWrapStyleWord(true); RecievedDataTextArea.setAutoscrolls(false); RecievedDataTextArea.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR)); RecievedDataTextArea.setFocusable(false); jScrollPane1.setViewportView(RecievedDataTextArea); TxButton.setText("Transmitt"); TxButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { TxButtonActionPerformed(evt); } }); MsgLabel.setText("MsgLabel"); PortsComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-Ports-" })); PortsComboBox.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { PortsComboBoxItemStateChanged(evt); } }); BaudRateComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-Baud Rate-", "300", "600", "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200" })); BaudRateComboBox.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { BaudRateComboBoxItemStateChanged(evt); } }); DataBitsComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-Data Bits-", "5", "6", "7", "8" })); DataBitsComboBox.setSelectedIndex(4); DataBitsComboBox.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { DataBitsComboBoxItemStateChanged(evt); } }); StopBitsComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-Stop Bits-", "1", "1.5", "2" })); StopBitsComboBox.setSelectedIndex(1); StopBitsComboBox.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { StopBitsComboBoxItemStateChanged(evt); } }); ParityComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-Parity-", "Even", "Mark", "None", "Odd", "Space" })); ParityComboBox.setSelectedIndex(3); ParityComboBox.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { ParityComboBoxItemStateChanged(evt); } }); HandShakeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-Hand Shake-", "None", "RTSCTS_IN", "TRSCTS_OUT", "XOnXOff_IN", "XOnXOff_OUT" })); HandShakeComboBox.setSelectedIndex(1); HandShakeComboBox.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { HandShakeComboBoxItemStateChanged(evt); } }); WriteTextField.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(java.awt.event.KeyEvent evt) { WriteTextFieldKeyReleased(evt); } public void keyTyped(java.awt.event.KeyEvent evt) { WriteTextFieldKeyTyped(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(WriteTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 763, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(TxButton)) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(MsgLabel) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(PortsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(BaudRateComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(DataBitsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(StopBitsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(ParityComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(HandShakeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(0, 0, Short.MAX_VALUE))) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(PortsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(BaudRateComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(DataBitsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(StopBitsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(ParityComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(HandShakeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 304, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(WriteTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(TxButton)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(MsgLabel) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jMenu1.setText("File"); jMenuBar1.add(jMenu1); WritePreferenceMenu.setText("Write Preference"); jMenu4.setText("Write Trigger"); PressReturnKeySelectMenuButton.setText("Press Return Key"); jMenu4.add(PressReturnKeySelectMenuButton); PressAnyKeySelectMenuButton.setText("Press Any Key"); jMenu4.add(PressAnyKeySelectMenuButton); WritePreferenceMenu.add(jMenu4); jMenu2.setText("Write Format"); WriteStrSelectMenuButton.setText("String"); WriteStrSelectMenuButton.addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent evt) { WriteStrSelectMenuButtonStateChanged(evt); } }); jMenu2.add(WriteStrSelectMenuButton); WriteBinSelectMenuButton.setText("Binary"); jMenu2.add(WriteBinSelectMenuButton); WriteOctSelectMenuButton.setText("Octal"); jMenu2.add(WriteOctSelectMenuButton); WriteDecSelectMenuButton.setText("Decimal"); jMenu2.add(WriteDecSelectMenuButton); WriteHexSelectMenuButton.setText("Hexadecimal"); jMenu2.add(WriteHexSelectMenuButton); WritePreferenceMenu.add(jMenu2); AddRSelectMenuCheckBox.setText("Add \\r"); WritePreferenceMenu.add(AddRSelectMenuCheckBox); AddNSelectMenuCheckBox.setText("Add \\n"); WritePreferenceMenu.add(AddNSelectMenuCheckBox); jMenuBar1.add(WritePreferenceMenu); ReadPreferenceMenu.setText("Read Preference"); jMenu3.setText("Read Mode"); ReadSelectMenuButton.setText("Read"); jMenu3.add(ReadSelectMenuButton); ReadLineSelectMenuButton.setText("ReadLine"); jMenu3.add(ReadLineSelectMenuButton); ReadPreferenceMenu.add(jMenu3); ReadFormatSelectMenu.setText("Read Format"); ReadStrSelectMenuButton.setText("String"); ReadFormatSelectMenu.add(ReadStrSelectMenuButton); ReadBinSelectMenuButton.setText("Binary"); ReadFormatSelectMenu.add(ReadBinSelectMenuButton); ReadOctSelectMenuButton.setText("Octal"); ReadFormatSelectMenu.add(ReadOctSelectMenuButton); ReadDecSelectMenuButton.setText("Decimal"); ReadFormatSelectMenu.add(ReadDecSelectMenuButton); ReadHexSelectMenuButton.setText("Hexadecimal"); ReadFormatSelectMenu.add(ReadHexSelectMenuButton); ReadPreferenceMenu.add(ReadFormatSelectMenu); Fix8BitSelectMenuCheckBox.setSelected(true); Fix8BitSelectMenuCheckBox.setText("Fix to 8bit"); ReadPreferenceMenu.add(Fix8BitSelectMenuCheckBox); FixCapitalLetterSelectMenuCheckBox.setSelected(true); FixCapitalLetterSelectMenuCheckBox.setText("Fix to Capital Letter"); ReadPreferenceMenu.add(FixCapitalLetterSelectMenuCheckBox); AddFormatRepresentationSelectMenuCheckBox.setSelected(true); AddFormatRepresentationSelectMenuCheckBox.setText("Add Format Representation"); ReadPreferenceMenu.add(AddFormatRepresentationSelectMenuCheckBox); jMenuBar1.add(ReadPreferenceMenu); jMenu5.setText("General Preference"); jMenu6.setText("Look And Feel"); LafMetalSelectMenuButton.setText("Metal"); LafMetalSelectMenuButton.setToolTipText(""); jMenu6.add(LafMetalSelectMenuButton); LafMotifSelectMenuButton.setText("Motif"); jMenu6.add(LafMotifSelectMenuButton); LafWindowsSelectMenuButton.setText("Windows"); jMenu6.add(LafWindowsSelectMenuButton); LafWindowsClassicSelectMenuButton.setText("Windows Classic"); LafWindowsClassicSelectMenuButton.setToolTipText(""); jMenu6.add(LafWindowsClassicSelectMenuButton); LafMacSelectMenuButton.setText("Mac"); jMenu6.add(LafMacSelectMenuButton); LafGTKSelectMenuButton.setText("GTK"); jMenu6.add(LafGTKSelectMenuButton); LafNimbusSelectMenuButton.setText("Nimbus"); jMenu6.add(LafNimbusSelectMenuButton); jMenu5.add(jMenu6); jMenuBar1.add(jMenu5); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); pack(); setLocationRelativeTo(null); }// //GEN-END:initComponents private void WriteStr(String StrSend){ if (BaudRateComboBox.getSelectedIndex() == 0){ MsgLabel.setText("Error : Please Select Baud Rate"); } try { byte[] data = StrSend.getBytes(); ComOut.write(data); //JOptionPane.showMessageDialog(rootPane, "送信完了!!!"); } catch (Exception e) { MsgLabel.setText("Error Occured At WriteStr()"); } } private void Write(int IntSend){ if (BaudRateComboBox.getSelectedIndex() == 0){ MsgLabel.setText("Error : Please Select Baud Rate"); } try { ComOut.write(IntSend); //ComOut.write(buffer, WIDTH, IntSend); } catch (Exception e) { MsgLabel.setText("An Error Occured At Write()"); } } private void PortsComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_PortsComboBoxItemStateChanged if (evt.getStateChange() == ItemEvent.SELECTED){ PortName = PortsComboBox.getSelectedItem().toString(); if (PortsComboBox.getSelectedIndex() >= 1){ ComboBoxEnSelect(true); MsgLabel.setVisible(true); CloseComPort(); OpenComPort(PortName); PreviousPortname = PortsComboBox.getSelectedItem().toString(); }else{ ComboBoxEnSelect(false); MsgLabel.setVisible(false); } } }//GEN-LAST:event_PortsComboBoxItemStateChanged private void BaudRateComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_BaudRateComboBoxItemStateChanged CloseComPort(); if (evt.getStateChange() == ItemEvent.SELECTED){ if (BaudRateComboBox.getSelectedIndex() >= 1){ BaudRate = ConvertStr2Int(BaudRateComboBox.getSelectedItem().toString(), 0); if (BaudRate != 0){ try { OpenComPort(PortName); MsgLabel.setText(""); } catch (Exception e) { MsgLabel.setText("Failed to Change BaudRate"); } }else{ MsgLabel.setText("Unknown BaudRate Value"); } }else{ //MsgLabel.setText(null); } } }//GEN-LAST:event_BaudRateComboBoxItemStateChanged private void DataBitsComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_DataBitsComboBoxItemStateChanged CloseComPort(); if (evt.getStateChange() == ItemEvent.SELECTED){ if (DataBitsComboBox.getSelectedIndex() >= 1){ DataBits = ConvertStr2Int(DataBitsComboBox.getSelectedItem().toString(), 0); if (DataBits != 0){ try { OpenComPort(PortName); MsgLabel.setText(null); } catch (Exception e) { MsgLabel.setText("Failed to Change Data Bits"); } } }else{ //MsgLabel.setText(null); } } }//GEN-LAST:event_DataBitsComboBoxItemStateChanged private void StopBitsComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_StopBitsComboBoxItemStateChanged CloseComPort(); if (evt.getStateChange() == ItemEvent.SELECTED){ if (StopBitsComboBox.getSelectedIndex() >= 1){ if (StopBitsComboBox.getSelectedIndex() == 1){ StopBits = 1; } if (StopBitsComboBox.getSelectedIndex() == 2){ StopBits = 3; } if (StopBitsComboBox.getSelectedIndex() == 3){ StopBits = 2; } try { OpenComPort(PortName); MsgLabel.setText(null); } catch (Exception e) { MsgLabel.setText("Failed to Change Stop Bits"); } }else{ //MsgLabel.setText(null); } } }//GEN-LAST:event_StopBitsComboBoxItemStateChanged private void ParityComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_ParityComboBoxItemStateChanged CloseComPort(); if (evt.getStateChange() == ItemEvent.SELECTED){ if (ParityComboBox.getSelectedIndex() >= 1){ if (ParityComboBox.getSelectedIndex() == 1){ Parity = 2; } if (ParityComboBox.getSelectedIndex() == 2){ Parity = 3; } if (ParityComboBox.getSelectedIndex() == 3){ Parity = 0; } if (ParityComboBox.getSelectedIndex() == 4){ Parity =1; } if (ParityComboBox.getSelectedIndex() == 5){ Parity = 4; } try { OpenComPort(PortName); MsgLabel.setText(""); } catch (Exception e) { MsgLabel.setText("Failed to Change Parity"); } }else{ //MsgLabel.setText(null); } } }//GEN-LAST:event_ParityComboBoxItemStateChanged private void HandShakeComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_HandShakeComboBoxItemStateChanged CloseComPort(); if (evt.getStateChange() == ItemEvent.SELECTED){ if (HandShakeComboBox.getSelectedIndex() >= 1){ if (HandShakeComboBox.getSelectedIndex() == 1){ HandShake = 0; } if (HandShakeComboBox.getSelectedIndex() == 2){ HandShake = 1; } if (HandShakeComboBox.getSelectedIndex() == 3){ HandShake = 2; } if (HandShakeComboBox.getSelectedIndex() == 4){ HandShake = 4; } if (HandShakeComboBox.getSelectedIndex() == 5){ HandShake = 8; } try { OpenComPort(PortName); MsgLabel.setText(null); } catch (Exception e) { MsgLabel.setText("Failed to Change HandShake"); } }else{ //MsgLabel.setText(null); } } }//GEN-LAST:event_HandShakeComboBoxItemStateChanged private void TxButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TxButtonActionPerformed if (WriteStrSelectMenuButton.isSelected() == true){ TxString = (WriteTextField.getText()); if (AddNSelectMenuCheckBox.isSelected() == true){ TxString += ("\n"); } if (AddRSelectMenuCheckBox.isSelected() == true){ TxString += ("\r"); } WriteStr(TxString); } if (WriteBinSelectMenuButton.isSelected() == true){ TxData = Integer.parseInt(WriteTextField.getText(), 2); Write(TxData); } if (WriteOctSelectMenuButton.isSelected() == true){ TxData = Integer.parseInt(WriteTextField.getText(), 8); Write(TxData); } if (WriteDecSelectMenuButton.isSelected() == true){ TxData = Integer.parseInt(WriteTextField.getText(), 10); Write(TxData); } if (WriteHexSelectMenuButton.isSelected() == true){ TxData = Integer.parseInt(WriteTextField.getText(), 16); Write(TxData); } }//GEN-LAST:event_TxButtonActionPerformed private void WriteTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_WriteTextFieldKeyReleased if (PressAnyKeySelectMenuButton.isSelected() == true){ TxString = (WriteTextField.getText()); if (AddNSelectMenuCheckBox.isSelected() == true){ TxString += ("\n"); } if (AddRSelectMenuCheckBox.isSelected() == true){ TxString += ("\r"); } WriteStr(TxString); WriteTextField.setText(null); } if ((WriteDecSelectMenuButton.isSelected() == true) && (PressedKeyCheck == true) && (WriteTextField.getText() != null)){ ConvertedInt = Integer.valueOf(WriteTextField.getText()); if (ConvertedInt > 255){ JOptionPane.showMessageDialog(rootPane, "Can't Transmitt Over a Byte"); } } if ((WriteOctSelectMenuButton.isSelected() == true) && (PressedKeyCheck == true) && (WriteTextField.getText() != null)){ ConvertedInt = Integer.parseInt(WriteTextField.getText(), 8); if (ConvertedInt > 255){ JOptionPane.showMessageDialog(rootPane, "Can't Transmitt Over a Byte"); } } if (WriteHexSelectMenuButton.isSelected() == true){ WriteTextField.setText(WriteTextField.getText().toUpperCase()); } }//GEN-LAST:event_WriteTextFieldKeyReleased private void WriteTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_WriteTextFieldKeyTyped //ここ超大切!! //2進数を送信する設定にされてる時とかに //テキストフィールドに入力される文字列に制限をかけるプログラムだけど //キーが押された時に,8文字だったら入力された文字列の先頭から8文字目までを入力してる //これをもしも,9文字押された時にしちゃうと,キーが押された時点では8文字だから,9文字入力されちゃうww //かと言って,キーが離された時にしちゃうと,キーを長押ししたりした時に面倒だからダメ! //だから,8文字入った時点で8文字に制限してあげる //16進数でも同じね! //8進数と10進数では文字列をint型に変換して,255よりも大きかったらエラー吐かせるww //でも,キーが離された時にしないと1桁多くなっちゃうからねー //キーコード //0=48 //1=49 //2=50 //3=51 //4=52 //5=53 //6=54 //7=55 //8=56 //9=57 //A=65 //B=66 //C=67 //D=68 //E=69 //F=70 //a=97 //b=98 //c=99 //d=100 //e=101 //f=102 if (evt.getKeyChar() == KeyEvent.VK_ESCAPE){ WriteTextField.setText(null); } if (PressReturnKeySelectMenuButton.isSelected() == true){ if (evt.getKeyChar()== KeyEvent.VK_ENTER){ TxButton.doClick(); } } //ここだけに書いとくと,一回押しただけじゃ何か送信されないみたいwww //だからキーがリリースされた時にも同じことさせてる if (PressAnyKeySelectMenuButton.isSelected() == true){ TxString = (WriteTextField.getText()); if (AddNSelectMenuCheckBox.isSelected() == true){ TxString += ("\n"); } if (AddRSelectMenuCheckBox.isSelected() == true){ TxString += ("\r"); } WriteStr(TxString); WriteTextField.setText(null); } if (WriteBinSelectMenuButton.isSelected() == true){ if ((evt.getKeyChar() == '0') || (evt.getKeyChar() == '1')){ if (WriteTextField.getText().length() >= 8){ WriteTextField.setText(WriteTextField.getText().substring(0, 7)); } }else{ evt.consume(); } } if (WriteDecSelectMenuButton.isSelected() == true){ PressedKeyChar = evt.getKeyChar(); if ((PressedKeyChar >= 48) && (PressedKeyChar <= 57)){//0〜9だったら PressedKeyCheck = true; if (WriteTextField.getText().length() >= 3){ WriteTextField.setText(WriteTextField.getText().substring(0, 2)); } }else{ evt.consume(); PressedKeyCheck = false; } } if (WriteOctSelectMenuButton.isSelected() == true){ PressedKeyChar = evt.getKeyChar(); if ((PressedKeyChar >= 48) && (PressedKeyChar <= 56)){ PressedKeyCheck = true; if (WriteTextField.getText().length() >= 3){ WriteTextField.setText(WriteTextField.getText().substring(0, 2)); } }else{ evt.consume(); PressedKeyCheck = false; } } if (WriteHexSelectMenuButton.isSelected() == true){ PressedKeyChar = evt.getKeyChar(); if (((PressedKeyChar >= 48) && (PressedKeyChar <= 57)) || ((PressedKeyChar >= 65) && (PressedKeyChar <= 70)) || ((PressedKeyChar >= 97) && (PressedKeyChar <= 102))){ WriteTextField.setText(WriteTextField.getText().toUpperCase()); PressedKeyCheck = true; if (WriteTextField.getText().length() >= 2){ WriteTextField.setText(WriteTextField.getText().substring(0, 1)); } }else{ evt.consume(); PressedKeyCheck = false; } } }//GEN-LAST:event_WriteTextFieldKeyTyped private void WriteStrSelectMenuButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_WriteStrSelectMenuButtonStateChanged if (WriteStrSelectMenuButton.isSelected() == true){ AddRSelectMenuCheckBox.setEnabled(true); AddNSelectMenuCheckBox.setEnabled(true); PressAnyKeySelectMenuButton.setEnabled(true); }else{ AddRSelectMenuCheckBox.setEnabled(false); AddNSelectMenuCheckBox.setEnabled(false); PressAnyKeySelectMenuButton.setEnabled(false); PressReturnKeySelectMenuButton.setSelected(true); } }//GEN-LAST:event_WriteStrSelectMenuButtonStateChanged public class SerialPortlistener implements SerialPortEventListener{ @Override public void serialEvent(SerialPortEvent e){ if (e.getEventType() == SerialPortEvent.DATA_AVAILABLE){ if (ReadSelectMenuButton.isSelected() == true){ Read(); } if (ReadLineSelectMenuButton.isSelected() == true){ ReadLine(); } } } } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { new MaceWindu().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JCheckBoxMenuItem AddFormatRepresentationSelectMenuCheckBox; private javax.swing.JCheckBoxMenuItem AddNSelectMenuCheckBox; private javax.swing.JCheckBoxMenuItem AddRSelectMenuCheckBox; public javax.swing.JComboBox BaudRateComboBox; private javax.swing.ButtonGroup ChangeLookAndFeelMenuButtonGroup; public javax.swing.JComboBox DataBitsComboBox; private javax.swing.JCheckBoxMenuItem Fix8BitSelectMenuCheckBox; private javax.swing.JCheckBoxMenuItem FixCapitalLetterSelectMenuCheckBox; public javax.swing.JComboBox HandShakeComboBox; private javax.swing.JRadioButtonMenuItem LafGTKSelectMenuButton; private javax.swing.JRadioButtonMenuItem LafMacSelectMenuButton; private javax.swing.JRadioButtonMenuItem LafMetalSelectMenuButton; private javax.swing.JRadioButtonMenuItem LafMotifSelectMenuButton; private javax.swing.JRadioButtonMenuItem LafNimbusSelectMenuButton; private javax.swing.JRadioButtonMenuItem LafWindowsClassicSelectMenuButton; private javax.swing.JRadioButtonMenuItem LafWindowsSelectMenuButton; public javax.swing.JLabel MsgLabel; public javax.swing.JComboBox ParityComboBox; public javax.swing.JComboBox PortsComboBox; private javax.swing.JRadioButtonMenuItem PressAnyKeySelectMenuButton; private javax.swing.JRadioButtonMenuItem PressReturnKeySelectMenuButton; private javax.swing.JRadioButtonMenuItem ReadBinSelectMenuButton; private javax.swing.JRadioButtonMenuItem ReadDecSelectMenuButton; private javax.swing.JMenu ReadFormatSelectMenu; private javax.swing.ButtonGroup ReadFormatSelectMenuButtonGroup; private javax.swing.JRadioButtonMenuItem ReadHexSelectMenuButton; private javax.swing.JRadioButtonMenuItem ReadLineSelectMenuButton; private javax.swing.ButtonGroup ReadModeSelectMenuButtonGroup; private javax.swing.JRadioButtonMenuItem ReadOctSelectMenuButton; private javax.swing.JMenu ReadPreferenceMenu; private javax.swing.JRadioButtonMenuItem ReadSelectMenuButton; private javax.swing.JRadioButtonMenuItem ReadStrSelectMenuButton; private javax.swing.JTextArea RecievedDataTextArea; public javax.swing.JComboBox StopBitsComboBox; private javax.swing.JButton TxButton; private javax.swing.JRadioButtonMenuItem WriteBinSelectMenuButton; private javax.swing.JRadioButtonMenuItem WriteDecSelectMenuButton; private javax.swing.ButtonGroup WriteFormatSelectMenuButtonGroup; private javax.swing.JRadioButtonMenuItem WriteHexSelectMenuButton; private javax.swing.JRadioButtonMenuItem WriteOctSelectMenuButton; private javax.swing.JMenu WritePreferenceMenu; private javax.swing.JRadioButtonMenuItem WriteStrSelectMenuButton; private javax.swing.JTextField WriteTextField; private javax.swing.ButtonGroup WriteTriggerSelectMenuButtonGroup; private javax.swing.JMenu jMenu1; private javax.swing.JMenu jMenu2; private javax.swing.JMenu jMenu3; private javax.swing.JMenu jMenu4; private javax.swing.JMenu jMenu5; private javax.swing.JMenu jMenu6; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane1; // End of variables declaration//GEN-END:variables }