title: How do I write a sequence of bytes to a serial port in java
body: <p>I am a beginner to java. I have to send 5 consecutive integers to the instument I am working with through the com port. I am using rxtx library. The instrument doesnt respond to my code.  I tried initialising the values to string and then later changed to byte using getbytes. But even this didnt help.It would be a great help if somebody can help me. The instrument is identified at COM5. I tried to first write to the Com port. Data available event is fired. But the inputstream read is unrecognizable characters. I think that the problem is with writing to the port. It would be a great help if somebody can help me.</p>
  
  <p>The serial interface specification of the device is as follow</p>
  
  <p>DIR START CMD VALLO VALHI END</p>
  
  <p>→ 53   1    4    0     83</p>
  
  <p>← 53    1    4    0      83</p>
  
  <p>← 53    27   SLO  SHI    83</p>
  
  <p>A transaction is defined by the following sequence of packets</p>
  
  <p>• → Software sends a request packet.</p>
  
  <p>• ← Instrument sends a response packet.</p>
  
  <p>• ← Instrument sends a status packet.</p>
  
  <p>This represents a single transaction in an entire run.</p>
  
  <p>Each START;CMD; VALLO;VALHI;END above represents a single byte in the packet</p>
  
  <pre><code>public class ReadWrite implements SerialPortEventListener {
  
      OutputStream outputStream;
      InputStream inputStream;
      static SerialPort serialPort;
  
      public static void main(String[] args) throws Exception {
          Enumeration portList = CommPortIdentifier.getPortIdentifiers();
          System.out.println("SimpleRead Started.");
          while (portList.hasMoreElements()) {
              CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
              if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                  System.out.println("Found " + portId.getName());
                  if (portId.getName().equals("COM5")) {
                      serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000);
                      serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
  
                      ReadWrite reader = new ReadWrite();
                  }
              }
          }
      }
  
      public ReadWrite() throws IOException {
          try {
              outputStream = serialPort.getOutputStream();
              inputStream = serialPort.getInputStream();
  
              outputStream.write(53);
              outputStream.flush();
              outputStream.write(1);
              outputStream.flush();
              outputStream.write(4);
              outputStream.flush();
              outputStream.write(0);
              outputStream.flush();
              outputStream.write(83);
              outputStream.flush();
  
              System.out.println("The port in use is " + serialPort);
  
              System.out.println("write done");
  
              serialPort.addEventListener(this);
          } catch (TooManyListenersException e) {
              e.printStackTrace();
          }
          serialPort.notifyOnDataAvailable(true);
      }
  
      public void serialEvent(SerialPortEvent event) {
  
          switch (event.getEventType()) {
          case SerialPortEvent.BI:
              System.out.println("The serial port event is BI ");
  
          case SerialPortEvent.OE:
  
          case SerialPortEvent.FE:
  
          case SerialPortEvent.PE:
  
          case SerialPortEvent.CD:
  
          case SerialPortEvent.CTS:
  
          case SerialPortEvent.DSR:
  
          case SerialPortEvent.RI:
  
          case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
              System.out.println("The serial port event is OUTPUT_BUFFER_EMPTY ");
              break;
          case SerialPortEvent.DATA_AVAILABLE:
  
              System.out.println("The serial port event is Data available ");
              byte[] readBuffer = new byte[20];
              System.out.println(event.getEventType());
              try {
                  while (inputStream.available() &gt; 0) {
                      int numBytes = inputStream.read(readBuffer);
                      System.out.print(new String(readBuffer, 0, numBytes, "us-ascii"));
                  }
                  System.out.print(new String(readBuffer));
              } catch (IOException e) {
                  e.printStackTrace();
              }
              serialPort.close();
              break;
          }
      }
  }
  </code></pre>