Skip to content

Commit

Permalink
test modbus
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonsm committed Jul 7, 2024
1 parent fda4949 commit 5f24717
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
12 changes: 4 additions & 8 deletions automations/temperature.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@

- alias: 主卧温度变化-调控空调
mode: restart
initial_state: false
trigger:
- platform: time
at: '21:00:01'
Expand All @@ -101,8 +100,8 @@
- service: zhiact.actuate
data:
sensor_id: sensor.zhu_wo_wen_du
sensor_values: [28, 28.5]
alt_sensor_values: [28.5, 29]
sensor_values: [28.5, 29]
alt_sensor_values: [29, 29.5]
# sensor_id: climate.zhu_wo_kong_diao
# sensor_attr: current_temperature
# sensor_values: [29, 30, 31, 32, 33]
Expand All @@ -114,7 +113,6 @@

- alias: 儿童房温度变化-调控空调
mode: restart
initial_state: false
trigger:
- platform: time
at: '21:00:01'
Expand All @@ -130,8 +128,8 @@
- service: zhiact.actuate
data:
sensor_id: sensor.er_tong_fang_wen_du
sensor_values: [22.5, 23, 23.5, 24, 24.5]
alt_sensor_values: [24, 24.5, 25, 25.5, 26]
sensor_values: [23, 23.2, 23.5, 24, 24.5]
alt_sensor_values: [23.5, 24, 24.5, 25, 25.5]
# sensor_id: climate.er_tong_fang_kong_diao
# sensor_attr: current_temperature
# sensor_values: [29, 30, 31, 32, 33]
Expand Down Expand Up @@ -191,7 +189,6 @@

- alias: 儿童房温度变化-调控风扇
mode: restart
initial_state: false
trigger:
- platform: time
at: '20:00:01'
Expand All @@ -215,7 +212,6 @@

- alias: 主卧温度变化-调控风扇
mode: restart
initial_state: false
trigger:
- platform: time
at: '20:00:01'
Expand Down
44 changes: 26 additions & 18 deletions extras/test/test_modbus.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,52 @@
#!/usr/bin/env python3

import struct
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
from pymodbus.transaction import ModbusRtuFramer as ModbusFramer
from pymodbus.client import ModbusTcpClient
from pymodbus.transaction import ModbusRtuFramer

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(5)
s.connect(('192.168.1.60', 8899))
s.sendall(b'\x55\xAA\x55\x00\x25\x80\x03\xA8') # For USR initialize
s.close()
HOST = '192.168.1.60'
PORT = 8899


def reset(host, port):
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(5)
s.connect((host, port))
s.sendall(b'\x55\xAA\x55\x00\x25\x80\x03\xA8') # For USR initialize
s.close()


ATTR_MAP = {
'temperature': {'registers': [3, 6, 9, 12], 'register_type': 'input'},
'target_temp': {'registers': [4, 8, 12, 16]},
'temperature': {'registers': [3, 6, 9, 12], 'register_type': 'input'},
'operation': {'registers': [5, 9, 13, 17]},
'fan_mode': {'registers': [6, 10, 14, 18]},
'state_is_on': {'registers': [1, 2, 3, 4], 'register_type': 'coil'}
}

client = ModbusClient(host='192.168.1.60', port=8899, framer=ModbusFramer)
#reset(HOST, PORT)

client = ModbusTcpClient(host=HOST, port=PORT, framer=ModbusRtuFramer)
kwargs = {'unit': 1}
ret = client.connect()

for key in ATTR_MAP:
dict = ATTR_MAP[key]
print("%s:\t" % key, end='')
for register in dict['registers']:
register_type = dict.get('register_type')
for k, v in ATTR_MAP.items():
print("%s:\t" % k, end='')
for register in v['registers']:
register_type = v.get('register_type')
if register_type == 'coil':
result = client.read_coils(register, 1, **kwargs)
value = bool(result.bits[0])
else:
if register_type == 'input':
result = client.read_input_registers(register, 1, **kwargs)
else:
result = client.read_holding_registers(register, 1, **kwargs)
byte_string = b''.join([x.to_bytes(2, byteorder='big')
for x in result.registers])
result = client.read_holding_registers(register)
if isinstance(result, Exception):
print("Exception: %s" % result)
continue
byte_string = b''.join([x.to_bytes(2, byteorder='big') for x in result.registers])
value = struct.unpack('>H', byte_string)[0]
print("%s\t" % value, end='')
print("")

0 comments on commit 5f24717

Please sign in to comment.