Page 1 of 1

UART issue

Posted: Wed Nov 30, 2016 8:27 am
by technodict
Hello ,
I am trying to run the following code on the ESP8266 Node MCU board. I have put this code in main.py. The input is being taken from a laptop running Cutecom as a terminal program. The issue is uart.read() doesn’t wait to receive any data from serial (Rx). The Rx and Tx pins are connected to corresponding Tx and Rx of a serial to USB converter.

from machine import UART
uart = UART(0, 9600, timeout=0)
uart.init(9600, bits=8, parity=None, stop=1)
text = uart.read(12)
while True:
if text == b' RECEIVEDTEXT':
uart.write("sucess")
else:
uart.write("failed")
break

Re: UART issue

Posted: Wed Nov 30, 2016 10:02 am
by platforma
technodict wrote:The Rx and Tx pins are connected to corresponding Rx and Tx of a serial to USB converter.
Those need to be crossed, i.e:

Code: Select all

Board -> |Rx - Tx| <- Usb2Serial
         |Tx - Rx|
You might already have them that way, just though I'll double check!

Re: UART issue

Posted: Wed Nov 30, 2016 5:11 pm
by dhylands
And also make sure you connect GND from the 8266 to GND on the usb-to-serial converter.

Re: UART issue

Posted: Thu Dec 01, 2016 5:11 am
by technodict
Yes the Pins are Rx-Tx Tx-Rx and yes grounds are connected !

Re: UART issue

Posted: Thu Dec 01, 2016 8:25 am
by Neil
I think the problem is that the UART.read is not a blocking read and will read the buffer UPTO but not neccessary the exact number of bytes specified. Therefore something like this may work.

Code: Select all

from machine import UART
uart = UART(0, 9600, timeout=0)
uart.init(9600, bits=8, parity=None, stop=1)
text=''
while len(text)<12:
	text += uart.read(1)
if text == b' RECEIVEDTEXT':
	uart.write("sucess")
else:
	uart.write("failed")
note this code is untested and just from the top of my head.

Re: UART issue

Posted: Sun Jul 12, 2020 12:37 pm
by abhi98

Code: Select all

from machine import UART
uart = UART(1, 9600)                         
uart.init(9600, bits=8, parity=None, stop=1) 
uart.write("hello")
using this code Node mcu and using serial monitor see whats happening as i plugged the serial monitor repl stops working ?
can anyone please help me ?

Re: UART issue

Posted: Mon Jul 13, 2020 2:31 am
by jimmo
abhi98 wrote:
Sun Jul 12, 2020 12:37 pm
using this code Node mcu and using serial monitor see whats happening as i plugged the serial monitor repl stops working ?
can anyone please help me ?
Is your serial monitor also running at 9600 baud?

This is a limitation of the ESP8266 -- there is only one UART, so it will be shared with the REPL.