2016-08-01 5 views
1

enter image description herePython Serien communitionwith Gerät

I serielles Schnittstelle Kommunikationsgerät aus einem genialen Kerl Arbeit instructables.com mache.

Es wird die Entfernung des Hamsters in einem Tag oder Monat messen.

Mit 4, 6 Pin des seriellen Anschlusskabels, wenn der Hamster läuft, kann das Gerät die Zahlen zählen, wie viel Zeit sie ausgeführt hat.

Wenn ich die py-Datei mit Python27 wie unten ausgeführt, einige Fehler auftreten. "python hamster-serial.py progress.txt"

Ich kann nicht verstehen, was vor sich geht. Ich verwende Windows8 und Python2.7 Version.

Könnten Sie bitte meine Quelle überprüfen?

import datetime 
 
import serial 
 
import sys 
 

 
# Check for commandline argument. The first argument is the the name of the program. 
 
if len(sys.argv) < 2: 
 
    print "Usage: python %s [Out File]" % sys.argv[0] 
 
    exit() 
 

 
# Open the serial port we'll use the pins on and the file we'll write to. 
 
ser = serial.Serial("/dev/ttyS1") 
 

 
# Open the file we're going to write the results to. 
 
f = open(sys.argv[1], 'a') 
 

 
# Bring DTR to 1. This will be shorted to DSR when the switch is activated as the wheel turns. 
 
ser.setDTR(1) 
 

 
# The circumferance of the wheel. 
 
circ = 0.000396 # miles 
 
# Total distance traveled in this run of the program. 
 
distance = 0.0 
 

 
print "%s] Starting logging." % datetime.datetime.now() 
 
start = datetime.datetime.now() 
 

 
# This function a period of the wheel to a speed of the hamster. 
 
def toSpeed(period): 
 
    global circ 
 
    seconds = period.days * 24 * 60 * 60 + period.seconds + period.microseconds/1000000. 
 
    return circ/(seconds/60./60.) 
 
    
 
# Waits for the DSR pin on the serial port to turn off. This indicates that the 
 
# switch has turned off and the magnet is no longer over the switch. 
 
def waitForPinOff(): 
 
    while ser.getDSR() == 1: 
 
    1 # Don't do anything while we wait. 
 

 
# Waits for the DSR pin on the serial port to turn on. This indicates that the 
 
# switch has turned on and the magnet is current over the switch. 
 
def waitForPinOn(): 
 
    while ser.getDSR() == 0: 
 
    1 # Don't do anything while we wait. 
 

 
# The main loop of the program. 
 
while 1: 
 
    waitForPinOn() 
 
    
 
    # Calculate the speed. 
 
    end = datetime.datetime.now() 
 
    period = end - start 
 
    start = end 
 
    speed = toSpeed(period) 
 
    # Increment the distance. 
 
    distance = distance + circ 
 
    
 
    waitForPinOff() 
 
    
 
    # We'll calculate the time the switch was held on too so but this isn't too useful. 
 
    hold = datetime.datetime.now() - start 
 
    
 
    # If the switch bounces or the hamster doesn't make a full revolution then 
 
    # it might seem like the hamster is running really fast. If the speed is 
 
    # more than 4 mph then ignore it, because the hamster can't run that fast. 
 
    if speed < 4.0: 
 
    # Print out our speed and distance for this session. 
 
    print "%s] Distance: %.4f miles Speed: %.2f mph" % (datetime.datetime.now(), distance, speed) 
 
     
 
    # Log it to and flush the file so it actually gets written. 
 
    f.write("%s\t%.2f\n" % (datetime.datetime.now().strftime("%D %T"), speed)) 
 
    f.flush() 
 

+0

Was ist der Fehler, den Sie bekommen? –

Antwort

0

Nun, ser = serial.Serial("/dev/ttyS1") ist für eine Linux-Maschine, auf Fenster, die Sie so etwas wie ser = serial.Serial("COM1") brauchen werden (können Sie überprüfen, welche COM tun Sie im Gerätemanager benötigen).

Als Randbemerkung,

def waitForPinOff(): 
    while ser.getDSR() == 1: 
     1 # Don't do anything while we wait. 

Wollen Sie CPU essen. Sie sind besser dran mit:

def waitForPinOff(): 
    while ser.getDSR() == 1: 
     time.sleep(1) # Don't do anything while we wait. 
+0

Wow !! Nicht zu fassen! Gerade jetzt, rettest du mein Leben ... in meinem wichtigsten Moment! Ich bin wirklich dankbar für dich! –

+0

Ich denke, es ist gelungen! Jetzt werde ich recherchieren und untersuchen. Danke für deine großartige Hilfe! –

+0

>>> 2016-08-02 14: 38: 34.145000] Starten der Protokollierung. Traceback (zuletzt letzten Aufruf): Datei "C: \ Python27 \ Lib \ Site-Pakete \ Pythonwin \ Pywin \ Framework \ scriptutils.py", Zeile 326, in RunScript Exec-Codeobjekt in __main __.__ dict__ Datei "C : \ Python27 \ hamster-serial.py ", Zeile 54, in Geschwindigkeit = toSpeed ​​(Punkt) Datei" C: \ Python27 \ hamster-serial.py ", Zeile 31, in toSpeed ​​ return circ/(Sekunden/60./60.) ZeroDivisionError: float division by zero –

Verwandte Themen