test looop
This commit is contained in:
parent
aa7f4b5e4f
commit
a5dcc63d0c
|
@ -39,6 +39,24 @@
|
||||||
"detail": "pymodbus.payload",
|
"detail": "pymodbus.payload",
|
||||||
"documentation": {}
|
"documentation": {}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"label": "time",
|
||||||
|
"kind": 6,
|
||||||
|
"isExtraImport": true,
|
||||||
|
"importPath": "time",
|
||||||
|
"description": "time",
|
||||||
|
"detail": "time",
|
||||||
|
"documentation": {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "struct",
|
||||||
|
"kind": 6,
|
||||||
|
"isExtraImport": true,
|
||||||
|
"importPath": "struct",
|
||||||
|
"description": "struct",
|
||||||
|
"detail": "struct",
|
||||||
|
"documentation": {}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"label": "ModbusClient",
|
"label": "ModbusClient",
|
||||||
"importPath": "pyModbusTCP.client",
|
"importPath": "pyModbusTCP.client",
|
||||||
|
@ -71,12 +89,57 @@
|
||||||
"detail": "client",
|
"detail": "client",
|
||||||
"documentation": {}
|
"documentation": {}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"label": "two_byte_convert",
|
||||||
|
"kind": 2,
|
||||||
|
"importPath": "client",
|
||||||
|
"description": "client",
|
||||||
|
"peekOfCode": "def two_byte_convert(result):\n # print(result)\n v0 = result.registers[0]\n value = (v0 << 16) + result.registers[1]\n if result.registers[0] > (65535 * 0.5):\n value = value - (1 << 32)\n return value\ndef bulb(addr):\n try:\n coils = client.read_coils(addr, 1, MODBUS_SLAVE_ID)",
|
||||||
|
"detail": "client",
|
||||||
|
"documentation": {}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"label": "bulb",
|
"label": "bulb",
|
||||||
"kind": 2,
|
"kind": 2,
|
||||||
"importPath": "client",
|
"importPath": "client",
|
||||||
"description": "client",
|
"description": "client",
|
||||||
"peekOfCode": "def bulb(addr):\n try:\n coils = client.read_coils(addr, 1, MODBUS_SLAVE_ID)\n print(\"читаем начальное состояние\", coils.bits)\n new_bits = coils.bits\n new_bits[0] = not new_bits[0]\n print(\"пытаемся записать\", new_bits, coils.slave_id)\n result = client.write_coil(addr, new_bits[0], coils.slave_id)\n print(result)\n print(\"читаем после попытки\", client.read_coils(0, len(coils.bits), coils.slave_id).bits)",
|
"peekOfCode": "def bulb(addr):\n try:\n coils = client.read_coils(addr, 1, MODBUS_SLAVE_ID)\n print(\"читаем начальное состояние\", coils.bits)\n new_bits = coils.bits\n new_bits[0] = not new_bits[0]\n print(\"пытаемся записать\", new_bits, coils.slave_id)\n result = client.write_coil(addr, new_bits[0], coils.slave_id)\n print(result)\n print(",
|
||||||
|
"detail": "client",
|
||||||
|
"documentation": {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "get_coordinates",
|
||||||
|
"kind": 2,
|
||||||
|
"importPath": "client",
|
||||||
|
"description": "client",
|
||||||
|
"peekOfCode": "def get_coordinates():\n try:\n data = [\n (\"j1\", 2268),\n (\"j2\", 2270),\n (\"j3\", 2272),\n (\"j4\", 2274),\n (\"j5\", 2276),\n (\"j6\", 2278),\n (\"x\", 2332),",
|
||||||
|
"detail": "client",
|
||||||
|
"documentation": {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "get_or_set_speed",
|
||||||
|
"kind": 2,
|
||||||
|
"importPath": "client",
|
||||||
|
"description": "client",
|
||||||
|
"peekOfCode": "def get_or_set_speed(number=None):\n result = client.read_holding_registers(20200, 1, MODBUS_SLAVE_ID)\n print(\"сейчас скорость\", result.registers[0] * 0.1)\n if number and number > 0 and number <= 1000:\n client.write_register(20200, number * 10, MODBUS_SLAVE_ID)\n result = client.read_holding_registers(20200, 1, MODBUS_SLAVE_ID)\n print(\"установлена скорость\", result.registers[0] * 0.1)\ndef set_user_register(n, values):\n client.write_registers(indent + s + k * n, values, MODBUS_SLAVE_ID)\ndef to_double(integr):",
|
||||||
|
"detail": "client",
|
||||||
|
"documentation": {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "set_user_register",
|
||||||
|
"kind": 2,
|
||||||
|
"importPath": "client",
|
||||||
|
"description": "client",
|
||||||
|
"peekOfCode": "def set_user_register(n, values):\n client.write_registers(indent + s + k * n, values, MODBUS_SLAVE_ID)\ndef to_double(integr):\n return [(integr >> 16) & 0xFFFF, integr & 0xFFFF]\nclient = ModbusTcpClient(\n host=MODBUS_SERVER_HOST,\n port=MODBUS_SERVER_PORT,\n)\nclient.connect()\n# максимальное количество coils = 286",
|
||||||
|
"detail": "client",
|
||||||
|
"documentation": {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "to_double",
|
||||||
|
"kind": 2,
|
||||||
|
"importPath": "client",
|
||||||
|
"description": "client",
|
||||||
|
"peekOfCode": "def to_double(integr):\n return [(integr >> 16) & 0xFFFF, integr & 0xFFFF]\nclient = ModbusTcpClient(\n host=MODBUS_SERVER_HOST,\n port=MODBUS_SERVER_PORT,\n)\nclient.connect()\n# максимальное количество coils = 286\n# bulb(2)\n# get_coordinates()",
|
||||||
"detail": "client",
|
"detail": "client",
|
||||||
"documentation": {}
|
"documentation": {}
|
||||||
},
|
},
|
||||||
|
@ -85,7 +148,7 @@
|
||||||
"kind": 5,
|
"kind": 5,
|
||||||
"importPath": "client",
|
"importPath": "client",
|
||||||
"description": "client",
|
"description": "client",
|
||||||
"peekOfCode": "MODBUS_SERVER_HOST = \"192.168.70.55\" # IP-адрес Modbus-сервера\nMODBUS_SERVER_PORT = 502\nMODBUS_SLAVE_ID=11\nfrom pymodbus.client import ModbusTcpClient\nfrom pymodbus.constants import Endian\nfrom pymodbus.payload import BinaryPayloadDecoder\ndef bulb(addr):\n try:\n coils = client.read_coils(addr, 1, MODBUS_SLAVE_ID)\n print(\"читаем начальное состояние\", coils.bits)",
|
"peekOfCode": "MODBUS_SERVER_HOST = \"192.168.70.55\" # IP-адрес Modbus-сервера\nMODBUS_SERVER_PORT = 502\nMODBUS_SLAVE_ID = 11\nindent = 21100\ns = 800\nk = 2\nfrom pymodbus.client import ModbusTcpClient\nfrom pymodbus.constants import Endian\nfrom pymodbus.payload import BinaryPayloadDecoder\nimport time",
|
||||||
"detail": "client",
|
"detail": "client",
|
||||||
"documentation": {}
|
"documentation": {}
|
||||||
},
|
},
|
||||||
|
@ -94,7 +157,43 @@
|
||||||
"kind": 5,
|
"kind": 5,
|
||||||
"importPath": "client",
|
"importPath": "client",
|
||||||
"description": "client",
|
"description": "client",
|
||||||
"peekOfCode": "MODBUS_SERVER_PORT = 502\nMODBUS_SLAVE_ID=11\nfrom pymodbus.client import ModbusTcpClient\nfrom pymodbus.constants import Endian\nfrom pymodbus.payload import BinaryPayloadDecoder\ndef bulb(addr):\n try:\n coils = client.read_coils(addr, 1, MODBUS_SLAVE_ID)\n print(\"читаем начальное состояние\", coils.bits)\n new_bits = coils.bits",
|
"peekOfCode": "MODBUS_SERVER_PORT = 502\nMODBUS_SLAVE_ID = 11\nindent = 21100\ns = 800\nk = 2\nfrom pymodbus.client import ModbusTcpClient\nfrom pymodbus.constants import Endian\nfrom pymodbus.payload import BinaryPayloadDecoder\nimport time\nimport struct",
|
||||||
|
"detail": "client",
|
||||||
|
"documentation": {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "MODBUS_SLAVE_ID",
|
||||||
|
"kind": 5,
|
||||||
|
"importPath": "client",
|
||||||
|
"description": "client",
|
||||||
|
"peekOfCode": "MODBUS_SLAVE_ID = 11\nindent = 21100\ns = 800\nk = 2\nfrom pymodbus.client import ModbusTcpClient\nfrom pymodbus.constants import Endian\nfrom pymodbus.payload import BinaryPayloadDecoder\nimport time\nimport struct\ndef two_byte_convert(result):",
|
||||||
|
"detail": "client",
|
||||||
|
"documentation": {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "indent",
|
||||||
|
"kind": 5,
|
||||||
|
"importPath": "client",
|
||||||
|
"description": "client",
|
||||||
|
"peekOfCode": "indent = 21100\ns = 800\nk = 2\nfrom pymodbus.client import ModbusTcpClient\nfrom pymodbus.constants import Endian\nfrom pymodbus.payload import BinaryPayloadDecoder\nimport time\nimport struct\ndef two_byte_convert(result):\n # print(result)",
|
||||||
|
"detail": "client",
|
||||||
|
"documentation": {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "s",
|
||||||
|
"kind": 5,
|
||||||
|
"importPath": "client",
|
||||||
|
"description": "client",
|
||||||
|
"peekOfCode": "s = 800\nk = 2\nfrom pymodbus.client import ModbusTcpClient\nfrom pymodbus.constants import Endian\nfrom pymodbus.payload import BinaryPayloadDecoder\nimport time\nimport struct\ndef two_byte_convert(result):\n # print(result)\n v0 = result.registers[0]",
|
||||||
|
"detail": "client",
|
||||||
|
"documentation": {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "k",
|
||||||
|
"kind": 5,
|
||||||
|
"importPath": "client",
|
||||||
|
"description": "client",
|
||||||
|
"peekOfCode": "k = 2\nfrom pymodbus.client import ModbusTcpClient\nfrom pymodbus.constants import Endian\nfrom pymodbus.payload import BinaryPayloadDecoder\nimport time\nimport struct\ndef two_byte_convert(result):\n # print(result)\n v0 = result.registers[0]\n value = (v0 << 16) + result.registers[1]",
|
||||||
"detail": "client",
|
"detail": "client",
|
||||||
"documentation": {}
|
"documentation": {}
|
||||||
},
|
},
|
||||||
|
@ -103,16 +202,25 @@
|
||||||
"kind": 5,
|
"kind": 5,
|
||||||
"importPath": "client",
|
"importPath": "client",
|
||||||
"description": "client",
|
"description": "client",
|
||||||
"peekOfCode": "client = ModbusTcpClient(\n MODBUS_SERVER_HOST,\n port=MODBUS_SERVER_PORT,\n)\nclient.connect()\ntry:\n bulb(1)\nexcept Exception as e:\n print(\"error\", e)\nclient.close()",
|
"peekOfCode": "client = ModbusTcpClient(\n host=MODBUS_SERVER_HOST,\n port=MODBUS_SERVER_PORT,\n)\nclient.connect()\n# максимальное количество coils = 286\n# bulb(2)\n# get_coordinates()\n# get_or_set_speed(10)\ntry:",
|
||||||
"detail": "client",
|
"detail": "client",
|
||||||
"documentation": {}
|
"documentation": {}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"label": "get_reg_range",
|
||||||
|
"kind": 2,
|
||||||
|
"importPath": "other_client",
|
||||||
|
"description": "other_client",
|
||||||
|
"peekOfCode": "def get_reg_range(start, stop):\n for i in range(start, stop):\n r = client.read_holding_registers(i, 1)\n if r and r != [0] and r != [65535]:\n print(f\"{i} === {r}\")\nget_reg_range(0, 100)\nget_reg_range(2100, 2600)\nget_reg_range(20000, 24000)\nget_reg_range(56000, 57000)\n# r = client.read_holding_registers(21901, 1)",
|
||||||
|
"detail": "other_client",
|
||||||
|
"documentation": {}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"label": "MODBUS_SERVER_HOST",
|
"label": "MODBUS_SERVER_HOST",
|
||||||
"kind": 5,
|
"kind": 5,
|
||||||
"importPath": "other_client",
|
"importPath": "other_client",
|
||||||
"description": "other_client",
|
"description": "other_client",
|
||||||
"peekOfCode": "MODBUS_SERVER_HOST = \"192.168.70.55\" # IP-адрес Modbus-сервера\nMODBUS_SERVER_PORT = 502\nclient = ModbusClient(\n host=MODBUS_SERVER_HOST,\n port=MODBUS_SERVER_PORT,\n unit_id=11,\n # debug=True\n)\nclient.open()\naddr = 2",
|
"peekOfCode": "MODBUS_SERVER_HOST = \"192.168.70.55\" # IP-адрес Modbus-сервера\nMODBUS_SERVER_PORT = 502\nclient = ModbusClient(\n host=MODBUS_SERVER_HOST,\n port=MODBUS_SERVER_PORT,\n unit_id=11,\n # debug=True\n)\nclient.open()\n# addr = 2",
|
||||||
"detail": "other_client",
|
"detail": "other_client",
|
||||||
"documentation": {}
|
"documentation": {}
|
||||||
},
|
},
|
||||||
|
@ -121,7 +229,7 @@
|
||||||
"kind": 5,
|
"kind": 5,
|
||||||
"importPath": "other_client",
|
"importPath": "other_client",
|
||||||
"description": "other_client",
|
"description": "other_client",
|
||||||
"peekOfCode": "MODBUS_SERVER_PORT = 502\nclient = ModbusClient(\n host=MODBUS_SERVER_HOST,\n port=MODBUS_SERVER_PORT,\n unit_id=11,\n # debug=True\n)\nclient.open()\naddr = 2\nlight = client.read_coils(addr, 1)",
|
"peekOfCode": "MODBUS_SERVER_PORT = 502\nclient = ModbusClient(\n host=MODBUS_SERVER_HOST,\n port=MODBUS_SERVER_PORT,\n unit_id=11,\n # debug=True\n)\nclient.open()\n# addr = 2\n# light = client.read_coils(addr, 1)",
|
||||||
"detail": "other_client",
|
"detail": "other_client",
|
||||||
"documentation": {}
|
"documentation": {}
|
||||||
},
|
},
|
||||||
|
@ -130,26 +238,35 @@
|
||||||
"kind": 5,
|
"kind": 5,
|
||||||
"importPath": "other_client",
|
"importPath": "other_client",
|
||||||
"description": "other_client",
|
"description": "other_client",
|
||||||
"peekOfCode": "client = ModbusClient(\n host=MODBUS_SERVER_HOST,\n port=MODBUS_SERVER_PORT,\n unit_id=11,\n # debug=True\n)\nclient.open()\naddr = 2\nlight = client.read_coils(addr, 1)\nprint(\"состояние лампочки\", light)",
|
"peekOfCode": "client = ModbusClient(\n host=MODBUS_SERVER_HOST,\n port=MODBUS_SERVER_PORT,\n unit_id=11,\n # debug=True\n)\nclient.open()\n# addr = 2\n# light = client.read_coils(addr, 1)\n# print(\"состояние лампочки\", light)",
|
||||||
"detail": "other_client",
|
"detail": "other_client",
|
||||||
"documentation": {}
|
"documentation": {}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"label": "addr",
|
"label": "res",
|
||||||
"kind": 5,
|
"kind": 5,
|
||||||
"importPath": "other_client",
|
"importPath": "other_client",
|
||||||
"description": "other_client",
|
"description": "other_client",
|
||||||
"peekOfCode": "addr = 2\nlight = client.read_coils(addr, 1)\nprint(\"состояние лампочки\", light)\nprint(\"пишем противоположное\", not light[0])\nclient.write_single_coil(addr, not light[0])\nprint(\"теперь состояние\", client.read_coils(addr, 1))\nclient.close()",
|
"peekOfCode": "res = []\ndef get_reg_range(start, stop):\n for i in range(start, stop):\n r = client.read_holding_registers(i, 1)\n if r and r != [0] and r != [65535]:\n print(f\"{i} === {r}\")\nget_reg_range(0, 100)\nget_reg_range(2100, 2600)\nget_reg_range(20000, 24000)\nget_reg_range(56000, 57000)",
|
||||||
"detail": "other_client",
|
"detail": "other_client",
|
||||||
"documentation": {}
|
"documentation": {}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"label": "light",
|
"label": "MODBUS_SERVER_HOST",
|
||||||
"kind": 5,
|
"kind": 5,
|
||||||
"importPath": "other_client",
|
"importPath": "send_photo",
|
||||||
"description": "other_client",
|
"description": "send_photo",
|
||||||
"peekOfCode": "light = client.read_coils(addr, 1)\nprint(\"состояние лампочки\", light)\nprint(\"пишем противоположное\", not light[0])\nclient.write_single_coil(addr, not light[0])\nprint(\"теперь состояние\", client.read_coils(addr, 1))\nclient.close()",
|
"peekOfCode": "MODBUS_SERVER_HOST = \"192.168.70.55\" # IP-адрес Modbus-сервера\nMODBUS_SERVER_PORT = 9760",
|
||||||
"detail": "other_client",
|
"detail": "send_photo",
|
||||||
|
"documentation": {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "MODBUS_SERVER_PORT",
|
||||||
|
"kind": 5,
|
||||||
|
"importPath": "send_photo",
|
||||||
|
"description": "send_photo",
|
||||||
|
"peekOfCode": "MODBUS_SERVER_PORT = 9760",
|
||||||
|
"detail": "send_photo",
|
||||||
"documentation": {}
|
"documentation": {}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
129
client.py
129
client.py
|
@ -3,11 +3,27 @@ print("modbus test")
|
||||||
# Настройки клиента Modbus
|
# Настройки клиента Modbus
|
||||||
MODBUS_SERVER_HOST = "192.168.70.55" # IP-адрес Modbus-сервера
|
MODBUS_SERVER_HOST = "192.168.70.55" # IP-адрес Modbus-сервера
|
||||||
MODBUS_SERVER_PORT = 502
|
MODBUS_SERVER_PORT = 502
|
||||||
MODBUS_SLAVE_ID=11
|
MODBUS_SLAVE_ID = 11
|
||||||
|
|
||||||
|
indent = 21100
|
||||||
|
s = 800
|
||||||
|
k = 2
|
||||||
|
|
||||||
from pymodbus.client import ModbusTcpClient
|
from pymodbus.client import ModbusTcpClient
|
||||||
from pymodbus.constants import Endian
|
from pymodbus.constants import Endian
|
||||||
from pymodbus.payload import BinaryPayloadDecoder
|
from pymodbus.payload import BinaryPayloadDecoder
|
||||||
|
import time
|
||||||
|
import struct
|
||||||
|
|
||||||
|
|
||||||
|
def two_byte_convert(result):
|
||||||
|
# print(result)
|
||||||
|
v0 = result.registers[0]
|
||||||
|
value = (v0 << 16) + result.registers[1]
|
||||||
|
if result.registers[0] > (65535 * 0.5):
|
||||||
|
value = value - (1 << 32)
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
def bulb(addr):
|
def bulb(addr):
|
||||||
try:
|
try:
|
||||||
|
@ -20,17 +36,122 @@ def bulb(addr):
|
||||||
result = client.write_coil(addr, new_bits[0], coils.slave_id)
|
result = client.write_coil(addr, new_bits[0], coils.slave_id)
|
||||||
print(result)
|
print(result)
|
||||||
|
|
||||||
print("читаем после попытки", client.read_coils(0, len(coils.bits), coils.slave_id).bits)
|
print(
|
||||||
|
"читаем после попытки",
|
||||||
|
client.read_coils(0, len(coils.bits), coils.slave_id).bits,
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
|
|
||||||
|
def get_coordinates():
|
||||||
|
try:
|
||||||
|
data = [
|
||||||
|
("j1", 2268),
|
||||||
|
("j2", 2270),
|
||||||
|
("j3", 2272),
|
||||||
|
("j4", 2274),
|
||||||
|
("j5", 2276),
|
||||||
|
("j6", 2278),
|
||||||
|
("x", 2332),
|
||||||
|
("y", 2334),
|
||||||
|
("z", 2336),
|
||||||
|
("u", 2338),
|
||||||
|
("v", 2340),
|
||||||
|
("w", 2342),
|
||||||
|
]
|
||||||
|
i = 2
|
||||||
|
for d in data:
|
||||||
|
result = client.read_holding_registers(d[1], 2, MODBUS_SLAVE_ID)
|
||||||
|
value = two_byte_convert(result)
|
||||||
|
adjusted_value = value / 1000.0
|
||||||
|
i *= 2
|
||||||
|
print(d[0], result.registers, adjusted_value)
|
||||||
|
except Exception as e:
|
||||||
|
raise e
|
||||||
|
|
||||||
|
|
||||||
|
def get_or_set_speed(number=None):
|
||||||
|
result = client.read_holding_registers(20200, 1, MODBUS_SLAVE_ID)
|
||||||
|
print("сейчас скорость", result.registers[0] * 0.1)
|
||||||
|
if number and number > 0 and number <= 1000:
|
||||||
|
client.write_register(20200, number * 10, MODBUS_SLAVE_ID)
|
||||||
|
result = client.read_holding_registers(20200, 1, MODBUS_SLAVE_ID)
|
||||||
|
print("установлена скорость", result.registers[0] * 0.1)
|
||||||
|
|
||||||
|
|
||||||
|
def set_user_register(n, values):
|
||||||
|
client.write_registers(indent + s + k * n, values, MODBUS_SLAVE_ID)
|
||||||
|
|
||||||
|
|
||||||
|
def to_double(integr):
|
||||||
|
return [(integr >> 16) & 0xFFFF, integr & 0xFFFF]
|
||||||
|
|
||||||
|
|
||||||
client = ModbusTcpClient(
|
client = ModbusTcpClient(
|
||||||
MODBUS_SERVER_HOST,
|
host=MODBUS_SERVER_HOST,
|
||||||
port=MODBUS_SERVER_PORT,
|
port=MODBUS_SERVER_PORT,
|
||||||
)
|
)
|
||||||
client.connect()
|
client.connect()
|
||||||
|
# максимальное количество coils = 286
|
||||||
|
|
||||||
|
# bulb(2)
|
||||||
|
# get_coordinates()
|
||||||
|
# get_or_set_speed(10)
|
||||||
try:
|
try:
|
||||||
bulb(1)
|
# старт в авторежиме single loop
|
||||||
|
client.write_register(20002, 1, MODBUS_SLAVE_ID)
|
||||||
|
|
||||||
|
total = 0
|
||||||
|
state = None
|
||||||
|
counter = 0
|
||||||
|
while True:
|
||||||
|
time.sleep(1)
|
||||||
|
total += 1
|
||||||
|
green_light = client.read_coils(0, 1, MODBUS_SLAVE_ID).bits[0]
|
||||||
|
if state == green_light:
|
||||||
|
continue
|
||||||
|
|
||||||
|
state = green_light
|
||||||
|
if state == False:
|
||||||
|
continue
|
||||||
|
|
||||||
|
counter += 1
|
||||||
|
# собираем предварительные данные
|
||||||
|
x = two_byte_convert(client.read_holding_registers(2332, 2, MODBUS_SLAVE_ID))
|
||||||
|
y = two_byte_convert(client.read_holding_registers(2334, 2, MODBUS_SLAVE_ID))
|
||||||
|
z = two_byte_convert(client.read_holding_registers(2336, 2, MODBUS_SLAVE_ID))
|
||||||
|
u = two_byte_convert(client.read_holding_registers(2338, 2, MODBUS_SLAVE_ID))
|
||||||
|
v = two_byte_convert(client.read_holding_registers(2340, 2, MODBUS_SLAVE_ID))
|
||||||
|
w = two_byte_convert(client.read_holding_registers(2342, 2, MODBUS_SLAVE_ID))
|
||||||
|
print(
|
||||||
|
f"Предварительные данные X {x * 0.001}, Y {y * 0.001}, Z {z*0.001}, U {u*0.001}, V {v*0.001}, W {w*0.001}"
|
||||||
|
)
|
||||||
|
if counter == 1:
|
||||||
|
x -= 100 * 1000
|
||||||
|
elif counter == 2:
|
||||||
|
y -= 100 * 1000
|
||||||
|
elif counter == 3:
|
||||||
|
x += 100 * 1000
|
||||||
|
elif counter == 4:
|
||||||
|
y += 100 * 1000
|
||||||
|
else:
|
||||||
|
counter = 0
|
||||||
|
data = [
|
||||||
|
(0, to_double(x)),
|
||||||
|
(1, to_double(y)),
|
||||||
|
(2, to_double(z)),
|
||||||
|
(3, [0, 0]),
|
||||||
|
(4, [0, 0]),
|
||||||
|
(5, [0, 0]),
|
||||||
|
]
|
||||||
|
for item in data:
|
||||||
|
d, values = item
|
||||||
|
set_user_register(d, values)
|
||||||
|
result = client.read_holding_registers(indent + s + k * d, k, MODBUS_SLAVE_ID)
|
||||||
|
print(s + d, "===", two_byte_convert(result) * 0.001)
|
||||||
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("error", e)
|
print("error", e)
|
||||||
|
|
||||||
|
|
|
@ -14,13 +14,33 @@ client = ModbusClient(
|
||||||
)
|
)
|
||||||
client.open()
|
client.open()
|
||||||
|
|
||||||
addr = 2
|
# addr = 2
|
||||||
light = client.read_coils(addr, 1)
|
# light = client.read_coils(addr, 1)
|
||||||
print("состояние лампочки", light)
|
# print("состояние лампочки", light)
|
||||||
|
|
||||||
print("пишем противоположное", not light[0])
|
# print("пишем противоположное", not light[0])
|
||||||
client.write_single_coil(addr, not light[0])
|
# client.write_single_coil(addr, not light[0])
|
||||||
|
|
||||||
print("теперь состояние", client.read_coils(addr, 1))
|
# print("теперь состояние", client.read_coils(addr, 1))
|
||||||
|
|
||||||
|
# максимум по input register 2413
|
||||||
|
# for i in range(2267, 2283):
|
||||||
|
# for i in range(2500, 2600):
|
||||||
|
res = []
|
||||||
|
def get_reg_range(start, stop):
|
||||||
|
for i in range(start, stop):
|
||||||
|
r = client.read_holding_registers(i, 1)
|
||||||
|
if r and r != [0] and r != [65535]:
|
||||||
|
print(f"{i} === {r}")
|
||||||
|
|
||||||
|
get_reg_range(0, 100)
|
||||||
|
get_reg_range(2100, 2600)
|
||||||
|
get_reg_range(20000, 24000)
|
||||||
|
get_reg_range(56000, 57000)
|
||||||
|
|
||||||
|
# r = client.read_holding_registers(21901, 1)
|
||||||
|
# print(r)
|
||||||
|
|
||||||
|
# 21901 --- 800 регистр
|
||||||
|
|
||||||
client.close()
|
client.close()
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
MODBUS_SERVER_HOST = "192.168.70.55" # IP-адрес Modbus-сервера
|
||||||
|
MODBUS_SERVER_PORT = 9760
|
||||||
|
|
Loading…
Reference in New Issue