Error on import standard classes


#1

I’m trying to reuse some standard python code that i wrote in my Zerynth project, but i’m getting some weird error in compile.

here’s my code
import socket
import crc16_modbus as crc
import struct

import threading

HOST = ‘’
MULTIMETER_IP = ‘192.168.0.99’
UDP_PORT_SEND = 1001
UDP_PORT_RECV = 5005

CMD_GET_PHASE_A_V = [0x01, 0x03, 0x00, 0x44, 0x00, 0x02, 0x84, 0x1e] # CRC já calculado char
CMD_GET_PHASE_A_F = [0x01, 0x03, 0x00, 0x42, 0x00, 0x02, 0x64, 0x1f] # CRC já calculado char
CMD_GET_PHASE_A_I = [0x01, 0x03, 0x00, 0x4a, 0x00, 0x02, 0xe5, 0xdd] # CRC já calculado char
CMD_GET_PHASE_A_POT_ATIVA = [0x01, 0x03, 0x00, 0x50, 0x00, 0x02, 0xc4, 0x1a] # CRC já calculado char
CMD_GET_PHASE_A_FAT_POT = [0x01, 0x03, 0x00, 0x68, 0x00, 0x02, 0x45, 0xd7] # CRC já calculado char
CMD_GET_POT_ATIV_TOTAL = [0x01, 0x03, 0x00, 0x56, 0x00, 0x02, 0x24, 0x1b] # CRC já calculado

class MultimeterController():
def init(self, lock = None):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.socket.bind((HOST, UDP_PORT_RECV))
self.socket.settimeout(0.5)
self.listen_thread = threading.Thread(target=self.listen_udp)
self.is_listening = True
self.start_listening()

    if lock is None:
        self.lock = threading.Lock()
    else:
        self.lock = lock

    self.phase_a_v = 0
    self.phase_a_f = 0
    self.phase_a_i = 0
    self.phase_a_pot_ativa = 0
    self.phase_a_fat_pot = 0
    self.pot_ativ_total = 0

def start_listening(self):
    self.is_listening = True
    self.listen_thread.start()

def stop_listening(self):
    self.is_listening = False

def test(self):
    self.socket.sendto(b'\x01\x03\x00\x44\x00\x02\x84\x1e', (MULTIMETER_IP, UDP_PORT_SEND))

def __get_float_from_bytes(self, received_bytes):
    float_bytes = [received_bytes[5], received_bytes[6], received_bytes[3], received_bytes[4]]
    return struct.unpack('>f', bytes(float_bytes))[0]

def __get_data_from_cmd(self, cmd):
    mbytes = bytearray(cmd)
    self.socket.sendto(mbytes, (MULTIMETER_IP, UDP_PORT_SEND))
    try:
        data, addr = self.socket.recvfrom(1024)
        print("received: " + data.hex(), end='\n')
        calculated = crc.calculate_crc(data[:-2])
        print(calculated.hex(), end=' ')
        if calculated == data[-2:]:
            print("crc correto\n")
            value = self.__get_float_from_bytes(data)
            print(value, end='\n')
            return value
        else:
            print("crc incorreto\n")
            return None
    except socket.timeout:
        print("timeout")
        return None

def __get_phase_a_v(self):
    data = self.__get_data_from_cmd(CMD_GET_PHASE_A_V)
    if data is not None:
        self.lock.acquire()
        self.phase_a_v = data
        self.lock.release()
        print("Tensao Fase A: " + str(data))

def __get_phase_a_i(self):
    data = self.__get_data_from_cmd(CMD_GET_PHASE_A_I)
    if data is not None:
        self.lock.acquire()
        self.phase_a_i = data
        self.lock.release()
        print("Corrente Fase A: " + str(data))

def __get_phase_a_f(self):
    data = self.__get_data_from_cmd(CMD_GET_PHASE_A_F)
    if data is not None:
        self.lock.acquire()
        self.phase_a_f = data
        self.lock.release()
        print("Frequência Fase A: " + str(data))

def __get_phase_a_pot_ativ(self):
    data = self.__get_data_from_cmd(CMD_GET_PHASE_A_POT_ATIVA)
    if data is not None:
        self.lock.acquire()
        self.phase_a_pot_ativa = data
        self.lock.release()
        print("Potência Ativa Fase A: " + str(data))

def __get_phase_a_fat_pot(self):
    data = self.__get_data_from_cmd(CMD_GET_PHASE_A_FAT_POT)
    if data is not None:
        self.lock.acquire()
        self.phase_a_fat_pot = data
        self.lock.release()
        print("Fator de Potência Fase A: " + str(data))

def listen_udp(self):
    while True:
        self.__get_phase_a_v()
        self.__get_phase_a_i()
        self.__get_phase_a_f()
        self.__get_phase_a_pot_ativ()
        self.__get_phase_a_fat_pot()

        sleep(1000)

This class is intended to get info from a multimeter via Modbus over UDP

here’s the errror

[info] Compiling module: threading @ /home/bruno/.zerynth2/dist/r2.1.1/stdlib/threading.py
[fatal] Unexpected exception
Traceback (most recent call last):
File “/home/bruno/.zerynth2/dist/r2.1.1/ztc/compiler/astwalker.py”, line 120, in visit_Import
mod = self.hooks.getModuleCode(alias.name)
File “/home/bruno/.zerynth2/dist/r2.1.1/ztc/compiler/compiler.py”, line 154, in getModuleCode
return self.modules[name]
KeyError: ‘multimeter_controller’
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “/home/bruno/.zerynth2/dist/r2.1.1/ztc/compiler/compilercmd.py”, line 87, in _zcompile
binary, reprs = compiler.compile()
File “/home/bruno/.zerynth2/dist/r2.1.1/ztc/compiler/compiler.py”, line 495, in compile
self.compileModule(self.mainfile)
File “/home/bruno/.zerynth2/dist/r2.1.1/ztc/compiler/compiler.py”, line 460, in compileModule
mc.visit(tree)
File “/home/bruno/.zerynth2/sys/python/lib/python3.5/ast.py”, line 245, in visit
return visitor(node)
File “/home/bruno/.zerynth2/dist/r2.1.1/ztc/compiler/astwalker.py”, line 103, in visit_Module
self.code.addCode(self.visit(stmt))
File “/home/bruno/.zerynth2/sys/python/lib/python3.5/ast.py”, line 245, in visit
return visitor(node)
File “/home/bruno/.zerynth2/dist/r2.1.1/ztc/compiler/astwalker.py”, line 122, in visit_Import
self.hooks.importHook(alias.name,node.lineno,self.filename)
File “/home/bruno/.zerynth2/dist/r2.1.1/ztc/compiler/compiler.py”, line 170, in importHook
self.compileModule(name,line,filename)
File “/home/bruno/.zerynth2/dist/r2.1.1/ztc/compiler/compiler.py”, line 460, in compileModule
mc.visit(tree)
File “/home/bruno/.zerynth2/sys/python/lib/python3.5/ast.py”, line 245, in visit
return visitor(node)
File “/home/bruno/.zerynth2/dist/r2.1.1/ztc/compiler/astwalker.py”, line 107, in visit_Module
self.generateCodeObjs()
File “/home/bruno/.zerynth2/dist/r2.1.1/ztc/compiler/astwalker.py”, line 902, in generateCodeObjs
self.generateCodeObjs()
File “/home/bruno/.zerynth2/dist/r2.1.1/ztc/compiler/astwalker.py”, line 853, in generateCodeObjs
fcode.addCode(self.genCodeList(node.body))
File “/home/bruno/.zerynth2/dist/r2.1.1/ztc/compiler/astwalker.py”, line 92, in genCodeList
listcode.append(self.visit(cc))
File “/home/bruno/.zerynth2/sys/python/lib/python3.5/ast.py”, line 245, in visit
return visitor(node)
File “/home/bruno/.zerynth2/dist/r2.1.1/ztc/compiler/astwalker.py”, line 1061, in visit_Try
if not self.env.hasException(h.type.id):
AttributeError: ‘Attribute’ object has no attribute ‘id’

The same thing happens when i remove the threading references with the struct library…