马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 lixiaofeimvp 于 2022-4-27 17:19 编辑 from concurrent.futures import thread
from operator import truediv
from optparse import Values
import turtle
from cv2 import TERM_CRITERIA_MAX_ITER
import serial
from time import time,sleep
from threading import Thread
class LedControl:
def __init__(self,Com,Baudr):
self.Com=str(Com)
self.Bauds=int(Baudr)
self.ser=serial.Serial()
self.readbufstr=''
def setLightness(self,ch,lihtness):
strs='$3'+str(ch)+'%03X'%int(lihtness)
Valuesx=strs+self.getXorResult(strs)
return self.send(Valuesx)
def getXorResult(self,string):
x=0
for i in string:
x=x^ ord(i)
return '%02X'%x
def port_Open(self):
self.ser.port=self.Com
self.ser.baudrate=self.Bauds
self.ser.bytesize=8
self.ser.stopbits=1
self.ser.parity="N"
if not(self.ser.isOpen()):
try:
self.ser.open()
except IOError:
print ("Error: 串口已占用")
if (self.ser.isOpen()):
return 1
else:
return 0
else:
return 1
def port_Close(self):
self.isOpen =self.ser.isOpen()
if (self.ser.isOpen()):
self.ser.close()
if(self.ser.isOpen()):
print("串口关闭失败!")
else:
print("串口关闭成功!")
def send(self,send_data):
if (self.ser.isOpen()):
self.readbuffThread=Thread(target=self.readbufThreadFuc)
self.readbuffThread.start()
self.ser.write(send_data.encode('utf-8'))
print("发送成功",send_data)
return (("发送成功"+send_data))
else:
print("发送失败",send_data)
return (("发送失败"+send_data))
def readbufThreadFuc(self):
time_start=time.time()
while True:
time_end=time.time()
time_Count=time_end-time_start
print(time_Count)
if time_Count>0.1:
break
if not(self.ser.isOpen()) :
break
data=self.ser.read_all()
if(data==''):
continue
sleep(0.001)
return str(data.decode('utf-16'))
if self.readbuffThread.is_alive():
self.readbuffThread.join();
def set_Sre_Parameter(self,Com,Baudr):
self.Com=str(Com)
self.Bauds=int(Baudr)
def ser_IsOpen(self):
return self.ser.isOpen()
if __name__ == "__main__":
ld=LedControl()
x1=ld.setLightness('1','100')
print(x1)
主程序调用的时候,显示
发生异常: AttributeError
'builtin_function_or_method' object has no attribute 'time'
File "C:\Users\Administrator\Desktop\Python\.vscode\ui\LedControl.py", line 75, in readbufThreadFuc
time_start=time.time()
可是我已经import 了time啊
你是使用 from time import time,sleep 导入的,所以导入的已经是 time 函数了,
直接使用 time() 即可,而且你这个 LedControl 实例化的不对吧,还需要传两个参数
|