|TCPmaker : Visual Tour Exploring and Customizing the Generated Code|
The mtConnect event:
The mtConnect() event handler is called whenever the PC browser connects to your device.
You can use this handler to perform initializations that need to happen when connection is first established, like updating / displaying certain variables, or sending the current states of everything, so PC browser display starts out right (i.e., displaying the current states of everything.
In that spirit, here is we customized mtConnect() in our PICDEM.net2 project. The lines shown in red are lines we either added or enabled (by removing comment operators) in the code:
// Example of mtConnect event code --------------
// Call user-defined sendLedStates() routine,
// to tell the browser the states of all the LEDs
// ********* Add your mtConnect event code here: *********
// OK, let's activate the sendLedStates() function below, and USE it
} // mtConnect()
// CHANGED BY HAND: Enable this function
// Set all the transmit flags
D1TxFlag = 1;
D2TxFlag = 1;
D3TxFlag = 1;
D4TxFlag = 1;
D5TxFlag = 1;
D6TxFlag = 1;
D7TxFlag = 1;
D8TxFlag = 1;
} // sendLedStates()
We added a line to call the setLedStates() function (provided in commented out form in mtGen.c as a reference example), so that whenever a PC browser connects to the device, we arrange to send the current states of all of the LEDs, so the PC browser display is in sync with the real device.
All we had to do to arrange that transfer was to set the corresponding Tx flag for each variable, D1TxFlag ... D8TxFlag, to a non-zero value. mtServer() will sense this the next time around the main loop, will immediately send the values of D1...D8 to the PC, and then reset these Tx flags to 0 again.
|1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21|