我的公司已經購買了幾個具有modbus功能的房間溫控器。那些來自中國。當我在Modbus RTU上與他們交流時,會發生有趣的事情。設備有時不會迴應我。然後在接下來的民意調查中,他們有時會迴應但最後,在一段時間之後,不管一小時還是兩天,他們完全停止響應,並且我從modbus master獲得一些錯誤。我使用串口分析器來查看發生了什麼。我做了所有的硬件檢查點(端接電阻等)。我嘗試了許多modbus大師。但即使只連接了一個恆溫器,也會發生問題。具體到這個問題,我用Python開發了自己的modbus master。最好的部分是,我使用最小的modbus庫,每次調查後我都有機會關閉端口。但是,即使我的modbus主程序繼續輪詢,恆溫器也會完全停止響應。不知何故,我必須通過編程來解決這個問題。我不能總是重新啓動恆溫器來解決通信問題。Modbus設備無故停止通訊
這裏是我的代碼:
import os
import sys
import sqlite3
import time
import datetime
import minimalmodbus
minimalmodbus.CLOSE_PORT_AFTER_EACH_CALL=True
PORT_NAME = '/com5'
SLAVE1_ADDRESS = 1
SLAVE2_ADDRESS = 2
SLAVE3_ADDRESS = 3
BAUDRATE = 9600 # baud (pretty much bits/s). Use 2400 or 38400 bits/s.
TIMEOUT = 0.4 # seconds. At least 0.2 seconds required for 2400 bits/s.
MODE = minimalmodbus.MODE_RTU
sqlite_file = 'ModbusTable.db'
instrument1 = minimalmodbus.Instrument(PORT_NAME, SLAVE1_ADDRESS, MODE)
instrument1.serial.baudrate = BAUDRATE
instrument1.serial.timeout = TIMEOUT
instrument1.debug = False
instrument1.precalculate_read_size = True
instrument2 = minimalmodbus.Instrument(PORT_NAME, SLAVE2_ADDRESS, MODE)
instrument2.serial.baudrate = BAUDRATE
instrument2.serial.timeout = TIMEOUT
instrument2.debug = False
instrument2.precalculate_read_size = True
instrument3 = minimalmodbus.Instrument(PORT_NAME, SLAVE3_ADDRESS, MODE)
instrument3.serial.baudrate = BAUDRATE
instrument3.serial.timeout = TIMEOUT
instrument3.debug = False
instrument3.precalculate_read_size = True
print ('Okuyor')
while 1:
# Connecting to the database file
conn = sqlite3.connect(sqlite_file)
c = conn.cursor()
try:
values1 = instrument1.read_registers(40005, 2)
except IOError:
print ('Timestamp: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now())+"Failed to read from instrument1")
# C) Updates the pre-existing entry
c.execute("""UPDATE ModbusData SET SetPoint = ? ,ActualTemp = ? WHERE ID= ? """,
(values1[0],values1[1],1))
time.sleep(0.5)
try:
values2 = instrument2.read_registers(40005, 2)
except IOError:
print ('Timestamp: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now())+"Failed to read from instrument2")
# C) Updates the pre-existing entry
c.execute("""UPDATE ModbusData SET SetPoint = ? ,ActualTemp = ? WHERE ID= ? """,
(values2[0],values2[1],2))
time.sleep(0.5)
try:
values3 = instrument3.read_registers(40005, 2)
except IOError:
print ('Timestamp: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now())+"Failed to read from instrument3")
# C) Updates the pre-existing entry
c.execute("""UPDATE ModbusData SET SetPoint = ? ,ActualTemp = ? WHERE ID= ? """,
(values3[0],values3[1],3))
time.sleep(0.5)
# Committing changes and closing the connection to the database file
conn.commit()
conn.close()
這裏是恆溫如何執行:
因此,我將感激這麼多,如果你拿出你的想法。
你說:「我用串口分析器看看發生了什麼事情」。您能否提供分析儀獲得的結果的副本? –