做串口通讯
时候基本都
通过io流读取、输出


java开发
发送数据
时候使用OutputStream,而其write()
参数
字节数组、int整形

使用字节数组发送
时候
通常
直接写成out.write("1234".getbytes())
样
来单片机读
数据则
31 32 33 34


串口发送
时候先把16进制字符串转化
byte数组
发送出来
则
发送
读取





使用:out.write(HexString2Bytes("1234"));
读取
还
1234
16进制字符串转化
byte数组
方法
:
public static byte[] HexString2Bytes(String src) {
if (null == src || 0 == src.length()) {
return 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 ret;
}
public static byte uniteBytes(byte src0, byte src1) {
byte _b0 = Byte.decode("0x" + new String(new byte[] {src0})).byteValue();
_b0 = (byte) (_b0 << 4);
byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 })).byteValue();
byte ret = (byte) (_b0 ^ _b1);
return ret;
}