USB port reset script


Advanced search

Message boards : Number crunching : USB port reset script

Author Message
Dagorath
Avatar
Send message
Joined: 4 Jul 11
Posts: 151
Credit: 42,738
RAC: 0

Message 1010 - Posted: 24 Apr 2012 | 6:57:07 UTC
Last modified: 24 Apr 2012 | 7:01:26 UTC

Here is a Python script named dm.py that monitors the % progress (% complete) of your R@H task. If the task is running (not suspended) and the % progress stops increasing, dm.py plays a sound then attempts to reset the USB port the detector is connected to.

The % progress for my R@H tasks never stops increasing. If it did I could test this script myself. If you want to test it and report what it does or does not do I might be able to improve/fix it.

All platforms need to have Python (the Python interpreter) installed in order to use dm.py. Get it from http://www.python.org. Python 3.2.3 is the latest but I recommend 2.8.x or 2.7.x for compatibility with wxWidgets.

In addition to the basic Python installation you need the pyUSB module for USB port code. Get it for all platforms from http://sourceforge.net/projects/pyusb/. Unzip the download into a temporary directory, read the README and run the setup.py script to install pyUSB. (I tried the pyUSB package in Ubuntu repositories but it gave some trouble so I uninstalled it and installed the package from the above link and the trouble disappeared.)

Windows users will need to install libusb-win from [url=http://sourceforge.net/projects/libusb-# win32/?source=recommended[/url].

Linux users will need to install libusb. The source is available from SourceForge but I am pretty sure most distros have the latest version in repository.

Linux users must install the pygame package and configure the name of the sound file below. I installed the pygame package on Ubuntu with "sudo apt-get install python-pygame".

If you have all of the above installed then copy the script below and paste it into a file named dm.py, save the file wherever you like. If you want a log of the script's activity open the script in a plain text editor and edit the line near the top of the file that says

logg = u'' # all platforms


Insert the path to wherever you want the log file to be saved between the two single-quotes, for example:

logg = u'c:\mydocuments\logs\dm.log' # all platforms


The path must exist which means you must create the path if it does not exist now. If you don't insert a path between the single-quotes the script will not save a log to disk. It will always log to stdout which will likely be the terminal in which you run the script unless redirected.

Linux users must configure the line that says:

sound_file = u'' # Linux only


Insert the path to a sound file between the 2 single-quotes. If you don't insert a path the script will not play a warning sound when the % progress stops. Wav, ogg and several other sound file formats are acceptable and are found in /usr/share/sounds/ on many distros.
Warning: The Python interpreter is very fussy about line indents. It use line indents to determine start/end of compound statements and other things. Yes it's wierd but it removes the need for ; to mark the end of statements and all those brackets other languages can't live without. But if you screw up the indents the script won't run.

The point is this ---> Before you go bashing around on the keyboard with your big thick fingers and screw up the indents, be sure to make a note of how the lines are indented immediately before and after the line you want to edit. Make sure those indents are the same before saving your edit.


Copy all the lines between the start start start start and stop stop stop stop lines but do not include those 2 lines.

start start start start start start start start
import usb from sys import platform from os import walk from os.path import join, isfile, exists from subprocess import check_output from time import sleep, strftime if platform.startswith(u'win'):import _winreg, winsound elif platform.startswith(u'linux'): from pygame import mixer # START CONFIGURE HERE ############################################################################ winks = 150 # all platforms logg = u'dm.log' # all platforms sound_file = u'/usr/share/sounds/KDE-Im-Connection-Lost.ogg' # Linux only # END CONFIGURE ################################################################################### plat = '' ################################################################################################### def log_it(x): if not logg == '': log_file = open(logg, 'a') try: log_file.write(strftime('%c') + ' ' + x + '\n') except: print (strftime('%c') + ' exception in log_it()\n') log_file.close() print(x) ################################################################################################### def search_paths(): global plat # searches the system for paths to BOINC data dir and BOINC binary dir, in the case of Windows the # info is read from the registry, for other OSs the file system is searched plat = platform if plat.startswith(u'win'): plat = u'win' reg_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, u'Software\\Space Sciences Laboratory, U.C. Berkeley\\BOINC Setup\\') boinc_data = _winreg.QueryValueEx(reg_key, u'DATADIR')[0] boinc_binary = _winreg.QueryValueEx(reg_key, u'INSTALLDIR')[0] _winreg.CloseKey(reg_key) elif plat.startswith(u'linux'): plat = u'linux' boinc_data = u'/' elif plat.startswith(u'darwin'): plat = u'darwin' boinc_data = u'/' else: plat = u'dunno' boinc_data = u'/' if boinc_data == u'/': # if / then it's Linux or OS X, find the BOINC binary dir boinc_binary = '' for root, dirs, files in walk(u'/'): for name in files: if name == u'boinccmd': boinc_binary = root break if not boinc_binary == '': break for root, dirs, files in walk(boinc_data): for name in files: if name == u'gui_rpc_auth.cfg': boinc_data = root break if not boinc_data == u'/': break return (boinc_data, boinc_binary) ################################################################################################### def dev_reset(): global plat if plat == u'win': winsound.MessageBeep(winsound.MB_ICONHAND) elif plat == u'linux': if exists(sound_file): mixer.init() alert = mixer.Sound(sound_file) alert.play() for bus in usb.busses(): for dev in bus.devices: if dev.idVendor == u'5824' and dev.idProduct == u'1503': dev.reset() ################################################################################################### frac = 0.0 old_frac = 0.0 b_dirs = search_paths() b_dirs = ('/home/kim/BOINC/', '/home/kim/BOINC/') infile = open(join(b_dirs[1], u'gui_rpc_auth.cfg' ), u'r') passwd = infile.read().strip('\n') infile.close() log_it('') log_it(u'Starting dm.py') #print(b_dirs, passwd) while True: tasks = check_output([join(b_dirs[0], u'boinccmd'), u'--host', u'localhost', u'--passwd', passwd, u'--get_tasks']).split(u'-------')[1:] for task in tasks: if u'http://radioactiveathome.org/boinc/' in task: break task = task.splitlines() old_frac = frac for line in task: if line.startswith(u' state: '): state = line.split(':')[1].strip(' ') elif line.startswith(u' scheduler state: '): sched_state = line.split(':')[1].strip(' ') elif line.startswith(u' fraction done: '): frac = float(line.split(':')[1].strip(' ').strip(' ')) if state == u'2' and sched_state == u'2': if frac == old_frac: log_it(state + u' ' + sched_state + u' ' + str(frac) + u' ' + u'possible disconnect') dev_reset() else: count = 0 log_it(state + ' ' + sched_state + ' ' + str(frac)) else: # not state == '2' or not sched_state == '2': count = 0 log_it(state + ' ' + sched_state + u' task is not running') sleep(winks)

stop stop stop stop stop stop stop stop stop
____________

FogBRD
Send message
Joined: 16 Feb 12
Posts: 22
Credit: 6,008
RAC: 0
Message 1054 - Posted: 30 Apr 2012 | 7:15:46 UTC - in response to Message 1010.

Why so difficult?
Simply make a timer in the dosimeter - if there was no treatment for the usb during 3 measurements - to make reconnection fee.
One disadvantage - Windows plays the sound off / on usb device is connected.
____________

Profile TJM
Project administrator
Project developer
Project tester
Send message
Joined: 16 Apr 11
Posts: 291
Credit: 1,367,404
RAC: 73

Message 1056 - Posted: 30 Apr 2012 | 7:31:46 UTC

That's not so easy, the sensor would have to monitor the connection at hardware level (not the actual data transfer) to avoid constantly reconnecting if connected to USB but without app running.
The main reason why it's not implemented yet is the very limited space on the attiny 4313.

FogBRD
Send message
Joined: 16 Feb 12
Posts: 22
Credit: 6,008
RAC: 0
Message 1060 - Posted: 30 Apr 2012 | 19:13:56 UTC - in response to Message 1056.

I realized - a check on the data and if there was no transfer of the connection to usb again initiated.
places there is still free to 0.5k.

If anyone knows how to diagnose without the usb connection to the data tell you. it is certainly better.
____________

Post to thread

Message boards : Number crunching : USB port reset script


Main page · Your account · Message boards


Copyright © 2024 BOINC@Poland | Open Science for the future