serial-portraspberry-pi-pico

I want to create a communication with raspberry pico and my windows pc


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.


Solution

  • 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.

    The issue from what I can understand is that Raspberry pi pico with micropython communicates like a python REPL. Due to that you need to have an "enter" key when sending anything.

    I have created this code for my communication needs

    import json
    from time import sleep
    
    import serial
    import serial.tools.list_ports
    
    #global variables
    terminator = "\r\n"
    last_send = ''
    
    def sendMessage(comport,msg):
        # message has to be formated, thus function to format and send
        b_msg = bytes(msg + terminator, 'UTF-8')
        global last_send
        last_send = b_msg
        comport.write(b_msg)
        
    def readMessage(comport, decoding = False, encoding = 'UTF-8'):
        # message has to be read and decoded, if you want
        # ! THIS FUNCTION WILL READ ONE LINE AFTER THE THE FEEDBACK MESSAGE !
        data = comport.readlines()
        #print(data)
        iterator = iter(data)
        index = 0
        while(True):
            index += 1
            if next(iterator) == last_send:
                break
        # used to get the message out of it and not the feedback
        better_data = data[index:]
        #print((better_data[0].decode('UTF-8')).strip() if decoding else better_data[0])
        return (better_data[0].decode('UTF-8')).strip() if decoding else better_data[0] # return decoded or encoded, doesn't matter
    
    def readMessageBlock(comport,decoding = False, encoding = 'UTF-8'):
        # The same as readMessage but used to get the entire block of data coming through
        data = comport.readlines()
        #print(last_send)
        iterator = iter(data)
        index = 0
        while(True):
            index += 1
            if next(iterator) == last_send:
                break
        
        better_data = data[index:]
        if decoding:
            counter = 0
            for line in better_data:
                better_data[counter] = (line.decode(encoding)).strip()
                counter += 1
        
        return better_data