Search in sources :

Example 1 with NceReply

use of jmri.jmrix.nce.NceReply in project JMRI by JMRI.

the class ClockMonPanel method readClockPacket.

private void readClockPacket(NceReply r) {
    NceReply priorClockReadPacket = lastClockReadPacket;
    int priorNceRatio = nceLastRatio;
    boolean priorNceRunning = nceLastRunning;
    lastClockReadPacket = r;
    //lastClockReadAtTime = internalClock.getTime();
    //log.debug("readClockPacket - at time: " + lastClockReadAtTime);
    nceLastHour = r.getElement(CS_CLOCK_HOURS) & 0xFF;
    nceLastMinute = r.getElement(CS_CLOCK_MINUTES) & 0xFF;
    nceLastSecond = r.getElement(CS_CLOCK_SECONDS) & 0xFF;
    if (r.getElement(CS_CLOCK_1224) == 1) {
        nceLast1224 = true;
    } else {
        nceLast1224 = false;
    }
    if (r.getElement(CS_CLOCK_AMPM) == 'A') {
        nceLastAmPm = true;
    } else {
        nceLastAmPm = false;
    }
    int sc = r.getElement(CS_CLOCK_SCALE) & 0xFF;
    if (sc > 0) {
        nceLastRatio = 250 / sc;
    }
    if (clockMode == SYNCMODE_NCE_MASTER) {
        if (priorClockReadPacket != null && priorNceRatio != nceLastRatio) {
            if (log.isDebugEnabled()) {
                log.debug("NCE Change Rate from cab: prior vs last: " + priorNceRatio + " vs " + nceLastRatio);
            }
            rateNce.setText("" + nceLastRatio);
            nceSyncInitStateCounter = 1;
            nceSyncInitStates();
        }
    }
    if (r.getElement(CS_CLOCK_STATUS) == 1) {
        nceLastRunning = false;
    } else {
        nceLastRunning = true;
    }
    if (clockMode == SYNCMODE_NCE_MASTER) {
        if (priorClockReadPacket != null && priorNceRunning != nceLastRunning) {
            if (log.isDebugEnabled()) {
                log.debug("NCE Stop/Start: prior vs last: " + priorNceRunning + " vs " + nceLastRunning);
            }
            if (nceLastRunning) {
                nceSyncInitStateCounter = 1;
            } else {
                nceSyncInitStateCounter = -1;
            }
            nceSyncInitStates();
            internalClock.setRun(nceLastRunning);
        }
    }
    updateSettingsFromNce();
}
Also used : NceReply(jmri.jmrix.nce.NceReply)

Example 2 with NceReply

use of jmri.jmrix.nce.NceReply in project JMRI by JMRI.

the class NceMonPanelTest method testReply.

@Test
@Ignore("see comments below for corrections required.")
public void testReply() {
    // Prior to JUnit4 conversion, this test method was commented out with a note reading
    // Following are timing-specific, occasionally fail, so commented out
    NceReply m = new NceReply(memo.getNceTrafficController());
    m.setBinary(false);
    m.setOpCode('C');
    m.setElement(1, 'o');
    m.setElement(2, ':');
    ((NceMonPanel) pane).reply(m);
// The following assertions need to be re-written.  There is no
// current method for retrieving the text panel from the NceMonPanel.
//Assert.assertEquals("display", "rep: \"Co:\"\n", ((NceMonPanel)pane).getPanelText()); 
//Assert.assertEquals("length ", "rep: \"Co:\"\n".length(), ((NceMonPanel)pane).getPanelText().length());
}
Also used : NceReply(jmri.jmrix.nce.NceReply) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with NceReply

use of jmri.jmrix.nce.NceReply in project JMRI by JMRI.

the class SimulatorAdapter method run.

@Override
public void run() {
    // report status?
    if (log.isInfoEnabled()) {
        log.info("NCE Simulator Started");
    }
    while (true) {
        try {
            wait(100);
        } catch (Exception e) {
        }
        NceMessage m = readMessage();
        if (log.isDebugEnabled()) {
            StringBuffer buf = new StringBuffer();
            buf.append("Nce Simulator Thread received message: ");
            for (int i = 0; i < m.getNumDataElements(); i++) {
                buf.append(Integer.toHexString(0xFF & m.getElement(i)) + " ");
            }
            log.debug(buf.toString());
        }
        if (m != null) {
            NceReply r = generateReply(m);
            writeReply(r);
            if (log.isDebugEnabled() && r != null) {
                StringBuffer buf = new StringBuffer();
                buf.append("Nce Simulator Thread sent reply: ");
                for (int i = 0; i < r.getNumDataElements(); i++) {
                    buf.append(Integer.toHexString(0xFF & r.getElement(i)) + " ");
                }
                log.debug(buf.toString());
            }
        }
    }
}
Also used : NceMessage(jmri.jmrix.nce.NceMessage) NceReply(jmri.jmrix.nce.NceReply) IOException(java.io.IOException)

Example 4 with NceReply

use of jmri.jmrix.nce.NceReply in project JMRI by JMRI.

the class SimulatorAdapter method generateReply.

// generateReply is the heart of the simulation.  It translates an 
// incoming NceMessage into an outgoing NceReply.
private NceReply generateReply(NceMessage m) {
    NceReply reply = new NceReply(this.getSystemConnectionMemo().getNceTrafficController());
    int command = m.getElement(0);
    if (// NOTE: NCE command station does not respond to
    command < 0x80) {
        // command less than 0x80 (times out)
        return null;
    }
    if (command > 0xBF) {
        // Command is out of range
        // Nce command not supported
        reply.setElement(0, NCE_ERROR);
        return reply;
    }
    switch(command) {
        case // Get Eprom revision
        NceBinaryCommand.SW_REV_CMD:
            // Send Eprom revision 6 2 1
            reply.setElement(0, 0x06);
            reply.setElement(1, 0x02);
            reply.setElement(2, 0x01);
            break;
        case // Read clock
        NceBinaryCommand.READ_CLOCK_CMD:
            // Return fixed time
            reply.setElement(0, 0x12);
            reply.setElement(1, 0x30);
            break;
        case // Read AUI 4 byte response
        NceBinaryCommand.READ_AUI4_CMD:
            // fixed data for now
            reply.setElement(0, 0xFF);
            // fixed data for now
            reply.setElement(1, 0xFF);
            // fixed data for now
            reply.setElement(2, 0x00);
            // fixed data for now
            reply.setElement(3, 0x00);
            break;
        case // Dummy instruction
        NceBinaryCommand.DUMMY_CMD:
            // return ! CR LF
            reply.setElement(0, NCE_OKAY);
            reply.setElement(1, 0x0D);
            reply.setElement(2, 0x0A);
            break;
        case // Read 16 bytes
        NceBinaryCommand.READ16_CMD:
            readMemory(m, reply, 16);
            break;
        case // Read AUI 2 byte response
        NceBinaryCommand.READ_AUI2_CMD:
            // fixed data for now
            reply.setElement(0, 0x00);
            // fixed data for now
            reply.setElement(1, 0x00);
            break;
        case // Read 1 bytes
        NceBinaryCommand.READ1_CMD:
            readMemory(m, reply, 1);
            break;
        case // Write 1 bytes
        NceBinaryCommand.WRITE1_CMD:
            writeMemory(m, reply, 1, false);
            break;
        case // Write 2 bytes
        NceBinaryCommand.WRITE2_CMD:
            writeMemory(m, reply, 2, false);
            break;
        case // Write 4 bytes
        NceBinaryCommand.WRITE4_CMD:
            writeMemory(m, reply, 4, false);
            break;
        case // Write 8 bytes
        NceBinaryCommand.WRITE8_CMD:
            writeMemory(m, reply, 8, false);
            break;
        case // Write n bytes
        NceBinaryCommand.WRITE_N_CMD:
            writeMemory(m, reply, m.getElement(3), true);
            break;
        case // accessory command
        NceBinaryCommand.ACC_CMD:
            accessoryCommand(m, reply);
            break;
        case NceMessage.READ_DIR_CV_CMD:
        case NceMessage.READ_PAGED_CV_CMD:
        case NceMessage.READ_REG_CMD:
            // dummy data
            reply.setElement(0, 123);
            //reply.setElement(1,NCE_DATA_OUT_OF_RANGE);  // forces fail
            // forces succeed
            reply.setElement(1, NCE_OKAY);
            break;
        default:
            // Nce okay reply!
            reply.setElement(0, NCE_OKAY);
    }
    return reply;
}
Also used : NceReply(jmri.jmrix.nce.NceReply)

Aggregations

NceReply (jmri.jmrix.nce.NceReply)4 IOException (java.io.IOException)1 NceMessage (jmri.jmrix.nce.NceMessage)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1