按照承诺。我的忏悔是连接到 TinyG SPJS 并在 SPJS 上打开 com4 以连接到 TinyG 板的代码。原谅,这是一个更大代码的 sn-p,但它是独立的。
.kv 文件:
CNCMainScreenBoxLayout:
<CNCMainScreenBoxLayout>:
orientation: "horizontal"
spacing: "20dp"
BoxLayout:
orientation: "vertical"
Label:
id: Status_Label
size_hint: 1,0.09
text: root.StatusText
#on_update: root.on_Update()
Button:
background_color: (0, 0, 0, 0)
size_hint: .4,1
on_press: root.on_Click_Emergency_pressed()
on_release: root.on_Click_Emergency_released()
Image:
id: EmergencyStop_Image
source: "imagesEmergencyStop_released.png"
size: self.parent.size
background: None
y: self.parent.y
x: self.parent.x
allow_stretch: True
和python文件:
from kivy.uix.widget import Widget
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import StringProperty, ObjectProperty
from kivy.clock import Clock
from kivy.logger import Logger
from kivy.uix.boxlayout import BoxLayout
import websocket
import _thread
import json
from kivy.support import install_twisted_reactor
install_twisted_reactor()
class CNCMainScreenBoxLayout(BoxLayout):
StatusText = StringProperty("")
def __init__(self, **kwargs):
super().__init__(**kwargs)
def Set_Status(self,displaytxt):
self.StatusText = displaytxt
def Get_URI(self):
return "ws://localhost:8989/ws"
def on_Click_Emergency_pressed(self):
self.ids.EmergencyStop_Image.source = "imagesEmergencyStop_Pressed.png"
self.StatusText = "EMERGENCY STOP"
def on_Click_Emergency_released(self):
self.ids.EmergencyStop_Image.source = "imagesEmergencyStop_Released.png"
class KivyWebSocket(websocket.WebSocketApp):
def __init__(self, *args, **kwargs):
super(KivyWebSocket, self).__init__(*args, **kwargs)
self.logger = Logger
self.logger.info('WebSocket: logger initialized')
class WebSocketTest(App):
ws = None
MainScreen = None
url = None
layout = ObjectProperty(None)
StoredSettingsStore = None
def __init__(self, **kwargs):
super(WebSocketTest, self).__init__(**kwargs)
self.MainScreen = Builder.load_file("CNCViewSO.kv")
self.layout = CNCMainScreenBoxLayout()
self.url = "ws://localhost:8989/ws"
self.ws = KivyWebSocket(self.url,
on_message=self.on_ws_message,
on_error=self.on_ws_error,
on_open=self.on_ws_open,
on_close=self.on_ws_close, )
app = App.get_running_app()
Clock.schedule_once(app.ws_connection)
self.logger = Logger
self.logger.info('App: initiallzed')
def build(self):
##self.layout = CNCMainScreenBoxLayout()
self.logger.info('Build: initiallzed')
return self.layout
def on_ws_message(self, ws, message):
#self.layout.the_btn.text = message
decoded = json.loads(message)
#print(decoded)
for keys in decoded:
#print(keys)
if keys == "Version":
print("Version is the version of the SPJS
" + decoded[keys])
self.layout.StatusText = "Connected to SPJS Version " + decoded[keys] + " at " + str(self.url)
elif keys == "Hostname":
print("Hostname is the hostname of the computer the SPJS is running on
" + decoded[keys])
elif keys == "Commands":
print("Commands you can send to the server")
for cmd in decoded[keys]:
print(" command --> ", str(cmd))
self.ws_send_ComConnect("Com4")
elif keys == "Cmd":
print(keys)
else:
print("Unhandled key: " + keys)
def on_ws_error(self, ws, error):
self.logger.info('WebSocket: [ERROR] {}'.format(error))
def ws_connection(self, dt, **kwargs):
# start a new thread connected to the web socket
_thread.start_new_thread(self.ws.run_forever, ())
def ws_send_ComConnect(self,comPort):
Comstring = "Open " + comPort + " 115200 tinyg"
self.ws.send(Comstring)
def on_ws_open(self, ws):
pass
"""def run(*args):
for i in range(1, 13):
time.sleep(1)
ws.send('version')
time.sleep(10)
ws.close()
_thread.start_new_thread(run, ())"""
def on_ws_close(self, ws):
self.layout.StatusText = '### closed ###'
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
WebSocketTest().run()
要运行,您需要在 ./images 中为按钮创建一个图像文件。以下是按钮文件:
emergencystop_released.png
emergencystop_pressed.png