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

问题描述 投票:0回答:1

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

我们想通过使用温度控制器(RKC Instruments的CB100 / CB400 / CB500 / CB700 / CB900系列:https://www.rkcinst.co.jp/english/download-center/?dc_cat=15#)来使我们的温度相关探针台自动化。控制器通过RS485转USB适配器连接到运行Windows 10 Pro(版本1903,内部版本18362.267)的主Dell OptiPlex5060小塔架(https://www.dell.com/en-us/work/shop/desktops-all-in-one-pcs/optiplex-5060-small-form-factor/spd/optiplex-5060-desktop/cto01o5060sffus)。

该设备将显示在设备管理器('COM3'端口)中,并希望已安装正确的驱动程序(请参见this screenshot)。另外,控制器和主计算机之间的设备设置也匹配以下值(port settings in device manager):

  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正常工作的最低工作代码是什么?为什么似乎很难找到快速的在线资源?

非常感谢,非常感谢您的帮助!

python serial-port modbus rs485 pymodbus
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.