
COMMUNICATING IN (NEAR) REAL TIME 159
Next, take action depending on the
state of the client and the messages to
be sent:
8
void stateCheck() {
// Everything in this method depends on the client's status:
switch (status) {
case connected:
// if you're connected, listen for serial in:
while (Serial.available() > 0) {
// if you get a 'D', it's from the Lantronix device,
// telling you that it lost the connection:
if (Serial.read() == 'D') {
status = disconnected;
}
}
// if there's a paddle message to send, send it:
if (paddleMessage != 0) {
Serial.print(paddleMessage);
// reset paddleMessage to 0 once you've sent the message:
paddleMessage = 0;
}
// if there's a connect message to send, send it:
if (connectMessage != 0) {
// if you're connected, disconnect:
Serial.print(connectMessage);
// reset connectMessage to 0 once you've sent the message:
connectMessage = 0;
}
break;
case disconnected:
// if there's a connect message, try to connect:
if (connectMessage !=0 ) {
deviceConnect();
// reset connectMessage to 0 once you've tried to connect:
connectMessage = 0;
}
break;
// if you sent a connect message but haven't connected, keep trying:
case connecting:
// read the serial port:
if (Serial.available()) {
inByte = Serial.read();
// if you