0

我有一个 python 程序,它读取串行端口,然后充当 ModbusTCP 服务器更新 Modbus 寄存器。我在使用 Modbus 客户端读取 Modbus 寄存器时遇到问题,如果我设置时间 = 5 # 更新延迟它甚至不显示值,如果我将其设置为 10 它会更新值并且我可以使用 modbus 客户端读取它们,但是值更新太慢了。

我明白这个问题。从串口读取应该是单独的功能,它应该等待串口接收到值,然后将这些值添加到 modbus 服务器寄存器。在串行端口等待值时,modbus 客户端应该能够读取已添加到 modbus 服务器寄存器的值。

LoopingCall 调用 updates_writer()时,它执行ser_read_JB1 = ser_JB1.read(386)并且我认为代码只是停在那里等待接收串行数据并且在它等待时# modbus 部分没有执行,所以 modbus 客户端正在请求数据,但是 modbus服务器无法响应。

代码:

import serial
from pymodbus.server.async import StartTcpServer
from pymodbus.server.async import StartUdpServer
from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
from pymodbus.transaction import ModbusRtuFramer, ModbusAsciiFramer
from twisted.internet.task import LoopingCall
import logging

# debug 
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)

# serial port settings
ser = serial.Serial('COM3', 9600, parity=serial.PARITY_EVEN)

def updating_writer(a):

# serial read part
# JB1 Data
    # read the whole package
    ser_read_JB1 = ser_JB1.read(386)
    # convert ascii to hex
    aTOh_JB1 = "".join("{:02x}".format(c) for c in ser_read_JB1)
    # convert to string
    iTOs_JB1 = str(aTOh_JB1)
    # bl_xx_JB1 location of the first byte to read
    # from string 2 char = 1 byte
        ### Momentānā jauda ###
    bl_MJ_JB1 = 258
    b_konv_MJ_JB1 = (iTOs_JB1[bl_MJ_JB1]+iTOs_JB1[bl_MJ_JB1+1]+
                iTOs_JB1[bl_MJ_JB1+2]+iTOs_JB1[bl_MJ_JB1+3])
    MJ_JB1 = int(b_konv_MJ_JB1, 16)
        ### Motorstundas 1. un 2. baits ###
    bl_MS_JB1 = 422
    b_konv_MS_JB1 = (iTOs_JB1[bl_MS_JB1]+iTOs_JB1[bl_MS_JB1+1]+
                    iTOs_JB1[bl_MS_JB1+2]+iTOs_JB1[bl_MS_JB1+3])
    MS_1_JB1 = int(b_konv_MS_JB1, 16)
        ### Motorstundas 3. un 4. baits ###
    b_konv_MS2_JB1 = (iTOs_JB1[bl_MS_JB1+4]+iTOs_JB1[bl_MS_JB1+5]+
                    iTOs_JB1[bl_MS_JB1+6]+iTOs_JB1[bl_MS_JB1+7])
    MS_2_JB1 = int(b_konv_MS2_JB1, 16)
        ### Kopējā reaktīvā jauda ###
    bl_KRJ_JB1 = 414
    b_konv_KRJ_JB1 = (iTOs_JB1[bl_KRJ_JB1]+iTOs_JB1[bl_KRJ_JB1+1]+
                iTOs_JB1[bl_KRJ_JB1+2]+iTOs_JB1[bl_KRJ_JB1+3])
    KRJ_JB1 = int(b_konv_KRJ_JB1, 16)
        ### Kopējā aktiva jauda 1. 2. baits ###
    bl_KAJ_1_JB1 = 402
    b_konv_KAJ_1_JB1 = (iTOs_JB1[bl_KAJ_1_JB1]+iTOs_JB1[bl_KAJ_1_JB1+1]+
                iTOs_JB1[bl_KAJ_1_JB1+2]+iTOs_JB1[bl_KAJ_1_JB1+3])
    KAJ_1_JB1 = int(b_konv_KAJ_1_JB1, 16)
        ### Kop aktiva jauda 3. 4. baits ##
    b_konv_KAJ_2_JB1 = (iTOs_JB1[bl_KAJ_1_JB1+4]+iTOs_JB1[bl_KAJ_1_JB1+5]+
                iTOs_JB1[bl_KAJ_1_JB1+6]+iTOs_JB1[bl_KAJ_1_JB1+7])
    KAJ_2_JB1 = int(b_konv_KAJ_2_JB1, 16)
        ### P Jacket Water (bar) ###
    bl_PJW_JB1 = 266
    b_konv_PJW_JB1 = (iTOs_JB1[bl_PJW_JB1]+iTOs_JB1[bl_PJW_JB1+1]+
                iTOs_JB1[bl_PJW_JB1+2]+iTOs_JB1[bl_PJW_JB1+3])
    PJW_JB1 = int(b_konv_PJW_JB1, 16)
        ### P oil  ###
    bl_POIL_JB1 = 274
    b_konv_POIL_JB1 = (iTOs_JB1[bl_POIL_JB1]+iTOs_JB1[bl_POIL_JB1+1]+
                iTOs_JB1[bl_POIL_JB1+2]+iTOs_JB1[bl_POIL_JB1+3])
    POIL_JB1 = int(b_konv_POIL_JB1, 16)
        ### T Oil C2 (temp) ###
    bl_TOIL_JB1 = 270
    b_konv_TOIL_JB1 = (iTOs_JB1[bl_TOIL_JB1]+iTOs_JB1[bl_TOIL_JB1+1]+
                iTOs_JB1[bl_TOIL_JB1+2]+iTOs_JB1[bl_TOIL_JB1+3])
    TOIL_JB1 = int(b_konv_TOIL_JB1, 16)
        ### Turbo bypass CTR1 2. v stav ###
    bl_TBP_JB1 = 558
    b_konv_TBP_JB1 = (iTOs_JB1[bl_TBP_JB1]+iTOs_JB1[bl_TBP_JB1+1]+
                iTOs_JB1[bl_TBP_JB1+2]+iTOs_JB1[bl_TBP_JB1+3])
    TBP_JB1 = int(b_konv_TBP_JB1, 16)
        ### Ainara A L1 ###
    bl_AAL1_JB1 = 530
    b_konv_AAL1_JB1 = (iTOs_JB1[bl_AAL1_JB1]+iTOs_JB1[bl_AAL1_JB1+1]+
                iTOs_JB1[bl_AAL1_JB1+2]+iTOs_JB1[bl_AAL1_JB1+3])
    AAL1_JB1 = int(b_konv_AAL1_JB1, 16)
        ### Ainara A L2 ###
    bl_AAL2_JB1 = 534
    b_konv_AAL2_JB1 = (iTOs_JB1[bl_AAL2_JB1]+iTOs_JB1[bl_AAL2_JB1+1]+
                iTOs_JB1[bl_AAL2_JB1+2]+iTOs_JB1[bl_AAL2_JB1+3])
    AAL2_JB1 = int(b_konv_AAL2_JB1, 16)
        ### Ainara A L3 ###
    bl_AAL3_JB1 = 538
    b_konv_AAL3_JB1 = (iTOs_JB1[bl_AAL3_JB1]+iTOs_JB1[bl_AAL3_JB1+1]+
                iTOs_JB1[bl_AAL3_JB1+2]+iTOs_JB1[bl_AAL3_JB1+3])
    AAL3_JB1 = int(b_konv_AAL3_JB1, 16)
        ### 1. Cilindra temp. ###
    bl_CIL1_JB1 = 322
    b_konv_CIL1_JB1 = (iTOs_JB1[bl_CIL1_JB1]+iTOs_JB1[bl_CIL1_JB1+1]+
                iTOs_JB1[bl_CIL1_JB1+2]+iTOs_JB1[bl_CIL1_JB1+3])
    CIL1_JB1 = int(b_konv_CIL1_JB1, 16)
        ### 2. Cilindra temp. ###
    bl_CIL2_JB1 = 326
    b_konv_CIL2_JB1 = (iTOs_JB1[bl_CIL2_JB1]+iTOs_JB1[bl_CIL2_JB1+1]+
                iTOs_JB1[bl_CIL2_JB1+2]+iTOs_JB1[bl_CIL2_JB1+3])
    CIL2_JB1 = int(b_konv_CIL2_JB1, 16)
        ### 3. Cilindra temp. ###
    bl_CIL3_JB1 = 330
    b_konv_CIL3_JB1 = (iTOs_JB1[bl_CIL3_JB1]+iTOs_JB1[bl_CIL3_JB1+1]+
                iTOs_JB1[bl_CIL1_JB3+2]+iTOs_JB1[bl_CIL3_JB1+3])
    CIL3_JB1 = int(b_konv_CIL3_JB1, 16)
        ### 4. Cilindra temp. ###
    bl_CIL4_JB1 = 334
    b_konv_CIL4_JB1 = (iTOs_JB1[bl_CIL4_JB1]+iTOs_JB1[bl_CIL4_JB1+1]+
                iTOs_JB1[bl_CIL1_JB4+2]+iTOs_JB1[bl_CIL4_JB1+3])
    CIL4_JB1 = int(b_konv_CIL4_JB1, 16)
        ### 5. Cilindra temp. ###
    bl_CIL5_JB1 = 338
    b_konv_CIL5_JB1 = (iTOs_JB1[bl_CIL5_JB1]+iTOs_JB1[bl_CIL5_JB1+1]+
                iTOs_JB1[bl_CIL5_JB1+2]+iTOs_JB1[bl_CIL5_JB1+3])
    CIL5_JB1 = int(b_konv_CIL5_JB1, 16)
        ### 6. Cilindra temp. ###
    bl_CIL6_JB1 = 342
    b_konv_CIL6_JB1 = (iTOs_JB1[bl_CIL6_JB1]+iTOs_JB1[bl_CIL6_JB1+1]+
                iTOs_JB1[bl_CIL6_JB1+2]+iTOs_JB1[bl_CIL6_JB1+3])
    CIL6_JB1 = int(b_konv_CIL6_JB1, 16)
        ### 7. Cilindra temp. ###
    bl_CIL7_JB1 = 346
    b_konv_CIL7_JB1 = (iTOs_JB1[bl_CIL7_JB1]+iTOs_JB1[bl_CIL7_JB1+1]+
                iTOs_JB1[bl_CIL7_JB1+2]+iTOs_JB1[bl_CIL7_JB1+3])
    CIL7_JB1 = int(b_konv_CIL7_JB1, 16)
        ### 8. Cilindra temp. ###
    bl_CIL8_JB1 = 350
    b_konv_CIL8_JB1 = (iTOs_JB1[bl_CIL8_JB1]+iTOs_JB1[bl_CIL8_JB1+1]+
                iTOs_JB1[bl_CIL8_JB1+2]+iTOs_JB1[bl_CIL8_JB1+3])
    CIL8_JB1 = int(b_konv_CIL8_JB1, 16)
        ### 9. Cilindra temp. ###
    bl_CIL9_JB1 = 354
    b_konv_CIL9_JB1 = (iTOs_JB1[bl_CIL9_JB1]+iTOs_JB1[bl_CIL9_JB1+1]+
                iTOs_JB1[bl_CIL9_JB1+2]+iTOs_JB1[bl_CIL9_JB1+3])
    CIL9_JB1 = int(b_konv_CIL9_JB1, 16)
        ### 10. Cilindra temp. ###
    bl_CIL10_JB1 = 358
    b_konv_CIL10_JB1 = (iTOs_JB1[bl_CIL10_JB1]+iTOs_JB1[bl_CIL10_JB1+1]+
                iTOs_JB1[bl_CIL10_JB1+2]+iTOs_JB1[bl_CIL10_JB1+3])
    CIL10_JB1 = int(b_konv_CIL10_JB1, 16)
        ### 11. Cilindra temp. ###
    bl_CIL11_JB1 = 362
    b_konv_CIL11_JB1 = (iTOs_JB1[bl_CIL11_JB1]+iTOs_JB1[bl_CIL11_JB1+1]+
                iTOs_JB1[bl_CIL11_JB1+2]+iTOs_JB1[bl_CIL11_JB1+3])
    CIL11_JB1 = int(b_konv_CIL11_JB1, 16)
        ### 12. Cilindra temp. ###
    bl_CIL12_JB1 = 366
    b_konv_CIL12_JB1 = (iTOs_JB1[bl_CIL12_JB1]+iTOs_JB1[bl_CIL12_JB1+1]+
                iTOs_JB1[bl_CIL12_JB1+2]+iTOs_JB1[bl_CIL12_JB1+3])
    CIL12_JB1 = int(b_konv_CIL12_JB1, 16)
        ### 13. Cilindra temp. ###
    bl_CIL13_JB1 = 370
    b_konv_CIL13_JB1 = (iTOs_JB1[bl_CIL13_JB1]+iTOs_JB1[bl_CIL13_JB1+1]+
                iTOs_JB1[bl_CIL13_JB1+2]+iTOs_JB1[bl_CIL13_JB1+3])
    CIL13_JB1 = int(b_konv_CIL13_JB1, 16)
        ### 14. Cilindra temp. ###
    bl_CIL14_JB1 = 374
    b_konv_CIL14_JB1 = (iTOs_JB1[bl_CIL14_JB1]+iTOs_JB1[bl_CIL14_JB1+1]+
                iTOs_JB1[bl_CIL14_JB1+2]+iTOs_JB1[bl_CIL14_JB1+3])
    CIL14_JB1 = int(b_konv_CIL14_JB1, 16)
        ### 15. Cilindra temp. ###
    bl_CIL15_JB1 = 378
    b_konv_CIL15_JB1 = (iTOs_JB1[bl_CIL15_JB1]+iTOs_JB1[bl_CIL15_JB1+1]+
                iTOs_JB1[bl_CIL15_JB1+2]+iTOs_JB1[bl_CIL15_JB1+3])
    CIL15_JB1 = int(b_konv_CIL15_JB1, 16)
        ### 16. Cilindra temp. ###
    bl_CIL16_JB1 = 382
    b_konv_CIL16_JB1 = (iTOs_JB1[bl_CIL16_JB1]+iTOs_JB1[bl_CIL16_JB1+1]+
                iTOs_JB1[bl_CIL16_JB1+2]+iTOs_JB1[bl_CIL16_JB1+3])
    CIL16_JB1 = int(b_konv_CIL16_JB1, 16)
        ### 17. Cilindra temp. ###
    bl_CIL17_JB1 = 386
    b_konv_CIL17_JB1 = (iTOs_JB1[bl_CIL17_JB1]+iTOs_JB1[bl_CIL17_JB1+1]+
                iTOs_JB1[bl_CIL17_JB1+2]+iTOs_JB1[bl_CIL17_JB1+3])
    CIL17_JB1 = int(b_konv_CIL17_JB1, 16)
        ### 18. Cilindra temp. ###
    bl_CIL18_JB1 = 390
    b_konv_CIL18_JB1 = (iTOs_JB1[bl_CIL18_JB1]+iTOs_JB1[bl_CIL18_JB1+1]+
                iTOs_JB1[bl_CIL18_JB1+2]+iTOs_JB1[bl_CIL18_JB1+3])
    CIL18_JB1 = int(b_konv_CIL18_JB1, 16)
        ### 19. Cilindra temp. ###
    bl_CIL19_JB1 = 394
    b_konv_CIL19_JB1 = (iTOs_JB1[bl_CIL19_JB1]+iTOs_JB1[bl_CIL19_JB1+1]+
                iTOs_JB1[bl_CIL19_JB1+2]+iTOs_JB1[bl_CIL19_JB1+3])
    CIL19_JB1 = int(b_konv_CIL19_JB1, 16)
        ### 20. Cilindra temp. ###
    bl_CIL20_JB1 = 398
    b_konv_CIL20_JB1 = (iTOs_JB1[bl_CIL20_JB1]+iTOs_JB1[bl_CIL20_JB1+1]+
                iTOs_JB1[bl_CIL20_JB1+2]+iTOs_JB1[bl_CIL20_JB1+3])
    CIL20_JB1 = int(b_konv_CIL20_JB1, 16)
        ### C1 (temp) ###
    #bl_C1_JB1 = 262
    #b_konv_C1_JB1 = (iTOs_JB1[bl_C1_JB1]+iTOs_JB1[bl_C1_JB1+1]+
    #            iTOs_JB1[bl_C1_JB1+2]+iTOs_JB1[bl_C1_JB1+3])
    #C1_JB1 = int(b_konv_C1_JB1, 16)


# modbus part
    log.debug("updating the context")
    context  = a[0]
    register = 0x03
    slave_id = 0x01
    address  = 0x01
    values   = context[slave_id].getValues(register, address, count=33)
    values   = [
                MJ_JB1, MS_1_JB1 ,MS_2_JB1, KRJ_JB1, KAJ_1_JB1, KAJ_2_JB1,
                PJW_JB1, POIL_JB1, TOIL_JB1, TBP_JB1, AAL1_JB1, AAL2_JB1,
                AAL3_JB1, CIL1_JB1, CIL2_JB1, CIL3_JB1, CIL4_JB1, CIL5_JB1,
                CIL6_JB1, CIL7_JB1, CIL8_JB1, CIL9_JB1, CIL10_JB1, CIL11_JB1,
                CIL12_JB1, CIL13_JB1, CIL14_JB1, CIL15_JB1, CIL16_JB1, CIL17_JB1,
                CIL18_JB1, CIL19_JB1, CIL20_JB1
                ]
    log.debug("new values: " + str(values))
    context[slave_id].setValues(register, address, values)

def run_updating_server():

    store = ModbusSlaveContext(
        di = ModbusSequentialDataBlock(0, [0]*1),
        co = ModbusSequentialDataBlock(0, [0]*1),
        hr = ModbusSequentialDataBlock(0, [33]*1),
        ir = ModbusSequentialDataBlock(0, [0]*1))
    context = ModbusServerContext(slaves=store, single=True)

    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'

    time = 5 # Update delay
    loop = LoopingCall(f=updating_writer, a=(context,))
    loop.start(time, now=True) 
    StartTcpServer(context, identity=identity, address=("10.10.10.253", 502))

if __name__ == "__main__":
    run_updating_server()
4

0 回答 0