Search in sources :

Example 1 with CbusAddress

use of jmri.jmrix.can.cbus.CbusAddress in project JMRI by JMRI.

the class CanSendPane method createPacket.

/**
     * Create a well-formed message from a String String is expected to be space
     * seperated hex bytes or CbusAddress, e.g.: 12 34 56 +n4e1
     *
     * @return The packet, with contents filled-in
     */
CanMessage createPacket(String s) {
    CanMessage m;
    // Try to convert using CbusAddress class
    CbusAddress a = new CbusAddress(s);
    if (a.check()) {
        m = a.makeMessage(tc.getCanid());
    } else {
        m = new CanMessage(tc.getCanid());
        // check for header
        if (s.charAt(0) == '[') {
            // extended header
            m.setExtended(true);
            int i = s.indexOf(']');
            String h = s.substring(1, i);
            m.setHeader(Integer.parseInt(h, 16));
            s = s.substring(i + 1, s.length());
        } else if (s.charAt(0) == '(') {
            // standard header
            int i = s.indexOf(')');
            String h = s.substring(1, i);
            m.setHeader(Integer.parseInt(h, 16));
            s = s.substring(i + 1, s.length());
        }
        // Try to get hex bytes
        byte[] b = StringUtil.bytesFromHexString(s);
        m.setNumDataElements(b.length);
        // Use &0xff to ensure signed bytes are stored as unsigned ints
        for (int i = 0; i < b.length; i++) {
            m.setElement(i, b[i] & 0xff);
        }
    }
    return m;
}
Also used : CbusAddress(jmri.jmrix.can.cbus.CbusAddress) CanMessage(jmri.jmrix.can.CanMessage)

Example 2 with CbusAddress

use of jmri.jmrix.can.cbus.CbusAddress in project JMRI by JMRI.

the class OpenLcbCanSendPane method createPacket.

/**
     * Create a well-formed message from a String String is expected to be space
     * seperated hex bytes or CbusAddress, e.g.: 12 34 56 +n4e1
     * @param s string of spaced hex byte codes
     * @return The packet, with contents filled-in
     */
CanMessage createPacket(String s) {
    CanMessage m;
    // Try to convert using CbusAddress class to reuse a little code
    CbusAddress a = new CbusAddress(s);
    if (a.check()) {
        m = a.makeMessage(tc.getCanid());
    } else {
        m = new CanMessage(tc.getCanid());
        // check for header
        if (s.charAt(0) == '[') {
            // NOI18N
            // extended header
            m.setExtended(true);
            // NOI18N
            int i = s.indexOf(']');
            String h = s.substring(1, i);
            m.setHeader(Integer.parseInt(h, 16));
            s = s.substring(i + 1, s.length());
        } else if (s.charAt(0) == '(') {
            // NOI18N
            // standard header
            // NOI18N
            int i = s.indexOf(')');
            String h = s.substring(1, i);
            m.setHeader(Integer.parseInt(h, 16));
            s = s.substring(i + 1, s.length());
        }
        // Try to get hex bytes
        byte[] b = StringUtil.bytesFromHexString(s);
        m.setNumDataElements(b.length);
        // Use &0xff to ensure signed bytes are stored as unsigned ints
        for (int i = 0; i < b.length; i++) {
            m.setElement(i, b[i] & 0xff);
        }
    }
    return m;
}
Also used : CbusAddress(jmri.jmrix.can.cbus.CbusAddress) CanMessage(jmri.jmrix.can.CanMessage)

Aggregations

CanMessage (jmri.jmrix.can.CanMessage)2 CbusAddress (jmri.jmrix.can.cbus.CbusAddress)2