use of jmri.jmrix.AbstractMRMessage in project JMRI by JMRI.
the class AbstractCanTrafficController method forwardToPort.
/**
* Actually transmits the next message to the port
*
* Overridden to include translation to the correct CAN hardware message
* format
*/
@Override
protected void forwardToPort(AbstractMRMessage m, AbstractMRListener reply) {
// if (log.isDebugEnabled()) log.debug("forwardToPort message: ["+m+"]");
//warn
log.debug("forwardToPort message: [" + m + "]");
// remember who sent this
mLastSender = reply;
// forward the message to the registered recipients,
// which includes the communications monitor, except the sender.
// Schedule notification via the Swing event queue to ensure order
Runnable r = new XmtNotifier(m, mLastSender, this);
javax.swing.SwingUtilities.invokeLater(r);
// Create the correct concrete class for sending to the hardware
AbstractMRMessage hm = newMessage();
// Encode the message to be sent
if (((CanMessage) m).isTranslated()) {
hm = m;
} else {
hm = encodeForHardware((CanMessage) m);
}
log.debug("Encoded for hardware: [" + hm.toString() + "]");
// stream to port in single write, as that's needed by serial
byte[] msg = new byte[lengthOfByteStream(hm)];
// add header
int offset = addHeaderToOutput(msg, hm);
// add data content
int len = hm.getNumDataElements();
for (int i = 0; i < len; i++) {
msg[i + offset] = (byte) hm.getElement(i);
}
// add trailer
addTrailerToOutput(msg, len + offset, hm);
// and stream the bytes
try {
if (ostream != null) {
if (log.isDebugEnabled()) {
//String f = "formatted message: ";
StringBuffer buf = new StringBuffer("formatted message: ");
//for (int i = 0; i<msg.length; i++) f=f+Integer.toHexString(0xFF&msg[i])+" ";
for (int i = 0; i < msg.length; i++) {
buf.append(Integer.toHexString(0xFF & msg[i]));
buf.append(" ");
}
log.debug(buf.toString());
}
while (hm.getRetries() >= 0) {
if (portReadyToSend(controller)) {
ostream.write(msg);
log.debug("message written");
break;
} else if (hm.getRetries() >= 0) {
if (log.isDebugEnabled()) {
log.debug("Retry message: " + hm.toString() + " attempts remaining: " + hm.getRetries());
}
hm.setRetries(hm.getRetries() - 1);
try {
synchronized (xmtRunnable) {
xmtRunnable.wait(hm.getTimeout());
}
} catch (InterruptedException e) {
// retain if needed later
Thread.currentThread().interrupt();
log.error("retry wait interupted");
}
} else {
log.warn("sendMessage: port not ready for data sending: " + Arrays.toString(msg));
}
}
} else {
// no stream connected
connectionWarn();
}
} catch (Exception e) {
portWarn(e);
}
}
use of jmri.jmrix.AbstractMRMessage in project JMRI by JMRI.
the class LawicellTrafficController method newMessage.
// New message for hardware protocol
@Override
protected AbstractMRMessage newMessage() {
log.debug("New Message created");
Message msg = new Message();
return msg;
}
use of jmri.jmrix.AbstractMRMessage in project JMRI by JMRI.
the class LawicellTrafficController method encodeForHardware.
/**
* Encode a CanMessage for the hardware
*/
@Override
public AbstractMRMessage encodeForHardware(CanMessage m) {
log.debug("Encoding for hardware");
Message ret = new Message(m);
if (log.isDebugEnabled()) {
log.debug("encoded as " + ret);
}
return ret;
}
use of jmri.jmrix.AbstractMRMessage in project JMRI by JMRI.
the class SerialTrafficController method pollMessage.
/**
* Handles initialization, output and polling for C/MRI Serial Nodes from
* within the running thread
*/
@Override
protected synchronized AbstractMRMessage pollMessage() {
// ensure validity of call
if (getNumNodes() <= 0) {
return null;
}
int previousPollPointer = curSerialNodeIndex;
// service next node next
updatePollPointer();
// ensure that each node is initialized
if (getMustInit(curSerialNodeIndex)) {
setMustInit(curSerialNodeIndex, false);
AbstractMRMessage m = getNode(curSerialNodeIndex).createInitPacket();
log.debug("send init message: " + m);
// wait for init to finish (milliseconds)
m.setTimeout(500);
return m;
}
// send Output packet if needed
if (getNode(curSerialNodeIndex).mustSend()) {
log.debug("request write command to send");
getNode(curSerialNodeIndex).resetMustSend();
AbstractMRMessage m = getNode(curSerialNodeIndex).createOutPacket();
// no need to wait for output to answer
m.setTimeout(2);
// reset poll pointer update, so next increment will poll from here
curSerialNodeIndex = previousPollPointer;
return m;
}
// poll for Sensor input
if (getNode(curSerialNodeIndex).getSensorsActive()) {
// Some sensors are active for this node, issue poll
SerialMessage m = SerialMessage.getPoll(getNode(curSerialNodeIndex).getNodeAddress());
return m;
} else {
// no Sensors (inputs) are active for this node
return null;
}
}
use of jmri.jmrix.AbstractMRMessage in project JMRI by JMRI.
the class AcelaNodeTest method testOutputBits1.
public void testOutputBits1() {
Assert.assertTrue("must Send", a1.mustSend());
a1.resetMustSend();
Assert.assertTrue("must Send off", !(a1.mustSend()));
a1.setOutputBit(0, false);
a1.setOutputBit(1, true);
a1.setOutputBit(2, true);
a1.setOutputBit(3, false);
Assert.assertEquals("Out StartingOutputAddress D8", 4, a2.getStartingOutputAddress());
Assert.assertEquals("Out EndingOutputAddress D8", 11, a2.getEndingOutputAddress());
a2.setOutputBit(4, true);
a2.setOutputBit(5, false);
a2.setOutputBit(6, false);
a2.setOutputBit(7, true);
a2.setOutputBit(8, true);
a2.setOutputBit(9, false);
a2.setOutputBit(10, false);
a2.setOutputBit(11, true);
Assert.assertTrue("must Send on", a1.mustSend());
AbstractMRMessage m1 = a1.createOutPacket();
Assert.assertEquals("m1 packet size", 4, m1.getNumDataElements());
Assert.assertEquals("m1 command", 7, m1.getElement(0) & 0xff);
// 'T'
Assert.assertEquals("m1 address high", 0, m1.getElement(1) & 0xff);
Assert.assertEquals("m1 address low", 0, (m1.getElement(2) & 0xff));
Assert.assertEquals("m1 value", 6, (m1.getElement(3) & 0xff));
AbstractMRMessage m2 = a2.createOutPacket();
Assert.assertEquals("m2 packet size", 4, m2.getNumDataElements());
Assert.assertEquals("m2 command", 8, m2.getElement(0) & 0xff);
// 'T'
Assert.assertEquals("m2 address high", 0, m2.getElement(1) & 0xff);
Assert.assertEquals("m2 address low", 4, (m2.getElement(2) & 0xff));
Assert.assertEquals("m2 value", 153, (m2.getElement(3) & 0xff));
}
Aggregations