Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to send hexadecimal number in java serial communication
How to send hexadecimal number in java serial communication
When doing serial communication, the output is basically read through io stream. Then the OutputStream is used when sending data in java development, and the parameters of its write () are byte array and int integer. If byte array is used to send, it can be written directly as out.write (" 1234 "). getbytes())。 In this way, the data read by the single chip microcomputer is 3 1 32 33 34. However, when sending through the serial port, the binary string of 16 is first converted into a byte array, and then the sending is read. Use: out.write (hexadecimal string 2 bytes ("1234")); Then you still read 1234. The method of converting 16 binary string into byte array is as follows:

public static byte[]hex String 2 bytes(String src){

if(null = = src | | 0 = = src . length()){

Returns null

}

Byte[] ret = new byte [src.length ()/2];

byte[]tmp = src . getbytes();

for(int I = 0; I < (tmp.length/2); i++) {

ret[i] = uniteBytes(tmp[i * 2],tmp[I * 2+ 1]);

}

Return to ret

}

It is suggested to use open source Rxtx when developing serial port with java. Efficiency and utilization rate should be excellent. Using sun's open source Comm, I feel that it is not very convenient. There are open source instances on rxtx. Can be modified and used according to their own needs.