I want to send four different commands to my Arduino board for color sensor through serial port using RxTx library and also I should get response from the device.
The command is 01, 0x80, 01,02 these are the commands which I wish to send.
I got output as 2.3TCS3200 EVM Version by sending 01
, but here how can I send the rest of the commands to the board and get reply, Any help would be appreciated?
Thanks in advance
If you do something like that:
byte[] data = {0x01, (byte) 0x80, 0x02, 0x01};
serial.send(data);
What's they happens?
I don't know how Arduino received data. But you can also send multiple data with a loop.
Cheers
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();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.