Is the Number 3 a Special Command/Escape Character/Reserved Byte When Transmitted via USB-VCP()

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
oli43
Posts: 2
Joined: Tue Sep 29, 2020 3:42 pm

Is the Number 3 a Special Command/Escape Character/Reserved Byte When Transmitted via USB-VCP()

Post by oli43 » Wed Sep 30, 2020 11:46 am

Overall Project Aim: PyBoard to interpret and act upon instructions received from PC via the USB virtual com port.
  • PyBoard V1.1
    uPython V1.2
    Python V3.8
While testing the USB_VCP() comms between the PyBoard and PC I encountered an unexpected behaviour on the pyboard. This behaviour was triggered when a number 3 (int) was present in the bytearray transmitted to the PyBoard. Though the behaviour of the pyboard varies depending on the position of the 3 in the bytearray it usually results in a keyboard interrupt triggering which affects the completion of the main script.

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[0]
	if z[0] == 1:
		flash_LED(z)
	elif z[0] == 2:
		print_string(z)
	else:
		usb.write("instruction not identified\r\n")

def flash_LED(z):
	count = z[1]
	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] == 1:
		usb.write("string 1\r\n")
	elif z[1] == 2:
		usb.write("string 2\r\n")
	elif z[1] == 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[0]
		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)
Python Code Running on PC

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()

User avatar
dhylands
Posts: 3592
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Is the Number 3 a Special Command/Escape Character/Reserved Byte When Transmitted via USB-VCP()

Post by dhylands » Wed Sep 30, 2020 8:20 pm

Binary 0x03 is the ASCII code for Control-C.

If you want to send that over USB-VCP and not trigger an interrupt then you need to call set_interrupt with a -1:
http://docs.micropython.org/en/latest/l ... tinterrupt

oli43
Posts: 2
Joined: Tue Sep 29, 2020 3:42 pm

Re: Is the Number 3 a Special Command/Escape Character/Reserved Byte When Transmitted via USB-VCP()

Post by oli43 » Thu Oct 01, 2020 9:41 am

Thank you for responding to my post, as I expected the solution to this problem was sat in plain sight.

Post Reply