当前位置: 首页>>代码示例>>Java>>正文


Java IoBuffer.order方法代码示例

本文整理汇总了Java中org.apache.mina.core.buffer.IoBuffer.order方法的典型用法代码示例。如果您正苦于以下问题:Java IoBuffer.order方法的具体用法?Java IoBuffer.order怎么用?Java IoBuffer.order使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.mina.core.buffer.IoBuffer的用法示例。


在下文中一共展示了IoBuffer.order方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: convertData

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
/**
 * 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;
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:32,代码来源:ModbusProtocol.java

示例2: decodeData

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
private Object decodeData ( final IoBuffer data ) throws ProtocolCodecException
{
    data.order ( ByteOrder.LITTLE_ENDIAN );

    final byte dataType = data.get ();
    switch ( dataType )
    {
        case 0:
            return null;
        case 1:
            return data.get () != 0x00;
        case 2:
            return data.getInt ();
        case 3:
            return data.getLong ();
        case 4:
            return data.getFloat ();
        default:
            throw new ProtocolCodecException ( String.format ( "Data type %02x is unknown", dataType ) );
    }

}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:23,代码来源:ArduinoCodec.java

示例3: encode

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
@Override
public void encode ( final IoSession session, final Object message, final ProtocolEncoderOutput output ) throws Exception
{
    final IoBuffer data = IoBuffer.allocate ( 0 );
    data.order ( ByteOrder.LITTLE_ENDIAN );
    data.setAutoExpand ( true );

    if ( message instanceof WriteRequestMessage )
    {
        encodeHeader ( data, (CommonMessage)message );
        encodeWriteRequest ( data, (WriteRequestMessage)message );
    }
    else if ( message instanceof CommonMessage )
    {
        encodeHeader ( data, (CommonMessage)message );
    }
    data.flip ();
    output.write ( data );
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:20,代码来源:ArduinoCodec.java

示例4: createMessage

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
protected IoBuffer createMessage ( final IoSession session, final byte command, final boolean longMessage )
{
    final IoBuffer data = IoBuffer.allocate ( 3 );
    data.setAutoExpand ( true );

    if ( Sessions.isLittleEndian ( session ) )
    {
        data.order ( ByteOrder.LITTLE_ENDIAN );
    }

    data.put ( (byte)0x12 );
    data.put ( (byte)0x02 );
    data.put ( command );
    if ( longMessage )
    {
        data.putShort ( (short)0 );
    }
    return data;
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:20,代码来源:ProtocolEncoderImpl.java

示例5: writeData

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
@Override
public void writeData ( final int blockAddress, byte[] data )
{
    if ( this.request.getType () != RequestType.HOLDING )
    {
        throw new IllegalStateException ( String.format ( "Modbus can only write data when the block is of type %s", RequestType.HOLDING ) );
    }
    if ( data.length == 2 )
    {
        final IoBuffer buffer = IoBuffer.wrap ( data );
        buffer.order ( this.dataOrder );
        final int value = buffer.getUnsignedShort ();
        this.slave.writeCommand ( new WriteSingleDataRequest ( this.transactionId.incrementAndGet (), this.slave.getSlaveAddress (), Constants.FUNCTION_CODE_WRITE_SINGLE_REGISTER, toWriteAddress ( blockAddress ), value ), this.request.getTimeout () );
    }
    else
    {
        data = ModbusProtocol.encodeData ( data, this.dataOrder ); // apply requested byte order
        this.slave.writeCommand ( new WriteMultiDataRequest ( this.transactionId.incrementAndGet (), this.slave.getSlaveAddress (), Constants.FUNCTION_CODE_WRITE_MULTIPLE_REGISTERS, toWriteAddress ( blockAddress ), data, data.length / 2 ), this.request.getTimeout () );
    }
    requestUpdate ();
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:22,代码来源:ModbusRequestBlock.java

示例6: processAnalogWrite

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
private void processAnalogWrite ( final int startAddress, final int numRegisters, final byte[] rawData )
{
    final int[] regs = new int[numRegisters];
    final IoBuffer data = IoBuffer.wrap ( rawData );

    data.order ( this.order );

    for ( int i = 0; i < numRegisters; i++ )
    {
        regs[i] = data.getUnsignedShort ();
    }

    handleAnalogWrite ( startAddress, regs );
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:15,代码来源:AbstractDataSlave.java

示例7: makeReadReply

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
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 );
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:14,代码来源:SlaveHost.java

示例8: decodeTimeoutFrame

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
private void decodeTimeoutFrame ( final IoSession session, final IoBuffer currentFrame, final ProtocolDecoderOutput out )
{
    logger.trace ( "timeout () frame = {}", currentFrame.getHexDump () );

    if ( currentFrame.limit () <= Constants.RTU_HEADER_SIZE )
    {
        throw new ModbusProtocolError ( "frame must be at least 4 bytes long (address + data[] + crc low + crc high" );
    }

    currentFrame.order ( ByteOrder.LITTLE_ENDIAN );
    final int receivedCrc = currentFrame.getUnsignedShort ( currentFrame.limit () - 2 );
    currentFrame.order ( ByteOrder.BIG_ENDIAN );

    final int actualCrc = Checksum.crc16 ( currentFrame.array (), 0, currentFrame.limit () - 2 );
    if ( receivedCrc != actualCrc )
    {
        final String hd = currentFrame.getHexDump ();
        logger.info ( "CRC error - received: {}, calculated: {} - data: {}", receivedCrc, actualCrc, hd );
        throw new ChecksumProtocolException ( String.format ( "CRC error. received: %04x, but actually was: %04x - Data: %s", receivedCrc, actualCrc, hd ) );
    }

    currentFrame.position ( 0 );

    // get unit id
    final byte unitIdentifier = currentFrame.get ();

    final int len = currentFrame.limit () - ( 2 /*crc*/+ 1/*unit id*/);

    final IoBuffer pdu = IoBuffer.allocate ( len );
    for ( int i = 0; i < len; i++ )
    {
        pdu.put ( currentFrame.get () );
    }
    pdu.flip ();

    // decode and send
    logger.trace ( "Decoded PDU - data: {}", pdu.getHexDump () );
    out.write ( new Pdu ( 0, unitIdentifier, pdu ) );
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:40,代码来源:ModbusRtuDecoder.java

示例9: encode

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
@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 () + 3 );
    buffer.setAutoExpand ( true );

    final IoBuffer pdu = request.getData ();

    // put slave id
    buffer.put ( request.getUnitIdentifier () );
    // put data
    buffer.put ( pdu );

    // make and put crc
    final int crc = Checksum.crc16 ( buffer.array (), 0, pdu.limit () + 1 ); // including slave address
    buffer.order ( ByteOrder.LITTLE_ENDIAN );
    buffer.putShort ( (short)crc );

    buffer.flip ();

    logger.trace ( "Encoded to: {}", buffer );

    out.write ( buffer );
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:28,代码来源:ModbusRtuEncoder.java

示例10: storeRemainingInSession

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
private void storeRemainingInSession(IoBuffer buf, IoSession session) {
    final IoBuffer remainingBuf = IoBuffer.allocate(buf.capacity()).setAutoExpand(true);

    remainingBuf.order(buf.order());
    remainingBuf.put(buf);

    session.setAttribute(BUFFER, remainingBuf);
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:9,代码来源:CumulativeProtocolDecoder.java

示例11: storeRemainingInSession

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
private void storeRemainingInSession(IoBuffer buf, IoSession session) {
	IoBuffer remainingBuf = IoBuffer.allocate(buf.capacity());
	remainingBuf.setAutoExpand(true);
	remainingBuf.order(buf.order());
	remainingBuf.put(buf);
	session.setAttribute(BUFFER, remainingBuf);
}
 
开发者ID:dianbaer,项目名称:grain,代码行数:8,代码来源:MinaDecoder.java

示例12: doDecode

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
@Override
protected boolean doDecode ( final IoSession session, final IoBuffer data, final ProtocolDecoderOutput out ) throws Exception
{
    byte marker1;
    byte marker2;

    do
    {
        if ( data.remaining () < 2 ) // we may only eat the start when there could be a packet after it
        {
            return false;
        }

        // peek marker
        marker1 = data.get ( data.position () + 0 );
        marker2 = data.get ( data.position () + 1 );

        // TODO: re-think the idea of just skipping

        if ( marker1 != 0x12 || marker2 != 0x02 )
        {
            data.skip ( 2 ); // eat garbage
        }
    } while ( marker1 != 0x12 || marker2 != 0x02 );

    if ( data.remaining () < 3 )
    {
        return false;
    }

    data.order ( Sessions.isLittleEndian ( session ) ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN );

    final byte command = data.get ( data.position () + 2 );
    switch ( command )
    {
        case Messages.MC_HELLO:
            return processHello ( session, data, out );
        case Messages.MC_WELCOME:
            return processWelcome ( session, data, out );
        case Messages.MC_READ_ALL:
            out.write ( new ReadAll () );
            return true;
        case Messages.MC_DATA_UPDATE:
            return processDataUpdate ( session, data, out );
        case Messages.MC_START_BROWSE:
            out.write ( new SubscribeBrowse () );
            return true;
        case Messages.MC_STOP_BROWSE:
            out.write ( new UnsubscribeBrowse () );
            return true;
        case Messages.MC_NS_ADDED:
            return processBrowseAdded ( session, data, out );
        case Messages.MC_WRITE_COMMAND:
            return processWriteCommand ( session, data, out );
        case Messages.MC_WRITE_RESULT:
            return processWriteResult ( session, data, out );
    }

    throw new ProtocolCodecException ( String.format ( "Message code %02x is unkown", command ) );
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:61,代码来源:ProtocolDecoderImpl.java


注:本文中的org.apache.mina.core.buffer.IoBuffer.order方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。