2013-04-21 67 views
0

我正在寫代碼,它將接受來自arduino的字節值,將它們存儲爲數組,然後執行一些數學計算,然後將值發送回arduino。現在我可以向Arduino發送127個值,並返回127個值,但是它們是字符串類型,並且任何使用Integer類轉換這些字符串的嘗試都會導致程序掛起。我相信緩衝區有時會提供空字符串,而parseInt()不知道該怎麼做。有沒有人有什麼建議?我非常喜歡java的初學者,並且願意提供更好的解決方案。在Java中將輸入流轉換爲數組的最佳方式是什麼?

這裏是我的代碼:

package GridMap; 

import gnu.io.CommPort; 
import gnu.io.CommPortIdentifier; 
import gnu.io.SerialPort; 
import gnu.io.SerialPortEvent; 
import gnu.io.SerialPortEventListener; 

import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

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

OutputStream out; 
byte array[] = new byte[10]; 
byte c; 

public SerialWriter(OutputStream out, byte[] in) { 
    this.out = out; 
    array = in; 
} 

public void run() { 

     try { 
      int index = 0; 
      c = array[index]; 
      while ((c) > -1) { 
       this.out.write(c); 
       System.out.println("sent " + c); 
       if (index == 64){ 
        Thread.sleep(2); 
       } 
       index++; 
       c = array[index]; 
      } 
      TwoWaySerialComm.recieve(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      System.exit(-1); 
     } 


} 
} 

public class SerialReader implements Runnable { 
static byte[] output = new byte[128]; 
private InputStream in; 
private int[] buffer = new int[11]; 
static SerialPort thisSerialPort; 
static OutputStream thisOut; 
static String total = new String("333"); 

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

    for (byte i = 0; i < 127; i++) { 
      output[i] = i; 
     } 
    output[127] = - 1; 
} 
public void run() 
    { 
     byte[] buffer = new byte[1024]; 
     int len = -1; 
     int index = 0; 
     int value; 
     try 
     { 
      Thread.sleep(200); 

      while ((len = this.in.read(buffer)) > -1 && index < 200) 
      { 


       String string = new String(buffer, 0, len); 
       //value = Integer.getInteger(string, len); 
       // System.out.print(value); 
       //System.out.println("buffer" + value); 
       System.out.print(string); 
       index++; 

      } 


      TwoWaySerialComm.send(output); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
public static int byteArrayToInt(byte[] b) 
{ 
return b[3] & 0xFF | 
     (b[2] & 0xFF) << 8 | 
     (b[1] & 0xFF) << 16 | 
     (b[0] & 0xFF) << 24; 
} 


} 
public class TwoWaySerialComm { 

static SerialPort serialPort; 
static OutputStream out = null; 
static InputStream in; 
static Thread receiveThread; 
static Thread sendThread; 
static byte[] output = new byte[11]; 


public TwoWaySerialComm() { 
    super(); 
} 

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) commPort; 
      serialPort.setSerialPortParams(114400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); 




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

} 

static void send(byte[] output) { 
    try { 
     out = serialPort.getOutputStream(); 
     sendThread = new Thread(new SerialWriter(out, output)); 
     sendThread.start(); 
     //sendThread.join(); 
    } catch (Exception e) { 
     System.out.println("Port Not Avaialable (send) "); 
    } 
} 

static void recieve(){ 
    try { 
    in = serialPort.getInputStream(); 
    receiveThread = new Thread(new SerialReader(in)); 
    receiveThread.start(); 
    receiveThread.join(); 
    } catch (Exception e) { 

    } 
} 

public static void main(String[] args) { 
    try { 

     (new TwoWaySerialComm()).connect("COM3"); 

     for (byte i = 0; i < 10; i++) { 
      output[i] = i; 
     } 
     output[10] = -1; 
     send(output); 



    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

public static SerialPort returnSerialPort(){ 
    return serialPort; 
} 
} 
+0

對不起,有沒有什麼辦法可以讓你更清楚! – NINCOMPOOP 2013-04-21 17:49:50

回答

0

如果你想從你的流得到整型,它是一個BuffereInputStream和使用更方便read()方法返回一個int - > no轉換需要。在SerialReader類 補充一點:

Thread.sleep(200); 
    BufferedInputStream input = new BufferedInputStream(in); 

    while ((value = input.read()) != 1 && index < 200) 
    { 
    compute(value); 
    index++; 
    } 
    input.close(); 

不要忘記關閉(),您的流時,您已經閱讀所有的數據。這在編寫時更重要,因爲如果不關閉(),則不會寫入所有數據(除非在之前使用flush())。

0

我並沒有完全得到說明。
但問題標題的答案在這裏。
Convert InputStream to byte array in Java

+0

編輯答案。因爲我發現之前詢問過的同一個問題。 – 2013-04-21 17:50:25

+0

這應該是一個評論,我想! – NINCOMPOOP 2013-04-21 17:51:02

+0

對不起,下次不會發生。 – 2013-04-21 17:52:21

0

從corsiKa的回答Determine if a String is an Integer in Java,您可以檢查一個字符串是這樣一個有效的int:

public static boolean isInteger(String s) { 
    try { 
     Integer.parseInt(s); 
    } catch(NumberFormatException e) { 
     return false; 
    } 
    // only got here if we didn't return false 
    return true; 
} 
相關問題