Question about .py program (minimalmodbus) on raspberry pi

91 Views Asked by At

I ran the .py script below in a window, got the results, and then passed them to the Raspberry Pi as pscp to test the execution.

import minimalmodbus as minimalmodbus
import serial,time
import serial.tools.list_ports as sp

for i in range(len(sp.comports())):
    desc = sp.comports()[i].description
    if desc.find('USB2.0') != -1:
        port = sp.comports()[i].device
        break
    
if __name__ == '__main__':
    # /dev/ttyUSB0
    instrument = minimalmodbus.Instrument(port, 2)
    instrument.serial.baudrate = 19200  # Baud
    instrument.serial.bytesize = 8
    instrument.serial.parity = serial.PARITY_NONE
    instrument.serial.stopbits = 1
    instrument.address = 2
    instrument.mode = minimalmodbus.MODE_RTU
    instrument.serial.timeout = 1  # seconds
    instrument.clear_buffers_before_each_transaction = True
    # instrument.debug = True
    instrument.precalculate_read_size = True
    
    while True:
        
    # try:
       
        temp1 = instrument.read_register(30033,functioncode=4)
        # temp2 = instrument.read_register(1,functioncode= int('0x04',16))
        print(temp1)
        # print(temp2)
        # time.sleep(10)
        
    
        # temp1 = instrument.read_register(0,functioncode= int('0x04',16))
                    
    # except IOError as e:
    #     print("Failed to read from instrument",e)

Two cases in which continuous execution is repeated:

administrator@pi:~ $ python3 modbus_test.py 
0
0
0
0
0
0
0
0
0
Traceback (most recent call last):
  File "/home/administrator/modbus_test.py", line 28, in <module>
    temp1 = instrument.read_register(30033,functioncode=4)
  File "/usr/local/lib/python3.9/dist-packages/minimalmodbus.py", line 484, in read_register
    returnvalue = self._generic_command(
  File "/usr/local/lib/python3.9/dist-packages/minimalmodbus.py", line 1276, in _generic_command
    payload_from_slave = self._perform_command(functioncode, payload_to_slave)
  File "/usr/local/lib/python3.9/dist-packages/minimalmodbus.py", line 1353, in _perform_command
    response_bytes = self._communicate(request_bytes, number_of_bytes_to_read)
  File "/usr/local/lib/python3.9/dist-packages/minimalmodbus.py", line 1521, in _communicate
    raise NoResponseError("No communication with the instrument (no answer)")
minimalmodbus.NoResponseError: No communication with the instrument (no answer)

enter image description here

administrator@pi:~ $ python3 modbus_test.py 
0
0
0
Traceback (most recent call last):
  File "/home/administrator/modbus_test.py", line 28, in <module>
    temp1 = instrument.read_register(30033,functioncode=4)
  File "/usr/local/lib/python3.9/dist-packages/minimalmodbus.py", line 484, in read_register
    returnvalue = self._generic_command(
  File "/usr/local/lib/python3.9/dist-packages/minimalmodbus.py", line 1276, in _generic_command
    payload_from_slave = self._perform_command(functioncode, payload_to_slave)
  File "/usr/local/lib/python3.9/dist-packages/minimalmodbus.py", line 1359, in _perform_command
    payload_from_slave = _extract_payload(
  File "/usr/local/lib/python3.9/dist-packages/minimalmodbus.py", line 1904, in _extract_payload
    raise InvalidResponseError(text)
minimalmodbus.InvalidResponseError: Checksum error in rtu mode: b'\xfd\xe4' instead of b'\xfd0' . The response is: b'\x02\x04\x02\x00\x00\xfd\xe4' (plain response: b'\x02\x04\x02\x00\x00\xfd\xe4')

I don't know why a script that used to run fine on Windows ends up like this.

I suspected a problem with the ch340 driver.

I try: $lsusb

Bus 001 Device 009: ID 1a86:7523 QinHeng Electronics CH340 serial converter

$lsmod

ch341                  16384  0

$dmesg

[11614.383123] usb 1-1.1.2: Product: USB2.0-Ser!
[11614.386616] ch341 1-1.1.2:1.0: ch341-uart converter detected
[11614.390040] ch341-uart ttyUSB0: break control not supported, using simulated break
[11614.390435] usb 1-1.1.2: ch341-uart converter now attached to ttyUSB0

I still don't know if the driver is installed. However, I confirmed that the value was output via modbus at first.

The strange thing is that in windows At first, the ch340 USB was not recognized. After installing the ch340 driver, the description of the USB changes to ch340. It has changed. But Linux uses USB2.0-Ser! The Description has been fixed.

Because of this, it is still questionable whether the ch340 driver is connected.

I wonder. Is the ch340 driver operating properly now? If it is running, is there any problem with the script that was running well? What to do if it doesn't work

1

There are 1 best solutions below

0
Yong Han On

I found the answer. This is a cable problem, not a coding problem, and when reconnecting (testing) with the cable provided by Sysbase, normal communication was confirmed.