我有一个问题,对于习惯使用串行接口的人来说似乎很容易,但对我来说这是第一次。
我们想通过使用温度控制器(RKC Instruments 的 CB100 / CB400 / CB500 / CB700 / CB900 系列:https ://www.rkcinst.co.jp/english/download-center/?dc_cat =15#)。控制器连接到主戴尔 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”端口)中,希望安装了正确的驱动程序(参见此屏幕截图)。此外,控制器和主计算机之间的设备设置与以下值匹配(设备管理器中的端口设置):
- 波特率 = 9600;
- 位大小 = 8;
- 奇偶校验 = 无;
- 停止位 = 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 工作的最小工作代码是什么?为什么似乎很难找到快速的在线资源?
非常感谢,任何帮助都非常感谢!