-1

I want to export the real time data that I am taking from my car to a .txt or .csv file (or something else that I don't know). How can I do that?

Here's the documentation for obdII.

import obd #import required libraries
import time
import serial

connection = obd.Async( fast= False) #auto-connects to USB or RF port


#RPM
def new_rpm(r):
    print("RPM:",r.value)
    print("\t")
    
#SPEED
def new_speed(r):
    print("SPEED:",r.value)
    print("\t")
    
    

**#Is it ok to do that with the watch or with the response ?    **
    
connection.watch(obd.commands.RPM, callback=new_rpm)
connection.watch(obd.commands.SPEED, callback=new_speed)


connection.start()

#the callback now will be Enabled for new values
time.sleep(60)
connection.stop()
STerliakov
  • 4,983
  • 3
  • 15
  • 37
BANI
  • 9
  • 4

1 Answers1

0

If i understand correctly you can simply use:

with open("output.txt", "a") as f:
    f.write(f"SPEED:{r.value}")

This also automatically closes the file after using it (when the execution gets out of the with open() scope)

Teddy
  • 452
  • 1
  • 12
  • Teddy --> Perfect.....and for that i don't need to add some time.sleep()?...because i want to write until i close the running script ? – BANI Dec 18 '22 at 15:58
  • I can't really test your code but as far as i can see you wouldn't need a sleep if you just close it with a keyboard interrupt. (`CTR+C`) – Teddy Dec 18 '22 at 16:02
  • this code i think work only to be in each function. Because i tried out of it and i had errors – BANI Dec 21 '22 at 08:32
  • ohh, it brobably tries to open the file twice at the same time. try using 2 diferent files. if you still want the result in one file in the end you could but a timestamp at the beginning of each line and write a script to merge them. – Teddy Dec 21 '22 at 10:44
  • with f.write(f"SPEED:{r.value}") i have a AttributeError : 'int' object has no attribute 'value' – BANI Dec 21 '22 at 12:20
  • In that case just do `f.write("SPEED: {r}")`. "'int' object has no attribute 'value'" means that r is already an int and probably the value you are searching for. – Teddy Dec 21 '22 at 13:59
  • *`f.write(f"SPEED: {r}")` – Teddy Dec 21 '22 at 14:07