java - 通过串口上传文件

标签 java file-upload rxtx

有什么方法可以通过串行端口将文件上传到嵌入式设备吗?我使用 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();
                    }
                }
            }
        }
    }
}

最佳答案

您可能应该使用文件传输协议(protocol)上传

看看这里的 java 初学者:Implementation of X-modem protocol in Java

关于java - 通过串口上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7160313/

相关文章:

java - 遍历列表并汇总类型相同的所有金额

PHP ajax使用新数组上传多个文件

java - redis.clients.jedis.exceptions.JedisConnectionException : Could not get a resource from the pool

java - Hibernate 上下文 session

javascript - 在 Chrome 中通过 XHR 上传 HTML5 失败

javascript - 使用 Ruby on Rails 上传静态网站并显示它们

java - 我想在java中使用RxTx通过串口检测设备

arduino - 无法对 ESP8266 进行编程

java - 我想将从串行端口接收到的数据存储在一个字符串变量中,该变量将在另一个类中访问

Java 系统托盘图标