Hello everyone,
I've created an app by using python, now i want to create values on that .py and send to raspberry pico then get response back to my computer. Example pc should send i="x" value could be char then raspberry should response like "if i==1 print("hello"), if i==2("good bye") back to the computer. I saw some examples for C but my app is written by py file so i'm trying to solve by using serial.read, serial.writeline, uart.read etc. Any suggestions please?
PC.py file
import serial
ser = serial.Serial('COM7', baudrate=9600, timeout=1)
try:
while True:
data_to_send = input("input a char: ")
ser.write(data_to_send.encode('utf-8'))
response = ser.readline().decode('utf-8').strip()
print("response from device:", response)
except KeyboardInterrupt:
ser.close()
Board codes
import machine
# Initialize an output pin to control the LED
led = machine.Pin(25, machine.Pin.OUT)
# Initialize the UART communication with a baud rate of 9600
uart = machine.UART(0, baudrate=9600, tx=machine.Pin(0), rx=machine.Pin(1))
while True:
# Check if there is any data available to read from the UART
if uart.any():
received_data = uart.read(1)
# Check if data was received
if received_data:
print("Received data: ", received_data.decode("utf-8"))
# Perform actions based on the received data to turn the LED on/off
# For example, if the character 'k' is received, turn on the LED,
# and if any other character is received, turn off the LED
if received_data == b'k':
# Turn on the LED
led.value(1)
# Send "OK" response to confirm that the character 'k' was received
uart.write("OK")
else:
# Turn off the LED
led.value(0)
# Send "UNKNOWN" response to indicate that an unknown character was received
uart.write("UNKNOWN")