我是树莓派和PLC领域的新手,就像我知道的非常非常少。然而,我正试图在树莓派和PLC之间进行USB通信。 我使用的PLC是VersaMax(IC200NDR010-FJ),它只有一个ej45端口,所以我使用USB到ej45转换器进行通信。下面是我在网上找到的代码,看起来非常简单。然而,当我试图运行该程序时,我得到 "ValueError。不是一个有效的奇偶校验:'0'"。这是我的第1次尝试。
pi@raspberrypi:~ $ ipython
SudoPython 2.7.16 (default, Apr 6 2019, 01:42:57)
Type "copyright", "credits" or "license" for more information.
IPython 5.9.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: from pymodbus.client.sync import ModbusSerialClient
In [2]: client = ModbusSerialClient(method='rtu' ,port=' /dev/ttyUSB0' ,baudrate
...: =9600, parity='0')
In [3]: client.read_coils(2048,1,unit=1).bits
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-3-ecf7c3f6dc42> in <module>()
----> 1 client.read_coils(2048,1,unit=1).bits
/home/pi/.local/lib/python2.7/site-packages/pymodbus/client/common.pyc in read_coils(self, address, count, **kwargs)
46 '''
47 request = ReadCoilsRequest(address, count, **kwargs)
---> 48 return self.execute(request)
50 def read_discrete_inputs(self, address, count=1, **kwargs):
/home/pi/.local/lib/python2.7/site-packages/pymodbus/client/sync.pyc in execute(self, request)
104 :returns: The result of the request execution
105 """
--> 106 if not self.connect():
107 raise ConnectionException("Failed to connect[%s]" % (self.__str__()))
108 return self.transaction.execute(request)
/home/pi/.local/lib/python2.7/site-packages/pymodbus/client/sync.pyc in connect(self)
595 stopbits=self.stopbits,
596 baudrate=self.baudrate,
--> 597 parity=self.parity)
598 if self.method == "rtu":
599 if self._strict:
/usr/lib/python2.7/dist-packages/serial/serialutil.pyc in __init__(self, port, baudrate, bytesize, parity, stopbits, timeout, xonxoff, rtscts, write_timeout, dsrdtr, inter_byte_timeout, exclusive, **kwargs)
219 self.baudrate = baudrate
220 self.bytesize = bytesize
--> 221 self.parity = parity
222 self.stopbits = stopbits
223 self.timeout = timeout
/usr/lib/python2.7/dist-packages/serial/serialutil.pyc in parity(self, parity)
330 """Change parity setting."""
331 if parity not in self.PARITIES:
--> 332 raise ValueError("Not a valid parity: {!r}".format(parity))
333 self._parity = parity
334 if self.is_open:
ValueError: Not a valid parity: '0'
In [4]:
我试着一次一次地提交命令,希望能解决无效的奇偶性问题,但我最后收到了一个不同的错误:"TypeError: unbound method read_coils() must be called with ModbusSerialClient instance as first argument (got int instance instead)"。
pi@raspberrypi:~ $ ipython
Python 2.7.16 (default, Apr 6 2019, 01:42:57)
Type "copyright", "credits" or "license" for more information.
IPython 5.9.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: from pymodbus.client.sync import ModbusSerialClient
In [2]: client = ModbusSerialClient
In [3]: method='rtu'
In [4]: port=' /dev/ttyUSB0'
In [5]: baudrate=9600
In [6]: parity='0'
In [7]: client.read_coils(2048,1,unit=1).bits
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-ecf7c3f6dc42> in <module>()
----> 1 client.read_coils(2048,1,unit=1).bits
TypeError: unbound method read_coils() must be called with ModbusSerialClient instance as first argument (got int instance instead)