001package jmri.jmrix.powerline;
002
003/**
004 * Contains the data payload of a serial packet.
005 * <p>
006 * The transmission protocol can come in one of several forms:
007 * <ul>
008 * <li>If the interlocked parameter is false (default), the packet is just sent.
009 * If the response length is not zero, a reply of that length is expected.
010 * <li>If the interlocked parameter is true, the transmission will require a CRC
011 * interlock, which will be automatically added. (Design note: this is done to
012 * make sure that the messages remain atomic)
013 * </ul>
014 *
015 * @author Bob Jacobsen Copyright (C) 2001,2003, 2006, 2007, 2008 Converted to
016 * multiple connection
017 * @author kcameron Copyright (C) 2011
018 */
019abstract public class SerialMessage extends jmri.jmrix.AbstractMRMessage {
020    // is this logically an abstract class?
021
022    /**
023     * Suppress the default ctor, as the length must always be specified
024     */
025    protected SerialMessage() {
026    }
027
028    public SerialMessage(int l) {
029        super(l);
030        setResponseLength(0);  // only polls require a response
031        setBinary(true);
032        setTimeout(5000);
033    }
034
035    /**
036     * This ctor interprets the String as the exact sequence to send,
037     * byte-for-byte.
038     * @param m sequence to send
039     * @param l expected reply length
040     *
041     */
042    public SerialMessage(String m, int l) {
043        super(m);
044        setResponseLength(l);
045        setBinary(true);
046        setTimeout(5000);
047    }
048
049    boolean interlocked = false;
050
051    public void setInterlocked(boolean v) {
052        interlocked = v;
053    }
054
055    public boolean getInterlocked() {
056        return interlocked;
057    }
058
059    /**
060     * This ctor interprets the byte array as a sequence of characters to send.
061     *
062     * @param a Array of bytes to send
063     * @param l expected reply length
064     */
065    public SerialMessage(byte[] a, int l) {
066        super(String.valueOf(a));
067        setResponseLength(l);
068        setBinary(true);
069        setTimeout(5000);
070    }
071
072    int responseLength = -1;  // -1 is an invalid value, indicating it hasn't been set
073
074    public void setResponseLength(int l) {
075        responseLength = l;
076    }
077
078    public int getResponseLength() {
079        return responseLength;
080    }
081
082    // static methods to recognize a message
083    public boolean isPoll() {
084        return getElement(1) == 48;
085    }
086
087    public boolean isXmt() {
088        return getElement(1) == 17;
089    }
090
091    public int getAddr() {
092        return getElement(0);
093    }
094
095    // static methods to return a formatted message
096    static public SerialMessage getPoll(int addr) {
097        // Powerline implementation does not currently poll
098        return null;
099    }
100}
101
102