Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1269

Python • Connecting RS485 via Modbus to Mitsubishi

$
0
0
Hi everybody,
i hope this is the correct section to ask this question.

This is what i have:
- Raspberry Pi 4
- I have this RS485 CAN HAT: https://www.waveshare.com/wiki/RS485_CAN_HAT
- I have a Mitsubishi Melservo J4. I have the model MR-J4-40A-RJ.

My scope is to read and write some parameter.

Following the documentation (https://dl.mitsubishielectric.com/dl/fa ... 30175b.pdf) at page 17 I find the half duplex connection, so i wired like this:
MR-J4 Raspberry
1 NC --> NC
2 NC --> NC
3 RDP --> PIN A of the RS485 HAT
4 SDN --> NC
5 SDP --> NC
6 RDN --> PIN B of the RS485 HAT
7 LG --> to GND of the Raspberry
8 TRE --> NC

i also tried reversing the B and the A but the same result. Cannot get any message from the Mitsubishi.

From the RS485 CAN HAT official page (https://www.waveshare.com/wiki/RS485_CAN_HAT) i found some examples in the section "RS485 Usage" in python, and these are what i tried but with no solution (P.S. i followed the website instruction and i can correctly see the opened serial port):
receive.py:

Code:

# -*- coding:utf-8 -*-import RPi.GPIO as GPIOimport serialEN_485 =  4GPIO.setmode(GPIO.BCM)GPIO.setup(EN_485,GPIO.OUT)GPIO.output(EN_485,GPIO.LOW)ser = serial.Serial("/dev/serial0",115200,timeout=1)  # open first serial port    ser.method = 'rtu'while 1:      str = ser.readall()      if str:          print (str) 
send.py:

Code:

# -*- coding:utf-8 -*-import RPi.GPIO as GPIOimport serialEN_485 =  4GPIO.setmode(GPIO.BCM)GPIO.setup(EN_485,GPIO.OUT)GPIO.output(EN_485,GPIO.HIGH)t = serial.Serial("/dev/serial0",115200)    print (t.portstr)    strInput = input('enter some words:')    n = t.write(strInput.encode())    print (n)    str = t.read(n)    print (f"whats going on: {str}")   
This was another i code i tried:

Code:

# -*- coding:utf-8 -*-import RPi.GPIO as GPIOfrom pymodbus.client.sync import ModbusSerialClient as ModbusClientimport serial# GPIO setupEN_485 = 4GPIO.setmode(GPIO.BCM)GPIO.setup(EN_485, GPIO.OUT)GPIO.output(EN_485, GPIO.LOW)# Configure Modbus RTU clientclient = ModbusClient(    method='rtu',    port='/dev/serial0',  # Serial port    baudrate=115200,    parity='N',    stopbits=1,    bytesize=8,    timeout=1)# Connect to Modbus clientconnection = client.connect()if connection:    print("Connected to Modbus RTU")else:    print("Failed to connect")try:    while True:        # Convert hexadecimal address to decimal        address = int('2B02', 16)  # 2B02h is 11010 in decimal        count = 1  # Number of registers to read        # Enable transmission mode        GPIO.output(EN_485, GPIO.HIGH)        # Read holding registers        result = client.read_holding_registers(address, count, unit=1)        # Disable transmission mode        GPIO.output(EN_485, GPIO.LOW)        if not result.isError():            print(result.registers)        else:            print("Error reading registers")except KeyboardInterrupt:    print("Program interrupted")finally:    # Cleanup    client.close()    GPIO.cleanup()
I found this info in this pdf: https://dl.mitsubishielectric.com/dl/fa ... 30175b.pdf at page 21, where it says that the default value of the baud rate is 4h (115200 bts). Also when using MR Configurator 2, that automatically gets the device, if i go to the parameter PC71 i get this: 0040 so that mean that is actually using 115200 bts and it using the RS485 communication protocol and not Modbus-RTU.

Do you have any idea on how to solve this?

Statistics: Posted by Tiziano Faver — Fri May 31, 2024 3:52 pm



Viewing all articles
Browse latest Browse all 1269

Trending Articles