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