Producing K-line initialization sequence

#1

Hello.

I would like to begin building a library for the K-line as I have need for that myself. I have done some research on it and I’ve found out that messing with the RX and TX pins make serial communication unavailable. This must be normal for an Arduino, but is there some way to reset the pins for serial communication after sending the initialization sequence?

The initialization consists of either a 25ms low followed by a 25ms high and 10.4kbps communication OR the 5 baud init which begins with a 200ms low, a 400ms high, a 400ms low, a 400ms high, a 400ms low and ends with a 200ms high. This cannot be reproduced correctly with Serial.write() command for it sends the start and stop bits as well. There’d come a gap between each 0 bit. If it worked I’d only loop the low time writing a zero and delay the high.

Then another problem comes with the 5 baud initialization sequence speed. The speed may, in some cases, be set by 0x55 byte (01010101) and I need to get that speed to be used with communications. I thought about calculating the time between the edges but is there any easier way? I have no idea if it even works to calculate the speed so for now it may be the best just to assume communication happens at 10.4 kbps instead of messing around trying to calculate the speed.

I hope I will get it working, hopefully even according to the ISO standards, and can help making the M2 a standardized OBD II scanner that can be used as any other OBD II scanner. There’s a whole lot of potential but I’m afraid if it doesn’t comply with the standards there will come trouble getting the device spread so writing the library would help M2 gain some more market potential. Of course M2 already has a lot more potential than most OBD II scanners for it can be reprogrammed, but I’m sure there are people who only need to read the error data.

#2

These problems have been cleared now. If someone is looking for a library made for the Macchina M2 I will be producing one and I will try to comply with the ISO standards 9141 and 14230 so that even more people can benefit from the device. I hope someone will be able to test these things and I will try to write as good code as I can. I will upload the code once I believe it has received a stage at which it can be used to communicate with the vehicle. For now I have solved my problems with beginning the communication. Or at least for the serial part, the speed part is still a question but I hope I will get something out of it as well. For now my code is working.

1 Like
#3

I would love to test it out let me know when we can see the library!

#4

Might it help to think of the slow init as sending the char $33 at 5bps, and the fast init as sending $F0 at 200 bps?
That’s not how we do it (we bit-bang), but if your uart can handle those rates, why not.

#5

I have a fast initialization sequence working for now. I’m going to see out if I can edit the OBD9141 library to change it to work with the ISO 14230 standard as well. Also, I’ll make the changes required for Macchina M2 compliance. I’ll also see if I can change the initialization sequence to slow initialize the ISO 14230 standard. I’ve been out for quite a while but I’m quite sure it’ll be quite an easy task. If someone could please explain me uploading the code I’d appreciate it. I have no experience on using GitHub etc for uploading my own code.

#6

Take a look at this https://blog.udacity.com/2015/06/a-beginners-git-github-tutorial.html or this https://m.youtube.com/watch?v=E8TXME3bzNs if you still need help shoot me a message and I’ll be happy to help you out

#7

I have been quite busy doing other things than the library and I have come to the conclusion I can’t really be the one making it. Even as much as I would love to be able to contribute to the M2 libraries I have too much other things to do than programming for I don’t have so much free time for that. But I can tell what I have learned so far.

The OBD9141 library does go well with Macchina M2 as long as there are changes made so that it will trigger the pin changes for the processor. Someone should really add M2 there somehow (one way is to use “#else ifdef” after Arduino Due lines and do the same thing as for Arduino Due). I haven’t even read out well enough how GitHub and that stuff work so I can’t help there either.

Producing the initialization sequences for ISO 14230 is for slow initialization the same as for ISO 9141 except for the keywords. If someone wishes to use OBD9141 library as a start for ISO 14230 there should be made changes so that it checks that either keywords match OR that keyword 2 is 0x8F. For fast initialization K-line should be set LOW for 25ms, HIGH for 25ms and then send 0xC1 0x33 0xF1 0x81 0x66. For more information you make check out VAG’s PDF file on K-line communication: https://obdclearinghouse.com/Files/viewFile?fileID=1380

For the messages to be sent I haven’t yet looked in deep enough to know how they should be produced. They are indeed similar to ISO 9141 but the addresses are different so the OBD9141 library would require some changes to be ISO 14230 compliant. But the initialization sequence is a start and also there is the StopCommunication service for ending communication which is though optional. The StopCommunication message is 0xC1 0x33 0xF1 0x82 if someone wishes to use it.

If someone wishes to begin the process of producing a library compliant with ISO 14230 I will be happy to help as I can but for now I don’t have the time to do it myself. If I began it I’d use the OBD9141 library as a base for it. There only is the thing it has to be changed to be Macchina M2 compliant. But if someone wishes to use his/her own code then these lines should be added for beginning serial communication:

// Begin serial:
g_APinDescription[LIN_KRX].pPort -> PIO_PDR = g_APinDescription[LIN_KRX].ulPin;
g_APinDescription[LIN_KTX].pPort -> PIO_PDR = g_APinDescription[LIN_KTX].ulPin;
Serial1.begin();

And these lines for ending the serial communication:

// End serial:
Serial1.end();
g_APinDescription[LIN_KRX].pPort -> PIO_PER = g_APinDescription[LIN_KRX].ulPin;
g_APinDescription[LIN_KTX].pPort -> PIO_PER = g_APinDescription[LIN_KTX].ulPin;
pinMode(LIN_KTX, OUTPUT);
digitalWrite(LIN_KTX, HIGH);

What the code does is that it changes the pin’s state to serial or to a gpio pin. It is a part of the SAM8XE’s features and it actually can be found in the datasheet (I can’t though tell which page it was on for I read about it some time ago and already forgot). I simply just edited the OBD9141 library’s part for the Arduino Due. So the state of the pin has to be changed if the pin has been used for other than serial communication. I also found out that serial.end won’t work if no serial communication is active so in case someone needs a workaround there could be something like bool serialActive = true.

1 Like
#8

Glad others are looking into this, I’m looking at getting my M2 working on my Mercedes collection, which are all mostly k-line. Could you possible share the code you’ve written so far? Maybe we can collaborate to make this a thing for all.

#9

Just wanted to make sure everyone was aware of this project:

I tested with an older Toyota and it works pretty well. Here was a quick project:

https://www.macchina.cc/content/real-time-mpg-display-older-car