-
오토닉스 ARIO-C-MT 파이썬 코드카테고리 없음 2023. 10. 15. 02:03
오토닉스 ARIO-C-MT는 오토닉스의 리모트I/O 시스템 제품 군인 ARIO 시리즈중 Modbus/TCP프로토콜 통신을 지원하는 제품입니다.
ARIO-C-MT 및 디지털 입력모듈 ARIO-S-DI04N, 디지털 출력모듈 ARIO-S-DO4N을 사용하여 파이썬에서 디지털 입출력제어 코드를 구현해 봤습니다.
1. H/W 구성
맨 우측부터 ARIO-C-MT / ARIO-S-DO4N / ARIO-S-DO4N 순으로 결합했습니다.
2. ARIO_C_MT 클래스 구현
기본 modbus는 pymodbus 패키지를 사용하였습니다.
from time import sleep from enum import Enum from typing import Dict from pymodbus.client import ModbusTcpClient class CH(Enum): CH1 = 1 << 8 << 0 CH2 = 1 << 8 << 1 CH3 = 1 << 8 << 2 CH4 = 1 << 8 << 3 class ARIO_C_MT(): client : ModbusTcpClient def __init__(self, ip) -> None: self.client = ModbusTcpClient(ip) def connect(self): if self.client.connected: raise Exception("already connected") self.client.connect() def close(self): if not self.client.connected: raise Exception("not connected") self.client.close() def set_output(self, channel : CH, output : bool): if not self.client.connected: raise Exception("not connected") result = self.client.read_holding_registers(0x07D0) current_output = result.registers[0] if output: val = channel.value | current_output else: if channel.value & current_output == 0: return else: val = (channel.value ^ current_output) result = self.client.write_register(0x07D0, val) def read_input(self, channel : CH) -> bool: result = self.client.read_input_registers(0x07D0, 1).registers[0] return result & channel.value == channel.value def read_output(self, channel : CH) -> bool: result = self.client.read_holding_registers(0x07D0, 1).registers[0] return result & channel.value == channel.value
3. 예제 코드
연결 -> 출력 채널 별 설정 -> 출력 채널 별 Read -> 입력 채널 별 Read -> 연결 해제 순 입니다.
ario = ARIO_C_MT("192.168.2.4") ario.connect() ario.set_output(CH.CH1, True) ario.set_output(CH.CH2, True) ario.set_output(CH.CH3, True) ario.set_output(CH.CH4, True) ario.read_output(CH.CH1) ario.read_output(CH.CH2) ario.read_output(CH.CH3) ario.read_output(CH.CH4) ario.read_input(CH.CH1) ario.read_input(CH.CH2) ario.read_input(CH.CH3) ario.read_input(CH.CH4) ario.close()