繁体   English   中英

使用 RS485 和 Modbus(Python 或 LabView)从温度 Controller 读取和写入数据

[英]Reading and writing data from Temperature Controller using RS485 and Modbus (Python or LabView)

我有一个问题,对于习惯使用串行接口的人来说似乎很容易,但对我来说这是第一次。

我们想通过使用温度 controller(RKC Instruments 的 CB100 / CB400 / CB500 / CB700 / CB900 系列: https://www.rkcinst/?dc_cat5Z://www.rkcinst/?dc_cat5Z =15# )。 controller 连接到主戴尔 OptiPlex5060 小型塔式机( https://www.dell.com/en-us/work/shop/desktops-all-in-one-pcs/optiplex-5060-small-form-factor/ spd/optiplex-5060-desktop/cto01o5060sffus ) 通过 RS485 到 USB 适配器运行 Windows 10 Pro(版本 1903,构建 18362.267)。

该设备出现在设备管理器(“COM3”端口)中,希望安装了正确的驱动程序(参见此屏幕截图)。 此外,controller 和主计算机之间的设备设置与以下值匹配(设备管理器中的端口设置):

  1. 波特率 = 9600;
  2. 位大小 = 8;
  3. 奇偶校验 = 无;
  4. 停止位 = 1

我想我通过使用带有以下代码的 pymodbus 库正确连接了设备:

连接到 RS485 设备 - pymodbus

#Import useful modules
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
#For the first test, I manually read the configuration from the 
#the controller display and hard-code the values in the script.

d_port='COM3'                    #Device address

commspeed={}                #Communication speed
commspeed_table={
    '0':2400,
    '1':4800,
    '2':9600,
    '3':19200,
        }
commspeed['flag']='2'
commspeed['value']=commspeed_table[commspeed['flag']]

bitconf={}                  #Bit configuration
bitconf_table={
    '0':(8,'N',1),
    '1':(8,'N',2),
    '2':(7,'E',1),
    '3':(7,'E',2),
    '4':(7,'O',1),
    '5':(7,'O',2),
        }
bitconf['flag']='0'
bitconf['size'],bitconf['parity'],bitconf['stopbit']=\
bitconf_table[bitconf['flag']]

intime={}
intime['flag']='5'
intime['value']=round(int(intime['flag'])*1.666,0)

def main():
    modbus=ModbusClient(method='ascii',port='COM3',\
                        baudrate=commspeed['value'],stopbits=bitconf['stopbit'],\
                        bytesize=bitconf['size'],parity=bitconf['parity'],\
                        timeout=1)
    modbus.connect()

if __name__ == "__main__":
    main()

但是,当我尝试使用此代码读取它时(设备的地址是 001Hex):

r = modbus.read_holding_registers(0x0001,1)
print(r)

我收到此错误:

Modbus 错误:[输入/输出] Modbus 错误:[无效消息] 收到不完整的消息,预计至少 5 个字节(已收到 1 个)。

谁能帮忙提出错误在哪里? 使 modbus 工作的最小工作代码是什么? 为什么似乎很难找到快速的在线资源?

非常感谢,任何帮助都非常感谢!

在这种情况下,拆分问题可能很有用。 尝试使用外部软件测试仪,例如https://www.schneider-electric.us/en/faqs/FA180037/ 当你的奴隶正确回复时,你将有一个可靠的参考来调试你的代码。祝你好运。问候

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM