1

我正在尝试使用pymodbus Callback Sever将 RS252 Ascii 字符串数据从传感器转换为 Modbus TCP 输入/保持寄存器,当请求客户端记录器时,服务器是主报告数据,我不确定我需要做什么才能得到这工作。我目前能够读取数据并将其记录到 csv 文件中

#!/usr/bin/env python
# Log data from serial port


import argparse
import serial
import datetime
import time
import os


parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("-d", "--device", help="device to read from", default="/dev/ttyUSB0")
parser.add_argument("-s", "--speed", help="speed in bps", default=9600, type=int)
args = parser.parse_args()

outputFilePath = os.path.join(os.path.dirname(__file__),
                 datetime.datetime.now().strftime("%Y-%m-%d") + ".csv")

with serial.Serial(args.device, args.speed) as ser, open(outputFilePath,'w') as outputFile:
    print("Logging started. Ctrl-C to stop.") 
    try:
        while True:
            time.sleep(0.2) 
            x = (ser.read(ser.inWaiting())) 
            data = x.decode('UTF-8')
            if data !="":
                outputFile.write(time.strftime("%Y/%m/%d %H:%M ") + " " + data  )
                outputFile.flush()

    except KeyboardInterrupt:
        print("Logging stopped")

来自传感器的字符串从设备中出来:

 0.00 0.0 0.0 346.70 25.14

我需要将每个部分都作为自己的 Modbus 寄存器,并且我正在尝试在 Raspberry Pi Zero 上使用 pymodbus。传感器每秒更新 4 次,我能够将数据分成几部分,但我还没有做到这一点,因为我不确定我需要在回调脚本中做什么,我还不太精通 Python我还在学习中。我确实了解 Modbus TCP,并且之前在 Arduino 系统上使用过它。任何帮助,将不胜感激。

4

1 回答 1

2

您需要的是更新服务器,您可以使用它来填充寄存器。您将必须专注于功能def updating_writer并进行串行读取,处理它们并写入您选择的寄存器。这个例子很难在第一次阅读和理解。我已经修改了示例以满足您的需求。但是这里有一些有助于理解代码的关键概念。

ModbusSlaveContext

二进制有效负载生成器

另请注意,该示例使用基于 twisted 的异步服务器,如果您不熟悉 twisted 或有一些限制不允许您在目标上使用 twisted,您也可以使用简单的线程来实现相同的目的。设计大概是这样的。

  1. 在单独的线程中启动更新功能
  2. 最后启动你的 TCP 服务器(阻塞)
# Complete gist here --> https://gist.github.com/dhoomakethu/540b15781c62de6d1f7c318c3fc8ae22
def updating_writer(context, device, baudrate):
    """ 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")
    log.debug("device - {}, baudrate-{}".format(device, baudrate))
    data = serial_reader(device, baudrate)  # Implement your serial reads, return list of floats.
    if data:
        # We can not directly write float values to registers, Use BinaryPayloadBuilder to convert float to IEEE-754 hex integer
        for d in data:
            builder.add_32bit_float(d)
        registers = builder.to_registers()
        context = context
        register = 3  # Modbus function code (3) read holding registers. Just to uniquely identify what we are reading from /writing in to.
        slave_id = 0x01 # Device Unit address , refer ModbusSlaveContext below
        address = 0x00 # starting offset of register to write (0 --> 40001)
        log.debug("new values: " + str(registers))
        context[slave_id].setValues(register, address, registers)

一旦服务器运行并且值正在更新,您可以使用客户端读取值并将其解析回浮动。

from pymodbus.client.sync import ModbusTcpClient as Client
from pymodbus.payload import BinaryPayloadDecoder, Endian
client = Client(<IP-ADDRESS>, port=5020)

# Each 32 bit float is stored in 2 words, so we will read 10 registers 
raw_values = client.read_holding_registers(0, 10, unit=1)
if not registers.isError():
    registers = raw_values.registers
    decoder = BinaryPayloadDecoder.fromRegisters(registers, 
    wordorder=Endian.Big, byteorder=Endian.Big)

    for _ in range(5):
        print(decoder.decode_32bit_float())

于 2019-03-12T08:56:11.830 回答