Hi, I'm trying to create a code with arduino that detects a string sent from the visualstudio console and based on the string received, enters a specific loop by responding. all via LAN. From the Visualstudio console I can detect the string sent by arduino, but from arduino I can't convert the received buffer into ASCII. the lan module is an ENC28J60 and I'm using the ethercard library.
Below are the codes for both.
ARDUINO
#include <EtherCard.h>
#include <EthernetUdp.h>
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
byte Ethernet::buffer[700];
static uint32_t timer;
String stringa="ok";
const byte remote[] = {192,168,1,91};
const int dstPort PROGMEM = 8080;
//static byte ip[] = {192,168,1,91};
const int srcPort PROGMEM = 8080;
EthernetUDP Udp;
//char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
void setup () {
Serial.begin(57600);
// Change 'SS' to your Slave Select pin, if you arn't using the default pin
if (ether.begin(sizeof Ethernet::buffer, mymac, 53) == 0)
Serial.println( "Failed to access Ethernet controller");
if (!ether.dhcpSetup())
Serial.println("DHCP failed");
// ip = ether.myip;
ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);
/*if(ether.begin(sizeof Ethernet::buffer, mymac, 53)>0);
{
Serial.println(String(buffer));
}*/
/* if (!ether.dnsLookup(remote))
Serial.println("DNS failed");
ether.printIp("SRV: ", ether.hisip);*/
}
//char textToSend[] = "test 123";
void loop ()
{
word len = ether.packetReceive();
word pos = ether.packetLoop(len);
//Serial.println(pos);
char* request = (char *) Ethernet::buffer + pos;
if(strncmp("Merit", request, 5) != 0)
{
Serial.println(request);
//delay(1000);
ether.sendUdp("CIAO GAE", sizeof("CIAO GAE"), srcPort, remote, dstPort );
}
else{
ether.sendUdp("PORCO MONDO", sizeof("PORCO MONDO"), srcPort, remote, dstPort );}
}
C# VISUALSTUDIO
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class UDPListener
{
private const int listenPort = 8080;
private static void StartListener()
{
UdpClient listener = new UdpClient(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
try
{
while (true)
{
Console.WriteLine($"premere s per ricevere risposta");
string s = Console.ReadLine();
if (s == "s")
{
byte[] bytes = listener.Receive(ref groupEP);
//UdpClient invio = new UdpClient(8080);
byte[] send = Encoding.ASCII.GetBytes("Merit");
listener.Send(send, send.Length, "192.168.1.25",8080);
Console.WriteLine("Pacchetto inviato");
Console.WriteLine("Waiting for broadcast");
Console.WriteLine($"Received broadcast from {groupEP} :");
Console.WriteLine($" {Encoding.ASCII.GetString(bytes, 0, bytes.Length)}");
}
}
}
catch (SocketException e)
{
Console.WriteLine(e);
}
finally
{
listener.Close();
}
listener.Close();
}
public static void Main()
{
StartListener();
}
}