Created
February 25, 2015 04:47
-
-
Save Craigson/47f1f17b3c8d6e5b5a17 to your computer and use it in GitHub Desktop.
Processing acting as a client, receiving data from python server
This file contains hidden or 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
| //Python acts as server | |
| //code for processing to run as client | |
| import processing.net.*; | |
| Client myClient; | |
| String dataIn; | |
| boolean firstContact; | |
| //code for Seiffert's spherical spiral | |
| float r; //radius of sphere | |
| float a; //elliptical modulus | |
| ArrayList xVals; | |
| ArrayList yVals; | |
| ArrayList zVals; | |
| int counter; | |
| void setup() { | |
| size(200, 200); | |
| xVals = new ArrayList(); | |
| yVals = new ArrayList(); | |
| zVals = new ArrayList(); | |
| counter = 0; //counter for counting number of requests to python | |
| firstContact = false; //boolean to establish first contact with server | |
| // Connect to the local machine at port 5001. | |
| // This example will not run if you haven't | |
| // previously started a server on this port. | |
| myClient = new Client(this, "127.0.0.1", 5001); | |
| } | |
| void draw() { | |
| estContact(); | |
| if (firstContact == true) { | |
| if (myClient.available() > 0 && counter < 100) { | |
| dataIn = myClient.readString(); | |
| myClient.clear(); | |
| xVals.add(float(dataIn)); //add x-values from python to arraylist | |
| myClient.write('A'); //request another value from server | |
| counter++; //increment counter | |
| myClient.clear(); //clear the buffer | |
| } else { | |
| myClient.stop(); //close the connection if counter is above 100 | |
| println("closing"); | |
| } | |
| } | |
| } | |
| //manually request information by pressing the 'r' key | |
| void keyPressed() { | |
| if (key == 'r') { | |
| myClient.write("A"); | |
| } | |
| if (key =='p') { | |
| println(xVals); | |
| println(xVals.size()); | |
| } | |
| } | |
| void estContact() { | |
| myClient.write('A'); | |
| firstContact = true; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment