Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import serial
- import glob
- import tkinter
- import sys
- import json
- import udp_receive
- import udp_send
- from tkinter import*
- separator = ', '
- LeftBodyArray = [0]*7
- RightBodyArray =[0]*8
- LeftHandArray = [0]*7
- RightHandArray = [0]*6
- numPoints = 4
- PressureArray = [0]*numPoints
- def serial_ports():
- """ Lists serial port names
- :raises EnvironmentError:
- On unsupported or unknown platforms
- :returns:
- A list of the serial ports available on the system
- """
- if sys.platform.startswith('win'):
- ports = ['COM%s' % (i + 1) for i in range(256)]
- elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
- # this excludes your current terminal "/dev/tty"
- ports = glob.glob('/dev/tty[A-Za-z]*')
- elif sys.platform.startswith('darwin'):
- ports = glob.glob('/dev/tty.*')
- else:
- raise EnvironmentError('Unsupported platform')
- result = []
- for port in ports:
- try:
- s = serial.Serial(port)
- s.close()
- result.append(port)
- except (OSError, serial.SerialException):
- pass
- return result
- # serial_ports() to display the current serial ports available
- serial_available = serial_ports()
- try:
- print('To run the GUI check the COM port of the Arduino in the Arduino IDE. Tools > Port')
- print('\nThese are the available COM ports: ', serial_ports())
- ser = serial.Serial('COM6', baudrate=9600, timeout=1, write_timeout=2)
- print('\nConnected to: ', ser.name)
- except:
- print('That COM port is unavailable.')
- FirstInput = input('Press "ENTER" to close')
- global PressureString
- PressureString = '0,0,0 >'
- global RightHandArray_Old
- RightHandArray_Old = ['0','0','0','0','0','0']
- global FailCount
- global SuccessCount
- FailCount = 0
- SuccessCount = 0
- def SendArray():
- while 1:
- global RightHandArray_Old
- global PressureString
- global FailCount
- global SuccessCount
- ser.write(PressureString.encode())
- GloveString = ser.readline() # reads from Arduino
- GloveString = str(GloveString, encoding='utf -8') # reads as string
- # sys.stdout.write(GloveString)
- # ser.flushInput()
- print(GloveString)
- RightHandArray_rec = GloveString.split(',') # makes list split by commas
- RightHandArray_rec = [s.replace('>\r\n', '') for s in RightHandArray_rec]
- RightHandArray_rec = [s.replace('>\n\n', '') for s in RightHandArray_rec]
- RightHandArray_rec = [s.replace('>', '') for s in RightHandArray_rec]
- if len(RightHandArray_rec) == 6: # array is of the expected size for right hand control
- SuccessCount = SuccessCount + 1
- print("Success :", SuccessCount)
- RightHandArray = [RightHandArray_rec[0], RightHandArray_rec[1], RightHandArray_rec[2],
- RightHandArray_rec[3], RightHandArray_rec[4], RightHandArray_rec[5]]
- RightHandArray_Old = RightHandArray
- else:
- FailCount = FailCount + 1
- print("Failed :", FailCount)
- RightHandArray = RightHandArray_Old # sends the last saved array for the right hand
- LeftBodyArray = ['10', '90', '90', '0', '90', '90', '90']
- RightBodyArray = ['10', '90', '90', '0', '90', '90', '90', '90']
- LeftHandArray = ['0', '0', '0', '0', '0', '0', '0']
- FullArray = LeftBodyArray + RightBodyArray + LeftHandArray + RightHandArray
- ProcessedString = separator.join(FullArray) + ' >'
- #
- # # print(ProcessedString)
- # # print(len(FullArray))
- #
- # # udp_send.UDPSEND("169.254.139.235", 2901,ProcessedString)
- udp_send.UDPSEND('127.0.0.1', 3001, ProcessedString)
- #
- # PressureString1 = udp_receive.UDPRECEIVE('127.0.0.1', 3002)
- PressureString1 = udp_receive.UDPRECEIVE_NONBLOCKING('127.0.0.1', 3002, 3)
- # # ser.flushInput()
- print(PressureString1)
- while True:
- SendArray()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement