use of jmri.jmrix.nce.NceMessage in project JMRI by JMRI.
the class NceConsistBackup method readConsistMemory.
// Reads 16 bytes of NCE consist memory
private NceMessage readConsistMemory(int consistNum) {
int nceConsistAddr = (consistNum * CONSIST_LNTH) + NceCmdStationMemory.CabMemorySerial.CS_CONSIST_MEM;
// Expect 16 byte response
replyLen = NceMessage.REPLY_16;
waiting++;
byte[] bl = NceBinaryCommand.accMemoryRead(nceConsistAddr);
NceMessage m = NceMessage.createBinaryMessage(tc, bl, NceMessage.REPLY_16);
return m;
}
use of jmri.jmrix.nce.NceMessage in project JMRI by JMRI.
the class NceConsistRestore 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("Consist Restore");
fstatus.setLocationRelativeTo(null);
fstatus.setSize(200, 100);
fstatus.getContentPane().add(ps);
ps.add(textConsist);
ps.add(consistNumber);
textConsist.setText("Consist line number:");
textConsist.setVisible(true);
consistNumber.setVisible(true);
// Now read the file and check the consist address
waiting = 0;
// in case we break out early
fileValid = false;
// for user status messages
int consistNum = 0;
// load the start address of the NCE consist memory
int curConsist = CS_CONSIST_MEM;
// NCE Consist data
byte[] consistData = new byte[CONSIST_LNTH];
String line = " ";
while (true) {
try {
line = in.readLine();
} catch (IOException e) {
break;
}
consistNumber.setText(Integer.toString(consistNum++));
if (line == null) {
// while loop does not break out quick enough
log.error("NCE consist file terminator :0000 not found");
break;
}
if (log.isDebugEnabled()) {
log.debug("consist " + line);
}
// check that each line contains the NCE memory address of the consist
String consistAddr = ":" + Integer.toHexString(curConsist);
String[] consistLine = line.split(" ");
// check for end of consist terminator
if (consistLine[0].equalsIgnoreCase(":0000")) {
// success!
fileValid = true;
break;
}
if (!consistAddr.equalsIgnoreCase(consistLine[0])) {
log.error("Restore file selected is not a vaild backup file");
log.error("Consist memory address in restore file should be " + consistAddr + " Consist address read " + consistLine[0]);
break;
}
// consist file found, give the user the choice to continue
if (curConsist == CS_CONSIST_MEM) {
if (JOptionPane.showConfirmDialog(null, "Restore file found! Restore can take over a minute, continue?", "NCE Consist Restore", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
break;
}
}
fstatus.setVisible(true);
// now read the entire line from the file and create NCE message
for (int i = 0; i < 8; i++) {
// i = word index, j = byte index
int j = i << 1;
byte[] b = StringUtil.bytesFromHexString(consistLine[i + 1]);
consistData[j] = b[0];
consistData[j + 1] = b[1];
}
NceMessage m = writeNceConsistMemory(curConsist, consistData);
tc.sendNceMessage(m, this);
curConsist += CONSIST_LNTH;
// wait for write 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 Consist", 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 Consist", JOptionPane.ERROR_MESSAGE);
}
}
use of jmri.jmrix.nce.NceMessage in project JMRI by JMRI.
the class NceConsistRestore method writeNceConsistMemory.
// writes 16 bytes of NCE consist memory
private NceMessage writeNceConsistMemory(int curConsist, byte[] b) {
// Expect 1 byte response
replyLen = REPLY_1;
waiting++;
byte[] bl;
bl = NceBinaryCommand.accMemoryWriteN(curConsist, 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;
}
use of jmri.jmrix.nce.NceMessage in project JMRI by JMRI.
the class NceMacroEditPanel method readUsbMemoryN.
// USB Read N bytes of NCE cab memory
private void readUsbMemoryN(int num) {
switch(num) {
case 1:
// Expect 1 byte response
replyLen = NceMessage.REPLY_1;
break;
case 2:
// Expect 2 byte response
replyLen = NceMessage.REPLY_2;
break;
case 4:
// Expect 4 byte response
replyLen = NceMessage.REPLY_4;
break;
default:
log.error("Invalid usb read byte count");
return;
}
waiting++;
byte[] bl = NceBinaryCommand.usbMemoryRead((byte) num);
NceMessage m = NceMessage.createBinaryMessage(tc, bl, replyLen);
tc.sendNceMessage(m, this);
}
use of jmri.jmrix.nce.NceMessage in project JMRI by JMRI.
the class NceMacroGenPanel method createOldMacroCmd.
NceMessage createOldMacroCmd(String s) {
int macroNum = 0;
try {
macroNum = Integer.parseInt(s);
} catch (NumberFormatException e) {
return null;
}
if (macroNum < 0 || macroNum > 255) {
return null;
}
// NCE responds with okay (!) if macro exist, (0) if not
NceMessage m = new NceMessage(2);
// Macro cmd
m.setElement(0, NceBinaryCommand.MACRO_CMD);
// Macro #
m.setElement(1, macroNum);
m.setBinary(true);
m.setReplyLen(REPLY_LEN);
return m;
}
Aggregations