Handling messages from an Arduino over TCP/IP with MEF

This is a continuation from a previous post Handling messages from an Arduino with MEF .  If you have not read the first post I would encourage you to do so as I am assuming you have in this post.  In this post we will change out the RS232 Service and replace it with a Tcp Ip Service.  So the Arduino will communicate to the PC over the network instead of RS232.  Very little code will be changed on the .Net side of things because we have loosely coupled our communications between the two devices using MEF and interfaces. 

For this project you will need the Arduino Ethernet Shield.

First we need to create a new Class Library that will hold the implementation of the TcpIpCommunicationService.  I choose to call this library Arduino.TcpIpService. Of course you will need to reference the Arduino.Contract project and implement the ICommunicationService interface for this new service. Also you will need to remove the Arduino.Services.DLL from the build directory and remove the Arduino.Service project from the solution so that VS 2010 does not re-copy the DLL to the Output directory.  If you dont do this MEF will get pretty confused as to which service to use in order to satisfy the ICommunicationService Import.

Here is the first part of the TcpIpCommunicationService class.

image 

 

As you can see we need the MessageProcessors exposed as an import just like the RS232CommunicationService in the first post.  We also need to export the entire class as ICommunicationService just like the RS232CommunicationService.  The Connect sets up some threads for servicing the incoming requests on port 9999 and the Disconnect tears down the threads.  You might need to change the 9999 port on your system if it is in use.

Here is the second part of the class that shows the private methods used to process the incoming messages.

image

As you can see in the HandleClientComm method we grab the incoming data then iterate over our message processors and invoke them if a match is found.  This is the same thing we were doing in the RS232 implementation of the ICommunicationService.

The Arduino code had a few more changes so that it can send the messages over the network.  A few more global declarations and includes for the ethernet libraries.  Keep in mind you will need to change the ip and server addresses to match your network.  The setup method now establishes a connection to the machine that is listening for the messages.

image

The loop pretty much stayed the same except for using the ethernet libraries for sending the messages.

image

I do have to change the Console app from my first post because I did not properly close the communications once the user presses enter on the keyboard.  Since the TCP/IP communications uses threads I need to call the Disconnect on the service in order to shutdown the threads correctly. So after the ReadLine I added the call to Disconnect as shown below.

image

Conclusion

As you can see it was very easy to write a new implementation of the ICommunicationService and have an entirely different way of connecting two devices together without a lot of impact on the code.  Using MEF and Interfaces forces you to define what is communicated between two devices not how.  You can plug in the how later.

In my next post I am going to attempt to extend this further and create a UdpBroadcastCommunicationService.  The Arduino will broadcast it’s message on the subnet and multiple PC listeners will be able to receive the message.  I know I can do this with .Net no problem at all, but I haven’t done any UDP communications with the Arduino.

You can download the code for this post here: ArduinoAndMEF_Part2.zip

Add a Comment