Python 3.x pyModbusTCP每秒都会失败一次
基本上,我现在应该测试modbus功能,无论出于何种原因,modbus无法每秒读取寄存器,例如: 职能:Python 3.x pyModbusTCP每秒都会失败一次,python-3.x,pymodbustcp,Python 3.x,Pymodbustcp,基本上,我现在应该测试modbus功能,无论出于何种原因,modbus无法每秒读取寄存器,例如: 职能: # Modbus functions def read_system_uptime(c, ssh): try: # Get uptime with ubus ssh_result = str(exec_ssh("ubus call system info", ssh)) ssh_obj = json.loads(ss
# Modbus functions
def read_system_uptime(c, ssh):
try:
# Get uptime with ubus
ssh_result = str(exec_ssh("ubus call system info", ssh))
ssh_obj = json.loads(ssh_result)
uptime = str(ssh_obj['uptime'])
regs = c.read_holding_registers(1, 2)
if (regs != None and regs[0] == 0):
result = check_match(uptime, regs[1])
logger("System uptime", uptime, regs[1], result)
elif (regs != None and regs[0] != 0):
#Check results
temp = regs[0] * 65536 + regs[1]
result = check_match(uptime, temp)
logger("System uptime", uptime, temp, result)
else:
error_logger("System hostname", "Could not read register value")
except Exception as error:
error_logger("system uptime", error)
def read_mobile_strength(c, ssh):
try:
regs = c.read_holding_registers(3, 2)
if regs != None:
bits = decimal_To_Binary(regs[1])
a = str(binary_To_Complement(bits)).strip()
#Check results
gsmctl = str(exec_ssh("gsmctl -q", ssh)).strip()
result = check_match(gsmctl, a)
logger("Mobile signal strength", gsmctl, a, result)
else:
error_logger("Mobile signal strength", "Could not read register value")
except Exception as error:
error_logger("Mobile signal strength", error)
def read_system_hostname(c, ssh):
try:
regs = c.read_holding_registers(7, 16)
if regs != None:
a = str(dec_to_text(regs)).strip()
#Check results
name = str(exec_ssh("uci get system.system.hostname", ssh)).strip()
result = check_match(name, a)
logger("System hostname", name, a, result)
else:
error_logger("System hostname", "Could not read register value")
except Exception as error:
error_logger("System hostname", error)
主要内容:
如果我像这样运行代码,我将无法获得系统正常运行时间的价值
if modbus != False:
read_system_hostname(modbus, ssh)
read_mobile_strength(modbus, ssh)
read_system_uptime(modbus, ssh)
现在我不会得到移动强度值,这发生在每一秒的功能上,我可以移动这些功能,比如移动到第一个位置,它就会工作,所以这不是功能故障,我猜是pyModbusTCP,或者甚至是我正在使用的设备故障,但我不确定。有人知道怎么处理吗?因为我以前从未在这个图书馆工作过。我必须清除我读到的数据或类似的东西吗?目前,我有一个愚蠢的解决方案,就是放置一个“假”函数来读取寄存器,不做任何处理,然后将它放在其他函数之间,所以重要的函数可以工作,但如果可能的话,我希望避免这样做
编辑:
Modbus启动功能:
def modbus_start(ip, port, device):
try:
client = ModbusClient(host=ip, port=port, unit_id=device, auto_open=True)
print("Modbus client start: Success")
logging.info("Started Modbus client")
return client
except Exception as error:
print("ERROR: Failed to start Modbus client: ")
print(error)
logging.error("Failed to start Modbus client: %s", error)
return False
您遗漏了两件重要的事情:
modbus_start
功能,我认为这可能非常重要,以及您连接的设备类型。因此,这只是一个猜测:当您反复打开和关闭连接时,一些(或许多)设备不喜欢,您是否将自动打开
和自动关闭
设置为真实
?编辑了帖子,很抱歉,谢谢。我会尝试将auto_open
和auto_close
设置为False
,以检查您是否看到行为的变化。如果这不好,可能将两者都设置为True
。现在看来,将两者都设置为True可以修复它,但我检查了很多寄存器,因此很难判断它是否缺少任何内容,我将不得不更详细地检查,以达到100%。知道为什么会发生这种情况吗?因为每第二个功能都停止工作真的很奇怪。我不能确定,但看起来你的设备正在单方面关闭连接。也许你应该看看它的说明书,看看是怎么回事
def modbus_start(ip, port, device):
try:
client = ModbusClient(host=ip, port=port, unit_id=device, auto_open=True)
print("Modbus client start: Success")
logging.info("Started Modbus client")
return client
except Exception as error:
print("ERROR: Failed to start Modbus client: ")
print(error)
logging.error("Failed to start Modbus client: %s", error)
return False