Java 如何将数据发送到外部设备
我有一个串行设备通过com端口连接到我的计算机。我想将十六进制数据发送到此设备,向该设备发送数据的最佳方法是什么 例如,我有这个数据-Java 如何将数据发送到外部设备,java,serial-port,hexdump,Java,Serial Port,Hexdump,我有一个串行设备通过com端口连接到我的计算机。我想将十六进制数据发送到此设备,向该设备发送数据的最佳方法是什么 例如,我有这个数据- String hexStr = "02303033434333d3037313131303131323639393130333131303139033f"; 我现在正在做的是- byte[] unsignedByte = HexUtil.convertToBytes(hexStr); serialPort.getOutputStream().writ
String hexStr = "02303033434333d3037313131303131323639393130333131303139033f";
我现在正在做的是-
byte[] unsignedByte = HexUtil.convertToBytes(hexStr);
serialPort.getOutputStream().write(unsignedByte);
我面临的问题是-串行设备没有响应?
当我通过软件发送相同的十六进制字符串时,终端正在响应。我做错什么了吗
这是代码的一部分-
mOutputToPort.write(unsignedByte);
logger.info("Byte Sent");
mOutputToPort.flush();
logger.info("Waiting for response");
Thread.sleep(10000);
byte mBytesIn [] = new byte[2000];
int num = mInputFromPort.read(mBytesIn);
System.out.println("Number of bytes read -"+ num);
for(byte b : mBytesIn){
System.out.print(b);
System.out.print(" ");
}
System.out.println();
嗨,伙计们,我终于自己找到了正确的方法。 我没有以正确的格式发送数据。我是在用其他软件监视串行数据交换时发现的 将数据发送到串行设备的最佳方法是-
ASCII数据-->十六进制字符串-->无符号字节您可能希望在写入操作后
刷新()输出流。如果这有帮助,请尝试:
byte[] unsignedByte = HexUtil.convertToBytes(hexStr);
serialPort.getOutputStream().write(unsignedByte);
serialPort.getOutputStream().flush(); // <- new line added here
byte[]unsignedByte=HexUtil.convertToBytes(hexStr);
serialPort.getOutputStream().write(unsignedByte);
serialPort.getOutputStream().flush();//嗨,伙计们,我终于自己找到了正确的方法。我没有以正确的格式发送数据。我是在用其他软件监视串行数据交换时发现的。将数据发送到串行设备的最佳方式是-ASCII数据-->十六进制字符串-->无符号字节。请在代码中发送ASCII数据-->十六进制字符串-->无符号字节是什么意思?据我所知,您的旧代码正在这样做(ascii字符串到十六进制字节)