2013-02-22 29 views
5

私はjavaのシリアルポートにデータを送受信する簡単なプログラムを作った。私はループバックテスト(Rx to Tx)とシリアルポートデバイスを接続します。それはうまく動作します。私は16進データをシリアルポートに送って受け取ることはできず、シリアルポートを受け取ることはできません。私のデバイスにはFT232BLチップが使われています。シリアルポートデバイスに16進データを送受信するために必要なDLLやライブラリがあります。私のコードは以下の通りです。文字列を16進数と16進数に変換する方法は?

六角へ
enter code here 
package x.rayimagecapture; 

import gnu.io.CommPort; 
import gnu.io.CommPortIdentifier; 
import gnu.io.SerialPort; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.Timer; 

public class TwoWaySerialComm extends JPanel implements ActionListener { 

    OutputStream out; 
    private javax.swing.JButton btn; 
    private javax.swing.JScrollPane jScrollPane1; 
    public static javax.swing.JLabel jLabel1; 
    public static javax.swing.JTextField textField; 
    public static boolean flag = false; 
    public static int count = 50; 
// TimerWindow tmr=new TimerWindow(); 
    public static Timer tmr = new Timer(1000, new ActionListener() { 
     public void actionPerformed(ActionEvent evt) { 
         count++; 
      jLabel1.setText("" + count); 
     } 
    }); 

    public TwoWaySerialComm() { 
     super(); 
     textField = new javax.swing.JTextField(); 
     btn = new javax.swing.JButton(); 
     jScrollPane1 = new javax.swing.JScrollPane(); 
     jLabel1 = new javax.swing.JLabel(); 
     textField.setText(""); // NOI18N 
//  tmr.setDelay(0); 

     btn.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N 
     btn.setText("Send"); // NOI18N 
     btn.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       serialwrite(); 
      } 
     }); 

//  textArea.setColumns(20); 
//  textArea.setRows(20); 
//  textArea.setTabSize(0); 
     jScrollPane1.setViewportView(jLabel1); 

     javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(this); 
     this.setLayout(panelLayout); 
     panelLayout.setHorizontalGroup(
       panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(panelLayout.createSequentialGroup() 
       .addComponent(textField, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(btn, javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE) 
       .addContainerGap()) 
       .addComponent(jScrollPane1)); 
     panelLayout.setVerticalGroup(
       panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(panelLayout.createSequentialGroup() 
       .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(textField, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(btn, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE))); 
    } 

    void connect(String portName) throws Exception { 
     CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName); 
     if (portIdentifier.isCurrentlyOwned()) { 
      System.out.println("Error: Port is currently in use"); 
     } else { 
      CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000); 

      if (commPort instanceof SerialPort) { 
       SerialPort serialPort = (SerialPort) commPort; 
       serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); 
       serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE); 
       InputStream in = serialPort.getInputStream(); 
       out = serialPort.getOutputStream(); 

       (new Thread(new SerialReader(in))).start(); 
//    (new Thread(new SerialWriter(out))).start(); 

      } else { 
       System.out.println("Error: Only serial ports are handled by this example."); 
      } 
     } 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     serialwrite(); 
    } 

    public void serialwrite() { 
     try { 
      this.out.write(textField.getText().toString().getBytes()); 
      count = 00; 
//   tmr.start(); 
      tmr.start(); 
      this.out.flush(); 
     } catch (IOException ex) { 
      Logger.getLogger(TwoWaySerialComm.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    /** 
    * 
    */ 
    public static class SerialReader implements Runnable { 

     InputStream in; 

     public SerialReader(InputStream in) { 
      this.in = in; 
     } 

     public void run() { 
      byte[] buffer = new byte[1024]; 
      int len = -1; 
      try { 
       while ((len = this.in.read(buffer)) != -1) { 
//     textArea.append(" " + new String(buffer, 0, len)); 
        String str = new String(buffer, 0, len); 
        System.out.print(str); 
        if (!str.isEmpty()) { 
//      tmr.stop(); 
        } 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    /** 
    * 
    */ 
    public static class SerialWriter implements Runnable { 

     OutputStream out; 

     public SerialWriter(OutputStream out) { 
      this.out = out; 
     } 

     public void run() { 
      try { 
       int c = 0; 
       while ((c = System.in.read()) != -1) { 
//     this.out.write(c); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    public static void main(String[] args) { 
     TwoWaySerialComm tws = new TwoWaySerialComm(); 
     JFrame frame = new JFrame(); 
     frame.setSize(400, 300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(tws); 
     try { 
      tws.connect("COM11"); 
     } catch (Exception ex) { 
      Logger.getLogger(TwoWaySerialComm.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     frame.setVisible(true); 
    } 
} 

答えて

20

StringString

Integer.decode(hexString); 

ヘキサン:

進に 文字列の
Integer.toHexString(integer); 
+2

'toHexString(I)'クラスの 'STRING'方法ではない文字列に

16進数( "0X" が進数に復号するための文字列に(接頭辞)を添加しなければなりません)私はあなたが 'Integer'を意味したと思う。 – raffian

8

Integer.decode("0x4d2"); // output is 1234 

Integer.toHexString(1234); // output is 4d2 
+0

int limitより大きいARGB形式の解はありますか? 'Integer.decode(0xffff6600)'がNumberFormatExceptionをスローする不正なint: "0xffff6600" '。 mCanvas.drawColor(-39424)が機能するので、-39424に折り返しても問題ありません。より大きい数値を扱うことができないのは単にInteger.decodeです。 @CarlJohn – gotube

+1

@gotube decodeはintではなくStringとして引数を取る。問題はあまりにも大きい数字ではありません。 – Mordechai

関連する問題