I'm looking for some guidance on implementing LIN in Build. From what I've gathered so far, I think I need:
To send data:
1 - initialise the port using a Serial Port class object (LIN port, LIN protocol)
2 - open a handle (Serial.GetHandle)
3 - set a LIN header (Serial.SetLinHeader)
4 - write some data (Serial.SetUnsignedInteger)
5 - checksum - calculated using Serial.XOR8 - just reading through the LIN spec I don't think this is correct, and that it should be the (inverted) 8-bit sum, which I guess I can achieve using Serial.Sum8 and a bitwise NOT operation
6 - transmit data (Serial.Transmit)
So what I have so far is this:
- Code: Select all
local h1 = Serial.GetHandle(true);
local offset = 0; // this will keep running tally of offset position
local dataOffset = 0; // stores where the data begins for XOR operation
dataOffset = Serial.SetLinHeader(h1, 0, 4, Tx Address, true, false);
offset = Serial.SetUnsignedInteger(h1, dataOffset, 1, data); // send data byte
offset = Serial.SetUnsignedInteger(h1, offset, 1, Serial.XOR8(h1, dataOffset, 4)); // calculate and send checksum
Serial.Transmit(h1, 1, offset);
I don't have an ECU spare to test this at the moment - but does it look roughly correct? Is there anything I'm missing?
Receiving seems a bit trickier, since the ECU will initiate the communication but then needs to listen for the response within the packet. The way I think it should be done is:
1 - open a handle (Serial.GetHandle)
2 - set a LIN header (Serial.SetLinHeader)
3 - transmit header (Serial.Transmit)
4 - receive data (Serial.GetUnsignedInteger)
But I'm not sure if that would be correct or not.
So any guidance, especially example code for sending and receiving, would be greatly appreciated.