whowantspi
Posts: 39
Joined: Mon Feb 11, 2013 12:00 am
Location: USA Alabama

not all arguments converted during string formatting

Thu Aug 31, 2017 12:35 am

I'm trying to read data from a VEML 6075 UV sensor. My code is ...

Code: Select all

#!/usr/bin/env python3

import smbus
import time

# Get I2C bus
bus = smbus.SMBus(1)

# VEML6075 address, 0x10(16)
bus.write_word_data(0x10, 0x00, 0x0040)

time.sleep(1.0)

uva = bus.read_word_data(0x10, 0x07)
uvb = bus.read_word_data(0x10, 0x09)
uvcomp1 = bus.read_word_data(0x10, 0x0A)
uvcomp2 = bus.read_word_data(0x10, 0x0B)

UVA = uva * 1  # Added this just to see what would happen, no error

# Output data to screen
print ("Raw UVA is :" %uva)
I get the error...

./veml6075.py
Traceback (most recent call last):
File "./veml6075.py", line 22, in <module>
print ("Raw UVA is :" %uva)
TypeError: not all arguments converted during string formatting

The data sheet can be found here https://www.vishay.com/docs/84304/veml6075.pdf

At the top of page 6 it says...

"The VEML6075 contains a CONF register (00h) used for operation control and parameter setup. Measurement results are stored in four separate registers, one each for UVA, UVD, UVB, UVcomp1, and UVcomp2 (07h to 0Bh respectively). All registers are accessible via I2C communication. Fig. 7 shows the basic I2C communication with the VEML6075. Each of the registers in the VEML6075 are 16 bit wide, so 16 bit should be written when a write command is sent, and 16 bit should be read when a read command is sent."

And this why I'm having to use "bus.write_word_data" and "bus.read_word_data". So far I haven't been able to print anything. I've Googled and found the error message but none of the solutions seemed to make any difference. This is the first device I've used that had 16 bit vs. 8 bit registers. Any help would be welcomed.

Max

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: not all arguments converted during string formatting

Thu Aug 31, 2017 5:54 am

That's not a string formatting command, it looks like you just wanted to append the value of uva to the end of the string.

Try this:

Code: Select all

print ("Raw UVA is : {}".format(uva))
Or

Code: Select all

print ("Raw UVA is : %s" % uva)
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

whowantspi
Posts: 39
Joined: Mon Feb 11, 2013 12:00 am
Location: USA Alabama

Re: not all arguments converted during string formatting

Thu Aug 31, 2017 2:36 pm

I tried the second example and it worked. But it must have something to do with the variable created by the "bus.read_word_data" because my other sensors that use "bus.read_byte_data" can print without the "%s". I'm new at programming and this really had me confused.

Thanks, now I can get on with the rest of the program.

Max

User avatar
paddyg
Posts: 2394
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: not all arguments converted during string formatting

Fri Sep 01, 2017 2:25 pm

It seems to be a function of passing a variable that is iterable which seems to bypass the error catching. You get the same effect with string.format() (which imho is a better system)

Code: Select all

>>> x = 9
>>> a = 9
>>> b = bytes(9)
>>> c = bytes([9])
>>> d = '9'
>>> for i in (a, b, c, d):
...   print('type(i): ', type(i))
...   try:
...     print('worked after a fasion', 'Raw UVA: ' % i)
...   except Exception as e:
...     print('failed with: ', e)
... 
type(i):  <class 'int'>
failed with:  not all arguments converted during string formatting
type(i):  <class 'bytes'>
worked after a fasion Raw UVA: 
type(i):  <class 'list'>
worked after a fasion Raw UVA: 
type(i):  <class 'str'>
failed with:  not all arguments converted during string formatting
>>>
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
yv1hx
Posts: 372
Joined: Sat Jul 21, 2012 10:09 pm
Location: Zulia, Venezuela
Contact: Website

Re: not all arguments converted during string formatting

Mon Dec 18, 2017 7:36 pm

whowantspi wrote:
Thu Aug 31, 2017 12:35 am
[...snip ....]

The data sheet can be found here https://www.vishay.com/docs/84304/veml6075.pdf

At the top of page 6 it says...

"The VEML6075 contains a CONF register (00h) used for operation control and parameter setup. Measurement results are stored in four separate registers, one each for UVA, UVD, UVB, UVcomp1, and UVcomp2 (07h to 0Bh respectively). All registers are accessible via I2C communication. Fig. 7 shows the basic I2C communication with the VEML6075. Each of the registers in the VEML6075 are 16 bit wide, so 16 bit should be written when a write command is sent, and 16 bit should be read when a read command is sent."

And this why I'm having to use "bus.write_word_data" and "bus.read_word_data". So far I haven't been able to print anything. I've Googled and found the error message but none of the solutions seemed to make any difference. This is the first device I've used that had 16 bit vs. 8 bit registers. Any help would be welcomed.

Max
Just in case you don't have catch this document: https://www.vishay.ru/docs/84339/designingveml6075.pdf

BR,
Marco-Luis
Telecom Specialist (Now Available for Hire!)

http://www.meteoven.org
http://yv1hx.ddns.net
http://twitter.com/yv1hx

Return to “Python”