Fix Python2/3 Errors

This commit is contained in:
Kwabena W. Agyeman 2020-05-12 12:24:13 -07:00
parent 9383e24b66
commit b5b6fec019
5 changed files with 22 additions and 4 deletions

View File

@ -10,6 +10,10 @@ The OpenMV Cam `rpc` library on the computer only depends on [pyserial](https://
pip install pyserial
However, the examples depend on [pygame](https://www.pygame.org/news) so you need to install pygame too:
pip install pygame
Because the interface library is implemented in pure python with no external dependencies it works on Windows, Mac, and Linux.
# How to use the Library

View File

@ -60,7 +60,9 @@ class rpc:
def _set_packet(self, magic_value, payload=bytes()): # private
new_payload = bytearray(len(payload) + 4)
new_payload[:2] = struct.pack("<H", magic_value)
new_payload[2:-2] = payload
# Fix Python 3.x.
try: new_payload[2:-2] = payload
except TypeError: new_payload[2:-2] = payload.encode()
new_payload[-2:] = struct.pack("<H", self.__crc_16(new_payload, len(payload) + 2))
return new_payload

View File

@ -6,6 +6,10 @@
import io, pygame, rpc, serial, serial.tools.list_ports, socket, struct, sys
# Fix Python 2.x.
try: input = raw_input
except NameError: pass
# The RPC library above is installed on your OpenMV Cam and provides mutliple classes for
# allowing your OpenMV Cam to control over USB or WIFI.
@ -22,7 +26,7 @@ for port, desc, hwid in serial.tools.list_ports.comports():
print("{} : {} [{}]".format(port, desc, hwid))
sys.stdout.write("\nPlease enter a port name: ")
sys.stdout.flush()
interface = rpc.rpc_usb_vcp_master(port=raw_input())
interface = rpc.rpc_usb_vcp_master(port=input())
print("")
sys.stdout.flush()

View File

@ -6,6 +6,10 @@
import io, pygame, rpc, serial, serial.tools.list_ports, socket, sys
# Fix Python 2.x.
try: input = raw_input
except NameError: pass
# The RPC library above is installed on your OpenMV Cam and provides mutliple classes for
# allowing your OpenMV Cam to control over USB or WIFI.
@ -22,7 +26,7 @@ for port, desc, hwid in serial.tools.list_ports.comports():
print("{} : {} [{}]".format(port, desc, hwid))
sys.stdout.write("\nPlease enter a port name: ")
sys.stdout.flush()
interface = rpc.rpc_usb_vcp_master(port=raw_input())
interface = rpc.rpc_usb_vcp_master(port=input())
print("")
sys.stdout.flush()

View File

@ -7,6 +7,10 @@
import json, rpc, serial, serial.tools.list_ports, struct, sys
from datetime import datetime
# Fix Python 2.x.
try: input = raw_input
except NameError: pass
##############################################################
# Choose the interface you wish to control an OpenMV Cam over.
##############################################################
@ -20,7 +24,7 @@ for port, desc, hwid in serial.tools.list_ports.comports():
print("{} : {} [{}]".format(port, desc, hwid))
sys.stdout.write("\nPlease enter a port name: ")
sys.stdout.flush()
interface = rpc.rpc_usb_vcp_master(port=raw_input())
interface = rpc.rpc_usb_vcp_master(port=input())
print("")
sys.stdout.flush()