I'm using an Arduino MEGA with an Ethernet Shield and the arduino.cc Ethernet library for a custom TCP-based message-exchanging chat-like protocol. Specifically I'm using the Client class with its print() and println() methods. It works.
But on each single print() or println() call a whole TCP packet is sent. Also even if just one byte is printed, a whole TCP packet is sent over the IP layer. I mean there's a lot of TCP packets with very few data inside.
My question is that if there is any way to send several different data by combining them in a single packet? I don't want to loose time by printing each data seperately; instead I want to send 20*24bit data all at once. Is that possible?
Yes. Construct one long character array and send it with one print.
Do you have an example code to give me gateway through the subject?
Thank you for your help, I found the way
look at this. the BufferPrinter class collects the strings. and then the buffer is sent as one string
class BufferPrinter : public Print {
char* buffer;
size_t size;
size_t pos;
public:
BufferPrinter(char* _buffer, size_t _size) {
buffer = _buffer;
size = _size - 1;
pos = 0;
}
void reset() {
pos = 0;
}
size_t length() {
return pos;
}
virtual size_t write(uint8_t b) {
if (pos == size)
return 0;
buffer[pos++] = b;
buffer[pos] = 0;
return 1;
}
};
// example
void setup()
{
Serial.begin(115200);
char buff[100];
BufferPrinter bp(buff, 100);
bp.println("abcd");
bp.println(1.234);
Serial.print(buff);
Serial.print("length ");
Serial.println(bp.length());
}