首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Modbus、python和最小modbus

Modbus、python和最小modbus
EN

Stack Overflow用户
提问于 2016-11-03 10:09:02
回答 1查看 2.3K关注 0票数 0

我正在使用python库最小Modbus读取一个电能表(RS-485)。

我试图使用这个函数同时读取多个寄存器

Read_registers(寄存器地址,numberOfRegisters,functioncode=3)

下面是python库的代码:

代码语言:javascript
复制
def read_registers(self, registeraddress, numberOfRegisters, functioncode=3):
        """Read integers from 16-bit registers in the slave.

        The slave registers can hold integer values in the range 0 to 65535 ("Unsigned INT16").

        Args:
            * registeraddress (int): The slave register start address (use decimal numbers, not hex).
            * numberOfRegisters (int): The number of registers to read.
            * functioncode (int): Modbus function code. Can be 3 or 4.

        Any scaling of the register data, or converting it to negative number (two's complement)
        must be done manually.

        Returns:
            The register data (a list of int).

        Raises:
            ValueError, TypeError, IOError

        """
        _checkFunctioncode(functioncode, [3, 4])
        _checkInt(numberOfRegisters, minvalue=1, description='number of registers')
        return self._genericCommand(functioncode, registeraddress, \
            numberOfRegisters=numberOfRegisters, payloadformat='registers')

我遇到的问题是,寄存器将数据保存为Long,但是这个函数将数据作为int列表返回,并查看值,这似乎不正确。

这是我目前的剧本:

代码语言:javascript
复制
##!/usr/bin/env python

import minimalmodbus
import time
import glob
import sys
import MySQLdb

instrument = minimalmodbus.Instrument('/dev/ttyUSB1', 7)

#Debug Options

#instrument.debug = True #debug modbus

read = False

def convert(value):
        data=chr(value)
        return data

def read_registers_ime():
        data = instrument.read_registers(4096,20)
        return data

while True:
        try:
                while read == False:
                        data = read_registers_ime()
                        print data
                        time.sleep(0.11)
                        read = True

                break

        except KeyboardInterrupt:
                print "Stopped"
                sys.exit()

        except TypeError:
                print "TypeError"

        except IOError:
                print "IOError"

目前,它返回以下内容:

3,39192,3,44592,3,44592,0,423,0,0,0,0,0,0,6,19884,6,24584,6,19884

到目前为止,我尝试将数据转换回原始格式的所有尝试都失败了。我真的很想在这里提供一些帮助。

非常感谢,

布莱恩

EN

回答 1

Stack Overflow用户

发布于 2016-11-03 12:21:38

我已经遇到了这样的问题,使用modbus而不是最小modbus来处理modbus。我解决它的方法是:

  • 按对获取寄存器
  • 使用结构将对转换为int

参见下面的示例(Python2.7):

代码语言:javascript
复制
import struct

registers = [3, 39192, 3, 44592, 3, 44592, 0, 423, 0, 0, 0, 0, 0, 0, 6, 19884, 6, 24584, 6, 19884]

def grouped(iterable, group_size):
    """iterates on iterable, yielding groups of group_size elements"""
    it = iter(iterable)
    while True:
        group = [next(it) for _ in range(group_size)]
        yield group

for group in grouped(registers, 2):
    bytes = b""
    for word in group:
        word_bytes = struct.pack("H", word)
        bytes += word_bytes
    bytes_hex = "0x" + "".join(["{:>02X}".format(ord(byte_)) for byte_ in bytes])
    print("{} -> {} -> {}".format(group, bytes_hex, struct.unpack("<i", bytes)[0]))

指纹:

代码语言:javascript
复制
[3, 39192] -> 0x03001899 -> -1726480381
[3, 44592] -> 0x030030AE -> -1372585981
[3, 44592] -> 0x030030AE -> -1372585981
[0, 423] -> 0x0000A701 -> 27721728
[0, 0] -> 0x00000000 -> 0
[0, 0] -> 0x00000000 -> 0
[0, 0] -> 0x00000000 -> 0
[6, 19884] -> 0x0600AC4D -> 1303117830
[6, 24584] -> 0x06000860 -> 1611137030
[6, 19884] -> 0x0600AC4D -> 1303117830

此示例可能需要根据您的确切需求进行一些调整,但可能会有所帮助。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40398773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档