【发布时间】:2014-12-10 16:37:40
【问题描述】:
我必须获取一些电脑通过 USB 连接的设备接收的数据。 我连接到串行端口(使用 javacomm 库)并每两分钟接收一次发送设备的数据。这些数据,将它们存储在一个 byte[] 中并将其显示给 InputStream,如下所示:
try { int count = inputStream.available(); byte[] bs = new byte[count]; System.out.print(inputStream.read(bs));//also with (bs,0,count)..
} catch (IOException e) { }
但这总是显示 0(零)。我尝试过使用其他方法,例如直接打印 de byte[] 并且输出是“ [B@e35bb7” 我不知道这是什么...
try { System.out.println(bs); } catch (IOException e) { }
我尝试将其转换为字符串:
try { String me=bs.toString(); String txtohex=String.format("%04x", new BigInteger(1, me.getBytes("UTF-8"))); System.out.println(txtohex);//returns 5b424031633765326461
} catch (IOException e) { }
或:
try {
StringBuilder sb = new StringBuilder();
for (byte b : bs){
sb.append(String.format("%02X ", b));
}
System.out.println(sb.toString());//returns C4 3C 00 A0 00 00 00 00 00
} catch (IOException e) { }
但这是错误的,因为我正在转换字符串 [B@e35bb7.从设备我必须得到一个很长的二进制或十六进制字符串,而我得到的不是。我还能做些什么来准确接收设备发送给我的信息?
【问题讨论】:
-
JavaDoc -> 如果方法 available() 返回 0,应该在子类中重写,我该怎么做?
标签: java serial-port byte inputstream