I am trying write data into my serial port using java. For now I can only populate the data into a table form. Can I know are there any ways to write short data into the serial port.
This is my code:
static short[] bytearray = {0x02, 0x08, 0x16, 0x00, 0x00, 0x33, 0xC6 , 0x1B};
outputStream = serialPort.getOutputStream();
outputStream.writeShort(bytearray);
outputStream.flush();
I cannot use the write short at the outputstream write method. Can anyone help me with this. Thank you.
As I understand it the
writeShort
takes as a parameter an integer and sends two shorts to the port. So all you have to do is to convert two shorts into an int and call the method with it.Note that this code hasn't been tested - use as an inspiration only! Especially the
bytearray[i + 1]
bit can throw index out of bounds!