我正在尝试让我的arduino串行监控器(图1)与速度计上的指针通信。例如,当数据为28.1时,指针将指向速度计中的28.1范围。现在,针刚好从左向右连续走。 Speed_text是包含arduino数据的变量。但是,我不将root.speed_text放在py文件中。
arduino数据
车速表
.py文件
import threading
import serial
from kivy.animation import Animation
from kivy.clock import Clock, mainthread
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.app import App
class SpeedWindow(Screen):
speed_text = StringProperty("")
def __init__(self, **kwargs):
super(SpeedWindow, self).__init__(**kwargs)
threading.Thread(target=self.tm).start()
def tm(self):
while (1):
with serial.Serial('COM4', 9600) as ser:
value = ser.readline()
ser.close()
self.update_fix(value.decode('utf-8'))
@mainthread
def update_fix(self, value):
self.speed_text = value
class WindowManager(ScreenManager):
pass
KV = """
WindowManager:
SpeedWindow:
<SpeedWindow>:
angle: 90
name: 'speedometer'
Image:
source: 'cadran.png'
size_hint: None, None
size: 400, 400
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
Image:
source: 'needle.png'
size_hint: None, None
size: 170, 170
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
# apply rotation matrix to this Image
canvas.before:
PushMatrix
Rotate:
angle: root.angle
axis: (0, 0, 1)
origin: (self.center_x, self.center_y , 0)
canvas.after:
PopMatrix
"""
class MyMainApp(App):
def build(self):
Clock.schedule_once(self.animate_needle, 1.0) # start the animation in 2 seconds
return Builder.load_string(KV)
def animate_needle(self, dt):
# animate the needle from +90 to -90 and then back to +90
self.anim = Animation(angle=-90.0) + Animation(angle=90)
self.anim.repeat = True # repeat forever
speedometer = self.root.get_screen('speedometer')
self.anim.start(speedometer)
if __name__ == "__main__":
MyMainApp().run()