2016-08-01 104 views
1

enter image description herePython的串行轨迹的通信与设备

我从一个天才要比男人的工作instructables.com使串口通信设备。

它会测量仓鼠在一天或一个月中跑步的距离。

使用串口电缆的4,6针,如果仓鼠运行,设备可以计数她运行多少次。

当我像下面的Python27运行py文件时,出现了一些错误。 “python hamster-serial.py progress.txt”

我不明白发生了什么事情。 我正在使用windows8和Python2.7版本。

你能检查我的来源吗?

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

你正在得到什么错误? –

回答

0

好,ser = serial.Serial("/dev/ttyS1")是一台Linux机器,在Windows下你需要像ser = serial.Serial("COM1")(你可以查阅一下COM你在设备管理器需要)。

作为一个侧面说明,

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

会吃掉你的CPU。你最好与:

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

哇!难以置信的!刚才,你救了我的生命...在我最重要的时刻!我真的很感激你! –

+0

我想,它成功了!现在我将研究和调查。感谢您的大力帮助! –

+0

>>> 2016-08-02 14:38:34.145000]开始记录。 Traceback(最近一次调用最后一次): 运行脚本中的第326行文件“C:\ Python27 \ Lib \ site \ packages \ pythonwin \ pywin \ framework \ scriptutils.py” exec_codeObject in __main __.__ dict__ 文件“C :\ Python27 \ hamster-serial.py“,第54行,在 speed = toSpeed(句号) 文件”C:\ Python27 \ hamster-serial.py“,第31行,在toSpeed return circ /(seconds/60/60) ZeroDivisionError:浮点除零 –