Python Forum
smbus.SMBus i2c code which worked to v.3.7.3 does not on Python 3.11.2
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
smbus.SMBus i2c code which worked to v.3.7.3 does not on Python 3.11.2
#1
The code below has worked on a set of Raspberry Pi 2B single board computers in my workshop for some years now, each of them using a local real time clock, a DS1307. (Internet access is inconveniently far away.) The most recent version among these is Python 3.7.3
Now I want to improve the accuracy by changing to the adjustable MCP7940N, and want to test the programming using a Pi 4 running the latest linux distro and Python 3.11.2 before changing the code in the instruments. Of course, the address was changed to suit the new RTC. However, this has thrown up a problem:
#code to write data to a real time clock e.g. DS1307:
import smbus
bus = smbus.SMBus(1)
address = 0x68
def  set_clk():
    global bus
    global address
    prmts = ['Year (0 - 99) = ?  ', 'Month (1 - 12) = ? ',  'Date (1 - 31) = ? ', 'Day (1 - 7) = ? ',
                 'Hour (1 - 24) = ? ', 'Minute (0 - 59) = ? 'Second (0 - 59) = ? ']
    vars = ['yr2', 'mon2', 'date2', 'day2', 'hr2', 'min2', 'sec2']

    for i in range(0,7):
        val = input(prmts[i])
        val = int(val)
        hi = int(val/10)
        lo = val - 10*hi
        val = 16*hi + lo
        vars[i] = val
    bus.write_byte_data(address, 0x00, vars[6])
    bus.write_byte_data(address, 0x00, vars[5])
    bus.write_byte_data(address, 0x00, vars[4])
    bus.write_byte_data(address, 0x00, vars[3])
    bus.write_byte_data(address, 0x00, vars[2])
    bus.write_byte_data(address, 0x00, vars[1])
    bus.write_byte_data(address, 0x00, vars[0])
This code on Python3.11.2, gives two errors: the first requires the removal of the (1) in the bus = smbus.SMBus(1) line. OK, so we can only have one I2C bus at a time now. The second is more of a problem:

Error:
Traceback (most recent call last) File "/home/pi/set_clock.py, line 15, in<module> bus.write_byte_data(address, 0x00, vars[6]) TypeError: descriptor 'write_byte_data for 'SMBus' objects doesn't apply to a 'int' object
I have tried the write_i2c_block_data( , , ) form of the command (same error as above). and many other imagined possibilities, none of which was accepted. I have downloaded the language reference, but 'SMBus' is nowhere to be found in the 2,200 pages - and the data types are said to be restricted to 'int' and 'str' - no 'byte' data type. I have tried putting in explicit binary values in the bus.write_byte_data() command, but everything I have tried is an 'int'.
I have run out of ideas, and would be most grateful for advice. I apologise that the code snippet above isn't in BBCode form - I couldn't see how to get to that - the info on it seemed to suggest that I would be pointed to that format anyway, but.... Incidentally, I typed it in with the correct indents, but they seem to have vanished.
Thanks for reading this far! NewbyTyro
Reply
#2
first, smbus code has been obsolete (see this which is listed as their homepage). It hasn't been updated since 2019, so I wouldn't rely on it much.

There are several other i2c packages listed here. I haven't used any of them, so you may have to try each until you find a fit,
buran likes this post
Reply
#3
Thanks very much. Now testing first... I will post again if I find one that does the job! - NewbyTyro
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [WORKED AROUND] Problem installing elitech-datareader, 'cannot import build_py_2to3' NeilUK 4 4,491 Jul-09-2023, 10:01 AM
Last Post: NeilUK
  Python: re.findall to find multiple instances don't work but search worked Secret 1 2,062 Aug-30-2022, 08:40 PM
Last Post: deanhystad
  SMBUS 'no such file or directory' hcccs 2 7,468 Jan-31-2021, 04:47 PM
Last Post: hcccs
  Iterating over a dictionary in a for loop - checking code has worked sallyjc81 1 2,614 Dec-29-2020, 05:14 PM
Last Post: ndc85430
  SMBUS Support RWP 1 5,183 Nov-06-2017, 08:45 PM
Last Post: Larz60+
  smbus contention handling PatM 1 3,136 Sep-21-2017, 07:13 PM
Last Post: PatM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020