Goal:
To write a program to transfer files to/from the netduino SD card to a PC.
Background:
After moving the SD card back and forth from my PC to the netduino, I ran into a problem where the PC claimed that the SD card was locked, but the netduino had no problem writing/reading to it. This issue, along with the fact that transferring the SD card between the netduino and a PC is a pain, prompted me to write a small program that can transfer files over the network.
Introduction:
The program consists of two parts: a server that runs on the netduino and a client that runs on the PC. The server listens for commands sent over the network and responds to the requests that it receives. The server is capable of executing the following commands:
List – lists the files that are on the netduino SD card
Get – transfers a file from the netduino to the PC
Put – transfers a file from the PC to the netduino
Delete – deletes a file from the netduino SD card
Implementation:
First, in order to be a good citizen, I spawn off a new thread for the file server. This is because socket calls can take a large amount of time and are blocking, so this leaves it open for something else to be going on. (This program doesn’t take advantage of that, but future ones might…) This is the same trick that was used in the netduino web server.
When a client makes a request, the first thing that is sent over is the type of request that they wish to perform, one of the four options listed above. After that a string is sent. The string is either a directory, in the case of list, or a file name in all of the other cases. In order to send that, first the length of the string is sent, then followed by the string, so that the server knows when to stop. The depending on the request that is made, the data is either returned – in the case of list or get, the server waits for more data – in the case of put, or the server executes the command – in the case of delete.
The actual transferring of data isn’t that exciting. In a similar fashion to the way that the file name is sent over, first the size of the transfer is sent followed by the actual data. The only tricky part was that since the netduino doesn’t have that much memory, I tried to keep the transfer sizes small. Unfortunately, by doing this, I ran into an issue where the socket buffer is full and I’m trying to make a send call and I get a socket error #10055 (WSAENOBUFS). The workaround to the problem is to issue a sleep after sending a small amount of data and giving the netduino a chance to clear out its buffer. This seemed to work very well, but it slows down the transfer of the file. I also tried sending larger chunks of data, but I had much better luck with adding a sleep.
The client code is a mirror of the server code, so there isn’t really anything new in there. If you have any questions, check out the attached code or ask a question in the comments.
Summary:
In this post, I described a simple netduino file server that can be used to quickly and seamlessly transfer data between the netduino and a PC over Ethernet. This means that you don’t have to remove the SD card every time you want to transfer data back and forth. This also opens up the possibility to use the netduino a network file store to transfer files back and forth between home and work. However, if you are going to do that, you should probably put some kind of authentication so that a third party can’t steal or delete your data. (Potential future project!)