Python Forum
'OSError: [Errno 16] Device or resource busy' for ralationship with ds1307
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'OSError: [Errno 16] Device or resource busy' for ralationship with ds1307
#1
i want to set time & date by ds1307 IC...I added rtc-ds1307 to /etc/modules my program is
from RTC_DS1307 import RTC

rtc = RTC()
rtc.setDate(seconds = 0, minutes = 47, hours = 9, dow = 3,
           day = 8, month = 6, year = 16)
print ("Date set to:", rtc.getDateStr())
'setDate' is a function in 'RTC_DS1307.py' that gets value of 'second', 'minute','hour',... and then puts every one of these values in itself register by '_write' function & 'write_byte_data' command i considered a seperate register for 'second','minute',...by defining a hex value for every register. now, my problem is i get the following error..please helpppp
Traceback (most recent call last):
 File "/home/pi/start/time/set-get.py", line 5, in <module>
   day = 8, month = 6, year = 16)
 File "/home/pi/start/time/RTC_DS1307.py", line 124, in setDate
   self._write(self._REG_SECONDS, intToBcd(seconds))
 File "/home/pi/start/time/RTC_DS1307.py", line 54, in _write
   self._bus.write_byte_data(self._addr, register, data)
OSError: [Errno 16] Device or resource busy
'RTC_DS1307.py' is as following

# RTC_DS1307.py

# Code inspired from GitHub: sorz/DS1307.py


import smbus



def bcdToInt(bcd):

    '''

    Converts byte interpreted as two digit bcd to integer

    (e.g. 88 = b01011000->bcd0101'1000 = 58)

    '''

    out = 0

    for d in (bcd >> 4, bcd):

        for p in (1, 2, 4 ,8):

            if d & 1:

                out += p

            d >>= 1

        out *= 10

    return out // 10



def intToBcd(n):

    '''

    Converts integer 0..99 to byte interpreted in two digit bcd format.

    (e.g. 58 = bcd0101'1000->b01011000 = 88)

    '''

    bcd = 0

    for i in (n // 10, n % 10):

        for p in (8, 4, 2, 1):

            if i >= p:

                bcd += 1

                i -= p

            bcd <<= 1

    return bcd >> 1



class RTC():

    _REG_SECONDS = 0x00

    _REG_MINUTES = 0x01

    _REG_HOURS = 0x02

    _REG_DOW = 0x03

    _REG_DAY = 0x04

    _REG_MONTH = 0x05

    _REG_YEAR = 0x06

    _REG_CONTROL = 0x07



    def __init__(self, type = 1, addr = 0x68):

        '''

        Creates a Real Time Clock abstraction using given SMBus type and I2C address

        @param type: 0 for RPi model A, 1 for higher versions (default: 1)

        @param addr: I2C address (default: 0x68)

        '''

        self._bus = smbus.SMBus(type)

        self._addr = addr



    def _write(self, register, data):

        self._bus.write_byte_data(self._addr, register, data)

        



    def _read(self, data):

        returndata = self._bus.read_byte_data(self._addr, data)

        return returndata



    def getSeconds(self):

        '''

        Returns current seconds.

        @return: seconds of current date/time (0..59)

        '''

        return bcdToInt(self._read(self._REG_SECONDS))



    def getMinutes(self):

        '''

        Returns current minutes.

        @return: minutes of current date/time (0..59)

        '''

        return bcdToInt(self._read(self._REG_MINUTES))



    def getHours(self):

        '''

        Returns current hours.

        @return: hours of current date/time (0..23)

        '''

        d = self._read(self._REG_HOURS)

        if (d == 0x64):

            d = 0x40

        return bcdToInt(d & 0x3F)



    def getDow(self):

        '''

        Returns current day of week.

        @return: day number of current date/time (1..7, 1 for Monday)

        '''

        return bcdToInt(self._read(self._REG_DOW))



    def getDay(self):

        '''

        Returns current day of month.

        @return: day of current date/time (1..31)

        '''

        return bcdToInt(self._read(self._REG_DAY))



    def getMonth(self):

        '''

        Returns current month.

        @return: month of current date/time (1..12)

        '''

        return bcdToInt(self._read(self._REG_MONTH))



    def getYear(self):

        '''

        Returns current year.

        @return: year of current date/time (0..99)

        '''

        return bcdToInt(self._read(self._REG_YEAR))



    def setDate(self, seconds = None, minutes = None, hours = None, dow = None,

            day = None, month = None, year = None):

        '''

        Sets the current date/time.

        Range: seconds [0,59], minutes [0,59], hours [0,23],

               day_of_week [1,7], day [1-31], month [1-12], year [0-99].

        If a parameter is None (default), the current value is unchanged

        '''

        if seconds is not None:

            if seconds < 0 or seconds > 59:

                raise ValueError('Seconds is out of range [0,59].')

            self._write(self._REG_SECONDS, intToBcd(seconds))



        if minutes is not None:

            if minutes < 0 or minutes > 59:

                raise ValueError('Minutes is out of range [0,59].')

            self._write(self._REG_MINUTES, intToBcd(minutes))



        if hours is not None:

            if hours < 0 or hours > 23:

                raise ValueError('Hours is out of range [0,23].')

            self._write(self._REG_HOURS, intToBcd(hours))



        if year is not None:

            if year < 0 or year > 99:

                raise ValueError('Years is out of range [0,99].')

            self._write(self._REG_YEAR, intToBcd(year))



        if month is not None:

            if month < 1 or month > 12:

                raise ValueError('Month is out of range [1,12].')

            self._write(self._REG_MONTH, intToBcd(month))



        if day is not None:

            if day < 1 or day > 31:

                raise ValueError('Day is out of range [1,31].')

            self._write(self._REG_DAY, intToBcd(day))



        if dow is not None:

            if dow < 1 or dow > 7:

                raise ValueError('Day Of Week is out of range [1,7].')

            self._write(self._REG_DOW, intToBcd(dow))



    def getDate(self):

        '''

        Returns the current date/time.

        @return: date/time in a tuple with order

        (year, month, day, dow, hours, minutes, seconds)

        '''

        return (self.getYear(), self.getMonth(), self.getDay(),

                self.getDow(), self.getHours(), self.getMinutes(),

                self.getSeconds())



    def getDateStr(self):

        '''

        Returns the current date/time.

        @return: date/time in a string with format

        year-month-day hours:minutes:seconds

        '''
        return "20%02d-%02d-%02d %02d:%02d:%02d" %(self.getYear(), self.getMonth(), self.getDay(), self.getHours(), self.getMinutes(), self.getSeconds())
Reply
#2
This thread was created some months ago. I do not know whether the author can solve the problem or not.
@gray: Have you solved the problem or not?

If you have not solved your problem, I would give my opinions for your problem.
Reply
#3
Quote: you missed one starting python tag -- Larz60+ added same
I probably missed it
Reply
#4
(Jul-28-2017, 07:31 AM)jackbk Wrote: This thread was created some months ago. I do not know whether the author can solve the problem or not.
@gray: Have you solved the problem or not?

If you have not solved your problem, I would give my opinions for your problem.

i realy didn't understand that how i solved it...i remember i again installed OS
Reply
#5
(Jul-28-2017, 11:32 AM)gray Wrote:
(Jul-28-2017, 07:31 AM)jackbk Wrote: This thread was created some months ago. I do not know whether the author can solve the problem or not.
@gray: Have you solved the problem or not?

If you have not solved your problem, I would give my opinions for your problem.

i realy didn't understand that how i solved it...i remember i again installed OS 

If you solved it already, I do not need to debug your problem anymore.

I met this problem in the past. It related to an I2C device controlling by a driver, so this I2C device can not be accessed by SMBus (SMBus is used in RTC DS1307 driver).

My solution is that I removed the driver controlling I2C device. After removing the driver, I can access to the I2C device normally.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  OSError occurs in Linux. anna17 2 185 Mar-23-2024, 10:00 PM
Last Post: snippsat
  OSError with SMPT script Milan 0 691 Apr-28-2023, 01:34 PM
Last Post: Milan
  OSERROR When mkdir Oshadha 4 1,659 Jun-29-2022, 08:50 AM
Last Post: DeaD_EyE
  How to set CPU and memory resource SriRajesh 1 1,438 Aug-16-2021, 05:23 PM
Last Post: Larz60+
  OSError: Unable to load libjvm when connecting to hdfs with pyarrow 3.0.0 aupres 0 3,096 Mar-22-2021, 10:25 AM
Last Post: aupres
  pyarrow throws oserror winerror 193 1 is not a valid win32 application aupres 2 3,721 Oct-21-2020, 01:04 AM
Last Post: aupres
  OSError: [Errno 26] Text file busy: '/var/tmp/tmp5qbeydbp batchenr 1 4,161 Mar-20-2020, 01:58 PM
Last Post: ibreeden
  Multiprocessing OSError 'too many open files' DreamingInsanity 3 10,189 Dec-27-2019, 04:50 PM
Last Post: DreamingInsanity
  Reading UDP from external device without device software ikdemartijn 2 3,334 Dec-03-2019, 04:29 PM
Last Post: Larz60+
  .py to exe error "oserror winerror 193" michael1789 3 4,824 Dec-03-2019, 05:49 AM
Last Post: michael1789

Forum Jump:

User Panel Messages

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