使用 Ubuntu 的 Raspberry 实时时钟

使用 Ubuntu 的 Raspberry 实时时钟

我正在尝试让 Raspberry Pi 的 sb 组件 USB RTC 与运行 Ubuntu 的 Raspberry 配合使用。当我将其与 Raspberrian 一起使用时,它可以正常工作,但当我尝试将其与 Ubuntu test.py 和 Set_Time.py 一起使用时,会出现以下错误。

Traceback (most recent call last):
  File "/home/ubuntu/USB-RTC/test.py", line 19, in <module>
    bus = SMBus.SMBus()
  File "/usr/local/lib/python3.6/dist-packages/PyMCP2221A/SMBus.py", line 13, in __init__
    self.mcp2221 = PyMCP2221A.PyMCP2221A(VID,PID,devnum)
  File "/usr/local/lib/python3.6/dist-packages/PyMCP2221A/PyMCP2221A.py", line 16, in __init__
    self.mcp2221a.open_path(hid.enumerate(VID, PID)[devnum]["path"])
  File "hid.pyx", line 142, in hid.device.open_path
OSError: open failed

我已经搜索了几个小时并尝试了所有我能找到的方法但没有任何收获。

这是 RTC; https://www.amazon.com/gp/product/B094R8H9B1/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1


谢谢您的回复。这是 Raspberry Pi 4。Ubuntu 18.4 和 Xfce 4.12。

以下是这些 Python 文件的内容;

#!/usr/bin/python3
# -*- coding: utf-8 -*-
#from PyMCP2221A import SMBus
from PyMCP2221A import SMBus

import time
 
address = 0x68  #i2c address of DS3231
register = 0x00
CONV = 32


#sec min hour week day month year
NowTime = [0x00,0x00,0x18,0x04,0x12,0x08,0x15]  #Edit this line to set RTC Date and Time

w  = ["SUN","Mon","Tues","Wed","Thur","Fri","Sat"];

#/dev/i2c-1
bus = SMBus.SMBus()
def ds3231SetTime():
    #bus.write_i2c_block_data(address,register,NowTime)
    bus.write_byte_data(0x68, 0x00, NowTime[0]) # set seconds and start clock
    bus.write_byte_data(0x68, 0x01, NowTime[1]) # set minutes
    bus.write_byte_data(0x68, 0x02, NowTime[2]) # set hours in 24 hour mode
    bus.write_byte_data(0x68, 0x03, NowTime[3]) # set day of week
    bus.write_byte_data(0x68, 0x04, NowTime[4]) # set date
    bus.write_byte_data(0x68, 0x05, NowTime[5]) # set month
    bus.write_byte_data(0x68, 0x06, NowTime[6]) # set year - last 2 digits
 
def ds3231ReadTime():
    return bus.read_i2c_block_data(address,register,7);

# Force a conversion and wait until it completes
def convTemp(address):
    byte_control = bus.read_byte_data(address,0x0E)
    if byte_control&CONV == 0:
        bus.write_byte_data(address, 0x0E, byte_control|CONV)
    byte_control = bus.read_byte_data(address,0x0E)
    while byte_control&CONV != 0:
        #time.sleep(1)
        byte_control = bus.read_byte_data(address,0x0E)
    return True

# Get temperature in degrees C
def getTemp(address):
    convTemp(address)
    byte_tmsb = bus.read_byte_data(address,0x11)
    byte_tlsb = bus.read_byte_data(address,0x12)
    tinteger = (byte_tmsb & 0x7f) + ((byte_tmsb & 0x80) >> 7) * -2**8
    tdecimal = (byte_tmsb >> 7) * 2**(-1) + ((byte_tmsb & 0x40) >> 6) * 2**(-2)
    return tinteger + tdecimal
 
ds3231SetTime()
while 1:
    t = ds3231ReadTime()
    t[0] = t[0]&0x7F  #sec
    t[1] = t[1]&0x7F  #min
    t[2] = t[2]&0x3F  #hour
    t[3] = t[3]&0x07  #week
    t[4] = t[4]&0x3F  #day
    t[5] = t[5]&0x1F  #month
    print("20%x/%x/%x %x:%x:%x  %s" %(t[6],t[5],t[4],t[2],t[1],t[0],w[t[3]-1]))
    time.sleep(1)

    '''Celsius = getTemp(address)
    Fahrenheit = 9.0/5.0 * Celsius + 32
    print (Fahrenheit, "*F /", Celsius, "*C")
    time.sleep(1)'''
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from PyMCP2221A import SMBus

import time
import os
 
address = 0x68
register = 0x00
CONV = 32


#sec min hour week day month year
NowTime = [0x00,0x00,0x18,0x04,0x12,0x08,0x15]  #Edit this line to set RTC Date and Time

w  = ["SUN","Mon","Tues","Wed","Thur","Fri","Sat"];

#/dev/i2c-1
bus = SMBus.SMBus()
def ds3231SetTime():
    #bus.write_i2c_block_data(address,register,NowTime)
    bus.write_byte_data(0x68, 0x00, NowTime[0]) # set seconds and start clock
    bus.write_byte_data(0x68, 0x01, NowTime[1]) # set minutes
    bus.write_byte_data(0x68, 0x02, NowTime[2]) # set hours in 24 hour mode
    bus.write_byte_data(0x68, 0x03, NowTime[3]) # set day of week
    bus.write_byte_data(0x68, 0x04, NowTime[4]) # set date
    bus.write_byte_data(0x68, 0x05, NowTime[5]) # set month
    bus.write_byte_data(0x68, 0x06, NowTime[6]) # set year - last 2 digits
 
def ds3231ReadTime():
    return bus.read_i2c_block_data(address,register,7);

# Force a conversion and wait until it completes
def convTemp(address):
    byte_control = bus.read_byte_data(address,0x0E)
    if byte_control&CONV == 0:
        bus.write_byte_data(address, 0x0E, byte_control|CONV)
    byte_control = bus.read_byte_data(address,0x0E)
    while byte_control&CONV != 0:
        #time.sleep(1)
        byte_control = bus.read_byte_data(address,0x0E)
    return True

# Get temperature in degrees C
def getTemp(address):
    convTemp(address)
    byte_tmsb = bus.read_byte_data(address,0x11)
    byte_tlsb = bus.read_byte_data(address,0x12)
    tinteger = (byte_tmsb & 0x7f) + ((byte_tmsb & 0x80) >> 7) * -2**8
    tdecimal = (byte_tmsb >> 7) * 2**(-1) + ((byte_tmsb & 0x40) >> 6) * 2**(-2)
    return tinteger + tdecimal
 
ds3231SetTime()
while 1:
    t = ds3231ReadTime()
    t[0] = t[0]&0x7F  #sec
    t[1] = t[1]&0x7F  #min
    t[2] = t[2]&0x3F  #hour
    t[3] = t[3]&0x07  #week
    t[4] = t[4]&0x3F  #day
    t[5] = t[5]&0x1F  #month
    print('20%x/%x/%x %x:%x:%x  %s' %(t[6],t[5],t[4],t[2],t[1],t[0],w[t[3]-1]))
    time.sleep(1)

    '''year = '20%x' %(t[6])
    month = '%x' %(t[5])
    day = '%x' %(t[4])

    hour = '%x' %(t[2])
    minute = '%x' %(t[1])
    sec = '%x' %(t[0])

    date_str=year +"-"+ month +"-"+ day
    time_str=hour +":"+ minute +":"+ sec


    data= ('\''+date_str +" "+ time_str+'\'')
    

    dd=os.system("sudo date -s "+data)
    #print(dd)'''

相关内容