title: Issue writing to serial port from Java
body: <blockquote>
    <p>Duplicate of <a href="https://stackoverflow.com/questions/606074/implementation-of-xmodem-protocol-in-java">Implementation of Xmodem Protocol in Java</a></p>
  </blockquote>
  
  <hr>
  
  <p>I've got to implement the xmodem protocol to receive a file from a target device.  For that, I have to request the file, then for every 128-byte packet received, I have to send an acknowledgment.  My problem is when I open an outputstream to request the file, it will write but after that I can't write again to the outputstream.  What is the problem I'm not getting?</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 Runnable, SerialPortEventListener {
      Enumeration portList;
      CommPortIdentifier portId;
      String messageString = "\r\nFLASH\r\n";
      SerialPort serialPort;
      OutputStream outputStream,outputStream2;
      InputStream inputStream;
      //Thread readThread;
      String one, two;
      String test = "ONLINE";
      String[] dispArray = new String[1];
      int i = 0;
  
      Thread readThread;
  
      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")) {
                      // if (portId.getName().equals("/dev/term/a")) {
                      try {
                          serialPort = (SerialPort) portId.open("SimpleWriteApp",
                                  1000);
                      } catch (PortInUseException e) {
                      }
  
                      try {
                          outputStream = serialPort.getOutputStream();
                          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 {
                          serialPort.setSerialPortParams(9600,
                                  SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
                                  SerialPort.PARITY_NONE);
                          serialPort
                                  .setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
                          // serialPort.disableReceiveTimeout();
  
                          // outputStream.write(messageString.getBytes());
  
                          // sendRequest("/r/n26-02-08.FLS/r/n");
                          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 {
                  while (inputStream.available() &gt; 0) {
                      numBytes = inputStream.available();
                      readBufferArray = new byte[numBytes];
                      // int readtheBytes = (int) inputStream.skip(2);
                      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";
                      // JOptionPane.showMessageDialog(null,
                      // "ONLINE",
                      // "Online Dump",
                      // JOptionPane.INFORMATION_MESSAGE);
                      // Toolkit.getDefaultToolkit().beep();
                      // outputStream.write("\r\nONLINEr\n".getBytes());
                      // outputStream.flush();
                      // outputStream.write("/r/n26-02-08.FLS/r/n".getBytes());
  
                      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;
          }
  
          // try {
          // int c;
          // while((c = inputStream.read()) != -1) {
          // out.write(c);
          // }
          // } catch (IOException e) {
          // // TODO Auto-generated catch block
          // e.printStackTrace();
          // }
          // // readBufferArray=null;
          // break;
          // }
          // if (inputStream != null)
          // try {
          // inputStream.close();
          // if (port != null) port.close();
          // } catch (IOException e) {
          // // TODO Auto-generated catch block
          // e.printStackTrace();
          // }
          //      
          //
          readThread = new Thread(this);
          readThread.start();
      }
  
      public void run() {
          try {
              System.out.println("In run() function ");
              Thread.sleep(1000);
  
          } catch (InterruptedException e) {
              System.out.println("Interrupted Exception in run() method");
          }
      }
  
      public void dispPacket(String packet) {
          if (response == "FLASH_OK") {
              System.out.println("disppacket " + packet);
          } else {
              System.out.println("No resust");
          }
      }
  
      public void sendRequest(int num) {
          switch (num) {
          case FLASH:
              try {
              //  outputStream = serialPort.getOutputStream();
                  outputStream.write("AT".getBytes());
              //  outputStream.write("\r\n26-02-08.FLS\r\n".getBytes());
  
                  System.out.println("Flash switch");
               //   outputStream.close();
                  outputStream.flush();
              } catch (IOException e) {
                  e.printStackTrace();
              }
              break;
          case FILENAME:
              try {
                  //outputStream =(serialPort.getOutputStream());
                  outputStream.write("\r\nSUNSHINE\\06-03-09.FLS\r\n".getBytes());
  
                  System.out.println("File name");
                  outputStream.flush();
  
              } catch (IOException e) {
                  e.printStackTrace();
              }
              break;
          }
      }
  }
  </code></pre>