In the previous post, I wrote that I didn’t manage to get the Direct Commands working that open and read a mailbox. And that I managed to get the workaround working to send data from the EV3 to the PC application. In this part I will tell you more about the other route that I found.
The workaround is based on the following: the EV3 program is able to write data to a file:
This block writes ’42’to the file ‘Outbox’. Note: you cannot choose the type at the EV3 side neither the file extension. With a telnet session, I managed to login to the brick and found out that the created outbox file is an ASCII file and has the extension .rtf.
The Direct Command ‘opFILE’ should be able to read the data into the PC application. If this is possible, we can send data from the EV3 to the PC. In fact, it should also possible to send the data by this means from the PC to the EV3. Since the Write-to-Mailbox works, this is not necessary but it might become convenient at a later stage to make the communication symmetric.
The ‘opFile’ command (chapter 4.14 in the Firmware Developer Kit doc) has several subcommands:
The subcommand ‘OPEN_READ’ should be able to open the “Outbox.rtf” file, the ‘READ_VALUE’ should be able to read (numeric) data. This should do the trick. But again: it was easier said than done.
It took me some time to figure out what the exact file path needed to be, but finally I got the return code 0x02 (success) when I used the following byte stream to open the file for reading (the file name of the EV3 program is “Example”, that is part of the file path):
Executing this command returns 9 bytes:
- Byte 1: return code (0x02 = success, 0x04 = failure)
- Byte 2-4: file handle
- Byte 5-8: size of file.
Not only the return code 0x02 indicated that the Direct Command was successfully executed, also the returned file size was different if the file didn’t exist yet (size = 0), ’42’ was written to the file (size = 3) or ’42’ was written several times to the file (6, 9, 12, etc). The factor 3 can be explained by the two digits plus the newline for each ’42’ that was written to the Outbox.rtf file. So, opening of the file was off my To Do list. Check.
My assumption was that the returned file handle (byte 2 – 4, typically a value between 1 and 8) could be used in a second byte stream. The documentation description of the ‘read data’ command is as follows:
Since I was able to open the file, it shouldn’t be too difficult to read the data. Just use the handle id from the previous command in a byte stream, and …. fail. I have tried all kind of variations, but none of them worked. Again, I felt desperate like Wile E Coyote …
The solution was found in the example to start a program (chapter 4.2 1 in the Communication Developer Kit). This example shows two things:
- It is possible to send more than one Direct Command in one byte stream.
- How to use the return value from the first command, in a second command (in one byte stream).
So, maybe, just maybe, if I use the returned file handle from the Open file command in the same way as the example in the Read data from file command, it might work. And yes, finally success! Every value that was written to the Outbox.rtf file, was read by the Direct Command. Note: the byte stream below always reads the first line in the file. Therefor, at the EV3 side, the Outbox file is deleted before I write a new number to it.
Below you find the C# code how to read the data from the Outbox.rtf file:
In the three articles about Sending data over WiFi, I have shown that there is a way to communicate between an EV3 program and a PC application. Unfortunately, I didn’t succeed in reading a mailbox. Yet, since I will not give up trying. For our project, we can continue with the workaround.
Hopefully this article inspires you to experiment with the Direct Commands as well. If you find a way to read an existing mailbox, please let me know.