Pyboard - UART to UART pass through for Nextion

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
User avatar
tsjoiner
Posts: 16
Joined: Tue Sep 05, 2017 3:09 pm
Location: Alberta Beach, Alberta, Canada

Pyboard - UART to UART pass through for Nextion

Post by tsjoiner » Sat Jun 23, 2018 4:03 pm

Hi,

I recently started testing a Nextion TFT display with my pyboard. I am able to send and receive simple instructions through UART(3). A raspberry pi is connected via uos.dupterm(UART(6)) mainly REPL and file transfers. Occasionally new programming -.tft files - need to be uploaded to the display. This can be done easily from the raspberry pi to the display with a USB to TTL adapter using the python code found here: https://github.com/g4klx/MMDVMHost/blob ... nextion.py. However, it would be nice to avoid all the plugging and unplugging and use a uart to uart pass through instead. There is an example of USB to UART pass through here: https://docs.micropython.org/en/latest/ ... rough.html

Not sure how to tailor this to my needs. Any advice will be appreciated - with examples even more so.

Thanks all.

User avatar
dhylands
Posts: 2841
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Pyboard - UART to UART pass through for Nextion

Post by dhylands » Sun Jun 24, 2018 12:51 am

You shouldn't need to change the past thru function at all.

Just open a UART rather than USB.

User avatar
tsjoiner
Posts: 16
Joined: Tue Sep 05, 2017 3:09 pm
Location: Alberta Beach, Alberta, Canada

Re: Pyboard - UART to UART pass through for Nextion

Post by tsjoiner » Mon Jun 25, 2018 6:00 pm

I have exchanged USB for UART as suggested. I am able to send and receive simple messages - like set display brightness or get buttons pressed. My excitement was short lived however. When I try to upload .tft file things hang very quickly.

Output:

Code: Select all

pi@raspberrypi:~ $ python /home/pi/DAPLC_PY/Screen/nextion2.py /home/pi/DAPLC_PY/Screen/Controlfreaks-7.tft /dev/ttyUSB1
Trying with baudrate: 9600...
Trying with baudrate: 115200...
Connected with baudrate: 115200...
Status: #���comok
Touchscreen: yes
Model: NX8048T050_011R
Firmware version: 99
MCU code: 61488
Serial: E466A0018F3E3822
Flash size: 167772
Downloading, 0.1%...()
Could not transfer file
pi@raspberrypi:~ $ 
Pyboard firmware:

Code: Select all

MicroPython v1.9.3-558-ga60efa82 on 2018-04-23; PYBv1.1 with STM32F405RG
My Pyboard code:

Code: Select all

import pyb
import select

uart1 = UART(1)
uart1.init(115200, timeout=0)
uart3 = UART(3)                         
uart3.init(115200, timeout=0)


while True:
	select.select([uart1, uart3], [], [])  # select line seems to make no difference
	if uart3.any():
		uart1.write(uart3.read(256))

	if uart1.any():
		uart3.write(uart1.read(256))
Feels like the timing is off. I suspect me naive rework of the USB to UART pass through example is faulty.

User avatar
pythoncoder
Posts: 2899
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Pyboard - UART to UART pass through for Nextion

Post by pythoncoder » Thu Jun 28, 2018 6:34 am

I would try something along these lines (note I haven't actually tested this):

Code: Select all

import pyb
uart1 = UART(1)
uart1.init(115200, read_buf_len = 256)
uart3 = UART(3)                         
uart3.init(115200, read_buf_len = 256)  # Try higher values if necessary

while True:
    nrx = uart3.any()  # Count available characters
    if nrx:
        uart1.write(uart3.read(nrx))  # Only read what's available: it should never time out

    nrx = uart1.any()
    if nrx:
        uart3.write(uart1.read(nrx))
If you want to use uselect I suggest you read this part of the docs. However in this simple example I don't think it brings much to the party.
Peter Hinch

User avatar
tsjoiner
Posts: 16
Joined: Tue Sep 05, 2017 3:09 pm
Location: Alberta Beach, Alberta, Canada

Re: Pyboard - UART to UART pass through for Nextion

Post by tsjoiner » Thu Jul 05, 2018 2:09 pm

Success - I can now communicate remotely with my Pyboard and Nextion display (ESP8266 controls communications, as well a few i/o pins on Pyboard including reset). The Pyboard can boot up in one of two ways: Dupterm enabled or UART to UART pass through enabled.

Code: Select all

# boot.py -- run on boot-up
# i/o A15 switches boot modes from dupterm to UART6 <> UART3 pass through for screen programming.

import pyb
from pyb import UART
import uos

sp = pyb.Pin('A15', pyb.Pin.IN)  # Activate input A15 (red led) connected to ESP8266 i/o 14 
pyb.delay(10)

if sp.value() == 1:
	uart6 = UART(6)
	uart6.init(115200, read_buf_len = 256)
	uart3 = UART(3)                         
	uart3.init(115200, read_buf_len = 256)  # Try higher values if necessary

	while True:
		nrx = uart3.any()  # Count available characters
		if nrx:
			uart6.write(uart3.read(nrx))  # Only read what's available: it should never time out

		nrx = uart6.any()
		if nrx:
			uart3.write(uart6.read(nrx))
	
else:
	uart6 = UART(6)
	uart6.init(115200)
	uos.dupterm(uart6)  # duplicate repl on UART(6)

	pyb.main('main.py') # main script to run after this one

	pyb.usb_mode('CDC') # act as a serial (CDC) and not a storage device (MSC)
When booted into dupterm mode, the Pyboard interacts directly with the TFT display.

All help was appreciated. Many thanks pythoncoder.

Post Reply