protected void writeMessageOut(ModbusMessage msg) throws ModbusIOException {
  try {
    int len;
synchronized (byteOutputStream) {
      clearInput();
      byteOutputStream.reset();
      msg.setHeadless();
      msg.writeTo(byteOutputStream);
      len=byteOutputStream.size();
      int[] crc=ModbusUtil.calculateCRC(byteOutputStream.getBuffer(),0,len);
      byteOutputStream.writeByte(crc[0]);
      byteOutputStream.writeByte(crc[1]);
      writeBytes(byteOutputStream.getBuffer(),byteOutputStream.size());
      logger.debug("Sent: {}",ModbusUtil.toHex(byteOutputStream.getBuffer(),0,byteOutputStream.size()));
      if (echo) {
        readEcho(len);
      }
      lastRequest=new byte[len];
      System.arraycopy(byteOutputStream.getBuffer(),0,lastRequest,0,len);
    }
  }
 catch (  IOException ex) {
    throw new ModbusIOException("I/O failed to write");
  }
}

protected void writeMessageOut ( ModbusMessage msg ) throws ModbusIOException { try { int len ; synchronized ( byteOutputStream ) { clearInput ( ) ; byteOutputStream . reset ( ) ; msg . setHeadless ( ) ; msg . writeTo ( byteOutputStream ) ; len = byteOutputStream . size ( ) ; int [ ] crc = ModbusUtil . calculateCRC ( byteOutputStream . getBuffer ( ) , 0 , len ) ; byteOutputStream . writeByte ( crc [ 0 ] ) ; byteOutputStream . writeByte ( crc [ 1 ] ) ; writeBytes ( byteOutputStream . getBuffer ( ) , byteOutputStream . size ( ) ) ; logger . debug ( "Sent: {}" , ModbusUtil . toHex ( byteOutputStream . getBuffer ( ) , 0 , byteOutputStream . size ( ) ) ) ; if ( echo ) { readEcho ( len ) ; } lastRequest = new byte [ len ] ; System . arraycopy ( byteOutputStream . getBuffer ( ) , 0 , lastRequest , 0 , len ) ; } } catch ( IOException ex ) { throw new ModbusIOException ( "I/O failed to write" ) ; } }
writes the modbus message to the comms port
