So I'm trying to send a few certain hex commands to a com port in Java. I have an example working in C# but I feel like the transmission is failing.
SerialPort serialPort = new SerialPort("COM7");
try {
serialPort.openPort();//Open serial port
serialPort.setParams(SerialPort.BAUDRATE_9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);//Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0);
byte[] setScan = new byte[] {(byte)0x00, (byte)0x05, (byte)0x06, (byte)0x07, (byte)0xc8, (byte)0x00, (byte)0xc8, (byte)0x00, (byte)0x01 };
serialPort.writeBytes(setScan);//Write data to port
byte[] buffer = serialPort.readBytes(5);
System.out.println(buffer);
serialPort.closePort();
}catch (SerialPortException ex) {
System.out.println(ex);
}
It's bothering me because I'm supposed to send those Hex bytes and receive a small hex string back. I just get a variation of "[B@2908ea79" back. I have never done Java before but any advice is appreciated!
EDIT* So now the output works because I wasn't sure as to how to read it. I am now using
System.out.println(javax.xml.bind.DatatypeConverter.printHexBinary(buffer));
For an output command.
Any Idea on how to send the correct hex value or what its sending in general?
byte[] setScan = new byte[9] { 0x00, 0x05, 0x06, 0x07, 0xc8, 0x00, 0xc8, 0x00, 0x01 };
port.Open();
port.Write(setScan, 0, 9);
is how I implement it on C# with hex. Im using
import jssc.SerialPort;
import jssc.SerialPortException;
import jssc.SerialPortList;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.Object;
import javax.xml.bind.DatatypeConverter;
import java.lang.*;
These Libraries! Thanks again y'all :D