我正在尝试使用串行端口从秤上读取重量。 但我认为我正在接收数据,并且我不知道如何在没有
的情况下打印它我的系列 Activity :
public void serialEvent(SerialPortEvent event){
switch(event.getEventType()) {
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
outputBufferEmpty(event);
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20];
try {
int numBytes = inputStream.read(readBuffer);
String str = new String (readBuffer, "UTF-8");
ok+=str;
//System.out.print(str);
System.out.println();
System.out.print(ok.replaceAll("\\s+", ""));
//System.out.print(str.replace("\n", "").replace("\r", ""));
} catch (IOException ex) {}
break;
}
}
我都试过了。 但这是我现在的输出:
0
0 0
0 0 0
0 0 0 .
0 0 0 . 0
0 0 0 . 0 5
0 0 0 . 0 5 0
0 0 0 . 0 5 0
我想要这个: 000.050公斤
这是我的秤的协议(protocol):
有关更多信息,我的体重秤是:Mettler Toledo VIVA 或 OHAUS RV 系列
最佳答案
你为什么不尝试直接使其加倍?
import java.nio.ByteBuffer;
public static double toDouble(byte[] bytes) {
return ByteBuffer.wrap(bytes).getDouble();
}
关于java - 从秤串口获取数据,无需 <CR>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22757792/