This commit is contained in:
aarizona 2024-08-13 19:37:28 +03:00
parent bd56ab2b36
commit 54bb74b2f5
7 changed files with 374 additions and 274 deletions

View File

@ -1,4 +1,36 @@
[
{
"label": "*",
"importPath": "func",
"description": "func",
"isExtraImport": true,
"detail": "func",
"documentation": {}
},
{
"label": "*",
"importPath": "func",
"description": "func",
"isExtraImport": true,
"detail": "func",
"documentation": {}
},
{
"label": "ModbusTcpClient",
"importPath": "pymodbus.client",
"description": "pymodbus.client",
"isExtraImport": true,
"detail": "pymodbus.client",
"documentation": {}
},
{
"label": "ModbusTcpClient",
"importPath": "pymodbus.client",
"description": "pymodbus.client",
"isExtraImport": true,
"detail": "pymodbus.client",
"documentation": {}
},
{
"label": "ModbusTcpClient",
"importPath": "pymodbus.client",
@ -15,6 +47,22 @@
"detail": "pymodbus.constants",
"documentation": {}
},
{
"label": "Endian",
"importPath": "pymodbus.constants",
"description": "pymodbus.constants",
"isExtraImport": true,
"detail": "pymodbus.constants",
"documentation": {}
},
{
"label": "BinaryPayloadDecoder",
"importPath": "pymodbus.payload",
"description": "pymodbus.payload",
"isExtraImport": true,
"detail": "pymodbus.payload",
"documentation": {}
},
{
"label": "BinaryPayloadDecoder",
"importPath": "pymodbus.payload",
@ -42,137 +90,11 @@
"documentation": {}
},
{
"label": "two_byte_convert",
"label": "multiply_1000",
"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",
"kind": 2,
"importPath": "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(",
"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": "collect_coordinates",
"kind": 2,
"importPath": "client",
"description": "client",
"peekOfCode": "def collect_coordinates():\n # собираем предварительные данные\n x = two_byte_convert(client.read_holding_registers(2332, 2, MODBUS_SLAVE_ID))\n y = two_byte_convert(client.read_holding_registers(2334, 2, MODBUS_SLAVE_ID))\n z = two_byte_convert(client.read_holding_registers(2336, 2, MODBUS_SLAVE_ID))\n u = two_byte_convert(client.read_holding_registers(2338, 2, MODBUS_SLAVE_ID))\n v = two_byte_convert(client.read_holding_registers(2340, 2, MODBUS_SLAVE_ID))\n w = two_byte_convert(client.read_holding_registers(2342, 2, MODBUS_SLAVE_ID))\n print(\n 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}\"",
"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]\ndef set_user_reg_800(data):\n for item in data:\n d, values = item\n set_user_register(d, values)\ndef start_in_auto():\n # старт в авторежиме single loop",
"detail": "client",
"documentation": {}
},
{
"label": "to_double",
"kind": 2,
"importPath": "client",
"description": "client",
"peekOfCode": "def to_double(integr):\n return [(integr >> 16) & 0xFFFF, integr & 0xFFFF]\ndef set_user_reg_800(data):\n for item in data:\n d, values = item\n set_user_register(d, values)\ndef start_in_auto():\n # старт в авторежиме single loop\n client.write_register(20002, 1, MODBUS_SLAVE_ID)\nclient = ModbusTcpClient(",
"detail": "client",
"documentation": {}
},
{
"label": "set_user_reg_800",
"kind": 2,
"importPath": "client",
"description": "client",
"peekOfCode": "def set_user_reg_800(data):\n for item in data:\n d, values = item\n set_user_register(d, values)\ndef start_in_auto():\n # старт в авторежиме single loop\n client.write_register(20002, 1, MODBUS_SLAVE_ID)\nclient = ModbusTcpClient(\n host=MODBUS_SERVER_HOST,\n port=MODBUS_SERVER_PORT,",
"detail": "client",
"documentation": {}
},
{
"label": "start_in_auto",
"kind": 2,
"importPath": "client",
"description": "client",
"peekOfCode": "def start_in_auto():\n # старт в авторежиме single loop\n client.write_register(20002, 1, MODBUS_SLAVE_ID)\nclient = ModbusTcpClient(\n host=MODBUS_SERVER_HOST,\n port=MODBUS_SERVER_PORT,\n)\nclient.connect()\n# максимальное количество coils = 286\n# bulb(0)",
"detail": "client",
"documentation": {}
},
{
"label": "MODBUS_SERVER_HOST",
"kind": 5,
"importPath": "client",
"description": "client",
"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",
"documentation": {}
},
{
"label": "MODBUS_SERVER_PORT",
"kind": 5,
"importPath": "client",
"description": "client",
"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 glob",
"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 glob\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 glob\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 glob\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 glob\ndef two_byte_convert(result):\n # print(result)\n v0 = result.registers[0]\n value = (v0 << 16) + result.registers[1]",
"peekOfCode": "def multiply_1000(n):\n return n * 1000\ntry:\n total = 0\n state = None\n step = 0\n counter = 0\n paths = data\n paths = [\n (\"line\", multiply_1000(300), multiply_1000(300), 0),",
"detail": "client",
"documentation": {}
},
@ -181,7 +103,7 @@
"kind": 5,
"importPath": "client",
"description": "client",
"peekOfCode": "client = ModbusTcpClient(\n host=MODBUS_SERVER_HOST,\n port=MODBUS_SERVER_PORT,\n)\nclient.connect()\n# максимальное количество coils = 286\n# bulb(0)\n# get_coordinates()\n# get_or_set_speed(10)\nall_files = glob.glob(\"data/*.NC.result\")",
"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)\nall_files = glob.glob(\"data/*.NC.result\")",
"detail": "client",
"documentation": {}
},
@ -190,7 +112,7 @@
"kind": 5,
"importPath": "client",
"description": "client",
"peekOfCode": "all_files = glob.glob(\"data/*.NC.result\")\nfile = all_files[0]\ndata = []\nwith open(file, 'r') as fp:\n lines = fp.readlines()\n for l in lines:\n data.append(l.strip().split(','))\nprint(data)\ntry:\n total = 0",
"peekOfCode": "all_files = glob.glob(\"data/*.NC.result\")\nfile = all_files[0]\ndata = []\nwith open(file, \"r\") as fp:\n lines = fp.readlines()\n for l in lines:\n r = l.strip().split(\",\")\n r[1] = int(float(r[1]) * 1000)\n r[2] = int(float(r[2]) * 1000)\n r[3] = int(float(r[3]) * 1000)",
"detail": "client",
"documentation": {}
},
@ -199,7 +121,7 @@
"kind": 5,
"importPath": "client",
"description": "client",
"peekOfCode": "file = all_files[0]\ndata = []\nwith open(file, 'r') as fp:\n lines = fp.readlines()\n for l in lines:\n data.append(l.strip().split(','))\nprint(data)\ntry:\n total = 0\n state = None",
"peekOfCode": "file = all_files[0]\ndata = []\nwith open(file, \"r\") as fp:\n lines = fp.readlines()\n for l in lines:\n r = l.strip().split(\",\")\n r[1] = int(float(r[1]) * 1000)\n r[2] = int(float(r[2]) * 1000)\n r[3] = int(float(r[3]) * 1000)\n data.append(r)",
"detail": "client",
"documentation": {}
},
@ -208,10 +130,145 @@
"kind": 5,
"importPath": "client",
"description": "client",
"peekOfCode": "data = []\nwith open(file, 'r') as fp:\n lines = fp.readlines()\n for l in lines:\n data.append(l.strip().split(','))\nprint(data)\ntry:\n total = 0\n state = None\n step = 0",
"peekOfCode": "data = []\nwith open(file, \"r\") as fp:\n lines = fp.readlines()\n for l in lines:\n r = l.strip().split(\",\")\n r[1] = int(float(r[1]) * 1000)\n r[2] = int(float(r[2]) * 1000)\n r[3] = int(float(r[3]) * 1000)\n data.append(r)\n# print(data)",
"detail": "client",
"documentation": {}
},
{
"label": "two_byte_convert",
"kind": 2,
"importPath": "func",
"description": "func",
"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, new_state, client):\n try:\n coils = client.read_coils(addr, 1, MODBUS_SLAVE_ID)",
"detail": "func",
"documentation": {}
},
{
"label": "bulb",
"kind": 2,
"importPath": "func",
"description": "func",
"peekOfCode": "def bulb(addr, new_state, client):\n try:\n coils = client.read_coils(addr, 1, MODBUS_SLAVE_ID)\n # print(\"читаем начальное состояние\", coils.bits)\n if not coils.bits[0] == new_state:\n result = client.write_coil(addr, new_state, coils.slave_id)\n # print(result)\n # print(\n # \"читаем после попытки\",\n # client.read_coils(0, len(coils.bits), coils.slave_id).bits,",
"detail": "func",
"documentation": {}
},
{
"label": "get_coordinates",
"kind": 2,
"importPath": "func",
"description": "func",
"peekOfCode": "def get_coordinates(client):\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": "func",
"documentation": {}
},
{
"label": "collect_coordinates",
"kind": 2,
"importPath": "func",
"description": "func",
"peekOfCode": "def collect_coordinates(client):\n # собираем предварительные данные\n x = two_byte_convert(client.read_holding_registers(2332, 2, MODBUS_SLAVE_ID))\n y = two_byte_convert(client.read_holding_registers(2334, 2, MODBUS_SLAVE_ID))\n z = two_byte_convert(client.read_holding_registers(2336, 2, MODBUS_SLAVE_ID))\n u = two_byte_convert(client.read_holding_registers(2338, 2, MODBUS_SLAVE_ID))\n v = two_byte_convert(client.read_holding_registers(2340, 2, MODBUS_SLAVE_ID))\n w = two_byte_convert(client.read_holding_registers(2342, 2, MODBUS_SLAVE_ID))\n print(\n 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}\"",
"detail": "func",
"documentation": {}
},
{
"label": "get_or_set_speed",
"kind": 2,
"importPath": "func",
"description": "func",
"peekOfCode": "def get_or_set_speed(number, client):\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, client):\n client.write_registers(indent + s + k * n, values, MODBUS_SLAVE_ID)\ndef to_double(integr):",
"detail": "func",
"documentation": {}
},
{
"label": "set_user_register",
"kind": 2,
"importPath": "func",
"description": "func",
"peekOfCode": "def set_user_register(n, values, client):\n client.write_registers(indent + s + k * n, values, MODBUS_SLAVE_ID)\ndef to_double(integr):\n return [(integr >> 16) & 0xFFFF, integr & 0xFFFF]\ndef set_user_reg_800(data, client):\n for item in data:\n d, values = item\n set_user_register(d, values, client)\ndef start_in_auto(client):\n # старт в авторежиме single loop",
"detail": "func",
"documentation": {}
},
{
"label": "to_double",
"kind": 2,
"importPath": "func",
"description": "func",
"peekOfCode": "def to_double(integr):\n return [(integr >> 16) & 0xFFFF, integr & 0xFFFF]\ndef set_user_reg_800(data, client):\n for item in data:\n d, values = item\n set_user_register(d, values, client)\ndef start_in_auto(client):\n # старт в авторежиме single loop\n client.write_register(20002, 1, MODBUS_SLAVE_ID)",
"detail": "func",
"documentation": {}
},
{
"label": "set_user_reg_800",
"kind": 2,
"importPath": "func",
"description": "func",
"peekOfCode": "def set_user_reg_800(data, client):\n for item in data:\n d, values = item\n set_user_register(d, values, client)\ndef start_in_auto(client):\n # старт в авторежиме single loop\n client.write_register(20002, 1, MODBUS_SLAVE_ID)",
"detail": "func",
"documentation": {}
},
{
"label": "start_in_auto",
"kind": 2,
"importPath": "func",
"description": "func",
"peekOfCode": "def start_in_auto(client):\n # старт в авторежиме single loop\n client.write_register(20002, 1, MODBUS_SLAVE_ID)",
"detail": "func",
"documentation": {}
},
{
"label": "MODBUS_SERVER_HOST",
"kind": 5,
"importPath": "func",
"description": "func",
"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": "func",
"documentation": {}
},
{
"label": "MODBUS_SERVER_PORT",
"kind": 5,
"importPath": "func",
"description": "func",
"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 glob",
"detail": "func",
"documentation": {}
},
{
"label": "MODBUS_SLAVE_ID",
"kind": 5,
"importPath": "func",
"description": "func",
"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 glob\ndef two_byte_convert(result):",
"detail": "func",
"documentation": {}
},
{
"label": "indent",
"kind": 5,
"importPath": "func",
"description": "func",
"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 glob\ndef two_byte_convert(result):\n # print(result)",
"detail": "func",
"documentation": {}
},
{
"label": "s",
"kind": 5,
"importPath": "func",
"description": "func",
"peekOfCode": "s = 800\nk = 2\nfrom pymodbus.client import ModbusTcpClient\nfrom pymodbus.constants import Endian\nfrom pymodbus.payload import BinaryPayloadDecoder\nimport time\nimport glob\ndef two_byte_convert(result):\n # print(result)\n v0 = result.registers[0]",
"detail": "func",
"documentation": {}
},
{
"label": "k",
"kind": 5,
"importPath": "func",
"description": "func",
"peekOfCode": "k = 2\nfrom pymodbus.client import ModbusTcpClient\nfrom pymodbus.constants import Endian\nfrom pymodbus.payload import BinaryPayloadDecoder\nimport time\nimport glob\ndef two_byte_convert(result):\n # print(result)\n v0 = result.registers[0]\n value = (v0 << 16) + result.registers[1]",
"detail": "func",
"documentation": {}
},
{
"label": "get_value",
"kind": 2,
@ -238,5 +295,14 @@
"peekOfCode": "file = all_files[0]\ndef get_value(c, s):\n [x] = [item.replace(s, '') for item in c if item.startswith(s)] or [0]\n return float(x)\nwith open(file, 'r') as fp:\n lines = fp.readlines()\n # print(lines)\n prev_x = None\n prev_y = None\n prev_z = None",
"detail": "prepare",
"documentation": {}
},
{
"label": "client",
"kind": 5,
"importPath": "test",
"description": "test",
"peekOfCode": "client = ModbusTcpClient(\n host=MODBUS_SERVER_HOST,\n port=MODBUS_SERVER_PORT,\n)\nclient.connect()\n# максимальное количество coils = 286\n# bulb(0, client)\n# get_coordinates()\nget_or_set_speed(30, client)\nclient.close()",
"detail": "test",
"documentation": {}
}
]

Binary file not shown.

Binary file not shown.

194
client.py
View File

@ -1,118 +1,13 @@
print("modbus test")
from func import *
# Настройки клиента Modbus
MODBUS_SERVER_HOST = "192.168.70.55" # IP-адрес Modbus-сервера
MODBUS_SERVER_PORT = 502
MODBUS_SLAVE_ID = 11
indent = 21100
s = 800
k = 2
# from func import set_user_reg_800
from pymodbus.client import ModbusTcpClient
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
import time
import glob
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):
try:
coils = client.read_coils(addr, 1, MODBUS_SLAVE_ID)
print("читаем начальное состояние", coils.bits)
new_bits = coils.bits
new_bits[0] = not new_bits[0]
print("пытаемся записать", new_bits, coils.slave_id)
result = client.write_coil(addr, new_bits[0], coils.slave_id)
print(result)
print(
"читаем после попытки",
client.read_coils(0, len(coils.bits), coils.slave_id).bits,
)
except Exception as 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 collect_coordinates():
# собираем предварительные данные
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}"
)
return [x, y, z, u, v, w]
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]
def set_user_reg_800(data):
for item in data:
d, values = item
set_user_register(d, values)
def start_in_auto():
# старт в авторежиме single loop
client.write_register(20002, 1, MODBUS_SLAVE_ID)
client = ModbusTcpClient(
host=MODBUS_SERVER_HOST,
port=MODBUS_SERVER_PORT,
@ -120,33 +15,46 @@ client = ModbusTcpClient(
client.connect()
# максимальное количество coils = 286
# bulb(0)
# bulb(2)
# get_coordinates()
# get_or_set_speed(10)
all_files = glob.glob("data/*.NC.result")
file = all_files[0]
data = []
with open(file, 'r') as fp:
with open(file, "r") as fp:
lines = fp.readlines()
for l in lines:
data.append(l.strip().split(','))
print(data)
r = l.strip().split(",")
r[1] = int(float(r[1]) * 1000)
r[2] = int(float(r[2]) * 1000)
r[3] = int(float(r[3]) * 1000)
data.append(r)
# print(data)
def multiply_1000(n):
return n * 1000
try:
total = 0
state = None
step = 0
counter = 0
type = "rel"
paths = data
paths = [
("line", 100, 100, 0),
("line", 100, -100, 0),
("line", -100, -100, 0),
("line", -100, 100, 0),
("line", 0, -50, 0),
("line", multiply_1000(300), multiply_1000(300), 0),
("line", multiply_1000(300), multiply_1000(-300), 0),
("line", multiply_1000(-300), multiply_1000(-300), 0),
("line", multiply_1000(-300), multiply_1000(300), 0),
# ("line", 0, multiply_1000(-50), 0),
]
bulb(2, False, client)
get_or_set_speed(10, client)
# ставим пользовательские переменные в ноль
set_user_reg_800(
[
@ -156,17 +64,19 @@ try:
(3, [0, 0]),
(4, [0, 0]),
(5, [0, 0]),
]
],
client,
)
_, _, _, u_target, v_target, w_target = collect_coordinates(client)
# старт в авторежиме single loop
start_in_auto()
start_in_auto(client)
while True:
time.sleep(1)
time.sleep(0.01)
total += 1
green_light = client.read_coils(0, 1, MODBUS_SLAVE_ID).bits[0]
print(f"total {total}, state {state}, green light {green_light}")
# print(f"total {total}, state {state}, green light {green_light}")
if state == green_light:
continue
@ -176,37 +86,39 @@ try:
if state == True:
continue
if type == "rel":
# работаем с REL относительными координатами
x = 0
y = 0
z = 0
else:
# работаем с абсолютными координатами
x, y, z, u, v, w = collect_coordinates()
x, y, z, u, v, w = collect_coordinates(client)
if step >= len(paths):
counter += 1
step = 0
current_step = paths[step]
line_type, *coord = current_step
print(f"{line_type} {step} of {len(paths)} {current_step}")
if line_type == "line":
x, y, z = coord
set_x, set_y, set_z = coord
u_set = u - u_target
v_set = v - v_target
w_set = w - w_target
set_user_reg_800(
[
(0, to_double(x)),
(1, to_double(y)),
(2, to_double(z)),
]
(0, to_double(set_x)),
(1, to_double(set_y)),
(2, to_double(set_z)),
(3, to_double(0)),
(4, to_double(0)),
(5, to_double(0)),
],
client,
)
bulb(4)
bulb(0)
step += 1
bulb(4, True, client)
if not x == y == z == 0:
bulb(0, True, client)
step += 1
except Exception as e:

View File

@ -1,4 +1,3 @@
line,0.0,0.0,0.0
line,0.0,0.0,0.07800000000000007
line,126.94399999999999,1.4160000000000004,0.23499999999999988
line,11.768,-16.211000000000002,0.039000000000000146

108
func.py Normal file
View File

@ -0,0 +1,108 @@
# Настройки клиента Modbus
MODBUS_SERVER_HOST = "192.168.70.55" # IP-адрес Modbus-сервера
MODBUS_SERVER_PORT = 502
MODBUS_SLAVE_ID = 11
indent = 21100
s = 800
k = 2
from pymodbus.client import ModbusTcpClient
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
import time
import glob
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, new_state, client):
try:
coils = client.read_coils(addr, 1, MODBUS_SLAVE_ID)
# print("читаем начальное состояние", coils.bits)
if not coils.bits[0] == new_state:
result = client.write_coil(addr, new_state, coils.slave_id)
# print(result)
# print(
# "читаем после попытки",
# client.read_coils(0, len(coils.bits), coils.slave_id).bits,
# )
except Exception as e:
raise e
def get_coordinates(client):
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 collect_coordinates(client):
# собираем предварительные данные
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}"
)
return [x, y, z, u, v, w]
def get_or_set_speed(number, client):
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):
client.write_registers(indent + s + k * n, values, MODBUS_SLAVE_ID)
def to_double(integr):
return [(integr >> 16) & 0xFFFF, integr & 0xFFFF]
def set_user_reg_800(data, client):
for item in data:
d, values = item
set_user_register(d, values, client)
def start_in_auto(client):
# старт в авторежиме single loop
client.write_register(20002, 1, MODBUS_SLAVE_ID)

15
test.py Normal file
View File

@ -0,0 +1,15 @@
from func import *
from pymodbus.client import ModbusTcpClient
client = ModbusTcpClient(
host=MODBUS_SERVER_HOST,
port=MODBUS_SERVER_PORT,
)
client.connect()
# максимальное количество coils = 286
# bulb(0, client)
# get_coordinates()
get_or_set_speed(30, client)
client.close()