001package jmri.jmrix.can.adapters.gridconnect.canrs;
002
003import jmri.jmrix.can.adapters.gridconnect.GridConnectReply;
004
005/**
006 * Class for replies in a MERG GridConnect based message/reply protocol.
007 * <p>
008 * The GridConnect protocol encodes messages as an ASCII string of up to 24
009 * characters of the form: :ShhhhNd0d1d2d3d4d5d6d7;
010 * <p>
011 * hhhh is the two byte (11
012 * useful bits) header The S indicates a standard CAN frame
013 * :XhhhhhhhhNd0d1d2d3d4d5d6d7; The X indicates an extended CAN frame Strict
014 * <p>
015 * Gridconnect protocol allows a variable number of header characters, e.g., a
016 * header value of 0x123 could be encoded as S123, X123, S0123 or X00000123.
017 * <p>
018 * MERG hardware uses a fixed 4 or 8 byte header when sending
019 * GridConnectMessages to the computer. The 11 bit standard header is left
020 * justified in these 4 bytes. The 29 bit standard header is sent as
021 * {@code <11 bit SID><0><1><0>< 18 bit EID>}
022 * N or R indicates a normal or remote frame, in position 6 or 10 d0 - d7 are
023 * the (up to) 8 data bytes
024 *
025 * @author Andrew Crosland Copyright (C) 2008
026 * @author Bob Jacobsen Copyright (C) 2008
027 */
028public class MergReply extends GridConnectReply {
029
030    /**
031     * Create new MergReply.
032     */
033    public MergReply() {
034        super();
035    }
036
037    /**
038     * Create new MergReply from String.
039     * @param s Frame data to create MergReply from.
040     */
041    public MergReply(String s) {
042        super(s);
043    }
044
045    /**
046     * Get the CAN header from MERG format in digits 2 to 9.
047     *
048     * @return the CAN header as an int
049     */
050    @Override
051    public int getHeader() {
052        int val = super.getHeader();
053        // Adjust standard header from MERG adapter received as 11 bits left
054        // justified in four bytes
055        if (_dataChars[1] == 'S') {
056            val = (val >> 5) & 0x07FF;
057        }
058        // Adjust extended header from MERG adapter received as
059        // <11 bit SID><0><1><0><18 bit EID> in four bytes
060        if (_dataChars[1] == 'X') {
061            val = ((val >> 3) & 0x1FFC0000) | (val & 0x3FFFF);
062        }
063        return val;
064    }
065}