Search in sources :

Example 26 with NceMessage

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

the class NceMacroGenPanel method sendButtonActionPerformed.

public void sendButtonActionPerformed(java.awt.event.ActionEvent e) {
    // Send Macro
    NceMessage m = createMacroCmd(packetTextField.getText());
    if (m == null) {
        macroReply.setText("error");
        JOptionPane.showMessageDialog(this, rb.getString("EnterMacroNumber"), rb.getString("NceMacro"), JOptionPane.ERROR_MESSAGE);
        return;
    }
    macroReply.setText(rb.getString("waiting"));
    tc.sendNceMessage(m, this);
    // Unfortunately, the new command doesn't tell us if the macro is empty
    // so we send old command for status
    NceMessage m2 = createOldMacroCmd(packetTextField.getText());
    tc.sendNceMessage(m2, this);
}
Also used : NceMessage(jmri.jmrix.nce.NceMessage)

Example 27 with NceMessage

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

the class NceMacroRestore method run.

@Override
public void run() {
    // Get file to read from
    JFileChooser fc = new JFileChooser(FileUtil.getUserFilesPath());
    fc.addChoosableFileFilter(new textFilter());
    int retVal = fc.showOpenDialog(null);
    if (retVal != JFileChooser.APPROVE_OPTION) {
        // Canceled
        return;
    }
    if (fc.getSelectedFile() == null) {
        // Canceled
        return;
    }
    File f = fc.getSelectedFile();
    BufferedReader in;
    try {
        in = new BufferedReader(new FileReader(f));
    } catch (FileNotFoundException e) {
        return;
    }
    // create a status frame
    JPanel ps = new JPanel();
    jmri.util.JmriJFrame fstatus = new jmri.util.JmriJFrame("Macro Restore");
    fstatus.setLocationRelativeTo(null);
    fstatus.setSize(200, 100);
    fstatus.getContentPane().add(ps);
    ps.add(textMacro);
    ps.add(macroNumber);
    textMacro.setText("Macro number:");
    textMacro.setVisible(true);
    macroNumber.setVisible(true);
    // Now read the file and check the macro address
    waiting = 0;
    // in case we break out early
    fileValid = false;
    // for user status messages
    int macroNum = 0;
    // load the start address of the NCE macro memory
    int curMacro = CS_MACRO_MEM;
    // NCE Macro data
    byte[] macroAccy = new byte[20];
    String line = " ";
    while (true) {
        try {
            line = in.readLine();
        } catch (IOException e) {
            break;
        }
        macroNumber.setText(Integer.toString(macroNum++));
        if (line == null) {
            // while loop does not break out quick enough
            log.error("NCE macro file terminator :0000 not found");
            break;
        }
        if (log.isDebugEnabled()) {
            log.debug("macro " + line);
        }
        // check that each line contains the NCE memory address of the macro
        String macroAddr = ":" + Integer.toHexString(curMacro);
        String[] macroLine = line.split(" ");
        // check for end of macro terminator
        if (macroLine[0].equalsIgnoreCase(":0000")) {
            // success!
            fileValid = true;
            break;
        }
        if (!macroAddr.equalsIgnoreCase(macroLine[0])) {
            log.error("Restore file selected is not a vaild backup file");
            log.error("Macro addr in restore file should be " + macroAddr + " Macro addr read " + macroLine[0]);
            break;
        }
        // macro file found, give the user the choice to continue
        if (curMacro == CS_MACRO_MEM) {
            if (JOptionPane.showConfirmDialog(null, "Restore file found!  Restore can take over a minute, continue?", "NCE Macro Restore", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
                break;
            }
        }
        fstatus.setVisible(true);
        // now read the entire line from the file and create NCE messages
        for (int i = 0; i < 10; i++) {
            // i = word index, j = byte index
            int j = i << 1;
            byte[] b = StringUtil.bytesFromHexString(macroLine[i + 1]);
            macroAccy[j] = b[0];
            macroAccy[j + 1] = b[1];
        }
        NceMessage m = writeNceMacroMemory(curMacro, macroAccy, false);
        tc.sendNceMessage(m, this);
        m = writeNceMacroMemory(curMacro, macroAccy, true);
        tc.sendNceMessage(m, this);
        curMacro += MACRO_LNTH;
        // wait for writes to NCE CS to complete
        if (waiting > 0) {
            synchronized (this) {
                try {
                    wait(20000);
                } catch (InterruptedException e) {
                    // retain if needed later
                    Thread.currentThread().interrupt();
                }
            }
        }
        // failed
        if (waiting > 0) {
            log.error("timeout waiting for reply");
            break;
        }
    }
    try {
        in.close();
    } catch (IOException e) {
    }
    // kill status panel
    fstatus.dispose();
    if (fileValid) {
        JOptionPane.showMessageDialog(null, "Successful Restore!", "NCE Macro", JOptionPane.INFORMATION_MESSAGE);
    } else {
        JOptionPane.showMessageDialog(null, "Restore failed. Check console for error messages. \r\n" + "If operating at 19,200 baud, try 9600 baud.", "NCE Macro", JOptionPane.ERROR_MESSAGE);
    }
}
Also used : JPanel(javax.swing.JPanel) NceMessage(jmri.jmrix.nce.NceMessage) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) JFileChooser(javax.swing.JFileChooser) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File)

Example 28 with NceMessage

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

the class NceMacroRestore method writeNceMacroMemory.

// writes 20 bytes of NCE macro memory, and adjusts for second write
private NceMessage writeNceMacroMemory(int curMacro, byte[] b, boolean second) {
    // Expect 1 byte response
    replyLen = REPLY_1;
    waiting++;
    byte[] bl;
    // write 4 bytes
    if (second) {
        // adjust for second memory write
        curMacro += 16;
        bl = NceBinaryCommand.accMemoryWrite4(curMacro);
        int j = bl.length - 4;
        for (int i = 0; i < 4; i++, j++) {
            bl[j] = b[i + 16];
        }
    // write 16 bytes 
    } else {
        bl = NceBinaryCommand.accMemoryWriteN(curMacro, 16);
        int j = bl.length - 16;
        for (int i = 0; i < 16; i++, j++) {
            bl[j] = b[i];
        }
    }
    NceMessage m = NceMessage.createBinaryMessage(tc, bl, REPLY_1);
    return m;
}
Also used : NceMessage(jmri.jmrix.nce.NceMessage)

Example 29 with NceMessage

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

the class NcePacketGenPanel method sendButtonActionPerformed.

public void sendButtonActionPerformed(java.awt.event.ActionEvent e) {
    if (checkBoxBinCmd.isSelected()) {
        // Binary selected, convert ASCII to hex
        NceMessage m = createPacket(packetTextField.getText());
        if (m == null) {
            JOptionPane.showMessageDialog(NcePacketGenPanel.this, "Enter hexadecimal numbers only", "NCE Binary Command", JOptionPane.ERROR_MESSAGE);
            return;
        }
        m.setBinary(true);
        int replyLen = getReplyLen(replyLenTextField.getText());
        if (replyLen > 0) {
            m.setReplyLen(replyLen);
        } else {
            m.setReplyLen(getMessageLength(m.getOpCode()));
        }
        tc.sendNceMessage(m, this);
    } else {
        // ASCII Mode selected
        NceMessage m = new NceMessage(packetTextField.getText().length());
        for (int i = 0; i < packetTextField.getText().length(); i++) {
            m.setElement(i, packetTextField.getText().charAt(i));
        }
        tc.sendNceMessage(m, this);
    }
}
Also used : NceMessage(jmri.jmrix.nce.NceMessage)

Example 30 with NceMessage

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

the class NcePacketGenPanel method createPacket.

// ignore replies
NceMessage createPacket(String s) {
    // gather bytes in result
    byte[] b;
    try {
        b = StringUtil.bytesFromHexString(s);
    } catch (NumberFormatException e) {
        return null;
    }
    if (b.length == 0) {
        // no such thing as a zero-length message
        return null;
    }
    NceMessage m = new NceMessage(b.length);
    for (int i = 0; i < b.length; i++) {
        m.setElement(i, b[i]);
    }
    return m;
}
Also used : NceMessage(jmri.jmrix.nce.NceMessage)

Aggregations

NceMessage (jmri.jmrix.nce.NceMessage)46 IOException (java.io.IOException)3 BufferedReader (java.io.BufferedReader)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 FileReader (java.io.FileReader)2 JFileChooser (javax.swing.JFileChooser)2 JPanel (javax.swing.JPanel)2 NceReply (jmri.jmrix.nce.NceReply)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1