3

I'm creating a program in Python 3.5.3 on my Raspberry Pi 3 B+ to interface with my car's OBDII port. The connection is established over Bluetooth correctly, the data reads correctly when the Python-OBD library is used as intended, but unfortunately when this library was made, it automatically adds a unit to the end with the Pint library.

I'm trying to convert responses that look like this:

1689.34 rotations per minute

To just the float part:

1689.34

So I can compare them over time to each other.

The error I'm getting is as follows:

TypeError: int() argument must be a string, a bytes-like object, or a number, not 'OBDresponse'

My code I tried was:

rpm1 = connection.query(rpm_sensor)

int(rpm1)

Any ideas to help with this?

vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20
  • 1
    Where does "1689.34 rotations per minute" come from? You'll likely need to get the information from the `OBDResponse` object. Have you checked its documentation? – Carcigenicate Oct 14 '18 at 23:20
  • Assuming https://github.com/brendan-w/python-OBD/blob/master/obd/OBDResponse.py, it looks like you'd do `reponse.value` to get the value, and if it's a string, you'd just need to cut the number from the rest of the text. I think you're going to need to give more information though. – Carcigenicate Oct 14 '18 at 23:22
  • Your assumption is correct. I'm not sure how to add response.value. Do you mean like rpm1.int? – Charles Phillips Oct 14 '18 at 23:27
  • What does `rpm1.value` give? – Carcigenicate Oct 14 '18 at 23:28
  • It ran, and i got: 3525 revolutions_per_minute I wonder if I could edit OBDResponse and remove the value from it. – Charles Phillips Oct 14 '18 at 23:30
  • Are these stored as strings? Do you just want to manipulate the string to get the float from it ? – vash_the_stampede Oct 14 '18 at 23:39
  • It says in the error that it's an object and i couldn't use .astype(str) to convert to string. I'd love it if I could just manipulate as a string though. – Charles Phillips Oct 14 '18 at 23:42

1 Answers1

2

Try this if you only want the number:

val = rpm1.value

print(val.magnitude)

Or:

print(rpm1.value.magnitude)

If you want the units then:

print(val.units)

If you read the documentation for OBDresponse, then you'll see that response.value returns a Pint object. You can then see the documentation for the Pint class to learn how to access the number and units separately.

You should be able to handle it from there.

Bilal Saleem
  • 633
  • 4
  • 8
  • Bilal Saleem, thank you. I used the first two lines and they did the trick. However, for anyone else dealing with this library, you then have to divide the RPMs by 4 because it reads RPM in 1/4 turns. – Charles Phillips Oct 15 '18 at 02:34
  • @CharlesPhillips - That is OBD standard, if you look at the [wiki page](https://en.wikipedia.org/wiki/OBD-II_PIDs#Service_01_PID_00_-_Show_PIDs_supported) (or any decent obd2 writeup) it gives how to convert from the codes that come back for service 01 commands (Such as RPM) – JohnP Oct 19 '22 at 17:32