I'm working on a little project. Im using an arduino do talk to my motorcycle via diagnostic port.
It uses only k-line (bidirectional serial) and i'm using the software serial (10400baud)
The thing is that i'm using two optocouplers for communication.. The optocoupler privide galvanic isolation and they combine RX and TX to one wire... The thing is when i send data (request for a table) i always get a echo+data rom ECU.. I could filter the data out but the echoed data is sometimes corrupt (software serial can't send and recieve at the same time).
So i'm i need of a function that will clear the input buffer as soon as i'm done with sending.
Serial.Flush() was a great function... Fon't know why they changed it instead of renamed it
While(myserial.available()>0){
Byte c = myserial.read();
}
This will not work as it filters out data from ECU.
Is it possible with myserial.end(); and then myserial.begin(10400);
Japanese cars and motorcycles most use k-line communication for diagnostics.
My bike has a diagnostic port that has 3 pins. 12v,gnd, and k-line. K-line is used for transmitting and recieving data... The line idles at 12V.. No data comming out. When you connect a tester to the port, it sends init bytes to the ECU and the ECU replyes... The line us bidirectional serial com and only one device can talk over the line.
It's more like an interrogation. The tester asks questions and ECU answers.
Sorry i don't know how to explain better...
Here is a schematics that converts MCU RX and TX lines to one line.... This is where the echo somes from... The MCU sees it own data.. This is normal in k-line interfaces i only need to figure out how to filter them
Possibly a simple hardware solution with a single OR gate. While you're transmitting, you force one of the inputs of the OR high so the input from the K-line (on the other input of the OR) has no effect.
I like the OR gate idea that is one possible hardware solution:)
I have a mega that has multiple serial ports. But the problem is baudrate... K-line uses 10400.. I was searching the web and i think hardware serial doesn't work with 10400 so i use software serial...
I can't filter out data.. As i said software serial doesn't work very well if you loopback... In my case i get some clear data end in some cases i ger garbage
Tady:
But the problem is baudrate... K-line uses 10400.. I was searching the web and i think hardware serial doesn't work with 10400 so i use software serial...
I just checked with an online baud rate calculator. With a 16MHz clock frequency, and 8N1, you should be able to get 10,400 baud with only 0.2% error, if I understand the calculator correctly:- 16MHz, 10400 baud, 8N1
Well i didnot know about the calculator never needed it before:)
Sorry Robin2 i missed you redit. I didn't check the datasheet. And i i saw that NewSoftSerial claims to send and read at the same time. Im using Software serial that came with 1.6.11 IDE. It sees the echoed data but the echo is not the same as sent data. It seems that it misses some bits. I don't know i only know that it doesn't work for me. Maybe it would work perfectly with a DUE or teensy 3.1.
Anyway the code works perfectly with myserial.end() and myserial.begin(10400). It is kind of an odd approach but it works!
Now I will try with hardware serial! And i will build the interface with an OR gate and a sending flag pin.
Yesterday i got the code working, its a mess but i can read motorcycle RPM,water temp, manifold air pressure,speed,battery voltage... And i think it's running at about 10hz So mission complete on the getting the data
Now i will also need to store the data on an SD card but that is for another day:)
Thank you guys for your help and time! I learned alot from this forum. I have been programming arduinos for 3 years now and i can say that i came a long way (still a long way to go). Before i didn't know anything about programming in any language. I just bought an UNO and started with a blink sketch for anything else i started searching this forum! 99% chance that there was someone else with the same question:)
Thank you again!