Arduino to C# using socket

luca 31/01/2013 27

When you want to make Arduino “talk” on your network, you usually choose the HTTP protocol, that allows to use a web browser  (IE, Firefox…) as a client. Sometimes however you need Arduino to send data to other applications: in this case you can implement a socket connection.

This tutorial is split in two parts:

  • in the first one, I’m going to show how to write a socket application using C#
  • in the second one, I’m going to show how to write a sketch for your Arduino

Socket server in C#

The .Net framework  can work with sockets in two modes: synchronous and asynchronous.

Synchronous methods are blocking: the thread in which they are called stops until the method finishes. If, for example, you call the Receive() method to read data from a socket, that method will block the execution until some bytes are available or until a timeout triggers.

Asynchronous method instead ask the framework to manage the request, passing the callback object that has to be called at the end of the request. Usually those methods start with Begin (for example BeginReceive()). Programming using asynchronous methods is more complex: for this tutorial I’m going to show you the synchronous approach, but using a different thread to avoid blocking my application’s GUI.

The SocketServer application is therefore composed by two elements:

  • the GUI (Form1.cs) to interact with the user

  • the SocketHandler class (Program.cs) to receive socket connections from Arduino

My program will receive from Arduino the room temperature and it will display it both in text and in graphical form (with a progress bar); I also added a text area to write a debug log.

The Socket class

.Net framework uses the Socket class to manage incoming/outgoing connections.

First, include the package System.Net.Sockets, then – when the user clicks the Start listening button – configure a Socket instance to listen on the specified TCP port:

IPAddress listeningIp = IPAddress.Any;
IPEndPoint listeningEndPoint = new IPEndPoint(listeningIp, listeningPort);
listeningSocket = new Socket(listeningIp.AddressFamily, SocketType.Stream, 

Create a new IPEndPoint object, specifying the IP address on which to listen from connections (in our code Any, that is any network interface of your PC) and the port. Then create a new Socket object for TCP protocol. Finally bind that object to the endpoint and start listening, waiting for new incoming connections (10 is the maximum number of connections in the queue).

SocketHandler socketHandler = new SocketHandler(listeningSocket, this);
serverThread = new Thread(new ThreadStart(socketHandler.startListening));

To avoid blocking the program, start a new Thread running the SocketHandler object that will manage each incoming connection:

  // Wait for incoming connection
  handlerSocket = listeningSocket.Accept();
  handlerSocket.ReceiveTimeout = 5000;
  IPAddress clientAddress = ((IPEndPoint)handlerSocket.RemoteEndPoint).Address;

The Accept() method is blocking: the thread will wait for a new connection… when this arrives, set 5 seconds as read timeout and get the connecting client’s IP address.

while (true)
  // Save received bytes
  byte[] buffer = new byte[1024];
  int bytesRec = handlerSocket.Receive(buffer);
  message += Encoding.ASCII.GetString(buffer, 0, bytesRec);
  // Message completed? Parse it...
  if (message.Contains("\r"))
    myForm.setTemp(message.Replace('.', ','));
    myForm.log(" message received: " + message);

// Message completed? Parse it…
if (message.Contains("\r"))
myForm.setTemp(message.Replace(‘.’, ‘,’));
myForm.log(" message received: " + message);

Read the incoming data with the Receive method (this is blocking too: if no data is available in 5 seconds, a timeout exception is raised) until you receive the “carriage return” char (\r) that indicates the end of the message. Now you can parse what you received and display the temperature.


The complete source code is available on Github; before looking at the Arduino’s sketch here are a couple of tips:

- if you use more than one thread, you need to pay attention to how you update the GUI elements: these indeed can be updated only by the same thread that created them. From a different thread you therefore have to call delegate methods of the main thread:

myForm.setTemp(message.Replace('.', ','));
public delegate void setTempCallback(string temperature);
public void setTemp(string temperature)
  if (InvokeRequired) this.Invoke(new setTempCallback(setTemp), 
    new object[] { temperature });
    float floatTemp;

- calling the Abort() method of a thread waiting in Accept() state is not enough to close that thread; you have to invoke the Close() method of the Socket object before aborting the thread:

log("Listening stopped");

Pages: 1 2


  1. pixel 14/03/2013 at 15:23 - Reply

    Ciao Luca,
    non riesco a trovare lo sketch puoi darmi maggiori indiaczioni? Puoi anche fornire l’eseguibile c#?


    • luca 14/03/2013 at 21:18 - Reply

      ciao, trovi tutto nel Repository su GitHub.

  2. igor 03/05/2013 at 15:28 - Reply

    Domanda, non ho capito a cosa serve:
    >> Stash::prepare(PSTR(“$H”), sd);

    • luca 03/05/2013 at 20:45 - Reply

      ciao, serve a preparare il pacchetto che poi invierai con tcpSend(). Il contenuto del pacchetto è soltanto “sd”, ovvero l’oggetto stash creato prima che contiene la temperatura e il terminatore di riga \r. $H è un “segnaposto” per la variabile che specifichi dopo (“sd”). Se guardi gli altri esempi vedrai pacchetti più complessi, con una parte fissa (“GET…”) e alcune variabili ($H, $F…).

  3. wolf 25/09/2013 at 03:09 - Reply

    Hi Luca.
    nice tutorial, do the job very well.
    I’m write the PC side (your C#) in Perl. the work is the same, the look and feel not.

    Reading your tutorial, analizing the TCP packages, working with my perl socket script, I can see how the TCP port open and close for every temperature send by the arduino.

    It is possible (I don’t know how) open permanently the port and send the text over and over the same open port?

    It is possible open the port and do something bidirectinally (receive and send data over TCP to the arduino)

    anyway, Thanks a lot for every tutorial in our webpage. it’s rocks

  4. mourinhothinh 14/10/2013 at 03:26 - Reply

    hi Luca
    I had download you code. I replace the sensor data by code: float float_temp = 27.5; because i don’t have the sensor and the library.
    But when i build the code in arduino and C#, nothing happen. it’s not connect between the arduino and pc.
    Can you help me?

    • luca 23/10/2013 at 21:40 - Reply

      Hi, are you sure the IP address of Arduino and of your PC (running the demo app) are in the same network? Are you able to ping the Arduino address from your PC?

  5. mourinhothinh 20/10/2013 at 10:59 - Reply

    hi Luca,

    I want to send data from C# to arduino. i try function :
    ether.tcpReply() but the arduino don’t receive anything. can you help me?

    if you have any example can you post it to your website?
    thank so much!

    • luca 23/10/2013 at 21:36 - Reply


      you use the tcpReply() to send data FROM Arduino… to receive data (sent from a C# app) to Arduino you can follow the examples as it was a webserver, like this one.

  6. Maximiliano El,izondo 26/10/2013 at 15:25 - Reply

    Hi! Nice work! ;)
    I am working with Arduino and C# but I need to work in a asyncronous way… do you have any example? Do you know if there is some web site or book to find some good information? Thank you very much!

    • luca 26/10/2013 at 16:20 - Reply

      Hi Maximiliano,

      what do you mean for “asyncronous” way? c# side, using the non-blocking socket methods?

  7. Salinda 16/11/2013 at 17:54 - Reply

    Hi Luca i’m working on RFID with LAN, i downloaded your program, and c# code works fine when i test i with a c# client (on my LAN using different PC ), but my Arduino does not send any data, but i can ping to it, this setup is on LAN no internet is that a problem? Thanx in advance

    • luca 16/11/2013 at 19:53 - Reply

      Hi Salinda,

      no, it doesn’t need an Internet access to work… did you use the exact Arduino sketch that is in my repository? Which ethernet shield are you using?

  8. salinda 17/11/2013 at 03:14 - Reply

    no i change it litte, coz i dont have a dhcp so i use Static setup function, and for the float i give fixed value. my problem is pings works, but there is no coming data to c# , this is the shield i use

  9. Salinda 17/11/2013 at 04:37 - Reply

    Hi Luca, i got it solved, it’s the Ether Card new Library, i use the old library form here

    i test it with both dhcp and static works fine, new library is not working for both(at the moment),

    can you please help me on how to send a data back, like Acknowledgmentt back to Arduino when i recive a data.(ex RFID recived and display a username on lcd)

    • luca 17/11/2013 at 10:39 - Reply

      Good! Very strange the new library doesn’t work… anyway I’m working on a bidirectional example!

  10. mourinhothinh 20/11/2013 at 02:03 - Reply

    Hi luca,
    I have a propblem. I send data from arduino to C# app. I use ether.tcpSend() function like you example. It work. But it just work about 5 minute or send about 32 times and then it doesn’t work. it don’t send data from arduino(ENC28j60) to C# app any more. I don’t know how?

    thank so much!

  11. mourinhothinh 20/11/2013 at 10:07 - Reply

    I think the buffer is full. can you tell me how to clear the buffer?


  12. rafael 08/07/2014 at 01:51 - Reply

    Hello Lucas. I am working on a project where the Arduino is a client and have to send and receive data received by the serial port to ethernet ENC28J60 like the example you have in C #. Could you please help me?

    • luca 08/07/2014 at 08:11 - Reply

      Hi Rafael, what help do you need?

  13. rafael 08/07/2014 at 22:43 - Reply

    I just need to send and receive against a set of strings through an ethernet connection client, already tried several examples but none worked so they send the data but not receive anything.

  14. Bogdan 05/08/2015 at 12:18 - Reply

    Can you please put libraries you used on github or just send them to my email. I im using W5100 ethernet shield and cant really just upload your sketch to arduino adn I dont really understand code that well to change your code to work on my shield.

  15. Fernando 04/09/2019 at 04:53 - Reply

    Buon pomeriggio, nel codice di c # non riesco a trovare dove configurare su quale porta il software PC ascolta. È il PC che funge da server e Arduino come client, giusto?

    • luca 20/09/2019 at 09:25 - Reply

      corretto, la porta è configurabile da GUI (nel mio esempio è la 1000)

Leave A Response »

This website uses cookies to ensure you get the best experience on our website maggiori informazioni

Questo sito utilizza i cookie per fonire la migliore esperienza di navigazione possibile. Continuando a utilizzare questo sito senza modificare le impostazioni dei cookie o clicchi su "Accetta" permetti al loro utilizzo.