0

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();
                    }
                }
            }
        }
    }
}
sehe
  • 374,641
  • 47
  • 450
  • 633

1 Answers1

2

You should probably upload with a filetransfer protocol like

Have a look here for starters in java: Implementation of X-modem protocol in Java

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633