赞
踩
安装驱动https://wenku.baidu.com/view/25b1e273ba1aa8114431d9e1.html
具体的实物图片:https://bbs.elecfans.com/jishu_1589521_1_1.html
python代码:https://blog.csdn.net/qq_41204464/article/details/89257763
import serial import serial.tools.list_ports size = 6 class Communication(): #初始化 def __init__(self,com,bps,timeout): self.port = com self.bps = bps self.timeout =timeout global size global Ret try: # 打开串口,并得到串口对象 self.main_engine= serial.Serial(self.port,self.bps,timeout=self.timeout) # 判断是否打开成功 if (self.main_engine.is_open): Ret = True except Exception as e: print("---异常---:", e) # 打印设备基本信息 def Print_Name(self): print(self.main_engine.name) #设备名字 print(self.main_engine.port)#读或者写端口 print(self.main_engine.baudrate)#波特率 print(self.main_engine.bytesize)#字节大小 print(self.main_engine.parity)#校验位 print(self.main_engine.stopbits)#停止位 print(self.main_engine.timeout)#读超时设置 print(self.main_engine.writeTimeout)#写超时 print(self.main_engine.xonxoff)#软件流控 print(self.main_engine.rtscts)#软件流控 print(self.main_engine.dsrdtr)#硬件流控 print(self.main_engine.interCharTimeout)#字符间隔超时 #打开串口 def Open_Engine(self): self.main_engine.open() #关闭串口 def Close_Engine(self): self.main_engine.close() print(self.main_engine.is_open) # 检验串口是否打开 # 打印可用串口列表 @staticmethod def Print_Used_Com(): port_list = list(serial.tools.list_ports.comports()) print(port_list) #接收指定大小的数据 #从串口读size个字节。如果指定超时,则可能在超时后返回较少的字节;如果没有指定超时,则会一直等到收完指定的字节数。 def Read_Size(self,size): return self.main_engine.read(size=size) #接收一行数据 # 使用readline()时应该注意:打开串口时应该指定超时,否则如果串口没有收到新行,则会一直等待。 # 如果没有超时,readline会报异常。 def Read_Line(self): return self.main_engine.readline() #发数据 def Send_data(self,data): self.main_engine.write(data) #更多示例 # self.main_engine.write(chr(0x06).encode("utf-8")) # 十六制发送一个数据 # print(self.main_engine.read().hex()) # # 十六进制的读取读一个字节 # print(self.main_engine.read())#读一个字节 # print(self.main_engine.read(10).decode("gbk"))#读十个字节 # print(self.main_engine.readline().decode("gbk"))#读一行 # print(self.main_engine.readlines())#读取多行,返回列表,必须匹配超时(timeout)使用 # print(self.main_engine.in_waiting)#获取输入缓冲区的剩余字节数 # print(self.main_engine.out_waiting)#获取输出缓冲区的字节数 # print(self.main_engine.readall())#读取全部字符。 #接收数据 #一个整型数据占两个字节 #一个字符占一个字节 def Recive_data(self,way): # 循环接收数据,此为死循环,可用线程实现 print("开始接收数据:") while True: try: # 一个字节一个字节的接收 if self.main_engine.in_waiting: if(way == 0): for i in range(self.main_engine.in_waiting): print("接收ascii数据:"+str(self.Read_Size(size))) data1 = self.Read_Size(size).hex()#转为十六进制 data2 = int(data1,16)#转为十进制 if (data2 == "exit"): # 退出标志 break else: print("收到数据十六进制:"+data1+" 收到数据十进制:"+str(data2)) if(way == 1): #整体接收 # data = self.main_engine.read(self.main_engine.in_waiting).decode("utf-8")#方式一 data = self.main_engine.read_all()#方式二 if (data == "exit"): # 退出标志 break else: print("接收ascii数据:", data) except Exception as e: print("异常报错:",e) while True: Communication.Print_Used_Com() Ret =False #是否创建成功标志 Engine1 = Communication("com11",9600,0.5) if (Ret): Engine1.Recive_data(0)
ubuntu 安装tkinter 包
https://www.cnblogs.com/mengjinxiang/p/12405744.html
比如:
#!/usr/bin/env python # -*- coding: utf-8 -*- # author:Master import tkinter as tk # 使用Tkinter前需要先导入 class movement: on_hit=False var = None def __init__(self,cor_list): window = tk.Tk() window.title('Bluetooth') window.geometry('800x600') self.var = tk.StringVar() l = tk.Label(window, textvariable=self.var, bg='green', fg='white', font=('Arial', 12), width=30, height=2) l.pack() self.on_hit = False b = tk.Button(window, text='点击开始', font=('Arial', 12), width=10, height=1, command=self.hit_me) b.pack() height = 500 width = 700 x0=0 y0=0 temp_x=0 temp_y=0 x1=0 y1=500 height = 500 width = 700 canvas = tk.Canvas(window, bg='white', height=height, width=width) rect = canvas.create_rectangle(0, 500, 10, 490,fill='red',outline='red',tag="red") # 画矩形正方形 for i in cor_list: temp_x=x1 temp_y=y1 print(i) x1=i[0] y1=height-i[1] print(x1) print(y1) x0=temp_x y0=temp_y print(x1-x0) print(y0-y1) canvas.create_line(x0,y0, x1,y1,tag="blue") canvas.move(rect, x1-x0, y1-y0) canvas.pack() window.mainloop() print("continue") def hit_me(self): on_hit = self.on_hit var = self.var if on_hit == False: on_hit = True var.set('bluetooth is working') else: on_hit = False var.set('') if __name__ == "__main__": #下面是三个点的坐标,将坐标传给二维列表movement,后续直接绘图 #图中红色代表机器人,拆线是两点间连线 #按键功能是为添加蓝牙功能作铺垫 n1= (20, 130) n2 = (150, 150) n3=(200,250) cor_list=[] cor_list.append(n1) cor_list.append(n2) cor_list.append(n3) movement(cor_list)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。