Problems receiving string from visualstudio

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();
    }
}

I don't see any attempt to do that in your code. As far as I can see, you only echo it to the serial port.

exactly, I have to take the byte received from the pc, see if the sent text = Merit and enter the cycle. this is my bug. then the arduino communicates quietly with the pc.

char* request = (char *) Ethernet::buffer + pos;
   if(strncmp("Merit", request, 5) != 0) 
   {  
   Serial.println(request); // I need to see what I get
   //delay(1000);
   ether.sendUdp("CIAO GAE", sizeof("CIAO GAE"), srcPort, remote, dstPort );
   }

Bug, or missing feature? By the way, does it at least print, "Merit"?

if I could print it I would not have written on this forum.

You mean like

  char* request = (char *) Ethernet::buffer + pos;
  Serial.println(request);

at the appropriate point fails?

Is it null terminated?

a7

That is a strange remark. All you need to do is print it? I thought you had additional expectations... an inference like that can only pertain to yourself, as you haven't made your problem/goal 100% clear. You never actually said, "it doesn't print".

I'll explain briefly. From the PC I have to send a message to Arduino, he has to take this data, read it and check a condition. verified this condition must send a message to the pc. Ex: when the cigarettes are about to run out (the infrared sensor detects that he has no obstacles in front of him, in this case a pack of cigarettes) he has to send a message saying that the cigarettes are running out. so the PC sends what status are the merits? and he has to answer. in a nutshell this is what I have to do.

I get strange symbols ex: °¥^$??. I was thinking isn't that maybe, since visualstudio sends an ASCII encoding Arduino can't enjoy it?

No, the Arduino serial port can perfectly communicate ASCII. It's possible that your baud rates are set incorrectly.

So, it's a missing feature?

At this point maybe write a loop and print as a numbers the values stored starting with what request points at and, say, the next ten values after that.

It might not be M e r i t 0. Untested, sry:

    char *p = request;
    for (int idx = 0; idx < 10; idx++, p++) {
        Serial.print((int) *p);
        Serial.print(" ");
    }
    Serial.println("");

to see if you get the ASCII codes and the terminating 0 you might be expecting.

I assume your other serial printing is correct, that means your baud rate matches the monitor window setting.

a7

as soon as i send the command from visualstudio i get this

Please what code produces that output?

I see the output you edited, I assume that was printing ten numbers with the code I posted, or something like it.

And those have no resemblance to the output you replaced it with.

Hence my question.

I encourage you to see where I am headed and attempt some variations on the experiments. I am under the umbrella not in my lab just now, but it seems that the characters starting at pointed to by request are not what you think ethernet should be serving you.

I am curious about this sequence

word len = ether.packetReceive();
word pos = ether.packetLoop(len);
//Serial.println(pos);
char* request = (char *) Ethernet::buffer + pos;

where you seem sure about where what you want to receive is. In the buffer. And that it is valid at the time you look.

I tried when I was at the big rig, but didn't take enough time to see what those calls on the ether object mean.

BTW it would be better to cut and paste any output you want us to examine. Squinting at screen shots is hard enough, but screen shots do not allow for easy manipulation with text editing tools. Not gonna type in numbers from a screen shot...

a7

this is what i get with your code

-1 -84 -64 13 -92 -100 116 105 105 45 
-120 -84 -64 13 -92 -100 116 105 105 45 
-120 -84 -64 13 -92 -100 116 105 105 45 
-120 -84 -64 13 -92 -100 116 105 105 45 
-120 -84 -64 13 -92 -100 116 105 105 45 
-1 -84 -64 13 -92 -100 116 105 105 45 
-120 -84 -64 13 -92 -100 116 105 105 45 
-120 -84 -64 13 -92 -100 116 105 105 45 
-1 -84 -64 13 -92 -100 116 105 105 45 
-1 -84 -64 13 -92 -100 116 105 105 45 
-1 -84 -64 13 -92 -100 116 105 105 45 
-1 -84 -64 13 -92 -100 116 105 105 45 
-1 -84 -64 13 -92 -100 116 105 105 45 

obviously taking the ascii code table doesn't match what i sent. But I know it's certain that from visualstudio the bytes [] start correctly encoded.
here is the string i use to convert the word merit from visualstudio:

byte[] send = Encoding.ASCII.GetBytes("merit");

So how did you produce the other output?

Srsly, get in this game and start helping yourself. Even a casual reading of my previous post would give you a clue about why this is still a mystery to us out here and what more you might say about what is happening, how those ether methods are serving you.

Sry, impatient. It just seems you are sure everything is correct, but it isn't working…

a7

I don't say it's right, but when I in the c# code I put breakpoints I see what is doing the string of code and the conversion. now i'll show you

		[0]	109	byte
		[1]	101	byte
		[2]	114	byte
		[3]	105	byte
		[4]	116	byte

here is all the stuff

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")
                {
                    
                    //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");

                    byte[] bytes = listener.Receive(ref groupEP);
                    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();
    }

Taking no hints at all. The problem seems to be on the receiving end.

How do you know you are looking in the right place for those five characters?

a7

ip arduino

 listener.Send(send, send.Length, "192.168.1.25",8080);

Sorry, don't wanna just ghost you. It is clear that I cannot help. Good luck.

a7