-
-
Save munho/e2d9b91bc699712cccaa87d764fa8ddb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| import sys | |
| import threading | |
| import queue | |
| # from kivy.compat import queue | |
| import serial | |
| import time | |
| import kivy | |
| kivy.require('1.9.0') | |
| from kivy.app import App | |
| from kivy.lang import Builder | |
| from kivy.uix.label import Label | |
| from kivy.uix.textinput import TextInput | |
| from kivy.uix.boxlayout import BoxLayout | |
| from kivy.uix.screenmanager import Screen,ScreenManager,NoTransition,FadeTransition | |
| from kivy.clock import Clock | |
| from kivy.config import Config | |
| # 전역 변수 | |
| # 템플릿 | |
| kvlang = ''' | |
| <ScreenManagement>: | |
| ScreenChild: | |
| <ScreenChild>: | |
| name: 'Second' | |
| the_time: _id_lbl_time | |
| BoxLayout: | |
| orientation: 'vertical' | |
| BoxLayout: | |
| Label: | |
| font_name: 'fonts/malgun.ttf' | |
| text: 'Received' | |
| Label: | |
| id: _id_lbl_time | |
| text: "Initial text" | |
| font_name: 'fonts/malgun.ttf' | |
| # color: 0,0,0,1 | |
| BoxLayout: | |
| orientation: 'vertical' | |
| BoxLayout: | |
| size_hint_y: None | |
| text: '' | |
| BoxLayout: | |
| Label: | |
| size_hint_x: None | |
| font_name: 'fonts/malgun.ttf' | |
| # color: 0,0,0,1 | |
| text: 'Send' | |
| TextInput: | |
| # size_hint_x: None | |
| font_name: 'fonts/malgun.ttf' | |
| color: 1,1,1,1 | |
| id: stxt | |
| text: '' | |
| Button: | |
| size_hint_x: None | |
| font_name: 'fonts/malgunbd.ttf' | |
| background_color: (42/255, 24/255, 77/255, 1.0) | |
| text: '전송' | |
| color: 1,1,1,1 | |
| on_press: | |
| app.root.app_main.on_send(stxt.text) | |
| ''' | |
| # 화면 설정 | |
| Config.set('graphics', 'width', '800') | |
| Config.set('graphics', 'height', '270') | |
| # Config.set('graphics', 'fullscreen', 1) | |
| Config.set('graphics','resizable',0) | |
| # 마우스 멀티터치 비활성 - 마우스 우클릭시 주황색 원형 마크가 생성되는 것 방지 목적 | |
| Config.set('input', 'mouse', 'mouse,multitouch_on_demand') | |
| # Serial COM | |
| class SerialThread(threading.Thread): | |
| seq = serial.Serial('COM10', 9600, writeTimeout=0.3, timeout=None) # MS-Windows | |
| # seq = serial.Serial('/dev/ttyUSB0', 9600) # Linux | |
| is_run = True | |
| def __init__(self, queue): | |
| threading.Thread.__init__(self) | |
| self.queue = queue | |
| def run(self): | |
| self.text = "" | |
| while self.is_run: | |
| time.sleep(0.05) | |
| if self.seq.inWaiting(): | |
| self.text += (self.seq.read(self.seq.inWaiting())).decode('utf-8') | |
| elif (self.seq.inWaiting() != True and self.text != ""): | |
| app_main.scm.get_screen("Second").updateText(self.text) | |
| self.text = "" | |
| class AppMain(App): | |
| def build(self): | |
| self.queue = queue.Queue() | |
| self.thread = SerialThread(self.queue) | |
| self.thread.start() | |
| # self.process_serial() # Tk에서는 되던게 Kivy에서는 안된다. | |
| Builder.load_string(kvlang) | |
| # Builder.load_file("layout.kv") # 한글이 들어가면 안열린다. | |
| self.scm = ScreenManagement() | |
| return self.scm | |
| def on_send(self, data): | |
| ''' | |
| Send data via serial port | |
| ''' | |
| try: | |
| SerialThread.seq.write(bytes(data, encoding='ascii')) | |
| except serial.serialutil.SerialException: | |
| print("Send failed! Please check equipments connection.") | |
| class ScreenChild(Screen): | |
| def switch(self, index): | |
| self.parent.current = index | |
| def updateText(self, text): | |
| self.the_time.text = text | |
| # pass | |
| class ScreenManagement(ScreenManager): | |
| current_menu = "Menu 2" | |
| app_main = AppMain() | |
| ScreenManager.transition = NoTransition() | |
| # pass | |
| if __name__ == '__main__': | |
| app_main = AppMain() | |
| app_main.run() | |
| SerialThread.is_run = False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment