0

I need to build the communication with micropython since I need it for school. The next issue that I can't seem to get done is that my communication needs to be from python program to raspberry pi pico and back. The farthest I've tried is this.

A program on the raspberry:

import sys
import utime

while(True):
    x = sys.stdin.buffer.read()
    if x == "1":
        sys.stdout.print(x)
    utime.sleep(1)
    if x == 'end':
        break

and a program on my pc: import serial from time import sleep

class Handler:
    TERMINATOR = '\n'.encode('UTF8')

    def __init__(self, device='COM19', baud=115200, timeout=1):
         self.serial = serial.Serial(device, baud, timeout=timeout)

    def receive(self) -> str:
         line = self.serial.read_until(self.TERMINATOR)
         return line.decode('UTF8').strip()

    def send(self, text: str):
        line = text
        self.serial.write(line.encode('UTF8'))

    def close(self):
        self.serial.close()

sender = Handler('COM19',115200,1)
while(True):
    x = input()
    sender.send(x)
    sleep(2)
    print(sender.receive())
    if x == 'end':
        break

This code is absolutely not mine and is an amalgam of what I was able to find on the internet. What I am trying to do is put a number into the console on my computer program and I am trying to send it back with raspberry pi pico and read it on my pc. But I wasn't able to get that response. Any help would be fine, either pointers or solutions. Thank you for anything in advance.

  • Welcome to Stack Overflow. Does ```sys.stdin.buffer.read()``` actually read the comport? – ewokx Oct 03 '22 at 06:49
  • Well honestly, since this is all stolen code, I don't really have an idea. From what I understood it does read the serial and I thought that it is the comport. But I understand very little about low level programing. – Patrik Holub Oct 03 '22 at 07:00

1 Answers1

0

Fixed it, finally realized that when I use serial.write() it doesn't write \r\n so everything was just always being put into a long line and never read on pi pico.

  • Hi, Patrik! Really great that you figured this one out, but I guess you should add the code that got the desired result in the answer as well as to clarify any others. Kudos! – Muneeb Ahmad Khurram Mar 16 '23 at 20:25