Code for receiving Compound CAN messages
Fast_Moto wrote:
/*Opens the Can Comms for messages with CAN address 1520 (0x123). The address mask 0x000 limits this to this single address. True indicates (normal) byte order.
*/
local h = CanComms.RxOpenStandard(0,0x123,0x000,true);
//Verifying address of received message
/*CanComms.GetID() retrieves the address of the received CAN Message. By subtracting the desired CAN message address, you can verify whether the received message is the message you are looking for. */
local messageid = CanComms.GetID(h) - 0x123;
local received = false;
if (messageid eq 0)
{
received = true;
}
else
{
received = false;
}
local timeout = Delay.Rising(received eq false, 1.0);
/* receives the desired channel 1 from the specified compound message and message slot.*/
static local channel1 = 0.0;
static local channel2 = 0.0;
local compoundid = CanComms.GetUnsignedInteger(h, 0, 8); /*this obtains the id of the compound message */
/*determines if message is received */
if (timeout eq true)
{ // No data coming in so set defaults
input = 0.0;
Status = Status.Fault;
}
/*Obtains desired channel if message at desired address is received*/
else
{
// obtain channel1 from CAN address 0x123, Compound ID 2, Offset 2 (16)
if (compoundid eq 0x0002)
{
channel 1 = CanComms.GetUnsignedInteger(h, 16, 16);
Status = Status.Ok;
}
// obtain channel2 from CAN address 0x123, Compound ID 0, Offset 3 (32)
if (compoundid eq 0x0000)
{
channel 2 = CanComms.GetUnsignedInteger(h, 32, 16);
Status = Status.Ok;
}
}
-------------------------------------------
Additional compound message channels can also be received by selecting a different compound id and the corresponding offset in the desired compound message. Please note that the channels received used no multiplier, divisor or adder.
Motec's code for receiving sequential CAN messages
YuriK wrote:Hi,
...
2. CAN Communications - various addresses can be received in a single function or in separate functions. You need to keep in mind that number of communication handles is limited (to approximately 60) so opening too many handles may result in missed messages. Each functions needs to be called at the combined rate of the messages to be received. here is a code example, you've asked about
/* An example for receiving 16 CAN UDs with a single handle. The script
need to run fast enough to receive all messages,
as only one message is received each time */
local h = CanComms.RxOpenStandard(0, 0x5E0, 0x00f, true); // receive 0x5E0 to 0x40f, big endian (normal) byte order
local received = CanComms.RxMessage(h);
local timeout = Delay.Rising(received eq false, 1.0);
if (received)
{
local id = CanComms.GetID(h) & 0x00f;
if (id eq 0x0)
{
Channel 1 = CanComms.GetUnsignedInteger(h, 0, 16) * 1.0e-3;
// etc
}
else if (id eq 0x1)
{
Channel 2 = CanComms.GetUnsignedInteger(h, 0, 16) * 1.0e-3;
// etc
}
...
else if (id eq 0xf)
{
Channel 3 = CanComms.GetUnsignedInteger(h, 0, 16) * 1.0e-3;
// etc
}
}
...
Regards!