Eastron SDM Modbus 解析器库
项目描述
sdm_modbus
sdm_modbus 是一个 python 库,它通过 Modbus RTU 或 Modbus TCP 从 Eastron SDM 单相和三相 kWh 电表收集数据。
支持的设备:
- Eastron SDM72D-M
- Eastron SDM120-Modbus
- Eastron SDM230-Modbus
- Eastron SDM630-Modbus
- 加罗 GNM3D-RS485
- ESP2866/32 P1 Modbus
安装
要安装,请克隆此项目并使用以下命令安装poetry:
poetry install
或从 PyPi 安装软件包:
pip3 install sdm_modbus
用法
该脚本提供了一个通过Modbus TCPexample-tcp.py连接并显示SDM120上的所有输入和保持寄存器的最小示例。要将值显示为 JSON 对象,请添加.--json
usage: example-tcp.py [-h] [--unit UNIT] [--json] host port
positional arguments:
host Modbus TCP address
port Modbus TCP port
optional arguments:
-h, --help show this help message and exit
--unit UNIT Modbus device address
--json Output as JSON
输出:
SDM120(10.0.0.123:502, connectionType.TCP: timeout=1, retries=3, unit=0x1):
Input Registers:
Voltage: 237.00V
Current: 8.63A
Power (Active): -1919.70W
Power (Apparent): 2035.57VA
Power (Reactive): -676.70VA
Power Factor: -0.94
Phase Angle: 0.00°
Frequency: 50.00Hz
Imported Energy (Active): 1551.37kWh
Imported Energy (Active): 1335.69kWh
Imported Energy (Reactive): 0.01kVAh
Exported Energy (Reactive): 4362.14kVAh
Total Demand Power (Active): 1668.02W
Maximum Total Demand Power (Active): 3347.26W
Import Demand Power (Active): 0.00W
Maximum Import Demand Power (Active): 3347.26W
Export Demand Power (Active): 1668.02W
Maximum Export Demand Power (Active): 2109.45W
Total Demand Current: 7.57A
Maximum Total Demand Current: 14.97A
Total Energy (Active): 2887.07kWh
Total Energy (Reactive): 0.00kVAh
Holding Registers:
Demand Time: 1s
Demand Period: 60s
Relay Pulse Width: 60ms
Network Parity Stop: N-1
Meter ID: 1
Baud Rate: 9600
P1 Output Mode: Export Energy (Active)
Display Scroll Timing: 0s
P1 Divisor: 0.001kWh/imp
Measurement Mode: 0
Pulse/LED Indicator Mode: Import + Export Energy (Active)
连接
如果您希望使用 Modbus TCP,以下参数是相关的:
host = IP or DNS name of your Modbus TCP gateway, required
port = TCP port of the Modbus TCP gateway, required
unit = Modbus device address, default=1, optional
如果您使用的是 Modbus RTU 连接,您可以指定:
device = path to serial device, e.g. /dev/ttyUSB0, required
baud = baud rate of your SDM unit, defaults to factory default, optional
unit = Modbus device address, defaults to 1, optional
连接仪表:
>>> import sdm_modbus
# SDM120 over Modbus TCP
>>> device = sdm_modbus.SDM120(host="10.0.0.123", port=502)
# SDM630 over Modbus RTU
>>> device = sdm_modbus.SDM630(device="/dev/ttyUSB0", baud=9600)
测试连接:
>>> device.connected()
True
打印对象会产生基本的设备参数:
>>> device
SDM120(10.0.0.123:502, connectionType.TCP: timeout=1, retries=3, unit=0x1):
连接到多个设备
parent通过在创建新实例时提供已连接的设备,可以重新使用现有的 RTU 或 TCP 连接。如果 Modbus TCP 网关只接受有限数量的连接,或者您希望在同一总线上寻址多个 RTU 设备,这可能是必要的。例如:
# Connect to a SDM630 over Modbus TCP
>>> device_1 = sdm_modbus.SDM630(host="10.0.0.123", port=502, unit=1)
# Connect to a SDM630 using the existing connection
>>> device_2 = sdm_modbus.SDM630(parent=device_1, unit=2)
读取寄存器
按名称读取单个输入寄存器:
>>> device.read("voltage")
236.89999389648438
通过将sdm_modbus.registerType.INPUT枚举传递给 来读取所有输入寄存器read_all()。将此留空以读取INPUT和HOLDING寄存器:
>>> device.read_all(sdm_modbus.registerType.INPUT)
{
"voltage": 238.60000610351562,
"current": 7.59499979019165,
"power_active": -1673.800048828125,
"power_apparent": 1797.5904541015625,
"power_reactive": -655.4000244140625,
"power_factor": -0.9311425685882568,
"phase_angle": 0.0,
"frequency": 50.0,
"import_energy_active": 1556.35595703125,
"export_energy_active": 1345.9210205078125,
"import_energy_reactive": 0.014999999664723873,
"export_energy_reactive": 4376.02001953125,
"total_demand_power_active": 1659.360107421875,
"maximum_total_demand_power_active": 3347.26318359375,
"import_demand_power_active": 0.0,
"maximum_import_demand_power_active": 3347.26318359375,
"export_demand_power_active": 1659.360107421875,
"maximum_export_demand_power_active": 2109.4541015625,
"total_demand_current": 7.531858921051025,
"maximum_total_demand_current": 14.968546867370605,
"total_energy_active": 2902.277099609375,
"total_energy_reactive": 4376.03515625
}
>>> device.read_all(sdm_modbus.registerType.HOLDING)
{
"demand_time": 1,
"demand_period": 60,
"relay_pulse_width": 60,
"network_parity_stop": 0,
"meter_id": 1,
"baud": 2,
"p1_output_mode": 4,
"display_scroll_timing": 0,
"p1_divisor": 0,
"measurement_mode": 0,
"indicator_mode": 0
}
写寄存器
也可以写入保持寄存器。设置新的波特率,例如:
>>> device.write("baud", 2)
WriteMultipleRegisterResponse (28,2)
您需要通过按住设置按钮 5 秒钟来启用设备上的设置模式Exception Response(134, 6, GatewayNoResponse),否则您将收到一个或类似的信息。
注册详情
如果您需要有关特定寄存器的更多信息,以查找单位或枚举,例如:
>>> device.registers["voltage"]
# address, length, type, datatype, valuetype, name, unit, batching
(
0,
2,
<registerType.INPUT: 1>,
<registerDataType.FLOAT32: 11>,
<class 'float'>,
'Voltage',
'V',
1
)
>>> device.registers["p1_divisor"]
# address, length, type, datatype, valuetype, name, unit, batching
(
63760,
2,
<registerType.HOLDING: 2>,
<registerDataType.FLOAT32: 11>,
<class 'int'>,
'P1 Divisor',
['0.001kWh/imp', '0.01kWh/imp', '0.1kWh/imp', '1kWh/imp'],
2
)
贡献
非常欢迎贡献,尤其是在支持的单元上进行测试,并添加其他 Eastron SDM 单元。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。
源分布
内置分布
sdm_modbus -0.5.2.tar.gz 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 1c5d4f82cdf71d3167f959342b5813a4ab172d39220a010f007cff0389530bc8 |
|
| MD5 | 75d4c1c4097b0090d2192ce27aa0500e |
|
| 布莱克2-256 | 6ed9a07df8429dbc14a9786ba81facb0885f2dc3513732f6c9e2c507d1f7e761 |
sdm_modbus -0.5.2-py3-none-any.whl 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | c50ed83aa636b4d22b117c5f0dd92b6e1b26e536124d290d965b291c8f409ad0 |
|
| MD5 | 344fcb57b3b30b4dabd7d9c3e5444cf9 |
|
| 布莱克2-256 | ec965b3d75e9a132ebc92442fa5dfd7ce4a7d63000e42dbdc42bc1b161870aa0 |