- PyBoard V1.1
The instructions I send from the PC (using python 3.8) take the form of a list of ints (each int is between 0 and 255) which is then converted into a bytearray before been transmitted via a com port. I know that the bytearray conversion of the list can produce some unexpected representations for the individual ints such as 10 becoming ‘n’ and 9 becoming ‘t’, however these convert back to the original int on the PyBoard when required.
Is the number 3 or its bytearray representation of \x03 a special command/escape character/reserved byte on the PyBoard V1.1 or should I be able to transmit it without any issue? Ideally I would like to be able to use all numbers in the range of 0 to 255 for each int in the instruction list.
Below I have included a copy of the python (running on PC) and uPython code used when encountering this error. I can upload some copies of the different outputs from my python console showing the different behaviours triggered by the number 3 if it will help. The list a is the list of ints sent from the PC to the PyBoard.
uPython Code running on PyBoard
Code: Select all
# main.py -- put your code here import pyb usb = pyb.USB_VCP() def function_sort(z): del z if z == 1: flash_LED(z) elif z == 2: print_string(z) else: usb.write("instruction not identified\r\n") def flash_LED(z): count = z i = 0 while i < count: pyb.LED(1).on() pyb.delay(1000) pyb.LED(3).on() pyb.delay(1000) pyb.LED(1).off() pyb.delay(500) pyb.LED(3).off() pyb.delay(500) i += 1 def print_string(z): if z == 1: usb.write("string 1\r\n") elif z == 2: usb.write("string 2\r\n") elif z == 3: usb.write("string 3\r\n") else: usb.write("string not found \r\n") usb.write("starting while loop\r\n") while True: if usb.any(): usb.write("data on serial buffer\r\n") a = usb.read() usb.write(a) b =  for i in a: b.append(i) #usb.write(i) usb.write("data moved from buffer to list\r\n") usb.write(bytes(b)) c = b usb.write(bytes(c)) usb.write("\r\n") d = b[0:c] usb.write("instruction taken from buffer for sorting\r\n") function_sort(d) usb.write("instruction sorted and appropriate action taken\r\n") pyb.LED(2).on() pyb.delay(500) pyb.LED(2).off() pyb.delay(500) pyb.LED(2).on() pyb.delay(500) pyb.LED(2).off() pyb.delay(500) pyb.LED(4).on() pyb.delay(1000) pyb.LED(4).off() pyb.delay(1000)
Code: Select all
#-*-coding; utf-8 -*- """ Created on Tue Sep 8 17:08:30 2020 @author: the script """ import serial import time if 'ser' in locals(): ser.close() del ser ser = serial.Serial(port ='COM42', baudrate=9600, timeout=None) ser.reset_input_buffer() ser.reset_output_buffer() a = [4,1,5,4,7,2,2,5,2,2,3] #the list used to transmit the instructions #message = a message = bytearray(a) print(message) ser.write(message) ser.reset_input_buffer() ser.reset_output_buffer() """ below reads from com port to recive output from pyboard """ y = 0 x =  z =  print("entering while loop") g = 0 while True: waiting = ser.in_waiting if waiting > 0: x=ser.readline() print(x) g = 0 if waiting == 0: g +=1 time.sleep(0.01) if g > 500: break ser.reset_output_buffer() ser.reset_input_buffer() print(ser.name) ser.close()