I am trying to send commands and receive data from Arduino beta Metro M4 express (programmed using circuitpython) via USB. The version of circuitpython is 4.0.1 and Python version is 3.7.3. I could able to send the data via usb but having problems while receiving the data. The command 'supervisor.runtime.serial_bytes_available' reads the command sent from PC via usb but I am receiving the data printed using 'print' statement in the python shell. Also, I cannot able to get and print the command using in 'data = sys.stdin.read(4)' command. Any help is much appreciated.
My python code is as follows:
import serial
import time
while(1):
ser = serial.Serial(port='COM9', baudrate=115200, timeout=0.01)
cmd = b'what'
print('cmd=', cmd)
ser.write(cmd)
ser.flushInput()
time.sleep(1)
x = ser.readline()
print("received1: ",x.decode("utf-8"))
y = ser.readline()
print("received2: ",y.decode("utf-8"))
ser.close()
Circuitpython code is as follows:
import supervisor
while(x):
if supervisor.runtime.serial_bytes_available:
print("True")
data = sys.stdin.read(4)
print("in code.py",data)
The expected and results received in the python shell are as follows:
Expected result is: received1: True, received2: what
Received results:
cmd= b'what'
received1: hat
received2:
cmd= b'what'
received1: hat
received2:
cmd= b'what'
received1: hat
received2:
cmd= b'what'
received1: hat
received2:
cmd= b'what'
received1: hat
received2:
cmd= b'what'