简体   繁体   English

Python与设备的串行通信

[英]Python Serial communition with device

enter image description here 在此输入图像描述

I am making serial port communication device from a genious guy's work instructables.com. 我正在从一个天才的家伙instructables.com制作串口通信设备。

It will measure the distance of the hamster's running in a day or month. 它将测量仓鼠在一天或一个月内的跑步距离。

Using 4, 6 pin of the serial port cable, if the hamster runs, the device can count the numbers how many time did she run. 使用4针6针串口电缆,如果仓鼠运行,设备可以计算她运行的时间。

When I run the py file with Python27 like below, some errors occure. 当我像下面的Python27一样运行py文件时,会出现一些错误。 "python hamster-serial.py progress.txt" “python hamster-serial.py progress.txt”

I cannot understand what's going on. 我无法理解发生了什么。 I am using windows8 and Python2.7 version. 我使用的是windows8和Python2.7版本。

Could you check my source, please? 你能查一下我的来源吗?

 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() 

Well, ser = serial.Serial("/dev/ttyS1") is for a linux machine, on windows you'll need something like ser = serial.Serial("COM1") (you can check what COM do you need in the device manager). 好吧, ser = serial.Serial("/dev/ttyS1")用于linux机器,在Windows上你需要像ser = serial.Serial("COM1") (你可以查看你需要什么COM)装置经理)。

As a side note, 作为旁注,

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

Will eat you CPU. 会吃掉你的CPU。 You are better of with: 你更善于:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM