Udp.print () issue

Hi, I found anomaly, witch does a lot of hard time to me.
I am reading serial data from a machine, and send via UDP. There is a long stream, wich i had to put into a String type, and sort by lines (;). shorting works fine and nice. But after sendig the String type with the udp.print(String) method it chops off after+ 7 bytes. I switchet to .write() method and using char array conversion witch resulted to the udp send the same chop.
If I send the same method, but with fixed payload it is working. The payload is like this: 1.20E+3
04:10:00
If i write the String type variable to the serial monitor it is okay, but the same variable in udp.print() then it looks like in the network as 1.20E+3.



/*
  UDPSendReceiveString:
  This sketch receives UDP message strings, prints them to the serial port
  and sends an "acknowledge" string back to the sender

  A Processing sketch is included at the end of file that can be used to send
  and received messages for testing with a computer.

  created 21 Aug 2010
  by Michael Margolis

  This code is in the public domain.
*/



#include <Ethernet.h>
#include <EthernetUdp.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};


unsigned int localPort = 55794;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  // buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged";        // a string to send back



int i;
int availableBytes;
int stringlenght;


String readstring, DataReadString;


// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {

  Ethernet.begin(mac);
  Serial.begin(115200);
  Serial1.begin(115200);

  Serial.println("Initialize Ethernet with DHCP:");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    } else if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
    // no point in carrying on, so do nothing forevermore:
    while (true) {
      delay(1);
    }
  }
  // print your local IP address:
  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());
  Serial.println(Ethernet.subnetMask());
  Serial.println(Ethernet.gatewayIP());








  // Open serial communications and wait for port to open:







  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }

  // start UDP
  Udp.begin(localPort);
}

void loop() {
  // if there's data available, read a packet
  
    if (Serial.available() > 0)
    {
      Serial.print("I read from Serial:");
      Serial.println(Serial.read());
      if ( Serial.read() == 'a')
      {
        Serial.println("teststring");
        Udp.beginPacket("193.6.179.115", 55793);
        Udp.print("1.20E+304:02:00");
        Udp.endPacket();
      }
    }
  

  int packetSize = Udp.parsePacket();

  if (packetSize) {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i = 0; i < 4; i++) {
      Serial.print(remote[i], DEC);
      if (i < 3) {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    Serial1.println(packetBuffer);

  }

  if (Serial1.available() > 0) {
    DataReadString = Serial1.readStringUntil(';');
    //DataReadString = Serial1.readString();
    stringlenght = DataReadString.length();
    Serial.print("String lenght to send:");
    Serial.println(stringlenght);
    Serial.println(DataReadString);

    //szét kell választani a stringeket mert kb 145 byte ot tud küldeni a könyvtár, a teljes kiolvasás meg tele buffer esetén majdnem 5k!

    // send a reply to the IP address and port that sent us the packet we received

    Udp.beginPacket(Udp.remoteIP(), 55793);
    Udp.print(DataReadString);
    Udp.endPacket();




    if (packetSize) {                                 //Végén töröljök az UDP csomag tartalmát!
      memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE);
    }

  }
}

Hi,
Le length of your buffer 'UDP_TX_PACKET_MAX_SIZE' is not defined, is it normal ?

const int UDP_TX_PACKET_MAX_SIZE = 256; // 256... or your value !

For sending UPD-packets I'm using

#define MaxMsgLength 1024
createSafeString(UDP_Msg_SS,MaxMsgLength);

SafeString is a library which name is program. Safe to use Strings

createSafeString is based on an array of char but with almost the same comfort as the variable-type String

which allows things like

  FileNameDateTime_SS = "Code running comes from file \r\n";
  FileNameDateTime_SS += __FILE__;
  FileNameDateTime_SS += "\r\ncompiled ";
  FileNameDateTime_SS += __DATE__;
  FileNameDateTime_SS += " ";
  FileNameDateTime_SS += __TIME__;  

concanating integers and floats and string-constants etc.

  UDP_Msg_SS = Header_SS;
  UDP_Msg_SS += HeaderDelimiter;
  int my DemoInt = 1234;
  UDP_Msg_SS += DemoInt;
//etc.

after concenating it all
I do a typecasting to uint8_t and then use the command Udp.write

  Udp.beginPacket(remoteIP, remotePort);
  Udp.write(  (const uint8_t*)UDP_Msg_SS.c_str(),  UDP_Msg_SS.length() );
  Udp.endPacket();    

best regards Stefan

Sadly no, but thanks for the tip.

Thanks for this solution, I gonna be use this in the future. But sadly i have the same output with this. I belive it is the UDP library not the string/String things the black sheep in my case.

Why does it show like this in the code?
Udp.print("1.20E+304:02:00");
Is there a strange character between the +3 and the 04?

Jeah, that was the problem, I found a CR there, but the UDP - Sender/Receiver Windows app doesn't handle the return.... Mea Culpa

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.