Read file from serial port using Java

后端 未结 2 640
花落未央
花落未央 2021-01-05 11:39

I\'m begginner in Java, I\'m writing (\"FLASH\").getbytes() like this to serialport.

After I\'ll get FLASH_OK as response, aga

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-05 12:04

    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();
                }
    

提交回复
热议问题