Java Code Examples for org.apache.mina.core.buffer.IoBuffer#putUnsignedShort()
The following examples show how to use
org.apache.mina.core.buffer.IoBuffer#putUnsignedShort() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ModbusProtocol.java From neoscada with Eclipse Public License 1.0 | 6 votes |
/** * Encode the data from Java byte order to requested modbus byte order * * @param data * the data to encode * @param dataOrder * the target modbus byte order * @return the converted data, or the input data if no conversion was * necessary */ public static IoBuffer convertData ( final IoBuffer data, final ByteOrder dataOrder ) { if ( dataOrder == ByteOrder.BIG_ENDIAN ) { return data; } final IoBuffer result = IoBuffer.allocate ( data.capacity () ); result.order ( dataOrder ); for ( int i = 0; i < data.remaining () / 2; i++ ) { // convert to LITTLE_ENDIAN result.putUnsignedShort ( data.getUnsignedShort ( i * 2 ) ); } // the byte order we use is BIG_ENDIAN result.order ( ByteOrder.BIG_ENDIAN ); return result; }
Example 2
Source File: ProtocolEncoderImpl.java From neoscada with Eclipse Public License 1.0 | 6 votes |
private void encodeBrowseUpdate ( final IoSession session, final Object message, final IoBuffer data ) throws ProtocolCodecException { // length data.putUnsignedShort ( ( (BrowseAdded)message ).getEntries ().size () ); final CharsetEncoder encoder = Sessions.getCharsetEncoder ( session ); // data for ( final BrowseAdded.Entry entry : ( (BrowseAdded)message ).getEntries () ) { data.putUnsignedShort ( entry.getRegister () ); data.put ( entry.getDataType ().getDataType () ); data.putEnumSet ( entry.getFlags () ); try { data.putPrefixedString ( entry.getName (), encoder ); data.putPrefixedString ( entry.getDescription (), encoder ); data.putPrefixedString ( entry.getUnit (), encoder ); } catch ( final CharacterCodingException e ) { throw new ProtocolCodecException ( e ); } } }
Example 3
Source File: ProtocolEncoderImpl.java From neoscada with Eclipse Public License 1.0 | 6 votes |
private void encodeProperties ( final IoBuffer data, final Map<String, String> properties ) throws ProtocolCodecException { final CharsetEncoder encoder = this.defaultCharset.newEncoder (); data.putUnsignedShort ( properties.size () ); for ( final Map.Entry<String, String> entry : properties.entrySet () ) { try { data.putPrefixedString ( entry.getKey (), encoder ); data.putPrefixedString ( entry.getValue (), encoder ); } catch ( final CharacterCodingException e ) { throw new ProtocolCodecException ( e ); } } }
Example 4
Source File: SlaveHost.java From neoscada with Eclipse Public License 1.0 | 5 votes |
protected Object makeReadReply ( final BaseMessage baseMessage, final int[] data, final ByteOrder order ) { final IoBuffer reply = IoBuffer.allocate ( data.length * 2 ); reply.order ( order ); for ( int i = 0; i < data.length; i++ ) { reply.putUnsignedShort ( data[i] ); } reply.flip (); return new ReadResponse ( baseMessage.getTransactionId (), baseMessage.getUnitIdentifier (), baseMessage.getFunctionCode (), reply ); }
Example 5
Source File: ModbusTcpEncoder.java From neoscada with Eclipse Public License 1.0 | 5 votes |
@Override public void encode ( final IoSession session, final Object message, final ProtocolEncoderOutput out ) throws Exception { logger.debug ( "Encoding: {}", message ); final Pdu request = (Pdu)message; final IoBuffer buffer = IoBuffer.allocate ( request.getData ().remaining () + 7 ); buffer.setAutoExpand ( true ); final IoBuffer pdu = request.getData (); // put transaction identifier buffer.putUnsignedShort ( request.getTransactionId () ); // put modbus protocol identifier (always 0) buffer.putUnsignedShort ( 0 ); // put length, including slave id buffer.putUnsignedShort ( request.getData ().remaining () + 1 ); // put slave id buffer.put ( request.getUnitIdentifier () ); // put data buffer.put ( pdu ); buffer.flip (); logger.trace ( "Encoded to: {}", buffer ); out.write ( buffer ); }
Example 6
Source File: ProtocolEncoderImpl.java From neoscada with Eclipse Public License 1.0 | 5 votes |
private void encodeEntry ( final IoSession session, final IoBuffer data, final Entry entry ) throws ProtocolCodecException { data.putUnsignedShort ( entry.getRegister () ); data.put ( entry.getMissedUpdates () ); data.putLong ( entry.getTimestamp () ); data.putEnumSetShort ( entry.getStates () ); // put payload encodeVariant ( session, data, entry.getValue () ); }
Example 7
Source File: UnsignedShortIntegerType.java From neoscada with Eclipse Public License 1.0 | 4 votes |
@Override public void putValue ( final IoBuffer slice, final Variant value ) { slice.putUnsignedShort ( makeValue ( value ) ); }
Example 8
Source File: ProtocolEncoderImpl.java From neoscada with Eclipse Public License 1.0 | 4 votes |
@Override public void encode ( final IoSession session, final Object message, final ProtocolEncoderOutput out ) throws Exception { IoBuffer data = null; if ( message instanceof Hello ) { data = createMessage ( session, Messages.MC_HELLO, true ); data.put ( (byte)0x01 ); // version data.putShort ( ( (Hello)message ).getNodeId () ); data.putEnumSetShort ( ( (Hello)message ).getFeatures () ); data.putUnsignedShort ( 3, data.position () - 3 ); // fill length } else if ( message instanceof Welcome ) { data = createMessage ( session, Messages.MC_WELCOME, true ); // put bit set data.putEnumSetShort ( ( (Welcome)message ).getFeatures () ); encodeProperties ( data, ( (Welcome)message ).getProperties () ); data.putUnsignedShort ( 3, data.position () - 3 ); // fill length } else if ( message instanceof ReadAll ) { data = createMessage ( session, Messages.MC_READ_ALL, false ); } else if ( message instanceof DataUpdate ) { data = createMessage ( session, Messages.MC_DATA_UPDATE, true ); data.putUnsignedShort ( ( (DataUpdate)message ).getEntries ().size () ); // put values for ( final DataUpdate.Entry entry : ( (DataUpdate)message ).getEntries () ) { encodeEntry ( session, data, entry ); } data.putUnsignedShort ( 3, data.position () - 3 ); // fill length } else if ( message instanceof SubscribeBrowse ) { data = createMessage ( session, Messages.MC_START_BROWSE, false ); } else if ( message instanceof UnsubscribeBrowse ) { data = createMessage ( session, Messages.MC_STOP_BROWSE, false ); } else if ( message instanceof BrowseAdded ) { data = createMessage ( session, Messages.MC_NS_ADDED, true ); // put browse update encodeBrowseUpdate ( session, message, data ); data.putUnsignedShort ( 3, data.position () - 3 ); // fill length } else if ( message instanceof WriteCommand ) { data = createMessage ( session, Messages.MC_WRITE_COMMAND, true ); data.putUnsignedShort ( ( (WriteCommand)message ).getRegisterNumber () ); data.putInt ( ( (WriteCommand)message ).getOperationId () ); encodeVariant ( session, data, ( (WriteCommand)message ).getValue () ); data.putUnsignedShort ( 3, data.position () - 3 ); // fill length } else if ( message instanceof WriteResult ) { data = createMessage ( session, Messages.MC_WRITE_RESULT, true ); data.putInt ( ( (WriteResult)message ).getOperationId () ); data.putUnsignedShort ( ( (WriteResult)message ).getErrorCode () ); data.putPrefixedString ( ( (WriteResult)message ).getErrorMessage (), Sessions.getCharsetEncoder ( session ) ); data.putUnsignedShort ( 3, data.position () - 3 ); // fill length } if ( data != null ) { data.flip (); out.write ( data ); } else { throw new ProtocolCodecException ( String.format ( "Message %s is not supported", message.getClass ().getName () ) ); } }
Example 9
Source File: UInt16Accessor.java From neoscada with Eclipse Public License 1.0 | 4 votes |
@Override public void put ( final IoBuffer data, final Integer value ) { data.putUnsignedShort ( value ); }