python程序运行后提示IOError: [Errno 22] Invalid argument 急啊!!!!
程序如下:importfcntlimportosimporttimefromstructimport*#ConstantsINPUT='0'OUTPUT='1'PWM_F...
程序如下:
import fcntl
import os
import time
from struct import *
# Constants
INPUT = '0'
OUTPUT = '1'
PWM_FREQ = 520
# Pin number
dir_pin = 2
pwm_pin = 3
pwm_value = 255
# File locations
MODE_FILE = '/sys/devices/virtual/misc/gpio/mode/gpio' + str(dir_pin)
PIN_FILE = '/sys/devices/virtual/misc/gpio/pin/gpio' + str(dir_pin)
PWM_FILE = '/dev/pwmtimer'
# Pin mode
os.system('echo ' + OUTPUT + ' > ' + MODE_FILE)
# Configure PWM
with open(PWM_FILE, 'wb') as f:
pwm_struct = pack('iiiI', pwm_pin, 0, 0, PWM_FREQ)
fcntl.ioctl(f, 0xfa, pwm_struct)
# Motor control
def drive_motor(dir, pwm):
# Set direction
os.system('echo ' + str(dir) + ' > ' + PIN_FILE)
# Output PWM to motor
with open(PWM_FILE, 'wb') as f:
pwm_struct = pack('ii', pwm_pin, pwm)
fcntl.ioctl(f, 0x19, pwm_struct)
# Do stuff with the motor
drive_motor(0, 0)
time.sleep(1)
drive_motor(0, 200)
time.sleep(2)
drive_motor(0, 0)
time.sleep(0.5)
drive_motor(1, 255)
time.sleep(2)
drive_motor(0, 0) 展开
import fcntl
import os
import time
from struct import *
# Constants
INPUT = '0'
OUTPUT = '1'
PWM_FREQ = 520
# Pin number
dir_pin = 2
pwm_pin = 3
pwm_value = 255
# File locations
MODE_FILE = '/sys/devices/virtual/misc/gpio/mode/gpio' + str(dir_pin)
PIN_FILE = '/sys/devices/virtual/misc/gpio/pin/gpio' + str(dir_pin)
PWM_FILE = '/dev/pwmtimer'
# Pin mode
os.system('echo ' + OUTPUT + ' > ' + MODE_FILE)
# Configure PWM
with open(PWM_FILE, 'wb') as f:
pwm_struct = pack('iiiI', pwm_pin, 0, 0, PWM_FREQ)
fcntl.ioctl(f, 0xfa, pwm_struct)
# Motor control
def drive_motor(dir, pwm):
# Set direction
os.system('echo ' + str(dir) + ' > ' + PIN_FILE)
# Output PWM to motor
with open(PWM_FILE, 'wb') as f:
pwm_struct = pack('ii', pwm_pin, pwm)
fcntl.ioctl(f, 0x19, pwm_struct)
# Do stuff with the motor
drive_motor(0, 0)
time.sleep(1)
drive_motor(0, 200)
time.sleep(2)
drive_motor(0, 0)
time.sleep(0.5)
drive_motor(1, 255)
time.sleep(2)
drive_motor(0, 0) 展开
3个回答
展开全部
代码中第二个open()函数调用的地方,语句pwm_struct = pack('ii', pwm_pin, pwm)中pack函数最后一个参数pwm哪来的?你自己找找,是不是写错了
参数错误了,文件操作肯定失败,所以提示I\O错误
参数错误了,文件操作肯定失败,所以提示I\O错误
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询