Arduino and C#


Overview

With the Kinect basics understood, it is time to understand how the Arduino will be interfacing with the C# program. The post outlines how a successful connection between the C# program acting as a client and the arudino acting as a server is successfully made.

Background

After the successful deployment of the Kinect sensor and receiving the data from the Kinect in a basic form. The next step was to determine if C# was a viable language to send data to arduino that will later be connected to the robot.

Activity

The first task is to understand how the arduino is going to receive the data. As the robot needs to be wireless then a USB (standard way to connect PC to Arduino) is not a viable options. The two options are Bluetooth or WiFi. Bluetooth has location restriction however using Wi-Fi it allows the robot to only be limited to a LAN/WAN connection.
The plan was to adopt a client/server scenario, to allow the arduino to receive data sent from the C# program attached to the Kinect. The diagram below illistrates the flow of data from Kinect to Robot.
Kinect Sensor --> C# Program --> Send data over network --> Arduino receive --> send command to robot.

The Arduino uses an Ethernet shield that has an RJ45 port to connect it the a network. By using a nano router it can act as a wireless receiver for the arduino as if it were connected straight in to the network via Ethernet cable.

To send the Kinect data through a network, the C# program will send using the TCP protocol. Firstly the C# program will act as a client connecting the arduino (server). To connect to the arduino I use the code below.

TcpClient tcpclnt = new TcpClient();
tcpclnt.Connect("192.168.0.177", 23);
The stream to send and receive data is then set up.

Stream stm = tcpclnt.GetStream();
Now that the lpathhas been set up to transmit the data the data has to be prepared.This involves encoding it to the correct format and adding to a byte array.

ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);

Once everything is in place the data can be sent using the code below. It send the data and information saying how the data is built.

stm.Write(ba, 0, ba.Length);

Now the data has been sent the arduino needs to understand it.
The arduino is configured by setting an IP and MAC address, as well as telling it what subnet and gateway to use. Using the code bellow;


byte mac[] = {

  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress ip(192,168,0, 177);

IPAddress gateway(192,168,1, 1);

IPAddress subnet(255, 255, 0, 0);

The server is then started and the arduino then starts to listen for bytes from the client.

Comments

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete

Post a Comment

Popular Posts