Click here to Skip to main content
15,888,025 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there, still not mature in c# programming, i have written an application in which it receives data from arduino using UDP protocol, when i click a button data is sent and received without hurdle, but i want my application to receive data automatically when arduino sends it. so that i wont click any button or i dont have to trigger any events manually. here is the total code. PS im using Visual Studio 2010 Express.


is there a way to receive data only when arduino sends it?
please help

What I have tried:

C#
private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Socket SOCK = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                SOCK.ReceiveTimeout = 2000;
                SOCK.SendTimeout = 2000;
                IPAddress serverAddres = IPAddress.Parse("192.168.1.177");
                IPEndPoint endPoint = new IPEndPoint(serverAddres, 23);
                String text = textBox1.Text;
                byte[] sendBuffer = Encoding.ASCII.GetBytes(text);
                SOCK.SendTo(sendBuffer, endPoint);
                byte[] recievedByte = new byte[1024];
                int bytesrec = SOCK.Receive(recievedByte);
                textBox1.Text = Encoding.UTF8.GetString(recievedByte, 0, bytesrec);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
            }

        }
Posted
Updated 20-Nov-18 21:52pm

1 solution

You need to attach the async callback1, that receives the data for your socket, on its behalf and then alerts you that data has been received. You cannot attach a receive function, as it might be blocking. You can argue, that,
C#
// Your receive call is useless as well, and so would be this, 
SOCK.Receive(buffer, offset, SOCK.Available, SocketFlags.None);
would read the bytes, but no, because even in this case, Available will be 0 as the device might have not yet sent a message, thus it will return, acting as a non-blocking call. A good approach is to use a callback, and do a BeginReceive(...), and asynchronously read from the wire,
C#
// Code from Microsoft documentations
// Begin receiving the data from the remote device.  
client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,  
     new AsyncCallback(ReceiveCallback), state);  
Please review this documentation to learn a bit more on the topic, Asynchronous Client Socket Example | Microsoft Docs[^], and if you are writin this as a server application, then go through this, Asynchronous Server Socket Example | Microsoft Docs[^]

1. I italicized that, because it is funny to consider a callback, and asynchronous approach in 21st century.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900