2016-12-13 45 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(); 
     } 
     } 
     } 
相关问题