Sending bytes w/h negative values through serial?

Hello. For my project I'm trying to send a few negative bytes trough the usb serial port of the board. Sounds simple, right? On a desktop application using JSSC for serial connection, I couldn't get it to properly send bytes w/h negative values. If I send any negative byte it gets received as a byte w/h value 63 no matter what the negative byte actually was. I don't use any unsigned variables to send/store bytes. I don't have much experience with serial communications so sorry for the stupid question. I'm suspecting that it's a problem with JSSC? I don't know so I thought that the best way is to ask this here. I could try to send unsigned bytes and then subtract 128 from any received bytes on arrival, but is there any other solution? I also could send them as ASCII, but I'm looking for speed and efficiency, so that's not a really good solution.

A byte is an unsigned char. Unsigned means has no sign, can't be negative. Use char.

so sorry for the stupid question

The question is only stupid because there is no code to demonstrate how you store a negative value in an unsigned variable (which byte is).

All right here's your lovely code

char rc; //the received char
rc = Serial.read();

and that char always has positive value.

Desktop application (java, don't hate pls):

byte[] bytes;
//...
serial.openPort();
 serial.setParams(SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serial.writeBytes(bytes);

The thing is that in java a byte is a signed type, while in arduno it's unsigned.

I doubt it matters as long both are 8 bits.

I'm not a java person so don't know if the first line below will work.

byte[] bytes = {0x00, 0x01, 0x7F, 0x80, 0xFE, 0xFF);
//...
serial.openPort();
 serial.setParams(SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serial.writeBytes(bytes);

At the Arduino side you should still receive those bytes as they are. How do you know that the received value in the Arduino is 63? Or any value anyway? Your code does not show anything.

sterretje: At the Arduino side you should still receive those bytes as they are. How do you know that the received value in the Arduino is 63? Or any value anyway? Your code does not show anything.

Right after I read a character from the arduino, I send it back to the serial port for debugging. I just found the problem. Usually any received data on the desktop side gets ASCII encoded automatically (because I don't usually receive any binary data). This replaces all negative values with 63 ('?'). There was no problem on the sending/receiving side after all (my code for data receiving and processing is a few hundred lines so that's why I didn't post it). Sorry for annoying you with this thread.

I think your question is back-to-front.

It is not so much how you send data as how the receiving system interprets it.

Java assumes a byte is signed but an Arduino assumes a byte is unsigned. In an Arduino a char is the equivalent of a Java byte.

IIRC in JRuby (which uses JSSC) you can convert a signed byte to an unsigned value with myUnsigned = mySigned && 0xFF I got this from a JRuby program I wrote some time ago so I may be mistaken.

...R