通过串口上传文件
有什么方法可以通过串口将文件上传到嵌入式设备吗?我使用 RXTX,但我认为我只从文件发送数据,而不上传此文件。谢谢你的建议。
public static void main(String[] args) throws PortInUseException, UnsupportedCommOperationException
{
java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
while (portEnum.hasMoreElements())
{
CommPortIdentifier portIdentifier = portEnum.nextElement();
if (portIdentifier.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
if (portIdentifier.isCurrentlyOwned())
{
System.out.println("Port is curently used");
System.exit(0);
}
else
{
CommPort commPort = portIdentifier.open("Zkouska", 2000);
if (commPort instanceof SerialPort)
{
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(57600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
try
{
OutputStream out = serialPort.getOutputStream();
File file = new File("C:/data/java/RXTX/01_Hello/SerialUploader/uploaddemo.dat");
byte[] content = new byte[(int)file.length()];
FileInputStream fin = new FileInputStream(file);
fin.read(content);
out.write(content);
out.flush();
out.close();
commPort.close();
System.out.println("Tady");
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
}
}
Is there any way how to upload file to embeded device throw Serial port? Im using RXTX, but I thing that I allready only sent data from file, not upload this file. Thanks for advice.
public static void main(String[] args) throws PortInUseException, UnsupportedCommOperationException
{
java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
while (portEnum.hasMoreElements())
{
CommPortIdentifier portIdentifier = portEnum.nextElement();
if (portIdentifier.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
if (portIdentifier.isCurrentlyOwned())
{
System.out.println("Port is curently used");
System.exit(0);
}
else
{
CommPort commPort = portIdentifier.open("Zkouska", 2000);
if (commPort instanceof SerialPort)
{
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(57600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
try
{
OutputStream out = serialPort.getOutputStream();
File file = new File("C:/data/java/RXTX/01_Hello/SerialUploader/uploaddemo.dat");
byte[] content = new byte[(int)file.length()];
FileInputStream fin = new FileInputStream(file);
fin.read(content);
out.write(content);
out.flush();
out.close();
commPort.close();
System.out.println("Tady");
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能应该使用文件传输协议进行上传,例如
看一下这里的 java 初学者:X-modem协议的Java实现
You should probably upload with a filetransfer protocol like
Have a look here for starters in java: Implementation of X-modem protocol in Java