protected void writeMessageOut ( ModbusMessage msg ) throws ModbusIOException { try { int len ; synchronized ( byteOutputStream ) { // first clear any input from the receive buffer to prepare // for the reply since RTU doesn't have message delimiters clearInput ( ) ; // write message to byte out 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 ] ) ; // write message writeBytes ( byteOutputStream . getBuffer ( ) , byteOutputStream . size ( ) ) ; if ( logger . isDebugEnabled ( ) ) { logger . debug ( "Sent: {}" , ModbusUtil . toHex ( byteOutputStream . getBuffer ( ) , 0 , byteOutputStream . size ( ) ) ) ; } // clears out the echoed message // for RS485 if ( echo ) { readEcho ( len + 2 ) ; } 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