2

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

dsolimano
  • 8,870
  • 3
  • 48
  • 63
  • you are printing out the pointer to the array, not the contents; you need this http://stackoverflow.com/questions/9655181/convert-from-byte-array-to-hex-string-in-java – guido Nov 04 '14 at 01:32
  • Do you think its sending the proper Hex values to the port? – Marin Smolcic Nov 04 '14 at 01:43
  • i suppose so, anyway seeing the proper output you get in the console would be a good start to know it – guido Nov 04 '14 at 01:47
  • No, you are probably writing binary values, rather than their printable representation in hex. – Chris Stratton Nov 04 '14 at 02:03
  • Y'all are correct, I'm reading the correct output because when I enter a bad code it spits out error code " 000206070000" which my code now matches – Marin Smolcic Nov 04 '14 at 02:05

0 Answers0