对于习惯于使用串行接口的人来说,我遇到了一个看起来很容易解决的问题,但这对我来说还是第一次。在
我们希望通过使用温度控制器(RKC Instruments生产的CB100/CB400/CB500/CB700/CB900系列)实现温度相关探测站的自动化。控制器通过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):
我想我通过使用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)读取它时:
^{pr2}$我得到这个错误:
Modbus Error: [Input/Output] Modbus Error: [Invalid Message] Incomplete message received, expected at least 5 bytes (1 received).
有人能帮我指出错误在哪里吗?使modbus工作的最小工作代码是什么?为什么很难找到快速的在线资源?在
非常感谢,任何帮助都将不胜感激!在
在这种情况下,将问题分开也许是有用的。尝试使用外部软件测试工具,如https://www.schneider-electric.us/en/faqs/FA180037/。当您的从机正确回答时,您将有一个可靠的参考来调试代码。很好祝你好运,祝你好运在
相关问题 更多 >
编程相关推荐