001package jmri.jmrix.secsi.serialmon;
002
003import jmri.jmrix.secsi.SerialListener;
004import jmri.jmrix.secsi.SerialMessage;
005import jmri.jmrix.secsi.SerialReply;
006import jmri.jmrix.secsi.SecsiSystemConnectionMemo;
007
008/**
009 * Frame displaying (and logging) serial command messages.
010 *
011 * @author Bob Jacobsen Copyright (C) 2001, 2006, 2007, 2008
012 */
013public class SerialMonFrame extends jmri.jmrix.AbstractMonFrame implements SerialListener {
014
015    private SecsiSystemConnectionMemo memo = null;
016
017    public SerialMonFrame(SecsiSystemConnectionMemo _memo) {
018        super();
019        memo = _memo;
020    }
021
022    @Override
023    protected String title() {
024        return Bundle.getMessage("MonitorXTitle", "SECSI");
025    }
026
027    @Override
028    protected void init() {
029        // connect to TrafficController
030        memo.getTrafficController().addSerialListener(this);
031    }
032
033    @Override
034    protected void setHelp() {
035        addHelpMenu("package.jmri.jmrix.secsi.serialmon.SerialMonFrame", true);  // NOI18N
036    }
037
038    @Override
039    public void dispose() {
040        memo.getTrafficController().removeSerialListener(this);
041        super.dispose();
042    }
043
044    @Override
045    public synchronized void message(SerialMessage l) {  // receive a message and log it
046        // check for valid length
047        if (l.getNumDataElements() < 5) {
048            nextLine("Truncated message of length " + l.getNumDataElements() + "\n", // TODO I18N
049                    l.toString());
050        } else if (l.isPoll()) {
051            nextLine("Poll addr=" + l.getAddr() + "\n", l.toString());
052        } else if (l.isXmt()) {
053            String s = "Transmit addr=" + l.getAddr()
054                    + " byte " + (l.getElement(2) & 0x000000ff)
055                    + " data = " + Integer.toHexString(l.getElement(3) & 0xff);
056            nextLine(s + "\n", l.toString());
057        } else {
058            nextLine("Unrecognized cmd: \"" + l.toString() + "\"\n", l.toString());
059        }
060    }
061
062    @Override
063    public synchronized void reply(SerialReply l) {  // receive a reply message and log it
064        // check for valid length
065        if (l.getNumDataElements() == 1) {
066            if (l.getElement(0) == 0) {
067                nextLine("NACK\n", l.toString());
068            } else {
069                nextLine("Ack from node " + l.getElement(0) + "\n", l.toString());
070            }
071        } else if (l.getNumDataElements() != 5) {
072            nextLine("Truncated reply of length " + l.getNumDataElements() + ":" + l.toString() + "\n",
073                    l.toString());
074        } else { // must be data reply
075            StringBuilder s = new StringBuilder(String.format("Receive addr=%d IB=", l.getAddr()));
076            for (int i = 2; i < 4; i++) {
077                s.append(Integer.toHexString(l.getElement(i))).append(" ");
078            }
079            nextLine(s.append("\n").toString(), l.toString());
080        }
081    }
082
083}