2016-12-13 65 views
0

我想通過使用RxTx庫的串行端口向我的Arduino板發送四個不同的命令,以便通過串口使用RxTx庫進行顏色傳感器,同時我也應該從設備獲得響應。Java使用RxTx庫進行串行通信

命令是01,0x80,01,02這些是我希望發送的命令。

  1. 如果我發送01到板子,設備應該響應板子的版本,即2.3TCS3200 EVM版本。
  2. 如果我發送0x80到板子,arduino應該停止發送數據。
  3. 如果我再次發送01給板子,設備應該響應TCS3200 EVM校準完成
  4. 如果我發送02,那麼設備應該響應ASCII值RGBÿÿÿ。

我作爲2.3TCS3200 EVM版輸出發送01,但在這裏,我怎麼能命令的其餘部分向委員會,並得到回覆,任何幫助,將不勝感激?

在此先感謝

+0

如果你做這樣的事情: – thepaulo

回答

0

如果你做這樣的事情:

byte[] data = {0x01, (byte) 0x80, 0x02, 0x01}; 
serial.send(data); 

什麼,他們會發生什麼?

我不知道Arduino如何收到數據。但是你也可以用循環發送多個數據。

乾杯

0
Here is the code.I am still getting the output as 2.3TCS3200 EVM Version. 
import gnu.io.CommPort; 
import gnu.io.CommPortIdentifier; 
import gnu.io.SerialPort; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
public class JavaColorSensorDemo { 

public JavaColorSensorDemo() 
{ 
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 
{ 
    System.out.println("Connect 1/2"); 
    CommPort commPort = portIdentifier.open(this.getClass().getName(),9600); 
if (commPort instanceof SerialPort) 
    { 
     System.out.println("Connect 2/2"); 
     SerialPort serialPort = (SerialPort) commPort; 
     System.out.println("BaudRate: " + serialPort.getBaudRate()); 
     System.out.println("DataBIts: " + serialPort.getDataBits()); 
     System.out.println("StopBits: " + serialPort.getStopBits()); 
     System.out.println("Parity: " + serialPort.getParity()); 
        serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_ODD); 
     serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN); 
        System.out.println("BaudRate: " + serialPort.getBaudRate()); 
       System.out.println("DataBIts: " + serialPort.getDataBits()); 
        System.out.println("StopBits: " + serialPort.getStopBits()); 
       System.out.println("Parity: " + serialPort.getParity()); 





     InputStream in = serialPort.getInputStream(); 
     OutputStream 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."); 
    } 
    } 
    } 
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.println("Received a signal."); 
      System.out.print(new String(buffer,0,len)); 
     } 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    }    
    } 
    } 
     public static class SerialWriter implements Runnable 
    { 
     OutputStream out; 

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

     public void run() 
     { 
     try 
     { 

     byte[] data = {0x01,(byte)0x80,0x02,0x01}; 



     while (true) 
     { 
      this.out.write(data); 
      this.out.flush(); 
      Thread.sleep(1000); 
       }     
     } 
    catch (IOException | InterruptedException e) 
    { 
     e.printStackTrace(); 
     }    
    } 
    } 
    public static void main (String[] args) 
    { 
    try 
    { 
    (new JavaColorSensorDemo()).connect("COM66"); 
    } 
    catch (Exception e) 
    { 
    // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } 
     } 
     } 
相關問題