From fd461c3446caadaf8d6bdb733559fed4bd2deb3c Mon Sep 17 00:00:00 2001 From: Jannik ZANDER Date: Mon, 20 Jun 2016 12:32:29 +0200 Subject: [PATCH] Move gf.py --- .vimrc | 2 +- {.local/bin => work/gf}/gf.py | 116 ++++++++++++++++----------- {.local/bin => work/gf}/gf_config.py | 0 3 files changed, 70 insertions(+), 48 deletions(-) rename {.local/bin => work/gf}/gf.py (89%) mode change 100755 => 100644 rename {.local/bin => work/gf}/gf_config.py (100%) diff --git a/.vimrc b/.vimrc index f2c84f2..75598d1 100644 --- a/.vimrc +++ b/.vimrc @@ -20,7 +20,7 @@ set runtimepath+=~/.cache/vim " section: plugins {{{1 " --------------------- -call plug#begin('~/.cache/vim/plugged') +silent call plug#begin('~/.cache/vim/plugged') "Plug 'tpope/vim-sensible' "Plug 'tpope/vim-ragtag' diff --git a/.local/bin/gf.py b/work/gf/gf.py old mode 100755 new mode 100644 similarity index 89% rename from .local/bin/gf.py rename to work/gf/gf.py index 5944122..8e82404 --- a/.local/bin/gf.py +++ b/work/gf/gf.py @@ -1,16 +1,17 @@ #!/usr/bin/env python3 # section: environment {{{1 -##################################################### +######### +############################################ import gf_config as gf -import usb.core -import usb.util import serial from serial.tools import list_ports -import io import fileinput import matplotlib.pyplot as plt -from datetime import datetime,date,time +import datetime +import time +#import ctype +#import logging # section: debugging {{{1 ###################################################### @@ -39,14 +40,13 @@ def dbg_speed(ids5,val5,ts,sa): # 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 + # return the port if FTDI vendor 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: + print(repr(p.device),repr(p.description)) + if p.vid==0x403: # FTDI vendor try: s = serial.Serial(p.device) s.close() @@ -55,6 +55,38 @@ def serial_ports(): pass return result +# section: serial_open {{{1 +###################################################### +def serial_open( vid, pid ): +#vid=0x403 +#pid=0x6001 +#pid=0x6015 + ctx = usb1.USBContext() + logging.basicConfig(level=logging.DEBUG) + + for device in ctx.getDeviceList(skip_on_error=True): + print(device) + if device.getVendorID() == vid and device.getProductID() == pid: + device = device + break + else: + raise Exception( "ERROR: USB device %04x:%04x not found." % (vid, pid)) + + logging.info( "Found device : ID %04x:%04x -> %s Address %d" % (device.getVendorID(), device.getProductID(), '->'.join(str(x) for x in ['Bus %03i' % (device.getBusNumber(), )] + device.getPortNumberList()), device.getDeviceAddress() )) + + handle = device.open() + print(handle) + logging.info( "Device opened, got handle" ) + + endpoint = 0x85 + length = 65536 + timeout = 100 # ms + print("enter loop") + while True: + print("reading") + data = handle.bulkRead ( endpoint, length, timeout ) + # do something with data + # section: checkcrc {{{1 ###################################################### def checkcrc(line): @@ -64,6 +96,21 @@ def checkcrc(line): # print("Ok:",ok) return ok +# section: checkglowpan {{{1 +###################################################### +def checkglowpan(line): + isgenidata=False + line=line[32:] + #print(repr(line)) + mac_hi,line=readbyte(line) + mac_lo,line=readbyte(line) + #print(mac_hi,mac_lo) + #print(repr(line)) + if mac_hi==8 and mac_lo==4: + isgenidata=True + #print(repr(line)) + #print(isgenidata) + return isgenidata,line # section: readtimestamp {{{1 ###################################################### def readtimestamp(line): @@ -90,21 +137,6 @@ def readtimestamp(line): print(ts) return ts,line -# section: checkglowpan {{{1 -###################################################### -def checkglowpan(line): - isgenidata=False - line=line[32:] - #print(repr(line)) - mac_hi,line=readbyte(line) - mac_lo,line=readbyte(line) - #print(mac_hi,mac_lo) - #print(repr(line)) - if mac_hi==8 and mac_lo==4: - isgenidata=True - #print(repr(line)) - #print(isgenidata) - return isgenidata,line # section: readbyte {{{1 ###################################################### @@ -368,45 +400,35 @@ def process_telegram(line): # section: main {{{1 ###################################################### - -lsusb=serial_ports() +# section: serial {{{2 +lsser=serial_ports() ser=serial.Serial() -ser.port=lsusb[0].device +ser.port=lsser[0].device ser.baudrate=9600 ser.parity=serial.PARITY_NONE ser.stopbits=serial.STOPBITS_ONE ser.bytesize=serial.EIGHTBITS -ser.timeout=0 +ser.timeout=40 # msec 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) -# +while True: + print("====================================================================") + line=ser.readline() + print(line) + process_telegram(line) ser.close() + + #for line in fileinput.input(): # print("====================================================================") ## print(line) # process_telegram(line) # -# section: plot {{{1 + +# section: plot {{{2 ###################################################### # Calculate timedelta diff --git a/.local/bin/gf_config.py b/work/gf/gf_config.py similarity index 100% rename from .local/bin/gf_config.py rename to work/gf/gf_config.py -- 2.43.0