使用Java从串口读取文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/544824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read file from serial port using Java
提问by
I'm begginner in Java, I'm writing ("FLASH").getbytes()
like this to serialport
.
我在Java中begginner,我写("FLASH").getbytes()
像这样serialport
。
After I'll get FLASH_OK
as response, again I've to send file request. After that I'll get response as FILE_OK
then I have read file up to end of the file.
在我得到FLASH_OK
回应后,我再次发送文件请求。之后我会得到响应,因为FILE_OK
我已经阅读了文件直到文件末尾。
I'm not getting how to do this, so please help me.
我不知道如何做到这一点,所以请帮助我。
Thanks for reply.
谢谢您的回复。
回答by VonC
Looks like to need a SerialPortReader
which needs to implement a SerialPortEventListener
看起来需要一个SerialPortReader
需要实现一个SerialPortEventListener
public void serialEvent(SerialPortEvent event)
{
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[40];
try
{
while (inputStream.available() > 0)
{
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
System.out.println();
System.out.println("DTR: " + serialPort.isDTR());
System.out.println("DSR: " + serialPort.isDSR());
System.out.println("CTS: " + serialPort.isCTS());
System.out.println("RTS: " + serialPort.isRTS());
System.out.println();
outputStream.write("ACTIVESYNC".getBytes());
}
catch (IOException e)
{
e.printStackTrace();
}