In the previous article about ‘Sending data over Wifi between our PC application and the EV3, part 3‘, I explained how the workaround works. This part describes the protocol that we are implementing to make sure that all data that is send, is correctly processed.
A major drawback of the workaround, is that you have to implement a protocol between the EV3 and the PC application to prevent data to be lost. The protocol for our application is as follows:
- The commands from the PC to EV3 are send by a regular mailbox. Since this functionality works, why not use it. For example, the command from the PC to the crane “LOAD BLUE” will be send to the Inbox mailbox at the corresponding EV3, the EV3 program reads the Inbox mailbox to process these commands.
- The commands from the EV3 to the PC are send by the ‘file’ workaround. For example, if the crane is ready with loading the blue candy, it wants to send back the message “LOADED BLUE READY” to the PC. This command will be written to a file, that can be read (polling) at the PC side.
Each command that is send to the PC, is written in a separate file, in our case we use the name “Outbox” plus a counter, e.g. ‘Outbox33’. The PC application polls the directory and reads the files. After a file has been read (and thus can be set to ‘processed’), a message is send back to the EV3 (mailbox) with the file name. The EV3 uses a separate mailbox that reads an incoming file number and then deletes the corresponding file (this number will always be one higher than the last one, otherwise something went wrong in the protocol). Therefor, with the use of two counters, we can reuse file names that have been removed before.
One item remains: in case the application aborts, the directory is left with Outboxnn files that need to be removed. In our current implementation for startup, an “INIT” command is send to all NXT and EV3 bricks to set the software and hardware in initial state again (for example, the crane moves to the grabber to an upper position, trains will stop). This INIT command will also remove all Outboxnn files by looping over a counter from 0 .. 99 and setting the array elements to ‘free’.