1

I am making an application that works with serial port. The problem is that the device I am controlling receive unsigned bytes range and as I saw java only accepts signed bytes range.

I have googled how to send, but I only got how to receive unsigned bytes.

Thanks EDIT 2: Fix proposed by @durandal to my code to receive:

 public void serialEvent(SerialPortEvent event) {
        switch (event.getEventType()) {
            case SerialPortEvent.DATA_AVAILABLE: {
                System.out.println("Datos disponibles");
                try {
                    int b;
                int disponibles = input.available();
                byte[] rawData = new byte[disponibles];
                int count = 0;
                while ((b = input.read()) != -1) {
                    if (count == disponibles - 1) {
                        break;
                    }
                    rawData[count] = (byte) b;
                    count++;

                    }
                    serial.serialDataReceived(bytesToHex(rawData), rawData);
                } catch (IOException ex) {
                    Logger.getLogger(PuertoSerie.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            break;
        }
Cako
  • 396
  • 3
  • 15
  • 1
    Try converting as per https://stackoverflow.com/questions/7401550/how-to-convert-int-to-unsigned-byte-and-back, and then send the value you get. – childofsoong Mar 18 '15 at 19:55
  • 1
    You can treat signed bytes as unsigned and vice versa. It's just a matter of being careful. – Louis Wasserman Mar 18 '15 at 19:57
  • @soong I have tried what you say I put in the first post a code that I use to test the receeive and I receive 00. Thanks – Cako Mar 18 '15 at 20:01

2 Answers2

4

A byte is just 8-bits. Java assumes it is signed by default but you can treat it as unsigned if you wish. A common way to handle this is to use an int value which can store 0 to 255.

// from unsigned byte
byte[] bytes = ...
int value = 255;
bytes[0] = (byte) value;

// to unsigned byte
int value2 = bytes[0] & 0xFF;
// value2 == 255
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

You're making things overly complicated over nothing. A byte is a byte, there are no signed/unsigned bytes, only bytes. There is a signed/unsigned interpretation of a byte, but thats an entirely different concept.

You receiving code is broken, it will stop reading when it receives the byte value 0xFF, treating it as end-of-stream:

                byte b;
                int disponibles = input.available();
                byte[] rawData = new byte[disponibles];
                int count = 0;
                while ((b = (byte) input.read()) != -1) {
                    if (count == disponibles - 1) {
                        break;
                    }
                    rawData[count] = b;
                    count++;
                }

The problem is the declaration of "b" as byte (it should be int, you absolutely need the return value of read() as an int!) and the cast of input.read() to byte before checking for the -1 value. You should instead cast the int when you put it into the array, not in the for.

Durandal
  • 19,919
  • 4
  • 36
  • 70
  • I have modified the part you said, but i continue receiving 00 – Cako Mar 18 '15 at 20:24
  • 1
    @Cako From your comment I gather you still have a problem getting it to work as intended, but from the scope of the question I cannot guess what the problem could be. I think you should see the correct hex values provided bytesToHex() works ... maybe extend a little on what problem is left? – Durandal Mar 18 '15 at 20:33
  • No problem. The part I want it is working, the other doesn't matter. Anyway, I thank your help – Cako Mar 18 '15 at 21:39