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:
[checklist]
- 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
[/checklist]
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, ProtocolType.Tcp); listeningSocket.Bind(listeningEndPoint); listeningSocket.Listen(10); |
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)); serverThread.Start(); |
To avoid blocking the program, start a new Thread running the SocketHandler object that will manage each incoming connection:
try { // 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); break; } } |
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.
Tips
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 }); else { 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:
listeningSocket.Close(); serverThread.Abort(); log("Listening stopped"); |
Ciao Luca,
non riesco a trovare lo sketch puoi darmi maggiori indiaczioni? Puoi anche fornire l’eseguibile c#?
Grazie
ciao, trovi tutto nel Repository su GitHub.
Domanda, non ho capito a cosa serve:
>> Stash::prepare(PSTR(“$H”), sd);
?
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…).
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
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?
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?
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!
Hello!
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.
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!
Hi Maximiliano,
what do you mean for “asyncronous” way? c# side, using the non-blocking socket methods?
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
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?
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
http://dereenigne.org/wp-content/uploads/ENC28J60.jpg
Hi Luca, i got it solved, it’s the Ether Card new Library, i use the old library form here https://github.com/vworp/OldEthercard
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)
Good! Very strange the new library doesn’t work… anyway I’m working on a bidirectional example!
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!
I think the buffer is full. can you tell me how to clear the buffer?
thanks!
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?
Hi Rafael, what help do you need?
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.
Can you please put libraries you used on github or just send them to my email. nmarbm@gmail.com. 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.
Hi! The library I use for my shield is the Ethercard one from Jeelabs: https://github.com/jcw/ethercard
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?
corretto, la porta è configurabile da GUI (nel mio esempio è la 1000)