UART writing
-
Hello,
Since now I'm using the UART just for reading like this:
def SePo(self): try: if (self.uart.any() > 0): trama = self.uart.read() print('TRAMA >>>> ', trama) ...
Basically I call the method and if I have some values in my Serial Port (using
any()
) I read it and I start processing the data. This a real action, I'm using the default pins (P3 and P4) and previously I had init it with the baudrate, parity, stop an bits and works well.But now what I want to do is write in my serial port, and I can't achieve it. Your example is simple, but I never obtain the "Hello" I always have
None
.from machine import UART # this uses the UART_1 default pins for TXD and RXD (``P3`` and ``P4``) uart = UART(1, baudrate=9600) uart.write('hello') print(uart.read(5)) # read up to 5 bytes
I though maybe I need more time and I tried the following:
# this uses the UART_1 default pins for TXD and RXD (``P3`` and ``P4``) uart = UART(1, baudrate=9600) uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters uart.write('hello') for _ in range(10): if (uart.any() > 0): trama = uart.read() print('TRAMA >>>> ', trama) else: print('nothing') time.sleep(1)
But always print nothing.
With this results I understand I'm writing wrong on my UART. Probably is an easy question but with the documentation I can't find the way to solve it. I tried using another baudrate like 115200 but don't work to.
Furthermore I found this question and it seems he is doing it as the same way as your example, but I can't see my mistake.
Thank you very much!
-
@M-m
No it is not echoed back from tx to rx, you must connect something to the line between.
Think about, you have 2 devices conected by uart and you send something to second device and in the same thime you have same data as incoming ;-)
How you can then know which data was send by second device to your first device?It not work in this way. You can try to connect rx to tx by the wire and then in should work in this way.
-
@jcaron mmm I'm not sure to understand you, by the moment the writing part I'm just simulating it, I want to write and see what I wrote. Because in addition to write in my serial port devices I want to create a Serial Port simulator function.
In this example,
from machine import UART # this uses the UART_1 default pins for TXD and RXD (``P3`` and ``P4``) uart = UART(1, baudrate=9600) uart.write('hello') print(uart.read(5)) # read up to 5 bytes
seems that if initialize it and then write, the message is in my
uart
object (or this is what I understand).Thanks for your help.
-
@M-m what is connected to your UART? Is it designed to echo what it receives? Does it actually receive the data you send?