-
Notifications
You must be signed in to change notification settings - Fork 1k
Closed
Description
pymodbus verison is 3.8.6
I go through the database and get the addresses and the ip of the modbus devices and establish the connection. after the wet is saved in the DB, the connection should actually be disconnected. but unfortunately this does not happen.
Too many connections are open after approx. 5 minutes.
x.10 is the server and x.45:502 the modbus devices
tcp 0 0 x.10:53964 x.45:502 TIME_WAIT timewait (15,60/0/0)
tcp 0 0 x.10:48414 x.47:502 TIME_WAIT timewait (19,34/0/0)
tcp 0 0 x.10:45588 x.27:502 TIME_WAIT timewait (25,87/0/0)
Here the Code:
includes:
from datetime import datetime, timedelta, timezone
from pymodbus.constants import Endian
#from pymodbus.payload import BinaryPayloadDecoder
import pymodbus.client as ModbusTcpClient
from pymodbus import (
ExceptionResponse,
FramerType,
ModbusException,
pymodbus_apply_logging_config,
)
async def con_modbus_device():
start_time = time.time()
start_timestamp=datetime.utcnow()
cur = GL.con_mariadb.cursor(buffered=True , dictionary=True)
table = GL.config['mariadb']['TableBMS']
sql = f'SELECT id, ip, device, device_id FROM {table} WHERE protocol="modbus"'
cur.execute(sql)
res = cur.fetchall()
if not res:
await GL.log('error',f'no modbus device found')
else:
for row in res:
bmsid=row["id"]
ip=row["ip"]
device=row["device"]
device_id=row["device_id"]
await GL.log('info',f'try to establish a connection {ip}')
if GL.config['modbus']['debug']:
pymodbus_apply_logging_config("DEBUG")
try:
client = ModbusTcpClient.AsyncModbusTcpClient(ip,port=GL.config['modbus']['port'],framer=FramerType.SOCKET, timeout=2)
connection = await client.connect()
if connection:
table = GL.config['mariadb']['TableBMSFields']
sql = f'SELECT id, field, adress, actual, units_state FROM {table} WHERE bmsid="{bmsid}"'
cur.execute(sql)
res2 = cur.fetchall()
for row2 in res2:
fieldid=row2["id"]
field=row2["field"]
adress=row2["adress"]
try:
rr = await client.read_holding_registers(int(adress), count=2, slave=1)
num = client.convert_from_registers(rr.registers, client.DATATYPE.FLOAT32)
value = float('{0:.2f}'.format(num))
end_timestamp=datetime.utcnow()
interval = await measuring_interval_start(start_timestamp) #(15 min. measuring interval)
await insert_value(field, value, start_timestamp, interval, bmsid)
if rr.isError():
await GL.log('error',f"Received Modbus library error({rr})")
if isinstance(rr, ExceptionResponse):
await GL.log('error',f"Received Modbus library exception ({rr})")
except ModbusException as exc:
await GL.log('error',f"Received ModbusException({exc}) from library")
client.close()
await GL.log('info',f'Connection to the device {ip} successful')
else:
print('client connection lost')
except ModbusException as e:
await GL.log('error',f'Connection to the device {ip} faild')
await GL.log('error',f'{e}')
pass
What am I doing wrong that client.close() does not work?
Metadata
Metadata
Assignees
Labels
No labels