0

I need to send some commands to my microcontroller. Iam using the RXTX library. I can connect to my microcontroller but i have problems to convert my commands to right bytes.

example: i want to send 0x80 hex value.

if i convert 128 integer (0x80) to bytes

   byte a = (byte) 128;

i get -128

the range from java bytes is -128 - 127

but i need to send 128 as a command. How can i convert it?

also i do have other commands to send.

80
20 F2 40 F8 3F 
FA 21 40 

one of them is 40 bits.

I write to the outputstream, that can accept byte[] or int.

...
  static OutputStream out;
out.write(byte[]);
out.write(int); 
..

But int is out of range with 40bit. How can i parse a 40bit Integer to a byte?

This Question helped me

Community
  • 1
  • 1
Khan
  • 1,418
  • 1
  • 25
  • 49

3 Answers3

1

0x80 is still 0x80 regardless of what Java's signed-byte type system shows it as.

Just send it.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • hm okay. My microcontroller is not responding to my commands, i thought maybe this is the problem :S – Khan May 19 '17 at 09:53
1

I have used RXTX liberary a long back i don't think so it matters. just send string.getBytes()

Swapnil
  • 29
  • 3
0

You can use

Byte.parseByte("a", 16);

but this will work only for values up to 127, values higher then that will need to cast to byte, due to signed/unsigned issues so i recommend to transfer it to an int and then cast it to byte

(byte) (Integer.parseInt("ef",16) & 0xff);