From: Jannik ZANDER Date: Sat, 18 Jun 2016 11:00:19 +0000 (+0200) Subject: Add pyusb to gf.py X-Git-Url: https://git.zndr.dk/?a=commitdiff_plain;h=d21c82d9c7b83a9ae18cbbe103772ae688913e24;p=dotfiles.git Add pyusb to gf.py --- diff --git a/.local/bin/gf.py b/.local/bin/gf.py index 79fcfde..5944122 100755 --- a/.local/bin/gf.py +++ b/.local/bin/gf.py @@ -3,13 +3,15 @@ # section: environment {{{1 ##################################################### import gf_config as gf -#import serial +import usb.core +import usb.util +import serial +from serial.tools import list_ports +import io import fileinput import matplotlib.pyplot as plt -import numpy as pl from datetime import datetime,date,time - # section: debugging {{{1 ###################################################### @@ -34,6 +36,25 @@ def dbg_speed(ids5,val5,ts,sa): speed[ids5[i]-67]=speed[ids5[i]-67]+[val5[i]/254] return td,master,speed +# section: serial_ports {{{1 +###################################################### +def serial_ports(): +# A list contains a tuple with the port number, description and hardware address + ports=list(list_ports.comports()) + # return the port if 'USB' is in the description + result=[] + print("Available ports:") + for p in ports: + print(repr(p.device),repr(p.description),repr(p.serial_number)) + if p.vid==0x0403 or p.vid==None: + try: + s = serial.Serial(p.device) + s.close() + result.append(p) + except (OSError, serial.SerialException): + pass + return result + # section: checkcrc {{{1 ###################################################### def checkcrc(line): @@ -347,10 +368,43 @@ def process_telegram(line): # section: main {{{1 ###################################################### -for line in fileinput.input(): - print("====================================================================") + +lsusb=serial_ports() +ser=serial.Serial() +ser.port=lsusb[0].device +ser.baudrate=9600 +ser.parity=serial.PARITY_NONE +ser.stopbits=serial.STOPBITS_ONE +ser.bytesize=serial.EIGHTBITS +ser.timeout=0 +ser.open() +print("Connected to",repr(ser.portstr)) + +dev=usb.core.find(idVendor=0x0403) +if dev is None: + dev=usb.core.find(idVendor=0x0403) +print(dev) +#print("-----------------------------") +#dev.set_configuration() +#cfg=dev.get_active_configuration() +#print(cfg) + +#io=io.TextIOWrapper(io.BufferedRWPair(ser,ser,1), +# newline='\r', +# line_buffering=True) +# +#line = [] +#while True: +# line=sio.readline() # print(line) - process_telegram(line) +# +ser.close() + +#for line in fileinput.input(): +# print("====================================================================") +## print(line) +# process_telegram(line) +# # section: plot {{{1 ######################################################