Created
January 11, 2018 04:56
-
-
Save ucaiado/2a5438b614084de75f0b956fb5e15770 to your computer and use it in GitHub Desktop.
Script client.py sends xy data over a datagram socket to the server.py which displays them on matplotlib. Run the server in one console and then the client on another. You should see an interactive display of xy data as they arrive. Using Python 3.
This file contains 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
import time | |
import socket | |
import math | |
import json | |
addr = ("127.0.0.1", 12000) | |
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
clientSocket.settimeout(1) | |
for x in range(1000): | |
point = dict(x=float(x), y=math.sin(x/10)) | |
message = json.dumps(point) | |
b = bytearray() | |
b.extend(map(ord, message)) | |
clientSocket.sendto(b, addr) | |
print('sent ', message) | |
time.sleep(0.2) |
This file contains 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
import numpy as np | |
import matplotlib.pyplot as plt | |
import time | |
import socket | |
import json | |
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
serverSocket.bind(('', 12000)) | |
plt.ion() | |
while True: | |
message, address = serverSocket.recvfrom(1024) | |
point = json.loads(message) # expects json in the format {"x": 123.23, "y": 321.2} | |
print('recv:', point) | |
plt.plot(point['x'], point['y'], 'ob') | |
plt.pause(0.001) | |
while True: | |
plt.pause(0.05) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment