Advertisement
Guest User

Untitled

a guest
Mar 26th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.04 KB | None | 0 0
  1. import serial
  2. import glob
  3. import tkinter
  4. import sys
  5. import json
  6. import udp_receive
  7. import udp_send
  8. from tkinter import*
  9.  
  10. separator = ', '
  11. LeftBodyArray = [0]*7
  12. RightBodyArray =[0]*8
  13. LeftHandArray = [0]*7
  14. RightHandArray = [0]*6
  15.  
  16. numPoints = 4
  17. PressureArray = [0]*numPoints
  18.  
  19.  
  20. def serial_ports():
  21.     """ Lists serial port names
  22.  
  23.        :raises EnvironmentError:
  24.            On unsupported or unknown platforms
  25.        :returns:
  26.            A list of the serial ports available on the system
  27.    """
  28.     if sys.platform.startswith('win'):
  29.         ports = ['COM%s' % (i + 1) for i in range(256)]
  30.     elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
  31.         # this excludes your current terminal "/dev/tty"
  32.         ports = glob.glob('/dev/tty[A-Za-z]*')
  33.     elif sys.platform.startswith('darwin'):
  34.         ports = glob.glob('/dev/tty.*')
  35.     else:
  36.         raise EnvironmentError('Unsupported platform')
  37.  
  38.     result = []
  39.     for port in ports:
  40.         try:
  41.             s = serial.Serial(port)
  42.             s.close()
  43.             result.append(port)
  44.         except (OSError, serial.SerialException):
  45.             pass
  46.     return result
  47.  
  48. # serial_ports() to display the current serial ports available
  49.  
  50. serial_available = serial_ports()
  51. try:
  52.     print('To run the GUI check the COM port of the Arduino in the Arduino IDE. Tools > Port')
  53.     print('\nThese are the available COM ports: ', serial_ports())
  54.     ser = serial.Serial('COM6', baudrate=9600, timeout=1, write_timeout=2)
  55.     print('\nConnected to: ', ser.name)
  56. except:
  57.     print('That COM port is unavailable.')
  58.     FirstInput = input('Press "ENTER" to close')
  59.  
  60. global PressureString
  61. PressureString = '0,0,0 >'
  62.  
  63. global RightHandArray_Old
  64. RightHandArray_Old = ['0','0','0','0','0','0']
  65.  
  66. global FailCount
  67. global SuccessCount
  68. FailCount = 0
  69. SuccessCount = 0
  70.  
  71. def SendArray():
  72.     while 1:
  73.         global RightHandArray_Old
  74.         global PressureString
  75.         global FailCount
  76.         global SuccessCount
  77.  
  78.         ser.write(PressureString.encode())
  79.         GloveString = ser.readline()  # reads from Arduino
  80.         GloveString = str(GloveString, encoding='utf -8')  # reads as string
  81.         # sys.stdout.write(GloveString)
  82.         # ser.flushInput()
  83.         print(GloveString)
  84.  
  85.         RightHandArray_rec = GloveString.split(',')  # makes list split by commas
  86.         RightHandArray_rec = [s.replace('>\r\n', '') for s in RightHandArray_rec]
  87.         RightHandArray_rec = [s.replace('>\n\n', '') for s in RightHandArray_rec]
  88.         RightHandArray_rec = [s.replace('>', '') for s in RightHandArray_rec]
  89.  
  90.         if len(RightHandArray_rec) == 6:  # array is of the expected size for right hand control
  91.             SuccessCount = SuccessCount + 1
  92.             print("Success :", SuccessCount)
  93.             RightHandArray = [RightHandArray_rec[0], RightHandArray_rec[1], RightHandArray_rec[2],
  94.                               RightHandArray_rec[3], RightHandArray_rec[4], RightHandArray_rec[5]]
  95.             RightHandArray_Old = RightHandArray
  96.         else:
  97.             FailCount = FailCount + 1
  98.             print("Failed :", FailCount)
  99.             RightHandArray = RightHandArray_Old  # sends the last saved array for the right hand
  100.  
  101.         LeftBodyArray = ['10', '90', '90', '0', '90', '90', '90']
  102.         RightBodyArray = ['10', '90', '90', '0', '90', '90', '90', '90']
  103.         LeftHandArray = ['0', '0', '0', '0', '0', '0', '0']
  104.  
  105.         FullArray = LeftBodyArray + RightBodyArray + LeftHandArray + RightHandArray
  106.         ProcessedString = separator.join(FullArray) + ' >'
  107.         #
  108.         # # print(ProcessedString)
  109.         # # print(len(FullArray))
  110.         #
  111.         # # udp_send.UDPSEND("169.254.139.235", 2901,ProcessedString)
  112.         udp_send.UDPSEND('127.0.0.1', 3001, ProcessedString)
  113.         #
  114.         # PressureString1 = udp_receive.UDPRECEIVE('127.0.0.1', 3002)
  115.  
  116.         PressureString1 = udp_receive.UDPRECEIVE_NONBLOCKING('127.0.0.1', 3002, 3)
  117.         # # ser.flushInput()
  118.         print(PressureString1)
  119.  
  120.  
  121.  
  122. while True:
  123.     SendArray()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement