//jleblanc 3.21.06 //Call and Response a bit cleaner than the other version //from the PIC before initial drawing to its screen //IMPORT JAVA .awt FOR GUI// import java.awt.*; import java.io.*; // this is the input/output library needed for data streams import java.net.*; // this is the network library needed for sockets public class Messenger extends PApplet implements ActionListener { //NETWORK STUFF String host; int port; Socket mySocket; // declare Socket DataInputStream myInputStream; // declare data input stream. This will run within a socket, bringing data into Java DataOutputStream myOutputStream; // declare data output stream. This will run within a socket, sending data out from Java byte myDataIn, myDataOut; // declare some variables to store the data we're sending and receiving //NOTE that in processing a byte is from 127 to -128 where: //for a byte from 0-255 0->0, 127->127, 128->-128, 255->-1 //String message; int holdsize; byte[] outhold = new byte[24]; byte[] hold = new byte[24]; //STRING STUFF String message; TextField input = new TextField("msg", 25); Button send = new Button(); PFont font; //VARIABLES for getting message from PIC String displayString; byte[] bytehold = new byte[24]; int i; int signal; //////////////////////////////////////////// void setup() { size(500,250); background(0); //SET NETWORK STUFF host = "128.122.151.199"; // define a host to communicate with. This can be a name or IP address port = 10001; // define a port to contact on that host. Must be a number, typically 10001 for an XPort //SET INTERFACE STUFF font = loadFont("ArialMT-24.vlw"); //BUTTON send.setLocation(width-150,100); send.setSize(100,20); send.addActionListener(this); send.setLabel("SEND");//this is the event message add(send); //TEXT INPUT input.addActionListener(this); input.setLocation(100, 100); input.setSize(200,20); add(input); //Storage Variables message = "I got it"; displayString="Waiting for Response"; }//END setup void draw () { background(50); smooth (); fill(255); textFont(font); textSize(24); //CHECK NETWORK checkConnection(host, port); //Get the incoming Message from PIC GatherMsg(); //After that send message that processing user enters back to PIC input.addActionListener(this); }//END draw void Acknowledge() { //SEND CONTROL BYTE myDataOut = 65; sendSomeData(myDataOut); //SEND LENGTH OF MESSAGE ////holdsize=message.length(); myDataOut = (byte) 24;//holdsize; sendSomeData(myDataOut); //LOAD HOLD WITH BLANK SPACES for(int i=0; i0) // check to see if any bytes are available { bytesAvailable = true; // ...and if they are set the variable to true println(myInputStream.available() + " bytes available..."); } } catch(Exception e) { e.printStackTrace(); println("error while checking for bytes available"); } return bytesAvailable; } ////////GET SOME DATA \\\\\\\\\\ byte getSomeData() { byte inData = 0; // declare and initialize the data variable try { if (myInputStream.available()>0) // only read the byte if there's a byte to read [this is a redundant check] { inData = myInputStream.readByte(); // read a byte from the input stream println("data received: " + inData); } } catch(Exception e) { e.printStackTrace(); println("no data"); } return inData; } ////////SEND SOME DATA\\\\\\\\\\ void sendSomeData(byte outData) { if (myOutputStream == null) // if there's no active output stream { try { myOutputStream = new DataOutputStream(mySocket.getOutputStream()); // create an new output stream from a particular socket } catch (Exception e) { e.printStackTrace(); // println("no output stream"); } } try { myOutputStream.writeByte(outData); // write a byte to the output stream //println("data sent: " + outData); } catch(Exception e) { e.printStackTrace(); //println("event send failed"); } } public void stop() // when the program quits { try { myInputStream.close(); // close the input stream myOutputStream.close(); // close the output stream mySocket.close(); // close the socket } catch (Exception e) { e.printStackTrace(); println("couldn't close connection"); } } }//END Messenger