Here is my above CAN message broken down for you, and with the necessary declarations to explain what's happening:
Firstly, you need to open a CAN communications transmit handle, the false in this function is specifying that the message needs to be transmitted in little endian format with the handle being named 'h' for the purposes of re-use throughout further implementations of the code:
- Code: Select all
local h = CanComms.TxOpen(false);
Next, i would normally specify a variable called 'bus' for the CANBus, again this is for the purpose of re-use throughout the code (remembering here, that i'm referencing an external input parameter called 'CAN Bus', this being a user selectable enumerator from within M1Tune):
- Code: Select all
local bus = CAN Bus.AsInteger() - 1;
Following this, i usually also setup a variable called 'ok', this variable will contain the result of the CAN message transmission. What this means is that you can have an additional piece of code called if there is a problem with the message (the default state i set is 'True'):
- Code: Select all
local ok = true;
After opening the transmit handle, you need to initialise a new CAN message, the parameters specified are the handle (which we created above), and the length of the CAN message:
- Code: Select all
CanComms.TxInitialise(h, 8);
At this point, you now construct the message with the information you wish to send.
The information can be sent as individual bits, or integers etc, it's entirely dependant on the information you want to transmit.
In my example, the first part of the message contains the ambient pressure value, sent as an unsigned integer beginning at position 0 in the message, for 8 bits.
- Code: Select all
CanComms.SetUnsignedInteger(h, 0, 8, Convert.ToUnsignedInteger(Ambient Pressure);
The second part of my message, is transmitting the engine state as an on/off flag (or bit) in position 8 of the message. Because it's a Bit, there is no need to specify a length.
- Code: Select all
CanComms.SetBit (h, 8, Engine State.AsInteger() < 2);
Lastly, you tell the ECU to transmit the message that you have constructed above. This function requires the handle, the CANBus to transmit using, and the address to send the message on.
- Code: Select all
ok = ok and CanComms.TxStandard(h, bus, 0x999);
At this point, the 'ok' variable will be 'True' if the message was successful, or 'False' if not.
This is the part where you would put your code to take action if the event was unsuccessful.
This part is required to avoid an issue when compiling in build, where you have not used the variable.
- Code: Select all
ok = ok;
Hopefully this breaks down the message a bit more for you.
If you need more information on CAN messaging there are some webinars that the guys at MoTeC have put online, that should explain some stuff in more detail for you.
As I mentioned, build assumes a prior level of programming knowledge suited to an advanced user, there is some documentation within the help section of the build software that may point you in the right direction though.