class TCPXboxController(object): def __init__(self, dev_num=0, send_socket_port=63636, rec_socket_port=63637, socket_ip="localhost"): self.send_socket = TCPSendSocket(tcp_port=send_socket_port, tcp_ip=socket_ip, send_type=JSON) self.rec_socket = TCPReceiveSocket(tcp_port=rec_socket_port, tcp_ip=socket_ip, handler_function=self.on_receive) joysticks = XInputJoystick.enumerate_devices() device_numbers = list(map(attrgetter('device_number'), joysticks)) print('found %d devices: %s' % (len(joysticks), device_numbers)) if not joysticks: sys.exit(0) self.joystick = joysticks[dev_num] print('using %d' % self.joystick.device_number) @self.joystick.event def on_button(button, pressed): self.send_socket.send_data([button, pressed]) @self.joystick.event def on_axis(axis, value): self.send_socket.send_data([axis, value]) def on_receive(self, data): ''' Do something here to activate the vibrations ''' pass def start_sockets(self): self.send_socket.start() self.rec_socket.start() def stop_sockets(self): self.send_socket.stop() self.rec_socket.stop()
def sending_function(): send_socket = TCPSendSocket(tcp_port=port, send_type=HDF, verbose=True) send_socket.start(blocking=True) for i in range(number_of_messages): send_socket.send_data({'data': "The value of i is now " + str(i)}) time.sleep(0.5) print("closing send socket.") send_socket.stop()
def sending_function(): send_socket = TCPSendSocket(tcp_port=port, send_type=NUMPY) send_socket.start(blocking=True) for i in range(number_of_messages): send_socket.send_data(i * 10) time.sleep(0.25) print("closing send socket.") send_socket.stop()
def sending_function(): send_socket = TCPSendSocket(tcp_port=port, send_type=JSON, verbose=False, as_server=True, include_time=True) send_socket.start(blocking=True) for i in range(number_of_messages): send_socket.send_data("The value of i is now " + str(i)) time.sleep(0.5) print("closing send socket.") send_socket.stop()
def __init__(self, dev_num=0, send_socket_port=63636, rec_socket_port=63637, socket_ip="localhost"): self.send_socket = TCPSendSocket(tcp_port=send_socket_port, tcp_ip=socket_ip, send_type=JSON) self.rec_socket = TCPReceiveSocket(tcp_port=rec_socket_port, tcp_ip=socket_ip, handler_function=self.on_receive) joysticks = XInputJoystick.enumerate_devices() device_numbers = list(map(attrgetter('device_number'), joysticks)) print('found %d devices: %s' % (len(joysticks), device_numbers)) if not joysticks: sys.exit(0) self.joystick = joysticks[dev_num] print('using %d' % self.joystick.device_number) @self.joystick.event def on_button(button, pressed): self.send_socket.send_data([button, pressed]) @self.joystick.event def on_axis(axis, value): self.send_socket.send_data([axis, value])
ip = '0.0.0.0' start_time = time.time() # define function to print the echo back from matlab def print_data(data): global start_time now = time.time() print('length of returned array:', np.frombuffer(data, dtype='float32').shape[0], 'Time for full trip:', now - start_time) # create a send and receive socket send_socket = TCPSendSocket(tcp_port=send_port, tcp_ip='', send_type=RAW) rec_socket = TCPReceiveSocket(tcp_port=receive_port, handler_function=print_data, receive_as_raw=True, as_server=True, receive_buffer_size=65536) # start the sockets send_socket.start() rec_socket.start() stop_flag = threading.Event() def send_sig(): global start_time
import struct import sys send_port = 4242 rec_port = 4242 ip = '127.0.0.1' # define function to print the echo back from matlab def print_data(data): print(data, "unpacked:", struct.unpack('ff', data)) # create a send and receive socket send_socket = TCPSendSocket(tcp_port=send_port, tcp_ip=ip, send_type=RAW, data_format_string='ff') receive_socket = TCPReceiveSocket(tcp_port=rec_port, tcp_ip=ip, receive_as_raw=True, handler_function=print_data) # start the sockets send_socket.start() receive_socket.start() stop_flag = threading.Event() def send_sig(): while not stop_flag.is_set(): data = np.random.random((1, 2)).tolist()[0]
from DataSocket import TCPSendSocket, JSON, TCPReceiveSocket import time import numpy as np send_port = 4242 receive_port = 4343 ip = '0.0.0.0' # define function to print the echo back from matlab def print_data(data): print(data) # create a send and receive socket send_socket = TCPSendSocket(tcp_port=send_port, tcp_ip=ip, send_type=JSON) rec_socket = TCPReceiveSocket(tcp_port=receive_port, handler_function=print_data) # start the sockets send_socket.start() rec_socket.start() # wait 5 seconds to give time to start the matlab script time.sleep(5) for i in range(10): # send a random 4x4 numpy array to matlab send_socket.send_data(np.random.random((4, 4))) time.sleep(0.5)