serial data reading with pico

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: serial data reading with pico

Post by scruss » Wed Jul 20, 2022 3:20 pm

filip12241224 wrote:
Tue Jul 19, 2022 5:07 pm
I have a device with rs232 output
What do you know about the device? Does it have a manual or datasheet?

If you don't know what baud rate (bit rate) and how characters are sent (7/8 bits, 1/2 start bits, 1/2 stop bits, No/Even/Odd/Mark/Space parity bit, you're probably going to have to spend some tedious time with a logic analyzer working out the protocol

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: serial data reading with pico

Post by karfas » Wed Jul 20, 2022 8:14 pm

filip12241224 wrote:
Wed Jul 20, 2022 10:24 am
today even the old code returns "none" so i think the connection may be bad
Maybe you should check first if your signal really reaches the UART ?

I usually have a few LEDs soldered to a 100 Ohm resistor in my toolbox. They are a very simple, very low-cost way to detect whether some signal arrives at a specific point in your circuit.

This works well with relatively low transfer speeds (19k2) for RS232, RS485, UART, whatever. You can correlate your actions (e.g. typing in putty) with the short flashes of the LED...
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

filip12241224
Posts: 9
Joined: Tue Jul 19, 2022 4:33 pm

Re: serial data reading with pico

Post by filip12241224 » Fri Jul 22, 2022 2:16 pm

.
Last edited by filip12241224 on Thu Sep 01, 2022 5:47 pm, edited 1 time in total.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: serial data reading with pico

Post by Roberthh » Fri Jul 22, 2022 2:28 pm

What's puzzling me is that the loopback test does not work. That is the minimal test case. Until that one does works, is make no sense to connected external data sources.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: serial data reading with pico

Post by Roberthh » Fri Jul 22, 2022 2:40 pm

So the wiring for loopback should be as in the sketch below:
loopback.jpg
loopback.jpg (148.49 KiB) Viewed 2514 times

TheSilverBullet
Posts: 50
Joined: Thu Jul 07, 2022 7:40 am

Re: serial data reading with pico

Post by TheSilverBullet » Fri Jul 22, 2022 3:03 pm

Once the hardware stuff is sorted out, maybe this one will be helpful: 8-)

Code: Select all

import machine
import time

baudratelist = [300*2**i for i in range(8)]
baudratelist.extend([57600*2**i for i in range(2)])
bitlist =[7,8,9]
paritylist = [None,0,1]
stoplist = [1,2]

print(baudratelist)
print(bitlist)
print(paritylist)
print(stoplist)

for baudrate in baudratelist:
    for bits in bitlist:
        for parity in paritylist:
            for stop in stoplist:
                try:
                    u = None
                    u = machine.UART(0, baudrate)
                    u.init(baudrate=baudrate, bits=bits, parity=parity, stop=stop)
                    print(baudrate,bits,parity,stop)
                    for t_ms in range(1000):
                        if u.any():
                            value = u.read(1)
                            if type(value) is bytes:
                                value = ord(value)
                                c = chr(value) if 32 <= value <= 127 else '.'
                                print(f'  {c}  Bin:{value:08b}   Dec:{value:3d}   Hex:{value:02x}')
                        else:
                            time.sleep_ms(1)
                except Exception as e:
                    print(e)
                    if u:
                        u.deinit()

filip12241224
Posts: 9
Joined: Tue Jul 19, 2022 4:33 pm

Re: serial data reading with pico

Post by filip12241224 » Fri Jul 22, 2022 3:35 pm

.
Last edited by filip12241224 on Thu Sep 01, 2022 5:47 pm, edited 1 time in total.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: serial data reading with pico

Post by Roberthh » Fri Jul 22, 2022 3:56 pm

Good. Now you know it works up to the RS232 side. Next: what is connected to the RS232 side?

filip12241224
Posts: 9
Joined: Tue Jul 19, 2022 4:33 pm

Re: serial data reading with pico

Post by filip12241224 » Fri Jul 22, 2022 5:02 pm

.
Last edited by filip12241224 on Thu Sep 01, 2022 5:48 pm, edited 1 time in total.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: serial data reading with pico

Post by Roberthh » Fri Jul 22, 2022 6:44 pm

Is the device connected with the RJ11 or the DB9 connector?
Did you check the silent voltage levels at the RS232 side? These should be -5 to -12 V.
Does the device need control signals at a specific level? Like RTS and DTR?

Post Reply