Author Topic: Issue with Receiving Data from Raspberry Pi Pico via RS485  (Read 1436 times)

0 Members and 1 Guest are viewing this topic.

Offline rdvncrtTopic starter

  • Newbie
  • Posts: 7
  • Country: tr
Issue with Receiving Data from Raspberry Pi Pico via RS485
« on: September 15, 2023, 12:26:10 pm »
Hello Forum Members,

I'm working on a serial communication project where I'm facing a few challenges. In the project, I'm trying to establish communication using the RS485 protocol between a Raspberry Pi 4 and a Raspberry Pi Pico.

Here are the basic code structures I'm using:

Raspberry Pi 4:

python

import serial

ser = serial.Serial('/dev/serial0', baudrate=9600, timeout=1)  # Serial port settings

def read_data_from_slave():
    data = ser.read(1)  # The first byte contains the slave address
    slave_address = int.from_bytes(data, byteorder='big')  # Get the slave address
    data = ser.read(ser.in_waiting)  # Get the remaining data
    return slave_address, data

try:
    slave_address, data = read_data_from_slave()  # Receive data from the slave device

    print(f"Slave Address: {slave_address}")
    print(f"Received Data: {data.decode()}")
except KeyboardInterrupt:
    ser.close()  # Close the serial port when the program is terminated
Raspberry Pi Pico:

python

import uos
import uos, machine
import ustruct as struct
from machine import Pin
import time

uart_tx = machine.Pin(4)  # TX pin: GPIO 0
uart_rx = machine.Pin(5)  # RX pin: GPIO 1

# Create a UART object
UART = machine.UART(1, baudrate=9600, tx=uart_tx, rx=uart_rx)

def send_data(data):
    UART.write(data)

slave_address = 7  # Set the slave address as 2

def send_data_to_master():
    send_data(struct.pack('b', slave_address))  # Send the slave address
    data="m"
    send_data(data)  # Send the data

while True:
    time.sleep(1)
    send_data_to_master()
Currently, I'm facing an issue in the project: I'm unable to receive data from the Raspberry Pi 4 device. The serial communication codes seem to be working correctly, but the data receiving function is not yielding the expected results. Does anyone have any insights on this?

Thank you!
 

Online Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 3364
  • Country: nl
Re: Issue with Receiving Data from Raspberry Pi Pico via RS485
« Reply #1 on: September 16, 2023, 11:56:39 am »
Start with the hardware side. Are your bits actually reaching their destination? RS485 is pretty reliable, but it needs to be done correctly for it to be reliable. How long are your cables? Do you have termination (and biasing) resistors? Do you have the (mandatory!) GND wire?

How are you switching direction on your RS485 transceivers, or do you have a full-duplex setup?
 

Offline rdvncrtTopic starter

  • Newbie
  • Posts: 7
  • Country: tr
Re: Issue with Receiving Data from Raspberry Pi Pico via RS485
« Reply #2 on: September 16, 2023, 01:11:15 pm »
I have termination resistors, and my cables are approximately 20 cm long. I am not changing the transmitter and receiver direction in my Python code. How can I do this?
 

Online Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 3364
  • Country: nl
Re: Issue with Receiving Data from Raspberry Pi Pico via RS485
« Reply #3 on: September 16, 2023, 01:42:30 pm »
I am not changing the transmitter and receiver direction in my Python code. How can I do this?

I'd say start with reading more about RS485 implementation details. "ten ways to bullet proof RS485" is a good read.

Then reverse engineer your HW-0519 modules. They appear to have PPTC's and TVS diodes on the output, that is good. but on the "other side" there is an extra IC. I assume that is being used to sense and auto switch the direction, and/or do level shifting. And what logic levels are you using? your raspi 4 is probably not 5V tolerant.

Also: Ive seen a few pictures of the HW-0519 module with sanded tops. I always give instant negative feedback for that. I want to know what I buy.
 

Offline rdvncrtTopic starter

  • Newbie
  • Posts: 7
  • Country: tr
Re: Issue with Receiving Data from Raspberry Pi Pico via RS485
« Reply #4 on: September 16, 2023, 02:18:02 pm »
Thank you so much, I started reading the document. Yes as far as I see, the other ic is the logic gate ic which does the receive-write change. Yes I am supplying the RP4 with 3.3V.

So, still confused about the direction-change that I should make in python in my RaspBerry Pi 4 and how.. Searching for sample codes, still not find any.. Could you please help me?
 

Offline Georgy.Moshkin

  • Regular Contributor
  • *
  • Posts: 146
  • Country: hk
  • R&D Engineer
    • Electronic projects, modules and courses on Arduino and STM32
Re: Issue with Receiving Data from Raspberry Pi Pico via RS485
« Reply #5 on: September 16, 2023, 04:04:25 pm »
ttl-to-485 converters usually change direction automatically (E.g., DE/Ri pins of 485 ic driven by rc-constant schematic which is triggered by ttl'sTX input). So no need to control direction in code.

Problem is usually solved by
1) fixing some flaw in software or
2) swapping A and B wires or
3) de-soldering of termination resistor - rare, but happened to me few times: some Ttl-to-485 transceivers was not able to drive device with standard 120 ohm termination resistor. On a scope, it looked like waveform was smeared (non-rectangular shape)
« Last Edit: September 16, 2023, 04:06:26 pm by Georgy.Moshkin »
 

Offline artag

  • Super Contributor
  • ***
  • Posts: 1075
  • Country: gb
Re: Issue with Receiving Data from Raspberry Pi Pico via RS485
« Reply #6 on: September 17, 2023, 10:25:16 am »
I don't see anything there to set the tty mode beyond baud rate. A Unix serial tty will normally be in cooked mode - it will accumulate a line of characters before providing them to a read(). You can change this with stty, or probably from python.

 
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf