2017-06-04 73 views
-1

我嘗試接收使用一個InputStream從Arduino的數據,但由於某種原因,我得到這個作爲輸出。的InputStream打印怪異字符

AAA AAA AAA到至AA AAAAA AAAAAA A AAA AAA至AA AAAAA AAAAAA AA AAA

這是我的代碼,用於接收數據:

package midistep; 
import gnu.io.CommPort; 
import gnu.io.CommPortIdentifier; 
import gnu.io.SerialPort; 

import java.io.BufferedReader; 
import java.io.FileDescriptor; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 

public class TwoWaySerialComm 
{ 
    public OutputStream out1; 
    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 = (SerialPort) commPort; 
       serialPort.setSerialPortParams(57600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); 

       InputStream in = serialPort.getInputStream(); 
       OutputStream out = serialPort.getOutputStream(); 
       out1=out; 
       (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."); 
      } 
     }  
    } 

    /** */ 
    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) 
       { 
        System.out.print(new String(buffer,0,len)); 
       } 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      }    
     } 
    } 

    /** */ 
    public static class SerialWriter implements Runnable 
    { 
     public 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 void writetoport(int Steps) { 

     try { 
       out1.write(Steps); 
       out1.flush(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
    } 
    public static void main (String[] args) 
    { 
     try 
     { 
      (new TwoWaySerialComm()).connect("COM3"); 
     } 
     catch (Exception e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

和我的Arduino代碼:

int received = 0; 
void setup() { 
    Serial.begin(9600); 
    pinMode(LED_BUILTIN, OUTPUT); 
} 

void loop() { 
    delay(1000); 
    Serial.println("Hello world"); 
    if (Serial.available() > 0) { 
    received = Serial.read(); 
    if (received > 1000) { 
     int speed = received; 
     String tempstring = String(speed); 
     tempstring.substring(1); 
     speed = tempstring.toInt(); 
     Serial.println("gotit" + speed); 
    } 
    else if (received = 1) { 
     Serial.print("I received something >"); 
     Serial.print(received); 
     Serial.println("<"); 
     digitalWrite(LED_BUILTIN, HIGH); 
     delay(1000); 
     digitalWrite(LED_BUILTIN, LOW); 
    } 
    } 
} 

希望日是是一個簡單的問題。謝謝

+0

Serial.read'()'返回字節。一個字節永遠不會大於1000並得到='1'是不是一個比較。 –

回答

0

你已經設置了錯誤的波特率。您的Java代碼中有

serialPort.setSerialPortParams(57600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); 

但你的Arduino素描你有

Serial.begin(9600); 

的波特率必須一致不然你會傳輸錯誤。

+0

謝謝!這個固定。唯一的一點是,它有很長的空間之初但沒關係!謝謝! – TheTrialBot