code: private void writeMessage(ModbusMessage msg) throws ModbusIOException {
          try {
              synchronized (byteOutputStream) {
                  int len = msg.getOutputLength();
                  byteOutputStream.reset();
                  msg.writeTo(byteOutputStream);
                  byte data[] = byteOutputStream.getBuffer();
                  data = Arrays.copyOf(data, len);
                  terminal.sendMessage(data);
              }
          }
          catch (Exception ex) {
              throw new ModbusIOException("I/O exception - failed to write", ex);
          }
      }
docs: Writes the request/response message to the port
  @param msg Message to write
  @throws ModbusIOException If the port cannot be written to