Pages: [1] 2 3   Go Down
Author Topic: How fast ist client.wirte() on MEGA  (Read 1870 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

I try to send a Serial stream from Serial3 (MEGA) direct to the Ethernet BreakOut in a while loop. It turns out, the serial3.read() is much more faster (serial.begin(115200)); than the SPI attached client.wirte() function. The 63 Byte buffer on Serial3 is full after 3 while cycles. Does anyone know how fast the client.print(c); function shifts data out over SPI to the ethernet chip?
I used to have a client.write(HEADERSTRING) with the F() function. But this takes me directly to a full rx buffer.

Here's my template code:
Code:
EthernetClient client.
serial3.begin(115200);
char c;

//SEND REQUEST
serial3.print(REQUESTSTRING);
while (do wait until CHARSTART is arriving) {}

// SREAM RESPONSE TO ETHNERT SHIELD
while (serial3.available()) {
 c=serial3.read();
 if (c==CHAREND) break;
 else client.print(c);
 //for debug over serial 1 to my pc only:
 serial.println(serial3.available());
}
« Last Edit: February 11, 2013, 09:14:51 am by markuszelg » Logged

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19350
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't see any SPI transmission there, and I doubt that even that snippet would compile, as it is "write" not "wirte".

Read this before posting a programming question

Please post all your code.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50924
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In addition, the fact that you are Serial.print()ing in the loop will slow down the loop.

You are better off collecting the data from the serial port into an array, and client.print()ing the whole array.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@PaulS: whole array: hmm.. may by my architecture is wrong: I do a external eeprom dump from a attached nano over serial to the MEGA. this will take me up to 1KB of data. buffing this amount of data is hard with MEGA. right?

@Nick: client.write() will write over SPI, no? I was not looking into #include <SPI.h> #include <Ethernet.h>. But one may find out, the SPI will go this way.

Does anyone know how fast the SPI port communication speed. I was once (a long time ago) reading 10Mhz.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50924
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
@PaulS: whole array: hmm.. may by my architecture is wrong: I do a external eeprom dump from a attached nano over serial to the MEGA. this will take me up to 1KB of data. buffing this amount of data is hard with MEGA. right?
Yes, and no. Yes, a 1K buffer is a real challenge.

But, why not establish handshaking with the remote device? Send it a request for data, say 64 bytes. Collect those 64 bytes. Send them to the client. Ask the remote device for 64 more bytes. Collect them, send them, lather, rinse, repeat.

There is no reason that the remote device has to tell its whole life store without pausing to breathe.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@PaulS: you're right. When my direct streaming design cannot handly timing issues, I do need to switch to this 64byte-sample approach. However, I will need to redisign my protocol. That's to say, it's only sending full information today.

Knowing the speed of ethernet SPI, I could go down one step with my serial speed. Does anyone knows about the speeds?

Update: default for SPI is 4 Mhz see http://arduino.cc/en/Reference/SPI. I wonder why my code accumulate the rx buffer. Maybe the serial3.available() takes as much time as serial3.read(). Any ideas?
« Last Edit: February 11, 2013, 07:02:01 am by markuszelg » Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why do you mention SPI at all? I don't see any SPI I/O happening here.

You're reading characters from a hardware serial port Serial3. For every received character, you print several characters on a different serial port Serial1.

Since you HAVEN'T SHOWN US ALL YOUR CODE we can't see how those serial ports were configured but I will guess they're running at the same speed. So naturally you will find that the output stream is the limiting factor and prevents your sketch from processing the input stream at its full speed.

This could be made to work if you change your sketch to only print one output character per input character, although it wouldn't be very resilient. For a more robust solution you would ensure that the output stream was at less than 100% capacity when the inputs were at 100% capacity. The best way to do that would be to run the output port at a higher speed.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6190
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Does anyone know how fast the client.print(c); function shifts data out over SPI to the ethernet chip?
If this is an ethernet shield, I see SPI I/O. Should I point it out to everyone?

edit: Listen to PaulS. This will certainly help. Send the array as one packet, instead of each character in its own packet.
Quote
You are better off collecting the data from the serial port into an array, and client.print()ing the whole array.
« Last Edit: February 11, 2013, 08:45:46 am by SurferTim » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, the ethernet shield. as in http://arduino.cc/en/Main/ArduinoEthernetShield.
I still have no clue how fast the SPI is in respect to the Serial.read() when setting up the serial.begin(115200). Any ideas.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For a more robust solution you would ensure that the output stream was at less than 100% capacity when the inputs were at 100% capacity. The best way to do that would be to run the output port at a higher speed.
That's exactly what I'm asking for. What's the speed of client.write() in respect to serial3.read() setting up by 115200bps.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6190
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think you are limited to 4MHz on the SPI bus with the w5100. However, sending an array of characters is much more efficient that one character per packet. The overhead becomes a real speed problem.
Code:
// this takes 6 packets
client.print('h');
client.print('e');
client.print('l');
client.print('l');
client.print('o');
client.println();

// this takes one packet
client.println("hello");

The w5100 4MHz SPI speed should handily outrun the serial port's 115200.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

!! UPDATE in code on the top of this post with some general settings to clearify my request.

@SurferTim: Just for be clear: You're saying the function call to 'client.print' is takeing more cpu runtime than it would be with serial3.read(). In my case: jumping during the while-loop every time to client.print will take more time (@16Mhz ATmega2560) than the serial3 will shift into rx buffer?
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6190
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That could be the case. Every call to the following functions generates a separate packet.
client.print()
client.println()
client.write()

There are at least 48 bytes of overhead with each packet, plus the time required to send them individually.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What about this:
Code:
byte counter=0;
char buffer[15];

while (serial3.available()) {
 c=serial3.read();
 if (c==CHAREND) break;
 else {
   buffer[counter]=c;
   if (counter==15) {
    client.print(buffer);
    counter=0;
   }
   else counter++;
}
Then of course I need the handle the half empty buffer with a '\0' char at the end....
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6190
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That look almost like mine!  smiley

edit: Insure you empty that after you have received all the serial data. The last buffer may not fill up, so transmit what you have before closing the connection.
Logged

Pages: [1] 2 3   Go Up
Jump to: