Serial buffer clear

Hello!

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);

Thank you!

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.

It does EXACTLY what the old flush() function did.

The optocoupler privide galvanic isolation and they combine RX and TX to one wire...

The first part makes sense. The second does not. Why are you sending and receiving on one wire?

K-line protocol (iso9141-2)

Tady: K-line protocol (iso9141-2)

That may be meaningful for you but I don't propose to search and read all about it.

Please explain it yourself.

...R

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

The MCU sees it own data… This is normal in k-line interfaces i only need to figure out how to filter them

If you know what you sent, why can’t you just remove those characters from the array, when the response is complete?

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.

Image from Reply #4 so we don’t have to download it. See this Image Guide

obdii_avr.gif

…R

Thanks for the diagram. That makes things clear.

I suggest you get an Arduino with at least 2 hardware serial ports - such as a Mega or Micro. Then stuff can be received in parallel with sending.

I don't think SoftwareSerial is at all suitable.

Edit to add. I just remembered that NeoSWSerial claims to send and receive at the same time. It may be worth trying

...R

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

You may have missed my edit to Reply #8

If you look at the Atmega 2560 datasheet there seems to be a lot of scope for setting different baud rates.

…R

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!

I changed the code. I now use Serial1 on my mega and an OR gate to block the RX line… The code now works perfectly!
Thank you guys!