1
0
micropython-rp2-rs485-examples/rs485_receive.py

58 lines
2.2 KiB
Python

'''
Simple program for receiving data over two UART channels to a TTL UART to RS485 converter, printing it out over Serial console.
Inspired by and partly based on Waveshare's example code to the 2 Channel RS485 HAT for the Pi Pico.
Copyright (C) 2024 Benjamin Burkhardt
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
from machine import UART, Pin
import time
uart0 = UART(0, baudrate=115200, tx=Pin(0), rx=Pin(1))
uart1 = UART(1, baudrate=115200, tx=Pin(4), rx=Pin(5))
print('Starting RS485 simple receive...')
module0_mode = Pin(2, Pin.OUT) # define pinout for the receiver/driver enable pins (shortened together)
module1_mode = Pin(3, Pin.OUT) # define pinout for the receiver/driver enable pins (shortened together)
module0_mode.low() # logic 0 = receive; logic 1 = send
module1_mode.low() # logic 0 = receive; logic 1 = send
rxData0concatenated = ''
rxData1concatenated = ''
while True:
rxData0 = uart0.readline()
if rxData0 is not None:
rxData0decoded = rxData0.decode('utf-8')
if not '\n' in rxData0decoded:
rxData0concatenated += rxData0decoded
else:
rxData0concatenated += rxData0decoded
print(f"[CH0] Received data: {rxData0concatenated}", end="")
rxData0concatenated = ''
rxData1 = uart1.readline()
if rxData1 is not None:
rxData1decoded = rxData1.decode('utf-8')
if not '\n' in rxData1decoded:
rxData1concatenated += rxData1decoded
else:
rxData1concatenated += rxData1decoded
print(f"[CH1] Received data: {rxData1concatenated}", end="")
rxData1concatenated = ''