Sending data over WiFi between our PC application and the EV3 (part 3)

Dead EndIn 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:

Write 42 To Outbox file icon

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:

Documentation EV3 Direct Command - opFile

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):

Direct Command - Open File Outbox - Byte Stream

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:

Direct Command - Read File Outbox

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 …

Help ...

Finally success!

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:

  1. It is possible to send more than one Direct Command in one byte stream.
  2. 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:

Code Snippit EV3 - Example Program - Success

Epilogue

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.

4 thoughts on “Sending data over WiFi between our PC application and the EV3 (part 3)

  1. Pingback: Sending data over WiFi between our PC application and the EV3 (part 4) | Sioux .NET on Track

  2. Hi Hans, thanks for the novel solution. I have a simple need, just to have a standalone program (in C/C++/C# or Python) running on PC/MAC to poll the ev3 brick periodically through either bluetooth or wifi (whichever is simpler) to check the contents of the Outbox.rtf file, and then do some action on the PC/MAC. The Outbox.rtf file will be created by a program running on the brick. I am familiar with ev3 programming through the interface but don’t have a clue as to how to control it from a PC with other self-created programs. Can you please help? I would greatly appreciate it.

  3. Hi Hans, this blog on “Sending DATA over WIFI between our PC application and the EV3 (Part 3)” has been very helpful for me to understand how to communicate between EV3 and a PC (Mac in my case). I have a specific need similar to what you had. I need to read the value of a text file (.rtf file) that is on the EV3 brick. And I need to do this using Python and Bluetooth. I have managed to get my Python application on Mac to communicate with the brick for playing a tone. However, I am wondering how I can send the direct command you have here (for opening, reading a closing a file) via Python to EV3 and process the reply. If I encode the direct command in a variable and use file write on Python (say, using bt.write(command_variable)) then since Python file write does not have a return value, how do I extract the direct command reply? Please help as I’m stuck at this point. Thanks, Aakaash.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s