Files
JIMRI/help/en/html/hardware/can/scripting.shtml
T
2026-06-17 14:00:51 +02:00

77 lines
2.6 KiB
Plaintext

<!DOCTYPE html>
<html lang="en">
<head>
<meta name="generator" content="HTML Tidy for HTML5 for Apple macOS version 5.8.0">
<title>JMRI Hardware Support - CAN Network - Scripting</title>
<!--#include virtual="/help/en/parts/Style.shtml" -->
</head>
<body>
<!--#include virtual="/help/en/parts/Header.shtml" -->
<div id="mBody">
<!--#include virtual="Sidebar.shtml" -->
<div id="mainContent">
<h1>Hardware Support: CAN - Scripting</h1>
<p>JMRI scripting can be used to send and receive CAN frames. You can use this to create test
routines for boards under development, etc.</p>
<p>This page describes the CAN extensions for JMRI scripting. For more information on JMRI
scripting itself, see the <a href="../../tools/scripting/index.shtml">Scripting Help
pages</a>.</p>
<p>There's a basic <a href="../../../../../jython/CanExample.py">CanExample.py</a> sample
script in the jython directory.</p>
<h2>Scripting Code Examples</h2>
<h3>Sending CAN Frames</h3>
<p>CAN frames are created as objects, then queued for transmission. The header (ID) and data
are handled separately.</p>
<pre style="font-family: monospace;">
frame = jmri.jmrix.can.CanMessage(2) # will load 2 bytes
frame.setId(0x123)
frame.setElement(0, 0x45)
frame.setElement(1, 0x67)
jmri.jmrix.can.TrafficController.instance().sendCanMessage(frame, None)
</pre>
<h3>Receiving CAN Frames</h3>
<p>A script can listen for CAN frames as they're received from the bus, and separately for
CAN frames that are being transmitted on the bus by JMRI. The CanListener interface provides
these separately via "reply" and "message" procedures, respectively.</p>
<pre style="font-family: monospace;">
class MyCanListener (jmri.jmrix.can.CanListener) :
def message(self, msg) :
# handle transmitted CAN frames by ignoring them
return
def reply(self, msg) :
# handle received CAN frames by printing their content
print "received Frame"
print "ID: 0x"+java.lang.Integer.toHexString(msg.getId())
print "content: ", msg.toString()
return
jmri.jmrix.can.TrafficController.instance().addCanListener(MyCanListener())
</pre>
<h2 id="documentation">Documentation</h2>
<h3>JMRI Help</h3>
<ul>
<li>Back to the <a href="index.shtml">Main CAN Network Support Help page</a>.
</li>
</ul>
<!--#include virtual="/help/en/parts/Footer.shtml" -->
</div>
<!-- closes #mainContent-->
</div>
<!-- closes #mBody-->
<script src="/js/help.js"></script>
</body>
</html>