use of jmri.jmrix.can.CanMessage in project JMRI by JMRI.
the class CbusCommandStation method setSpeedSteps.
/**
* Send a CBUS message to change the session speed step mode
*
* @param mode the speed step mode
*/
protected void setSpeedSteps(int handle, int mode) {
log.debug("Set speed step mode " + mode + " for session handle" + handle);
CanMessage msg = new CanMessage(3, tc.getCanid());
msg.setOpCode(CbusConstants.CBUS_STMOD);
msg.setElement(1, handle);
msg.setElement(2, mode);
tc.sendCanMessage(msg, this);
}
use of jmri.jmrix.can.CanMessage in project JMRI by JMRI.
the class CanSendPane method createPacket.
/**
* Create a well-formed message from a String String is expected to be space
* seperated hex bytes or CbusAddress, e.g.: 12 34 56 +n4e1
*
* @return The packet, with contents filled-in
*/
CanMessage createPacket(String s) {
CanMessage m;
// Try to convert using CbusAddress class
CbusAddress a = new CbusAddress(s);
if (a.check()) {
m = a.makeMessage(tc.getCanid());
} else {
m = new CanMessage(tc.getCanid());
// check for header
if (s.charAt(0) == '[') {
// extended header
m.setExtended(true);
int i = s.indexOf(']');
String h = s.substring(1, i);
m.setHeader(Integer.parseInt(h, 16));
s = s.substring(i + 1, s.length());
} else if (s.charAt(0) == '(') {
// standard header
int i = s.indexOf(')');
String h = s.substring(1, i);
m.setHeader(Integer.parseInt(h, 16));
s = s.substring(i + 1, s.length());
}
// Try to get hex bytes
byte[] b = StringUtil.bytesFromHexString(s);
m.setNumDataElements(b.length);
// Use &0xff to ensure signed bytes are stored as unsigned ints
for (int i = 0; i < b.length; i++) {
m.setElement(i, b[i] & 0xff);
}
}
return m;
}
use of jmri.jmrix.can.CanMessage in project JMRI by JMRI.
the class OpenLcbCanSendPane method createPacket.
/**
* Create a well-formed message from a String String is expected to be space
* seperated hex bytes or CbusAddress, e.g.: 12 34 56 +n4e1
* @param s string of spaced hex byte codes
* @return The packet, with contents filled-in
*/
CanMessage createPacket(String s) {
CanMessage m;
// Try to convert using CbusAddress class to reuse a little code
CbusAddress a = new CbusAddress(s);
if (a.check()) {
m = a.makeMessage(tc.getCanid());
} else {
m = new CanMessage(tc.getCanid());
// check for header
if (s.charAt(0) == '[') {
// NOI18N
// extended header
m.setExtended(true);
// NOI18N
int i = s.indexOf(']');
String h = s.substring(1, i);
m.setHeader(Integer.parseInt(h, 16));
s = s.substring(i + 1, s.length());
} else if (s.charAt(0) == '(') {
// NOI18N
// standard header
// NOI18N
int i = s.indexOf(')');
String h = s.substring(1, i);
m.setHeader(Integer.parseInt(h, 16));
s = s.substring(i + 1, s.length());
}
// Try to get hex bytes
byte[] b = StringUtil.bytesFromHexString(s);
m.setNumDataElements(b.length);
// Use &0xff to ensure signed bytes are stored as unsigned ints
for (int i = 0; i < b.length; i++) {
m.setElement(i, b[i] & 0xff);
}
}
return m;
}
use of jmri.jmrix.can.CanMessage in project JMRI by JMRI.
the class HubPane method startHubThread.
void startHubThread(int port) {
t = new Thread() {
@Override
public void run() {
hub.start();
}
};
t.setDaemon(true);
// add forwarder for internal JMRI traffic
hub.addForwarder(new Hub.Forwarding() {
@Override
public void forward(Hub.Memo m) {
if (m.source == null) {
// was from this
return;
}
// process and forward m.line;
GridConnectReply msg = new GridConnectReply();
byte[] bytes;
try {
// GC adapters use ASCII // NOI18N
bytes = m.line.getBytes("US-ASCII");
} catch (java.io.UnsupportedEncodingException e) {
log.error("Cannot proceed with GC input message since US-ASCII not supported");
return;
}
for (int i = 0; i < m.line.length(); i++) {
msg.setElement(i, bytes[i]);
}
workingReply = msg.createReply();
CanMessage result = new CanMessage(workingReply.getNumDataElements(), workingReply.getHeader());
for (int i = 0; i < workingReply.getNumDataElements(); i++) {
result.setElement(i, workingReply.getElement(i));
}
result.setExtended(workingReply.isExtended());
// Send over outbound link
memo.getTrafficController().sendCanMessage(result, HubPane.this);
// And send into JMRI
memo.getTrafficController().distributeOneReply(workingReply, HubPane.this);
}
});
t.start();
advertise(port);
}
use of jmri.jmrix.can.CanMessage in project JMRI by JMRI.
the class HubPane method reply.
@Override
public synchronized void reply(CanReply reply) {
if (reply != workingReply) {
GridConnectMessage gm = new GridConnectMessage(new CanMessage(reply));
if (log.isDebugEnabled()) {
log.debug("reply " + gm.toString());
}
hub.putLine(gm.toString());
}
}
Aggregations