4

我尝试编写一个与数据库连接的 ModbusTCP 服务器软件。我使用 pyModbus 库来完成该任务。该库有一个更新服务器示例。通过使用该代码,我将代码编写为:

'''
Pymodbus Server With Updating Thread
--------------------------------------------------------------------------
This is an example of having a background thread updating the
context while the server is operating. This can also be done with
a python thread::
    from threading import Thread
    thread = Thread(target=updating_writer, args=(context,))
    thread.start()
'''
#---------------------------------------------------------------------------# 
# import the modbus libraries we need
#---------------------------------------------------------------------------# 
from pymodbus.server.async import StartTcpServer
from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
from pymodbus.transaction import ModbusRtuFramer, ModbusAsciiFramer

#---------------------------------------------------------------------------# 
# import the twisted libraries we need
#---------------------------------------------------------------------------# 
from twisted.internet.task import LoopingCall

#---------------------------------------------------------------------------# 
# configure the service logging
#---------------------------------------------------------------------------# 
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)

#---------------------------------------------------------------------------# 
# for database reading
#---------------------------------------------------------------------------# 

import sqlite3
import sys

sqlite_file = 'ModbusTable.db'

thermostats = 25
registers_per_thermostat = 3

global total_registers
total_registers = thermostats * registers_per_thermostat

#---------------------------------------------------------------------------# 
# define your callback process
#---------------------------------------------------------------------------# 
def updating_writer(a):
    ''' A worker process that runs every so often and
    updates live values of the context. It should be noted
    that there is a race condition for the update.
    :param arguments: The input arguments to the call
    '''
    log.debug("updating the context")
    context  = a[0]
    functioncode = 3 
    slave_id = 0x01 # slave address
    address  = 0x10 # start register : 400017

    values = []

    # Connecting to the database file
    conn = sqlite3.connect(sqlite_file)
    c1 = conn.cursor()

    c1.execute("""SELECT ID, SetPoint, ActualTemp FROM ModbusData""")

    registers = c1.fetchall()

    c1.close()

    for register in registers:
        for value in register:
            values.append(value)

    log.debug("values from database: " + str(values))

    context[slave_id].setValues(functioncode, address, values)

    values = context[slave_id].getValues(functioncode, address, count=total_registers)

    log.debug("values to be written to database: " + str(values))

    c2 = conn.cursor()

    for index in range(len(values)):
        if (index+2)<len(values):
            column1 = values[index]
            column2 = values[index+1]
            column3 = values[index+2]
            c2.execute("""UPDATE ModbusData set SetPoint = ?, ActualTemp = ? where ID=?""",[column2, column3, column1])

    # Committing changes and closing the connection to the database file
    c2.close()
    conn.close()



#---------------------------------------------------------------------------# 
# initialize your data store
#---------------------------------------------------------------------------# 
store = ModbusSlaveContext(
    di = ModbusSequentialDataBlock(0, [17]*total_registers),
    co = ModbusSequentialDataBlock(0, [17]*total_registers),
    hr = ModbusSequentialDataBlock(0, [17]*total_registers),
    ir = ModbusSequentialDataBlock(0, [17]*total_registers))
context = ModbusServerContext(slaves=store, single=True)

#---------------------------------------------------------------------------# 
# initialize the server information
#---------------------------------------------------------------------------# 
identity = ModbusDeviceIdentification()
identity.VendorName  = 'pymodbus'
identity.ProductCode = 'PM'
identity.VendorUrl   = 'http://github.com/bashwork/pymodbus/'
identity.ProductName = 'pymodbus Server'
identity.ModelName   = 'pymodbus Server'
identity.MajorMinorRevision = '1.0'

#---------------------------------------------------------------------------# 
# run the server you want
#---------------------------------------------------------------------------# 
time = 5 # 5 seconds delay
loop = LoopingCall(f=updating_writer, a=(context,))
loop.start(time, now=False) # initially delay by time
StartTcpServer(context, identity=identity, address=("", 5007))

基本上,我试图实现的是服务器上下文与数据库同步。我尝试将值从数据库读取到上下文,然后最后将上下文值更新到数据库。我成功地将数据从数据库获取到上下文。因为当我使用位于另一台局域网计算机上的 modbus 客户端请求值时,我成功地在那里获得了请求的数据。但是,我无法通过 TCP 服务器写入数据库。在另一台局域网计算机上使用相同的 modbus 客户端,我发送请求以写入服务器上下文中的一些寄存器,然后将其写入数据库。在这一点上我失败了。我无法将服务器上下文的最新更新情况写入数据库。我想,我可能会从 pymodbus 文档中的“数据库数据存储示例”中受益,但我不知道该怎么做。所以我需要你对这个任务的想法。

4

1 回答 1

2

您没有为光标 C2 调用“commit()”。您只是关闭连接。因此,您的更改不会写入数据库。sqlite 文档清楚地提到了这一点。见这里。在关闭连接之前尝试执行 c2.commit() 并查看数据是否正在写入。

如果这不起作用,请分享您遇到的错误。这将有助于理解问题。

希望有帮助!

于 2018-05-30T09:58:15.993 回答