In this seminar we’ll look at simple networked communications using UDP sockets.
You’ll need :
- An MBED board connected to the off-campus networked
- get hold of a spare ethernet cable (filing cabinet in network lab)
- plug the MBED FRDM-K64F into one of the four network ports on the bench.
- in the network cabinet connect the port to a free connection on the “Internet” switch. (just as you would for the PCs)
- You will need a PC also connected to the off-campus network (the lab machines will do fine)
- The exercises need Both the right program running on the MBEB board and the PC.
- The sample PC programs below are written in Java and so should be runnable on any machine with Java.
The Java programs are supplied as source, so you can compare how I’ve done things in Java to how you might do them. And as a Jar file, run using:
- Simple Display
- java ui sends control messages
- Quote client for java quote server
- String Manipulation
- Hold on to your seats…
- Report state of Switches
- Data logging and graphing utility
Simple Display
You will need two sets of code.
- The MBED code s7.1
- The Java code s7.1.a
Compile and run both:
- You’ll need the Serial Monitor to see what IP address the MBED board reports itself at
- Use that IP address in the Java program
- enter messages in the text-box, they should appear on the LCD.
Take some time to look through the code for each, identify
- How are UDP Datagram sockets created
- How they are used to
- send messages
- read messages
- How is the receiving port number set?
Can messages be sent to any board?
java ui sends control messages
You will need two sets of code.
- The MBED code s7.2
- The Java code s7.2.a
Compile and run both:
- you should be able to turn on and off LEDs from the Java program
Take some time to look through the code for each, identify
- How messages are formatted and created
- How messages are read and split apart
- How the lookup-table is used to set the right LED
How might you extend the capabilities to control all the LEDs on the MBED board and the Application shield?
Think about the names used as keys
Quote client for java quote server
The Java Tutorials have an example quote server .
The Program in s7.3 has a client that gets a quote from the server and displays it.
The version of the Server in s7.3.a
is the version from the Tutorial, with some additional print statements
to show the connection details.
- compare the two programs with those in 7.1
- How does the code differ for the server and client in these cases?
- in s7.1 the MBED code is the Server, here the Java program is the server.
- how are the port numbers identified?
String Manipulation
In all the cases shown, the Datagram messages are blocks of formatted text.
A frequently used format is.
- One item per line
- items have a key-value format with a colon ‘:’ separating the key and value
Review the string manipulation features of C and Java.
How would you use these in each language to:
- Split the data in the Datagram buffer into lines
- Split the line into key:value pairs
- Convert the values into data
Hold on to your seats…
We now hit regions (“here be dragons”) where we end up having to use multiple Threads.
- In both the MBED C libraries and the Java JDK UDP communications is blocking.
We need a thread just to handle the communications.
- In Java, swing has it’s own threads if we want to update the display from code (ugh!)
- In the C code we have to have a thread to poll sensors that cannot genrate events.
Report state of Switches
This Pair of programs monitors the state of the switches, communicates this to the Java program that displays them as a set of Radio Buttons.
- s7.4.a is the Java program.
- s7.4 is the MBED program.
You will need to edit the source with the IP address of the Java program.
- Look at the console/monitor output from both. Why is there so much traffic?
- Comment out the line in the MBED program
eventqueue.call_every(500,jspoll);
What happens to the traffic?
- How would you rewrite the polling function to send messages on change of state rather than sending the current state?
Data logging and graphing utility
There is lots of complexity here, managing threads data, messaging,
sensor polling, and display updates.
- s7.4.a is the Java program.
- s7.4 is the MBED program.
You will need to edit the source with the IP address of the Java program.
There are several techniques I’ve used, in managing the complexity of the code.
How would you manage the system?
What changes and improvements can you suggest over my code?
© 2017 Dr Alun Moon
alun.moon@northumbria.ac.uk