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?

7
  • 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? Commented Oct 14, 2018 at 23:20
  • Assuming 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. Commented Oct 14, 2018 at 23:22
  • Your assumption is correct. I'm not sure how to add response.value. Do you mean like rpm1.int? Commented Oct 14, 2018 at 23:27
  • What does rpm1.value give? Commented Oct 14, 2018 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. Commented Oct 14, 2018 at 23:30

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
@CharlesPhillips - That is OBD standard, if you look at the wiki page (or any decent obd2 writeup) it gives how to convert from the codes that come back for service 01 commands (Such as RPM)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.