California
Posts: 191
Joined: Sat Jun 06, 2015 2:33 pm

Need help with using class

Mon Dec 26, 2016 10:05 am

Dear all, after studying many class tutorials I am at the same point as few months before and I am kindly asking for some help. I have a feeling that I am very close, but still far away ...
So, below is a code for reading few temperatures from slave over RS485 and it prints temperatures inside of function "read_temp", but not when calling a class (missing some arguments?).

main.py

Code: Select all

import modul_slave_160

my_temperature = modul_slave_160.slave_160()
show_temp = my_temperature.read_temp()
print show_temp[0]
modul_slave_160.py

Code: Select all

import serial
import array
import struct
from time import sleep

ser = serial.Serial(
    port="/dev/ttyAMA0",
    baudrate=19200,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS
)

ser.close()
ser.open()
ser.isOpen()

maxReadCount = 8
readBuffer = array.array('c')


class slave_160():
    def __init__(self):
        self.read_temp(self.crc_calc, self.convert_MICROCHIP_32bit_to_float)

    def convert_MICROCHIP_32bit_to_float(self, x):
        x_hex = format(x, 'x')
        bits = bin(int(x_hex, 16))[2:].zfill(len(x_hex) * 4)
        i = 0
        val = list("                                ")
        b_s = bytearray(val)
        for char in bits:
            current_bit = int(char)
            if current_bit == 1:
                if i < 8:
                    b_s[i + 1] = "1"
                elif i == 8:
                    b_s[0] = "1"
                else:
                    b_s[i] = "1"
            else:
                if i < 8:
                    b_s[i + 1] = "0"
                elif i == 8:
                    b_s[0] = "0"
                else:
                    b_s[i] = "0"
            i = i + 1
        val = str(b_s)
        hex_val = hex(int(val, 2))[2:].rstrip("L")
        float_val = struct.unpack('!f', hex_val.decode('hex'))[0]
        return float_val

    def crc_calc(self, start_, addr, datalen_, dat_0, dat_1, dat_2, stop_):
        crc = datalen_ ^ addr
        crc = crc ^ dat_0
        crc = crc ^ dat_1
        crc = crc ^ dat_2
        crc = 255 - crc
        if crc == start_ or crc == stop_:
            crc = crc + 1
        return crc

    def read_temp(self, crc_calc, convert_MICROCHIP_32bit_to_float):
        start = 0x96
        address = 0xa0
        datalen = 0x83
        data_0 = 0xba
        data_1 = 0xbc
        data_2 = 0xba
        stop = 0xa9

        crc_ = crc_calc(start, address, datalen, data_0, data_1, data_2, stop)
        data = bytearray(
            [start, address, datalen, data_0, data_1, data_2, crc_, stop]
            )
        ser.write(data)
        readBuffer = array.array('c')
        readCount = 0
        while readCount < maxReadCount:
            readBuffer.append(ser.read(1))
            readCount = readCount + 1

        sleep(1.00)   # Master pocaka 1 sekundo pred prenosom temperatur
        start = 0x96
        address = 0xa0
        datalen = 0x83
        data_0 = 0xbb
        data_1 = 0xbc
        data_2 = 0
        stop = 0xa9

        crc_ = crc_calc(
            start, address, datalen, data_0, data_1, data_2, stop
            )
        data = bytearray(
            [start, address, datalen, data_0, data_1, data_2, crc_, stop]
            )
        ser.write(data)
        readBuffer = array.array('c')
        readCount = 0

        while readCount < maxReadCount:
            readBuffer.append(ser.read(1))
            readCount = readCount + 1

        completeTransfer = 33
        allPackets = 0
        start = 0x96
        address = 0xa0
        datalen = 0x83
        data_0 = 0xbc
        data_1 = 0
        data_2 = 0
        stop = 0xa9

        while allPackets < completeTransfer:
            crc_ = crc_calc(
                start, address, datalen, data_0, data_1, data_2, stop
                )
            data = bytearray(
                [start, address, datalen, data_0, data_1, data_2, crc_, stop]
                )
            ser.write(data)
            readBuffer = array.array('c')
            readCount = 0

            while readCount < maxReadCount:
                readBuffer.append(ser.read(1))
                readCount = readCount + 1

            i = 0
            while i < maxReadCount:
                val = ord(readBuffer[i])
                if i == 4:
                    a = val
                    if allPackets == 0:
                        ZG = int(a)
                    if allPackets == 1:
                        ZG = ZG + (int(a) << 8)
                    if allPackets == 2:
                        ZG = ZG + (int(a) << 16)
                    if allPackets == 3:
                        ZG = ZG + (int(a) << 24)

                    if allPackets == 4:
                        SP = int(a)
                    if allPackets == 5:
                        SP = SP + (int(a) << 8)
                    if allPackets == 6:
                        SP = SP + (int(a) << 16)
                    if allPackets == 7:
                        SP = SP + (int(a) << 24)

                    if allPackets == 8:
                        BO = int(a)
                    if allPackets == 9:
                        BO = BO + (int(a) << 8)
                    if allPackets == 10:
                        BO = BO + (int(a) << 16)
                    if allPackets == 11:
                        BO = BO + (int(a) << 24)

                    if allPackets == 12:
                        SO = int(a)
                    if allPackets == 13:
                        SO = SO + (int(a) << 8)
                    if allPackets == 14:
                        SO = SO + (int(a) << 16)
                    if allPackets == 15:
                        SO = SO + (int(a) << 24)

                    if allPackets == 16:
                        PE = int(a)
                    if allPackets == 17:
                        PE = PE + (int(a) << 8)
                    if allPackets == 18:
                        PE = PE + (int(a) << 16)
                    if allPackets == 19:
                        PE = PE + (int(a) << 24)

                    if allPackets == 20:
                        ZU = int(a)
                    if allPackets == 21:
                        ZU = ZU + (int(a) << 8)
                    if allPackets == 22:
                        ZU = ZU + (int(a) << 16)
                    if allPackets == 23:
                        ZU = ZU + (int(a) << 24)

                    if allPackets == 24:
                        SZ = int(a)
                    if allPackets == 25:
                        SZ = SZ + (int(a) << 8)
                    if allPackets == 26:
                        SZ = SZ + (int(a) << 16)
                    if allPackets == 27:
                        SZ = SZ + (int(a) << 24)

                    if allPackets == 28:
                        SS = int(a)
                    if allPackets == 29:
                        SS = SS + (int(a) << 8)
                    if allPackets == 30:
                        SS = SS + (int(a) << 16)
                    if allPackets == 31:
                        SS = SS + (int(a) << 24)

                    if allPackets == 32:
                        OUT = int(a)

                i = i + 1
            data_1 = data_1 + 1
            allPackets = allPackets + 1
        zgoraj = convert_MICROCHIP_32bit_to_float(ZG)
        spodaj = convert_MICROCHIP_32bit_to_float(SP)
        c = convert_MICROCHIP_32bit_to_float(BO)
        d = convert_MICROCHIP_32bit_to_float(SO)
        e = convert_MICROCHIP_32bit_to_float(PE)
        f = convert_MICROCHIP_32bit_to_float(ZU)
        g = convert_MICROCHIP_32bit_to_float(SZ)
        h = convert_MICROCHIP_32bit_to_float(SS)
        i = OUT

        print zgoraj, spodaj, c, d, e, f, g, h, i

        return zgoraj, spodaj, c, d, e, f, g, h, i
result:

Code: Select all

Running: /home/pi/Desktop/Class/main.py (Mon Dec 26 10:40:39 2016)

23.5 23.4375 23.5 24.0 20.7999992371 21.8999996185 22.3999996185 24.2999992371 4

Traceback (most recent call last):
  File "/home/pi/Desktop/Class/main.py", line 7, in <module>
    show_temp = my_temperature.read_temp()
TypeError: read_temp() takes exactly 3 arguments (1 given)


Execution Successful!
Please be so kind to do corrections in my code to get results ...

Vlado

User avatar
B.Goode
Posts: 10558
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Need help with using class

Mon Dec 26, 2016 10:45 am

So the error is:

Code: Select all

TypeError: read_temp() takes exactly 3 arguments (1 given)
and in the module where you define the Class you have:

Code: Select all

    def read_temp(self, crc_calc, convert_MICROCHIP_32bit_to_float):
which you have called as:

Code: Select all

 show_temp = my_temperature.read_temp()
So the error looks exactly correct.

Maybe putting the names of those other functions into the function definition for read_temp is the underlying problem that you need to correct?

California
Posts: 191
Joined: Sat Jun 06, 2015 2:33 pm

Re: Need help with using class

Mon Dec 26, 2016 11:40 am

It was tried before with an error:

Code: Select all

Running: /home/pi/Desktop/Class/main.py (Mon Dec 26 12:38:18 2016)

23.125 23.0625 23.125 23.5 20.7999992371 21.8999996185 22.3999996185 24.2999992371 4

Traceback (most recent call last):
  File "/home/pi/Desktop/Class/main.py", line 5, in <module>
    show_temp = my_temperature.read_temp(crc_calc, convert_MICROCHIP_32bit_to_float)
NameError: name 'crc_calc' is not defined


Execution Successful!

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

Re: Need help with using class

Mon Dec 26, 2016 12:20 pm

It's a bit of a strange method definition. It seems to be asking for references to two other class methods but there's no need to pass a reference as they are already available to the class.

You've got two options:
1) change your calling line to:

Code: Select all

show_temp = my_temperature.read_temp(my_temperature.crc_calc, my_temperature.convert_MICROCHIP_32bit_to_float)
or
2) change read_temp to this:

Code: Select all

    def read_temp(self):
        start = 0x96
        address = 0xa0
        datalen = 0x83
        data_0 = 0xba
        data_1 = 0xbc
        data_2 = 0xba
        stop = 0xa9

        crc_ = self.crc_calc(start, address, datalen, data_0, data_1, data_2, stop)
        data = bytearray(
            [start, address, datalen, data_0, data_1, data_2, crc_, stop]
            )
        ser.write(data)
        readBuffer = array.array('c')
        readCount = 0
        while readCount < maxReadCount:
            readBuffer.append(ser.read(1))
            readCount = readCount + 1

        sleep(1.00)   # Master pocaka 1 sekundo pred prenosom temperatur
        start = 0x96
        address = 0xa0
        datalen = 0x83
        data_0 = 0xbb
        data_1 = 0xbc
        data_2 = 0
        stop = 0xa9

        crc_ = self.crc_calc(
            start, address, datalen, data_0, data_1, data_2, stop
            )
        data = bytearray(
            [start, address, datalen, data_0, data_1, data_2, crc_, stop]
            )
        ser.write(data)
        readBuffer = array.array('c')
        readCount = 0

        while readCount < maxReadCount:
            readBuffer.append(ser.read(1))
            readCount = readCount + 1

        completeTransfer = 33
        allPackets = 0
        start = 0x96
        address = 0xa0
        datalen = 0x83
        data_0 = 0xbc
        data_1 = 0
        data_2 = 0
        stop = 0xa9

        while allPackets < completeTransfer:
            crc_ = self.crc_calc(
                start, address, datalen, data_0, data_1, data_2, stop
                )
            data = bytearray(
                [start, address, datalen, data_0, data_1, data_2, crc_, stop]
                )
            ser.write(data)
            readBuffer = array.array('c')
            readCount = 0

            while readCount < maxReadCount:
                readBuffer.append(ser.read(1))
                readCount = readCount + 1

            i = 0
            while i < maxReadCount:
                val = ord(readBuffer[i])
                if i == 4:
                    a = val
                    if allPackets == 0:
                        ZG = int(a)
                    if allPackets == 1:
                        ZG = ZG + (int(a) << 8)
                    if allPackets == 2:
                        ZG = ZG + (int(a) << 16)
                    if allPackets == 3:
                        ZG = ZG + (int(a) << 24)

                    if allPackets == 4:
                        SP = int(a)
                    if allPackets == 5:
                        SP = SP + (int(a) << 8)
                    if allPackets == 6:
                        SP = SP + (int(a) << 16)
                    if allPackets == 7:
                        SP = SP + (int(a) << 24)

                    if allPackets == 8:
                        BO = int(a)
                    if allPackets == 9:
                        BO = BO + (int(a) << 8)
                    if allPackets == 10:
                        BO = BO + (int(a) << 16)
                    if allPackets == 11:
                        BO = BO + (int(a) << 24)

                    if allPackets == 12:
                        SO = int(a)
                    if allPackets == 13:
                        SO = SO + (int(a) << 8)
                    if allPackets == 14:
                        SO = SO + (int(a) << 16)
                    if allPackets == 15:
                        SO = SO + (int(a) << 24)

                    if allPackets == 16:
                        PE = int(a)
                    if allPackets == 17:
                        PE = PE + (int(a) << 8)
                    if allPackets == 18:
                        PE = PE + (int(a) << 16)
                    if allPackets == 19:
                        PE = PE + (int(a) << 24)

                    if allPackets == 20:
                        ZU = int(a)
                    if allPackets == 21:
                        ZU = ZU + (int(a) << 8)
                    if allPackets == 22:
                        ZU = ZU + (int(a) << 16)
                    if allPackets == 23:
                        ZU = ZU + (int(a) << 24)

                    if allPackets == 24:
                        SZ = int(a)
                    if allPackets == 25:
                        SZ = SZ + (int(a) << 8)
                    if allPackets == 26:
                        SZ = SZ + (int(a) << 16)
                    if allPackets == 27:
                        SZ = SZ + (int(a) << 24)

                    if allPackets == 28:
                        SS = int(a)
                    if allPackets == 29:
                        SS = SS + (int(a) << 8)
                    if allPackets == 30:
                        SS = SS + (int(a) << 16)
                    if allPackets == 31:
                        SS = SS + (int(a) << 24)

                    if allPackets == 32:
                        OUT = int(a)

                i = i + 1
            data_1 = data_1 + 1
            allPackets = allPackets + 1
        zgoraj = self.convert_MICROCHIP_32bit_to_float(ZG)
        spodaj = self.convert_MICROCHIP_32bit_to_float(SP)
        c = self.convert_MICROCHIP_32bit_to_float(BO)
        d = self.convert_MICROCHIP_32bit_to_float(SO)
        e = self.convert_MICROCHIP_32bit_to_float(PE)
        f = self.convert_MICROCHIP_32bit_to_float(ZU)
        g = self.convert_MICROCHIP_32bit_to_float(SZ)
        h = self.convert_MICROCHIP_32bit_to_float(SS)
        i = OUT

        print zgoraj, spodaj, c, d, e, f, g, h, i

        return zgoraj, spodaj, c, d, e, f, g, h, i

i.e. use references to class methods.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

California
Posts: 191
Joined: Sat Jun 06, 2015 2:33 pm

Re: Need help with using class

Mon Dec 26, 2016 1:33 pm

Thanks elParaguayo, your first option works. It seems I have in this case double calling of "read_temp" before printing all variables outside of the class, why's that?

Code: Select all

Running: /home/pi/Desktop/Class/main.py (Mon Dec 26 14:31:36 2016)

22.75 22.625 22.6875 23.0 20.7999992371 21.8999996185 22.3999996185 24.2999992371 4
22.75 22.625 22.75 23.0 20.7999992371 21.8999996185 22.3999996185 24.2999992371 4
22.75
22.625
22.75
23.0
20.7999992371
21.8999996185
22.3999996185
24.2999992371
4

User avatar
Cancelor
Posts: 777
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Need help with using class

Tue Dec 27, 2016 3:58 pm

You have

Code: Select all

        print zgoraj, spodaj, c, d, e, f, g, h, i
        return zgoraj, spodaj, c, d, e, f, g, h, i
at the end of the slave_160 class and in main.py you print

Code: Select all

print show_temp[0]
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

California
Posts: 191
Joined: Sat Jun 06, 2015 2:33 pm

Re: Need help with using class

Tue Dec 27, 2016 4:11 pm

First two lines were printed from function just for test.
Correct result is:

Code: Select all

22.75
22.625
22.75
23.0
20.7999992371
21.8999996185
22.3999996185
24.2999992371
4
printed with:

Code: Select all

print show_temp[0]
print show_temp[1]
print show_temp[2]
print show_temp[3]
print show_temp[4]
print show_temp[5]
print show_temp[6]
print show_temp[7]

Return to “Python”