
— 35 —
Thanks to the system hardware filters (see VII.3), messages which are required for the normal operation
of the NMEA 2000 network (ISO Acknowledgement, ISO Request, ISO Address Claim) will be forwarded
between CAN1 and CAN2 interfaces unmodified.
In this program a hardware filter for CAN2 interface is not defined, so Device will add a default filter 1
to accept all messages from CAN2 and pass them to filters for further processing (see VII.3). For CAN1
interface, CAN1_HARDWARE_FILTER_1 is set up to block all PGNs on CAN1 interface. As a result, all
messages received on CAN1 will be dropped before they reach
match()
filters, which saves a lot of CPU
resources.
2. Use match() instead of multiple if()
One may be tempted to use multiple
if()
statements to process different PGNs inside a single filter like in
Example 8.a below.
This technique is inferior to
match()
filters usage, as
match()
filters use hardware-accelerated CAN
ID comparison — adding additional
match()
filters does not introduce extra overhead or wasted CPU
resources. On the other hand,
if()
is executed on Device virtual machine, hence is several times slower.
Example 8.a (incorrect)
match(CAN1,0x00000010,0x000000FF) # sent from 0x10 address?
{
p = (get(0,UINT32) >> 8) & 0x1FFFF # get the PGN
if (p == 0x1FD0A) # is it "Actual Pressure" message?
{
# process data
}
}