CAN Messaging

Support forum for users of the M1 Build

CAN Messaging

Postby Fast_Moto on Thu Apr 03, 2014 5:38 am

A while ago, I asked Motec for an example code to receive compound messages (i.e. alternating the channels sent in the messages sent on a single address. After reading the response from Motec, it appears that the example provided (below) merely received ordinary (rather than compound) messages sent on different addresses. So, to help others, I have included some sample code for receiving compound messages below. In addition, I also had a couple of questions regarding Motec's CAN sensors below.

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!
Fast_Moto
 
Posts: 111
Joined: Mon Apr 19, 2010 12:49 pm

Re: CAN Messaging

Postby Fast_Moto on Thu Apr 03, 2014 5:40 am

I had a couple questions regarding the CanComms.RxOpenStandard function
1. Is the “address mask” applied as a bitwise AND Operation to the “11-bit Address match” to determine which CAN comms are received? If not, how is the mask applied?
2. Does the length of the mask need to match the length of the address?
3. Does the “address match” have to be in hexadecimal form?
4. If an address match have to be in hexadecimal form, and I want a user to designate a receiving address, is there a function available to convert decimal to hexadecimal?
Fast_Moto
 
Posts: 111
Joined: Mon Apr 19, 2010 12:49 pm

Re: CAN Messaging

Postby Fast_Moto on Thu Apr 03, 2014 5:41 am

Can the CAN Sensors in the Motec M1 build receive compound messages? Best as I can tell, they can not.
Fast_Moto
 
Posts: 111
Joined: Mon Apr 19, 2010 12:49 pm

Re: CAN Messaging

Postby AlanB on Thu Apr 03, 2014 7:08 pm

No, the 'Sensor with CAN' classes utilise sequential messages only, if you wish to receive compound messages you will need to use a custom script.
Alan Bell
MoTeC Europe Ltd
User avatar
AlanB
 
Posts: 104
Joined: Tue May 20, 2008 11:25 pm


Return to M1 Build

Who is online

Users browsing this forum: No registered users and 1 guest