Hardware Reference
In-Depth Information
Continued from previous page.
// trim the carrige return and linefeed from the input string:
inputString = trim(inputString);
// clear the resultString:
resultString = "";
// split the input string at the commas
// and convert the sections into integers:
int sensors[] = int(split(inputString, ','));
// add the values to the result string:
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
resultString += "Sensor " + sensorNum + ": ";
resultString += sensors[sensorNum] + "\t";
}
// print the results to the console:
println(resultString);
}
Data Packets, Headers, Payloads, and Tails
Now that you've got data going from one device (the microcontroller attached to the monkey) to another (the computer
running Processing), take a closer look at the sequence of bytes you're sending to exchange the data. Generally, it's
formatted like this (with a comma, ASCII 44, between each field):
Left-arm sensor
(0-1023)
Right-arm sensor
(0-1023)
Reset button
(0 or 1)
Serve button
(0 or 1)
Return character,
linefeed character
1-4 bytes
1-4 bytes
1 byte
1 byte
2 bytes
Each section of the sequence is separated by a single
byte whose value is ASCII 44 (a comma). You've just
made another data protocol. The bytes representing
your sensor values and the commas that separate them
are the payload , and the return and newline characters
are the tail . The commas are the delimiters . This data
protocol doesn't have a header, but many do.
the message. In this example, where there is no header,
the tail performs a similar function, separating one
message from the next.
On a network, many messages like this are sent out all
the time. Each discrete group of bytes is called a packet
and includes a header, a payload, and usually a tail. Any
given network has a maximum packet length . In this
example, the packet length is determined by the size
of the serial buffer on the personal computer. Process-
ing can handle a buffer of a few thousand bytes, so
this 16-byte packet is easy for it to handle. If you had a
much longer message, you'd have to divide the message
into several packets and reassemble them once they
all arrived. In that case, the header might contain the
packet number so the receiver knows the order in which
the packets should be reassembled.
A header is a sequence of bytes identifying what's
to follow. It might also contain a description of the
sequence to follow. On a network, where many possible
devices could receive the same message, the header
might contain the address of the sender, the receiver,
or both. That way, any device can just read the header
to decide whether it needs to read the rest of the
message. Sometimes a header is as simple as a single
byte of a constant value, identifying the beginning of
Search WWH ::




Custom Search