SHARE
TWEET

Untitled

a guest Oct 21st, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import serial
  2. from time import sleep
  3. import sys
  4.  
  5. # Settings
  6. COM_PORT = '/dev/tty.usbmodem14101'
  7. BAUD_RATES = 9600
  8. ser = serial.Serial(COM_PORT, BAUD_RATES)
  9.  
  10. # 傳值給 Arduino
  11. def serial_write(text):
  12.     s_to_byte_text = bytes(text + '\n', encoding = "utf8")
  13.     ser.write(s_to_byte_text)
  14.  
  15. # LED example
  16. def led(ser, choice):
  17.     if choice == '1':
  18.         print('傳送開燈指令')
  19.         serial_write('LED_ON')
  20.         sleep(0.5)
  21.     elif choice == '2':
  22.         print('傳送關燈指令')
  23.         serial_write('LED_OFF')
  24.         sleep(0.5)
  25.     elif choice == 'e':
  26.         ser.close()
  27.         print('再見!')
  28.         sys.exit()
  29.     else:
  30.         print('指令錯誤…')
  31.  
  32.     while ser.in_waiting:
  33.         mcu_feedback = ser.readline().decode()
  34.         print('控制板回應:', mcu_feedback)
  35.  
  36. try:
  37.     while True:
  38.         choice = input('按1開燈、按2關燈、按e關閉程式  ').lower()
  39.         led(ser, choice);
  40.  
  41. except KeyboardInterrupt:
  42.     ser.close()
  43.     print('再見!')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top