001package jmri.jmrix.cmri.serial; 002 003import jmri.util.StringUtil; 004 005/** 006 * Contains the data payload of a CMRI serial packet. 007 * <p> 008 * Note that <i>only</i> the payload, not the header or trailer, nor the padding 009 * DLE characters are included. These are added during transmission. 010 * 011 * @author Bob Jacobsen Copyright (C) 2001,2003 012 */ 013public class SerialMessage extends jmri.jmrix.AbstractMRMessage { 014 // is this logically an abstract class? 015 016 final static int POLL_TIMEOUT = 250; 017 018 public SerialMessage() { 019 super(); 020 } 021 022 // create a new one 023 public SerialMessage(int i) { 024 super(i); 025 } 026 027 // copy one 028 public SerialMessage(SerialMessage m) { 029 super(m); 030 } 031 032 /** 033 * This ctor interprets the String as the exact sequence to send, 034 * byte-for-byte. 035 * 036 * @param m message string. 037 */ 038 public SerialMessage(String m) { 039 super(m); 040 } 041 042 /** 043 * This ctor interprets the byte array as a sequence of characters to send. 044 * @deprecated 5.13.5, unused, requires further development. 045 * @param a Array of bytes to send 046 */ 047 @Deprecated( since="5.13.5", forRemoval=true) 048 public SerialMessage(byte[] a) { 049 // super(String.valueOf(a)); // Spotbug toString on array 050 // requires further development to produce correct values for hardware type. 051 super(StringUtil.hexStringFromBytes(a).replaceAll("\\s", "")); 052 } 053 054 @Override 055 public String toString() { 056 StringBuilder s = new StringBuilder(); 057 for (int i = 0; i < getNumDataElements(); i++) { 058 if (i != 0) { 059 s.append(" "); 060 } 061 s.append(StringUtil.twoHexFromInt(getElement(i))); 062 } 063 return s.toString(); 064 } 065 066 // static methods to recognize a message 067 public boolean isPoll() { 068 return getElement(1) == 0x50; 069 } 070 071 public boolean isXmt() { 072 return getElement(1) == 0x54; 073 } 074 075 public boolean isInit() { 076 return (getElement(1) == 0x49); 077 } 078 079 public int getUA() { 080 return getElement(0) - 65; 081 } 082 083 // static methods to return a formatted message 084 static public SerialMessage getPoll(int UA) { 085 SerialMessage m = new SerialMessage(2); 086 m.setElement(0, 65 + UA); 087 m.setElement(1, 0x50); // 'P' 088 m.setTimeout(POLL_TIMEOUT); // minumum reasonable timeout 089 return m; 090 } 091 092}