title: Reading file from serial port in Java
body: <p>i'm beginner in java technology, I have to read file from port. Frst I'll write "FLASH" to outputstream then I'll get response as a "FLASH_OK" from target device, after getting FLASH_OK as response then again i have to write name of the file which i want,but problem is its not writing file name to outputstream, below is my code. Please help me.</p>
  
  <pre><code>package writeToPort;
  
  import java.awt.Toolkit;
  import java.io.*;
  import java.util.*;
  
  import javax.comm.*;
  import javax.swing.JOptionPane;
  
  import constants.Constants;
  
  public class Flashwriter implements SerialPortEventListener {
  Enumeration portList;
  CommPortIdentifier portId;
  String messageString = "\r\nFLASH\r\n";
  SerialPort serialPort;
  OutputStream outputStream;
  InputStream inputStream;
  Thread readThread;
  String one, two;
  String test = "ONLINE";
  String[] dispArray = new String[1];
  int i = 0;
  
  byte[] readBufferArray;
  int numBytes;
  String response;
  FileOutputStream out;
  final int FLASH = 1, FILENAME = 2;
  int number;
  
  File winFile;
  
  public static void main(String[] args) throws IOException {
      Flashwriter sm = new Flashwriter();
      sm.FlashWriteMethod();
  }
  
  public void FlashWriteMethod() throws IOException {
  
      portList = CommPortIdentifier.getPortIdentifiers();
      winFile = new File("D:\\testing\\out.FLS");
  
      while (portList.hasMoreElements()) {
          portId = (CommPortIdentifier) portList.nextElement();
          if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
              if (portId.getName().equals("COM2")) {
                  try {
                      serialPort = (SerialPort) portId.open("SimpleWriteApp",
                              1000);
                  } catch (PortInUseException e) {
                  }
  
                  try {
                      inputStream = serialPort.getInputStream();
  
                      System.out.println(" Input Stream... " + inputStream);
                  } catch (IOException e) {
                      System.out.println("IO Exception");
                  }
                  try {
                      serialPort.addEventListener(this);
  
                  } catch (TooManyListenersException e) {
                      System.out.println("Tooo many Listener exception");
                  }
                  serialPort.notifyOnDataAvailable(true);
  
                  try {
                      outputStream = serialPort.getOutputStream();
                      inputStream = serialPort.getInputStream();
                  } catch (IOException e) {
                  }
                  try {
                      serialPort.setSerialPortParams(9600,
                              SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
                              SerialPort.PARITY_NONE);
                      serialPort
                              .setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
                                              number = FLASH;
                      sendRequest(number);
  
                  } catch (UnsupportedCommOperationException e) {
                  }
  
              }
          }
      }
  }
  
  public void serialEvent(SerialPortEvent event) {
      SerialPort port = (SerialPort) event.getSource();
  
      switch (event.getEventType()) {
      case SerialPortEvent.DATA_AVAILABLE:
          try {
              if (inputStream.available() &gt; 0) {
                  numBytes = inputStream.available();
                  readBufferArray = new byte[numBytes];
                  int readBytes = inputStream.read(readBufferArray);
  
                  one = new String(readBufferArray);
                  System.out.println("readBytes " + one);
              }
              if (one.indexOf("FLASH_") &gt; -1 &amp; !(one.indexOf("FLASH_F") &gt; -1)) {
                  System.out.println("got message");
                  response = "FLASH_OK";
                  number = FILENAME;
                  sendRequest(number);
              }
  
              out = new FileOutputStream(winFile, true);
              out.write(readBufferArray);
              out.close();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          readBufferArray = null;
          // break;
      }
  
  
  }
  
  
  public void sendRequest(int num) {
      switch (num) {
      case FLASH:
          try {
              outputStream.write(messageString.getBytes());
              outputStream.flush();
          } catch (IOException e) {
              e.printStackTrace();
          }
          break;
      case FILENAME:
          try {
              outputStream.write("\r\n26-02-08.FLS\r\n".getBytes());
              outputStream.flush();
          } catch (IOException e) {
              e.printStackTrace();
          }
          break;
  
      }
  }
  
  }
  </code></pre>