loloToster
Posts: 7
Joined: Fri Apr 16, 2021 8:59 am

Communication betwenn pico and python on Windows

Fri Apr 16, 2021 9:13 am

Hi!
I am new to pico microcontroller and I cannot manage to find an answer for my question. I want a python script that is running on windows to send values to pico over usb cable but I can't find easy solution. I did it with arduino and it was pretty straight forward but I cant find a simple way to do it with pico. Help would be appreciated :D !

User avatar
joan
Posts: 15560
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Communication betwenn pico and python on Windows

Fri Apr 16, 2021 10:10 am

Not sure what you mean by send values.

If you are not fussed about an Arduino like interface have a look at my picod.

http://abyz.me.uk/picod/index.html

It is still an experimental pre-release.

loloToster
Posts: 7
Joined: Fri Apr 16, 2021 8:59 am

Re: Communication betwenn pico and python on Windows

Fri Apr 16, 2021 12:33 pm

What i mean is to just send string ,then receive it with pico and display it on a display.
Last edited by loloToster on Fri Apr 16, 2021 12:43 pm, edited 1 time in total.

loloToster
Posts: 7
Joined: Fri Apr 16, 2021 8:59 am

Re: Communication betwenn pico and python on Windows

Fri Apr 16, 2021 12:39 pm

joan wrote:
Fri Apr 16, 2021 10:10 am
Not sure what you mean by send values.

If you are not fussed about an Arduino like interface have a look at my picod.

http://abyz.me.uk/picod/index.html

It is still an experimental pre-release.
Do you have any serial examples?
And from what i can see you use C can i use it with micropython?

User avatar
B.Goode
Posts: 11843
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Communication betwenn pico and python on Windows

Fri Apr 16, 2021 1:32 pm

loloToster wrote:
Fri Apr 16, 2021 12:39 pm
joan wrote:
Fri Apr 16, 2021 10:10 am
Not sure what you mean by send values.

If you are not fussed about an Arduino like interface have a look at my picod.

http://abyz.me.uk/picod/index.html

It is still an experimental pre-release.
Do you have any serial examples?
And from what i can see you use C can i use it with micropython?


"can i use it with micropython?"

Is there a version of micropython for Windows? In any case, why not just use conventional 'full-cream' Python?



"Do you have any serial examples?"


http://abyz.me.uk/picod/py_picod.html#serial_open
http://abyz.me.uk/picod/py_picod.html#serial_read

hippy
Posts: 9697
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Communication betwenn pico and python on Windows

Fri Apr 16, 2021 1:38 pm

B.Goode wrote:
Fri Apr 16, 2021 1:32 pm
Is there a version of micropython for Windows? In any case, why not just use conventional 'full-cream' Python?
To answer the first; yes, MicroPython does exist for Windows, Pi and Linux, but it's not really of much use for anything. Using Python on the host is a better and easier solution as you suggest.

User avatar
joan
Posts: 15560
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Communication betwenn pico and python on Windows

Fri Apr 16, 2021 1:40 pm

Do you mean the Pico receives a string over serial, then passes that string back to Python on your Window's box for display?

If so, yes. Use Pico UART 1 as UART 0 does not work properly for me yet.

loloToster
Posts: 7
Joined: Fri Apr 16, 2021 8:59 am

Re: Communication betwenn pico and python on Windows

Fri Apr 16, 2021 1:57 pm

I'm gonna explain what i want to achive. I want to have a normal .py file on my computer with, windows to send strings in loop, with lets say 2 second interval and then on pico that is running micropython i want to receive this strings, modify them and display them on ili9341 display.

User avatar
joan
Posts: 15560
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Communication betwenn pico and python on Windows

Fri Apr 16, 2021 3:09 pm

loloToster wrote:
Fri Apr 16, 2021 1:57 pm
I'm gonna explain what i want to achive. I want to have a normal .py file on my computer with, windows to send strings in loop, with lets say 2 second interval and then on pico that is running micropython i want to receive this strings, modify them and display them on ili9341 display.
You can't do that with my module.

  1. I want to have a normal .py file on my computer with, windows YES
  2. to send strings in loop, with lets say 2 second interval YES
  3. and then on pico that is running micropython NO - a C based command interpreter needs to be running on the Pico.

hippy
Posts: 9697
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Communication betwenn pico and python on Windows

Fri Apr 16, 2021 5:00 pm

loloToster wrote:
Fri Apr 16, 2021 1:57 pm
I'm gonna explain what i want to achive. I want to have a normal .py file on my computer with, windows to send strings in loop, with lets say 2 second interval and then on pico that is running micropython i want to receive this strings, modify them and display them on ili9341 display.
Sending the data from Windows should be trivial. This should send the date and time as a line of text to a Pico every couple of seconds -

Code: Select all

import serial
import time
ser = serial.Serial("COM13")
while True:
  ser.write(time.strftime("%Y-%m-%d %H:%M:%S\n").encode())
  time.sleep(2)
On the Pico side it's probably something like below, but I have not tested it -

Code: Select all

import sys
While True:
  line = sys.stdin.readline()

loloToster
Posts: 7
Joined: Fri Apr 16, 2021 8:59 am

Re: Communication betwenn pico and python on Windows

Fri Apr 16, 2021 7:56 pm

Looks like it works. Thank you very much. <3

loloToster
Posts: 7
Joined: Fri Apr 16, 2021 8:59 am

Re: Communication betwenn pico and python on Windows

Sat Apr 17, 2021 1:39 pm

hippy wrote:
Fri Apr 16, 2021 5:00 pm
loloToster wrote:
Fri Apr 16, 2021 1:57 pm
I'm gonna explain what i want to achive. I want to have a normal .py file on my computer with, windows to send strings in loop, with lets say 2 second interval and then on pico that is running micropython i want to receive this strings, modify them and display them on ili9341 display.
Sending the data from Windows should be trivial. This should send the date and time as a line of text to a Pico every couple of seconds -

Code: Select all

import serial
import time
ser = serial.Serial("COM13")
while True:
  ser.write(time.strftime("%Y-%m-%d %H:%M:%S\n").encode())
  time.sleep(2)
On the Pico side it's probably something like below, but I have not tested it -

Code: Select all

import sys
While True:
  line = sys.stdin.readline()
i have one more question is it possible to have a timeout on sys.stdin.readline()? What i mean is that if it doesnt receive anything for 5 seconds it moves on further in the program?

User avatar
B.Goode
Posts: 11843
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Communication betwenn pico and python on Windows

Sat Apr 17, 2021 3:56 pm

loloToster wrote:
Sat Apr 17, 2021 1:39 pm

i have one more question is it possible to have a timeout on sys.stdin.readline()? What i mean is that if it doesnt receive anything for 5 seconds it moves on further in the program?


For many computing/IT/coding problems it is worth checking whether anyone else has asked the same thing before....

https://github.com/micropython/micropython/issues/774

loloToster
Posts: 7
Joined: Fri Apr 16, 2021 8:59 am

Re: Communication betwenn pico and python on Windows

Sat Apr 17, 2021 4:01 pm

B.Goode wrote:
Sat Apr 17, 2021 3:56 pm
loloToster wrote:
Sat Apr 17, 2021 1:39 pm

i have one more question is it possible to have a timeout on sys.stdin.readline()? What i mean is that if it doesnt receive anything for 5 seconds it moves on further in the program?


For many computing/IT/coding problems it is worth checking whether anyone else has asked the same thing before....

https://github.com/micropython/micropython/issues/774
I read this github issue before writing this and i dont understand most of it. :(

hippy
Posts: 9697
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Communication betwenn pico and python on Windows

Sat Apr 17, 2021 7:04 pm

Threading may be one option. This seems to sort of work but was thrown together without regard to being thread safe or any issues it may have -

Code: Select all

import sys
import time
import _thread

available = False
line = ""

def ReadLineThread():
    global available
    global line
    while True:
        while available:
            time.sleep(0.1)
        line = sys.stdin.readline()
        available = True

_thread.start_new_thread(ReadLineThread, ( ))

while True:
    t = 0
    while not available:
        time.sleep(0.1)
        t = t + 0.1
        if t >= 5:
            print("Nothing received")
            t = 0
    print("Got {}".format(line))
    available = False 

Return to “MicroPython”