I'm trying to write a small application that will do the following
1. Recieve a UDP packet containing 360 8 bit binary values
2. Convert each binary value to decimal
3. Plot the 360 Decimal values to the screen
4. Clear the screen and wait for the next UDP packet
Using a the UDP examples I have created one sketch that reads the binary data from a file then sends it out via UDP
by using wireshark packet capture i can see that the data is being sent from the transmit application but the recive application doesnt seem to do anything.
The graph side of things works when i load the binary values from a file using the following
byte b[] = loadBytes("bytes.dat");
Does anyone have any idea what i'm doing wrong?
/**
Transmit application
Read binary data from file and send as a message over UDP
*/
// import UDP library
import hypermedia.net.*;
UDP udp; // the UDP object
/**
* init the frame and the UDP object.
*/
void setup() {
// create a multicast connection on port 6000
// and join the group at the address "224.0.0.1"
udp = new UDP( this, 6000, "224.0.0.1" );
// wait constantly for incomming data
udp.listen( true );
// ... well, just verifies if it's really a multicast socket and blablabla
println( "init as multicast socket ... "+udp.isMulticast() );
println( "joins a multicast group ... "+udp.isJoined() );
}
// process events
void draw() {
}
/**
* on Key press
* send the data over the network
*/
void keyPressed() {
byte b[] = loadBytes("bytes.dat");
// by default if the ip address and the port number are not specified, UDP
// send the message to the joined group address and the current socket port.
udp.send( b ); // = send( data, group_ip, port );
println (b);
// note: by creating a multicast program, you can also send a message to a
// specific address (i.e. send( "the messsage", "192.168.0.2", 7010 ); )
}
/**
RX application
Get a UDP packet, convert the data and graph
*/
import hypermedia.net.*;
UDP udp;
void setup () { //setup
// set the window size:
size(360, 200); //display window 500 pixels x 500 pixels
// set inital background:
udp = new UDP( this, 9091, "224.0.0.1"); // this, port, ip address
udp.listen(true);
background(0); //0 is black
}
//void draw () {
void receive( byte[] b, String ip, int port ) {
// open a file and read its binary data
//byte b[] = loadBytes("bytes.dat");
// print each value, from 0 to 255 index starts from 1 so previous index is never less than 0
for (int i = 1; i < b.length; i++) {
// previous i value starts at 0 and increments
int iz = i-1;
// bytes are from -128 to 127, this converts to 0 to 255
int a = b[i] & 0xff; //bitwise AND b[i] and 0xFF to get decimal equiv of b[i]
//previous a value store as c
int c = b[i-1] & 0xff; //bitwise AND b[i-1] and 0xFF to get decimal equiv of b[i-1]
stroke(255,255,255); //set color as white
//draw a line from previous point (iz, height-c) to current point (i, height-a)
line (iz, height - c, i, height -a);