Issue with Receiving Data from Raspberry Pi Pico via RS485 - Page 1
Forum section: electronics / general

author: rdvncrt
author_rank: Newbie
timestamp: September 15, 2023, 12:26:10 pm
content: 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:pythonimport serialser = serial.Serial('/dev/serial0', baudrate=9600, timeout=1) # Serial port settingsdef read_data_from_slave():data = ser.read(1) # The first byte contains the slave addressslave_address = int.from_bytes(data, byteorder='big') # Get the slave addressdata = ser.read(ser.in_waiting) # Get the remaining datareturn slave_address, datatry:slave_address, data = read_data_from_slave() # Receive data from the slave deviceprint(f"Slave Address: {slave_address}")print(f"Received Data: {data.decode()}")except KeyboardInterrupt:ser.close() # Close the serial port when the program is terminatedRaspberry Pi Pico:pythonimport uosimport uos, machineimport ustruct as structfrom machine import Pinimport timeuart_tx = machine.Pin(4) # TX pin: GPIO 0uart_rx = machine.Pin(5) # RX pin: GPIO 1# Create a UART objectUART = 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 2def send_data_to_master():send_data(struct.pack('b', slave_address)) # Send the slave addressdata="m"send_data(data) # Send the datawhile 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!

author: Doctorandus_P
author_rank: Super Contributor
timestamp: September 16, 2023, 11:56:39 am
content: 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?

author: rdvncrt
author_rank: Newbie
timestamp: September 16, 2023, 01:11:15 pm
content: 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?

author: Doctorandus_P
author_rank: Super Contributor
timestamp: September 16, 2023, 01:42:30 pm
content: Quote from: rdvncrt on September 16, 2023, 01:11:15 pmI'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.

author: rdvncrt
author_rank: Newbie
timestamp: September 16, 2023, 02:18:02 pm
content: 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?

author: Georgy.Moshkin
author_rank: Frequent Contributor
timestamp: September 16, 2023, 04:04:25 pm
content: 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 by1) fixing some flaw in software or2) swapping A and B wires or3) 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)

author: artag
author_rank: Super Contributor
timestamp: September 17, 2023, 10:25:16 am
content: 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.