I need to be able to send data to the Arduino via the UDP. I searched for guides and I came across this Arduino Ethernet Board UDP communication between C# and Arduino - Interfacing w/ Software on the Computer - Arduino Forum. I tested it out and as I've understood based on the code it was supposed to run continuously but when I tried it, it sort of gets stuck after the first send and receive.
Here's the arduino sketch
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
//MAC on board: 90-A2-DA-0E-D9-91
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0E, 0xD9, 0x91 };
IPAddress ip(172, 29, 38, 80);
int led = 9;
unsigned int localPort = 8888;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
//Initialize EthernetUDP
EthernetUDP Udp;
char replyMsg[] = "Value Received";
char errMsg[] = "Problem";
void setup()
{
//Start Ethernet and UDP
pinMode(led, OUTPUT);
Ethernet.begin(mac,ip);
Udp.begin(localPort);
Serial.begin(9600);
}
void loop()
{
int packetSize = Udp.parsePacket();
char noNo[] = "Hello";
if(packetSize)
{
for(int i=0;i<UDP_TX_PACKET_MAX_SIZE;i++) packetBuffer[i] = 0;
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
char valIn[packetSize]; // = packetBuffer;
for(int i = 0; i < packetSize; i++)
{
valIn[i] = packetBuffer[i];
}
String theVal = valIn;
int strIndex = theVal.length();
if(theVal.substring(0,1) == "1")
{
digitalWrite(led, HIGH);
Udp.beginPacket(Udp.remoteIP(), 8888);
Udp.write("Flame On!");
Udp.endPacket();
}
else if(theVal.substring(0,1) == "0")
{
digitalWrite(led, LOW);
Udp.beginPacket(Udp.remoteIP(), 8888);
Udp.write("Flame off.");
Udp.endPacket();
}
else if(theVal.substring(0,1) == "3")
//Blinks the LED - off, on; ending off
{
digitalWrite(led, LOW);
Udp.beginPacket(Udp.remoteIP(), 8888);
Udp.write("Fade in...");
Udp.endPacket();
int i = 1;
for(int x = 0; x > -1; x = x + i)
{
analogWrite(led, x);
if(x == 255) i = -1;
delay(20);
}
Udp.beginPacket(Udp.remoteIP(), 8888);
Udp.write("Fade out...");
Udp.endPacket();
digitalWrite(led, LOW);
}
else
{
Udp.beginPacket(Udp.remoteIP(), 8888);
Udp.write(errMsg);
Udp.endPacket();
}
}
delay(10);
}
and this is the c# code:
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace SendReceiveUDP
{
class Program
{
static void Main(string[] args)
{
//Port and IP Data for Socket Client
var IP = IPAddress.Parse("172.29.38.80");
int port = 8888;
string inputVal = "1";
while (inputVal != string.Empty)
{
var udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
var sendEndPoint = new IPEndPoint(IP, port);
var receiveEndPoint = new IPEndPoint(IP, port);
var clientReturn = new UdpClient(port);
inputVal = string.Empty;
inputVal = Console.ReadLine();
if (inputVal == string.Empty) break;
try
{
int inputInt;
int.TryParse(inputVal, out inputInt);
// Sends a message to the host to which you have connected.
Byte[] sendBytes = Encoding.ASCII.GetBytes(inputInt.ToString());
udpClient.SendTo(sendBytes, sendEndPoint);
string returnData = string.Empty;
// Blocks until a message returns on this socket from a remote host.
Byte[] receiveBytes;
receiveBytes = clientReturn.Receive(ref receiveEndPoint);
returnData = Encoding.ASCII.GetString(receiveBytes);
// Uses the IPEndPoint object to determine which of these two hosts responded.
Console.WriteLine("Message Received: " +
returnData.ToString());
if (inputInt == 3)
{
receiveBytes = clientReturn.Receive(ref receiveEndPoint);
returnData = Encoding.ASCII.GetString(receiveBytes);
// Uses the IPEndPoint object to determine which of these two hosts responded.
Console.WriteLine("Message Received: " +
returnData.ToString());
}
udpClient.Close();
clientReturn.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
}
Can anyone tell me what's wrong and what I need to do to get this to work?
and by the way can I receive UDP packets and then send stuff via the serial port at the same time?