Exemple #1
0
def create_devices():
    """ создание объектов устройств"""

    # создание объектов устройств по конфигурационному файлу
    config = configparser.RawConfigParser()
    config.read(CONFIG_FILE)
    for section in config.sections():

        # выбор общих параметров
        name = config.get(section, 'name')
        dev_type = config.get(section, 'type')
        enabled = config.getint(section, 'enabled')

        # создание устройства
        device = Device(name, dev_type, enabled)

        # определение устройства в управляющие или исполнительные
        if dev_type in CONTROL_TYPES:
            controls = config.get(section, 'controls')
            priority = config.getint(section, 'priority')
            init_regs = [
                config.getint(section, 'reg1'), config.getint(section, 'reg2')]
            device.init_as_controller(controls, priority, init_regs)
            controllers.append(device)

        elif dev_type in EXEC_TYPES:
            executors.append(device)

        else:
            print('Error creating "%s": incorrect device type "%s"' %
                  (name, dev_type))

        # инициализация железного устройства
        if dev_type in HARDWARE_TYPES:

            address = config.getint(section, 'address')
            init_regs = [
                config.getint(section, 'reg1'), config.getint(section, 'reg2')]

            if device.init_as_hardware(address, init_regs, conn):
                print("{} addr {:d} ok".format(name, address))
            else:
                print(
                    "ERROR writing initial regs to {} with addr {:d}".format(name, address))

        # инициализация таймера
        if dev_type == "timer":
            regs_on = [
                config.getint(section, 'reg1on'), config.getint(section, 'reg2on')]
            regs_off = [
                config.getint(section, 'reg1off'), config.getint(section, 'reg2off')]
            time_on = config.get(section, 'time_on')
            time_off = config.get(section, 'time_off')
            device.init_as_timer(regs_on, regs_off, time_on, time_off)
Exemple #2
0
from comm485 import Conn485
from device485 import Device
import time

conn = Conn485()
time.sleep(0.1)

# led1 = Device("led1","pwm")
# led1.init_as_hardware(60, [0,0], conn)

enc1 = Device("enc1", "encoder", 1)
enc1.init_as_hardware(50, [0, 0], conn)

# dummy = input(">")

try:
    while True:

        # time.sleep(0.1)
        # print(led1.write_registers([100,1],conn))

        # time.sleep(0.1)
        # print(led1.write_registers([200,0],conn))

        time.sleep(0.1)
        enc1.read_registers(conn)
        print(enc1.get_registers())

finally:
    del conn
Exemple #3
0
from comm485 import Conn485
from device485 import Device
import time

conn = Conn485()
time.sleep(0.1)

# led1 = Device("led1","pwm")
# led1.init_as_hardware(60, [0,0], conn)

enc1 = Device("enc1", "encoder", 1)
enc1.init_as_hardware(50, [0, 0], conn)

# dummy = input(">")

try:
    while True:

        # time.sleep(0.1)
        # print(led1.write_registers([100,1],conn))

        # time.sleep(0.1)
        # print(led1.write_registers([200,0],conn))

        time.sleep(0.1)
        enc1.read_registers(conn)
        print(enc1.get_registers())

finally:
    del conn