USB interface to Python program

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
labRat
Posts: 4
Joined: Wed Sep 20, 2017 6:16 pm

USB interface to Python program

Post by labRat » Thu Sep 21, 2017 1:56 pm

I have a Micro Python board being used to operate multiple peripheral devices on a product test card via a set of methods in main.py. I would like to automate it by replacing the puTTY console with output from a remote PC running Python via the USB interface.
Remote PC code example:

import serial
import time
PyBoard_port = 'COM3'

class microPy(object):
'''A class to control the PyBoard micro controller used for programming
the DAC, relay drivers and reading data from the ADC on the LTM9057
automated test card.'''

def __init__(self, port=PyBoard_port, baudrate=115200, timeout=0.5):
# Establish the serial connection
self.ser = serial.Serial(port=port, baudrate=baudrate, timeout=timeout)
time.sleep(2)

# New universal function using dictionaries defined in the relay_connections file
def writeCommand(self,command):
self.ser.write(command)
return()

What do I need to make this work?

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

Re: USB interface to Python program

Post by dhylands » Thu Sep 21, 2017 3:22 pm

You need to write some MicroPython code to read/parse the commands coming from the host.

You can either use a read UART, along with a USB-to-TTL-serial adapter, or you can read from the USB-serial.
The docs for the USB serial interface are here:
http://docs.micropython.org/en/latest/p ... B_VCP.html
and the UART here:
http://docs.micropython.org/en/latest/p ... .UART.html

If you're sending binary data over the USB serial link, then you'll want to make sure you call setinterrupt(-1) to disable the Control-C parsing.

User avatar
labRat
Posts: 4
Joined: Wed Sep 20, 2017 6:16 pm

Re: USB interface to Python program

Post by labRat » Fri Sep 22, 2017 6:27 pm

Not having luck with that, however I was pointed to something on github that looks exactly like what I want:

https://github.com/micropython/micropyt ... pyboard.py

However when I try to run it I get:

Traceback (most recent call last):
File "C:\Users\markmi071556\eclipse-workspace\LTM9057_Auto_test\Run_PyBoard.py", line 6, in <module>
pyb = pyboard.Pyboard('192.168.1.1')
File "C:\Users\markmi071556\eclipse-workspace\LTM9057_Auto_test\pyboard.py", line 195, in __init__
self.serial = TelnetToSerial(device, user, password, read_timeout=10)
File "C:\Users\markmi071556\eclipse-workspace\LTM9057_Auto_test\pyboard.py", line 48, in __init__
self.tn = telnetlib.Telnet(ip, timeout=15)
File "C:\Python27\lib\telnetlib.py", line 211, in __init__
self.open(host, port, timeout)
File "C:\Python27\lib\telnetlib.py", line 227, in open
self.sock = socket.create_connection((host, port), timeout)
File "C:\Python27\lib\socket.py", line 575, in create_connection
raise err
socket.timeout: timed out

I've pinged ip 192.168.1.1 and it responds. Thanks for any help.

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

Re: USB interface to Python program

Post by dhylands » Fri Sep 22, 2017 9:39 pm

What MicroPython board are you using?

My understanding is that the WiPy board runs a telnet server. I didn't think that any of the others did.

How are you invoking pyboard.py?

User avatar
labRat
Posts: 4
Joined: Wed Sep 20, 2017 6:16 pm

Re: USB interface to Python program

Post by labRat » Sat Sep 23, 2017 3:07 pm

The Micro Python board I'm using has PYBv1.1 printed on it's underside. Just trying to get the sample code running:

import pyboard

# pyb = pyboard.Pyboard('/dev/ttyACM0')
pyb = pyboard.Pyboard('192.168.1.1')

pyb.enter_raw_repl()
pyb.exec_('pyb.LED(1).on()')
pyb.exit_raw_repl()


Both pyboard.Pyboard('/dev/ttyACM0') and pyb = pyboard.Pyboard('192.168.1.1') fail similarly.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: USB interface to Python program

Post by pythoncoder » Sun Sep 24, 2017 7:29 am

It works here with a USB connected Pyboard under Linux using

Code: Select all

pyb = pyboard.Pyboard('/dev/ttyACM0')
However it seems you're running Windows. Device names are different under Windows and I'm sure the key is to use the correct serial port name. The docs here http://docs.micropython.org/en/latest/p ... /repl.html indicate how to get the Pyboard running under Windows, and how to determine the name of the serial port on your PC.

It's not evident how

Code: Select all

pyb = pyboard.Pyboard('192.168.1.1')

could be expected to work. How is your Pyboard connected to the network?
Peter Hinch
Index to my micropython libraries.

User avatar
labRat
Posts: 4
Joined: Wed Sep 20, 2017 6:16 pm

Re: USB interface to Python program

Post by labRat » Mon Sep 25, 2017 11:17 am

It's working now. I didn't recognize that the code example was written for Linux, (I'm running on Windows 10). I changed '/dev/ttyACM0' to "COM3' and all is well.
Thanks again for your help.

Post Reply