Guten Abend liebe Gemeinde,
zugegeben, Python ist noch relativ neu für mich - allerdings ein fertiges script ausführen, das bekomme ich im Normalfall hin. Auch in diesem Fall, jedoch enthält das Skript scheinbar einen Fehler.
kann mir vielleicht jemand von euch helfen? Ich habe mir ein 8 Kanal USB Relais gekauft und das soweit auch programmiert, sodass ich es über eine Webseite ein und ausschalten kann. Das funktioniert auch alles recht gut. Allerdings die Rückmeldung, welches Relais nun eingeschalten ist, funktioniert noch nicht so wirklich. Für die Relais 1-4 einwandfrei, dann bei 5-8 ist die Rückmeldung jedoch „verschoben“. Frage ich Relais 8 ab, bekomme ich die Rückmeldung für Relais 7. 8 bleibt unverändert. Frage ich 7 ab, meldet sich 6. Bei 6, die 5. Und wenn ich Relais 5 abfrage, bekomme ich keine Antwort angezeigt…
Abfrage Relais 8 Status, dann Relais 8 einschalten und dann nochmal Relais 8 abfragen. Für Relais 7, welches vorher aus war, ist der Status nun ON, obwohl das Relais NICHT eingeschalten ist.
ZitatAlles anzeigenpi@raspberrypi:~ $ sudo /home/pi/drcontrol/drcontrol.py -d AI048ED3 -r 8 -c state -v
DRControl 0.12
Device: AI048ED3
Send command: Relay 8 (0x40) to STATE
Relay 8 state: OFF (0)
pi@raspberrypi:~ $ sudo /home/pi/drcontrol/drcontrol.py -d AI048ED3 -r 8 -c on -v
DRControl 0.12
Device: AI048ED3
Send command: Relay 8 (0x40) to ON
Relay 8 to ON
pi@raspberrypi:~ $ sudo /home/pi/drcontrol/drcontrol.py -d AI048ED3 -r 8 -c state -v
DRControl 0.12
Device: AI048ED3
Send command: Relay 8 (0x40) to STATE
Relay 8 state: OFF (0)
pi@raspberrypi:~ $ sudo /home/pi/drcontrol/drcontrol.py -d AI048ED3 -r 7 -c state -v
DRControl 0.12
Device: AI048ED3
Send command: Relay 7 (0x10) to STATE
Relay 7 state: ON (64)
pi@raspberrypi:~ $
Die originale drcontrol.py wie ich sie heruntergeladen habe (http://drcontrol.googlecode.com
#!/usr/bin/python
# coding=UTF-8
# ----------------------------------------------------------------------------
#
# DRCONTROL.PY
#
# Copyright (C) 2012 Sebastian Sjoholm, sebastian.sjoholm@gmail.com
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Version history can be found at
# http://code.google.com/p/drcontrol/wiki/VersionHistory
#
# $Rev: 53 $
# $Date: 2013-01-03 18:13:01 +0100 (Thu, 03 Jan 2013) $
#
# ----------------------------------------------------------------------------
from optparse import OptionParser
from pylibftdi import Driver
from pylibftdi import BitBangDevice
from ctypes.util import find_library
import sys
import time
# ----------------------------------------------------------------------------
# VARIABLE CLASSS
# ----------------------------------------------------------------------------
class app_data:
def __init__(
self,
name = "DRControl",
version = "0.12",
date = "$Date: 2013-01-03 18:13:01 +0100 (Thu, 03 Jan 2013) $",
rev = "$Rev: 53 $",
author = "Sebastian Sjoholm"
):
self.name = name
self.version = version
self.build = date
self.rev = rev
self.author = author
class cmdarg_data:
def __init__(
self,
device = "",
relay = "",
command = "",
verbose = False
):
self.device = device
self.relay = relay
self.command = command
self.verbose = verbose
class relay_data(dict):
address = {
"1":"2",
"2":"8",
"3":"20",
"4":"80",
"5":"1",
"6":"4",
"7":"10",
"8":"40",
"all":"FF"
}
def __getitem__(self, key): return self[key]
def keys(self): return self.keys()
# ----------------------------------------------------------------------------
# testBit() returns a nonzero result, 2**offset, if the bit at 'offset' is one.
# http://wiki.python.org/moin/BitManipulation
# ----------------------------------------------------------------------------
def testBit(int_type, offset):
mask = 1 << offset
return(int_type & mask)
def get_relay_state( data, relay ):
if relay == "1":
return testBit(data, 1)
if relay == "2":
return testBit(data, 3)
if relay == "3":
return testBit(data, 5)
if relay == "4":
return testBit(data, 7)
if relay == "5":
return testBit(data, 2)
if relay == "6":
return testBit(data, 4)
if relay == "7":
return testBit(data, 6)
if relay == "8":
return testBit(data, 8)
# ----------------------------------------------------------------------------
# LIST_DEVICES()
#
# Routine modified from the original pylibftdi example by Ben Bass
# ----------------------------------------------------------------------------
def list_devices():
print "Vendor\t\tProduct\t\t\tSerial"
dev_list = []
for device in Driver().list_devices():
device = map(lambda x: x.decode('latin1'), device)
vendor, product, serial = device
print "%s\t\t%s\t\t%s" % (vendor, product, serial)
# ----------------------------------------------------------------------------
# SET_RELAY()
#
# Set specified relay to chosen state
# ----------------------------------------------------------------------------
def set_relay():
if cmdarg.verbose:
print "Device:\t\t" + cmdarg.device
print "Send command:\tRelay " + cmdarg.relay + " (0x" + relay.address[cmdarg.relay] + ") to " + cmdarg.command.upper()
try:
with BitBangDevice(cmdarg.device) as bb:
# Action towards specific relay
if cmdarg.relay.isdigit():
if int(cmdarg.relay) >= 1 and int(cmdarg.relay) <= 8:
# Turn relay ON
if cmdarg.command == "on":
if cmdarg.verbose:
print "Relay " + str(cmdarg.relay) + " to ON"
bb.port |= int(relay.address[cmdarg.relay], 16)
# Turn relay OFF
elif cmdarg.command == "off":
if cmdarg.verbose:
print "Relay " + str(cmdarg.relay) + " to OFF"
bb.port &= ~int(relay.address[cmdarg.relay], 16)
# Print relay status
elif cmdarg.command == "state":
state = get_relay_state( bb.port, cmdarg.relay )
if state == 0:
if cmdarg.verbose:
print "Relay " + cmdarg.relay + " state:\tOFF (" + str(state) + ")"
else:
print "OFF"
else:
if cmdarg.verbose:
print "Relay " + cmdarg.relay + " state:\tON (" + str(state) + ")"
else:
print "ON"
# Action towards all relays
elif cmdarg.relay == "all":
if cmdarg.command == "on":
if cmdarg.verbose:
print "Relay " + str(cmdarg.relay) + " to ON"
bb.port |= int(relay.address[cmdarg.relay], 16)
elif cmdarg.command == "off":
if cmdarg.verbose:
print "Relay " + str(cmdarg.relay) + " to OFF"
bb.port &= ~int(relay.address[cmdarg.relay], 16)
elif cmdarg.command == "state":
for i in range(1,8):
state = get_relay_state( bb.port, str(i) )
if state == 0:
if cmdarg.verbose:
print "Relay " + str(i) + " state:\tOFF (" + str(state) + ")"
else:
print "OFF"
else:
if cmdarg.verbose:
print "Relay " + str(i) + " state:\tON (" + str(state) + ")"
else:
print "ON"
else:
print "Error: Unknown command"
else:
print "Error: Unknown relay number"
sys.exit(1)
except Exception, err:
print "Error: " + str(err)
sys.exit(1)
def check():
# Check python version
if sys.hexversion < 0x02060000:
print "Error: Your Python need to be 2.6 or newer"
sys.exit(1)
# Check availability on library, this check is also done in pylibftdi
ftdi_lib = find_library('ftdi')
if ftdi_lib is None:
print "Error: The pylibftdi library not found"
sys.exit(1)
if __name__ == '__main__':
# Init objects
cmdarg = cmdarg_data()
relay = relay_data()
app = app_data()
# Do system check
check()
parser = OptionParser()
parser.add_option("-d", "--device", action="store", type="string", dest="device", help="The device serial, example A6VV5PHY")
parser.add_option("-l", "--list", action="store_true", dest="list", default=False, help="List all devices")
parser.add_option("-r", "--relay", action="store", type="string", dest="relay", help="Relay to command by number: 1...8 or all")
parser.add_option("-c", "--command", action="store", type="string", dest="command", help="State: on, off, state")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="Verbose, print all info on screen")
(options, args) = parser.parse_args()
if options.verbose:
cmdarg.verbose = options.verbose
print app.name + " " + app.version
else:
cmdarg.verbose = False
if options.list:
list_devices()
sys.exit(0)
if options.relay or options.command:
if not options.device:
print "Error: Device missing"
if options.device:
if not options.relay:
print "Error: Need to state which relay"
sys.exit(1)
if not options.command:
print "Error: Need to specify which relay state"
sys.exit(1)
cmdarg.device = options.device
cmdarg.relay = options.relay.lower()
cmdarg.command = options.command.lower()
set_relay()
sys.exit(0)
Alles anzeigen
Hat zwar nichts mit dem Problem direkt zu tun, aber hier ist es eben aufgefallen, da ich mir jede minute den Relaisstatus auf den Monitor ausgeben lasse (in dem Fall gibt es zwei Bilder, in rot: off.jpg und in grün: on.jpg). Dies wird in einer Tabelle auf einer Webseite angezeigt, nicht mehr und nicht weniger. Wie gesagt 1-4 funktionieren einwandfrei, 5-8 sind verschoben und ich weiß nicht, an welcher Stelle dieser Fehler passiert...
#!/bin/bash
DateString=$(date +"%Y%m%d")
var1=$(/home/pi/drcontrol/drcontrol.py -d AI048ED3 -r 1 -c state)
var2=$(/home/pi/drcontrol/drcontrol.py -d AI048ED3 -r 2 -c state)
var3=$(/home/pi/drcontrol/drcontrol.py -d AI048ED3 -r 3 -c state)
var4=$(/home/pi/drcontrol/drcontrol.py -d AI048ED3 -r 4 -c state)
var5=$(/home/pi/drcontrol/drcontrol.py -d AI048ED3 -r 5 -c state)
var6=$(/home/pi/drcontrol/drcontrol.py -d AI048ED3 -r 6 -c state)
var7=$(/home/pi/drcontrol/drcontrol.py -d AI048ED3 -r 7 -c state)
var8=$(/home/pi/drcontrol/drcontrol.py -d AI048ED3 -r 8 -c state)
echo $var1
echo $var2
echo $var3
echo $var4
echo $var5
echo $var6
echo $var7
echo $var8
cp $var1.jpg rs1.jpg
cp $var2.jpg rs2.jpg
cp $var3.jpg rs3.jpg
cp $var4.jpg rs4.jpg
cp $var5.jpg rs5.jpg
cp $var6.jpg rs6.jpg
cp $var7.jpg rs7.jpg
cp $var8.jpg rs8.jpg
Alles anzeigen
Ich hoffe, mir kann hier jemand weiterhelfen. Vielen Dank schonmal,
Felix