Page 1 of 1

not all arguments converted during string formatting

Posted: Thu Aug 31, 2017 12:35 am
by whowantspi
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

Re: not all arguments converted during string formatting

Posted: Thu Aug 31, 2017 5:54 am
by elParaguayo
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)

Re: not all arguments converted during string formatting

Posted: Thu Aug 31, 2017 2:36 pm
by whowantspi
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

Re: not all arguments converted during string formatting

Posted: Fri Sep 01, 2017 2:25 pm
by paddyg
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
>>>

Re: not all arguments converted during string formatting

Posted: Mon Dec 18, 2017 7:36 pm
by yv1hx
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,