简体   繁体   中英

Read file from serial port using Java

I'm begginner in Java, I'm writing ("FLASH").getbytes() like this to 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.

I'm not getting how to do this, so please help me.

Thanks for reply.

jSSC is a stable serial IO java library, take a look at following examples :

  • writing data to serial port
  • reading data from serial port

Looks like to need a SerialPortReader which needs to implement a 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();
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM