Hello,
I have an old ethernet shield (with the full-size SD card slot) and an arduino duemilanove (atmega328p), when I upload the ChatServer example and I open a connection with telnet it seems that my arduino gets a lot of garbage even before I start to type in the terminal.
My serial terminal outputs:
We have a new client
ÿý
ÿû
ÿû
ÿû ÿû!ÿû"ÿû'ÿý
ÿû#ÿû
ÿþ
ÿþ
ÿþ ÿþ!ÿþ"ÿþ'ÿü
ÿþ#Z[£¨vÁ
2?
?ñ|
?z|
? úP”æPÊ
_
\ÂÂ!QT?U4øØ1e?Q?JMù
?Õ!+
+ªcé%a?´Ú_E«Ø
¯?n)A”
¿Hãd?¤ðgÿB
And my telnet:
mig@laptop ~ $ telnet 192.168.1.177
Trying 192.168.1.177...
Connected to 192.168.1.177.
Escape character is '^]'.
Hello, client!
2[ch65533][[ch65533][ch65533]v
[ch65533][ch65533]|
[ch65533]z|
[ch65533]P[ch65533][ch65533]P[ch65533]
\[ch65533][ch65533]!QT[ch65533]U4[ch65533][ch1552]1e[ch65533]Q[ch65533]JM[ch65533]
[ch65533][ch65533]!++[ch65533]c[ch65533]%a[ch65533]_E[ch65533][ch65533][ch65533]
[ch65533][ch65533]n)A[ch65533]
[ch65533]H[ch65533]d[ch65533][ch65533][ch65533]g
[ch65533]*[ch65533]
0[ch65533]
[ch65533][ch65533][ch65533][ch65533]
[ch65533]V*[ch65533]
[ch65533]=,[ch1309];][ch65533][ch65533]
F[ch65533][ch65533][ch65533]P[ch65533]
\[ch65533][ch65533]!QT[ch65533]U4[ch65533][ch1552]1e[ch65533]Q[ch65533]JM[ch65533]
[ch65533][ch65533]!++[ch65533]c[ch65533]%a[ch65533]_E[ch65533][ch65533][ch65533]
[ch65533][ch65533]n)A[ch65533]
[ch65533]H[ch65533]d[ch65533][ch65533][ch65533]g
[ch65533]*[ch65533][ch65533][ch65533]
[ch65533]V*[ch65533]
[ch65533]=,[ch1309];][ch65533][ch65533]
F[ch65533][ch65533]M[ch65533]
[ch65533][ch65533]!++[ch65533]c[ch65533]%a[ch65533]_E[ch65533][ch65533][ch65533]
[ch65533][ch65533]n)A[ch65533][ch65533][ch65533]\[ch65533]
[ch65533] [ch65533][ch65533]0[ch65533]s<[ch65533]1w[ch65533]e[ch65533]9@e[ch65533][ch65533]#
[ch65533]H[ch65533]d[ch65533][ch65533][ch65533]g
[ch65533]*[ch65533]
0[ch65533]
[ch65533][ch65533]8[ch65533][ch65533]Sa[ch65533][ch333]Zn[ch65533]A[ch357][ch65533][ch65533]
[ch65533]V*[ch65533]
[ch65533]=,[ch1309];][ch65533][ch65533]
F[ch65533][ch65533][ch65533]n)A[ch65533]$[ch65533][ch65533]\[ch65533]
[ch65533] [ch65533][ch65533]0[ch65533]s<[ch65533]1w[ch65533]e[ch65533]9@e[ch65533][ch65533]#
[ch65533]H[ch65533]d[ch65533][ch65533][ch65533]g
[ch65533]*[ch65533]
0[ch65533]
[ch65533][ch65533]8[ch65533][ch65533]Sa[ch65533][ch333]Zn[ch65533]A[ch357]3y
U
'a[ch844][ch65533][ch65533]
[ch8719]v4[ch65533][ch65533]x[ch65533][ch65533]"#[ch65533][ch65533]
[ch65533]V*[ch65533]
[ch65533]=,[ch1309];][ch65533][ch65533]
F[ch65533][ch65533]
0[ch65533]
[ch65533][ch65533]8[ch65533][ch65533]Sa[ch65533][ch333]Zn[ch65533]A[ch357]3y
U
'a[ch844][ch65533][ch65533]
[ch8719]v4[ch65533][ch65533]x[ch65533][ch65533]"#[ch65533]}[ch65533]
\$[ch65533][ch65533]\[ch65533]
[ch65533] [ch65533][ch65533]0[ch65533]s[ch65533][ch65533]
[ch65533]V*[ch65533]
[ch65533]=,[ch1309];][ch65533][ch65533]
F[ch65533][ch65533][ch1309];][ch65533][ch65533]
F[ch65533][ch65533]^C[ch65533][ch65533]
[ch65533][ch65533]
[ch65533][ch65533]
[ch65533][ch65533]
[ch65533][ch65533]
^]e[ch65533][ch65533]#[ch65533][ch65533]0[ch65533]s<[ch65533]1w[ch65533]e[ch65533]9@e[ch65533][ch65533]#
telnet> Connection closed.
Playing with the code I found that checking the number of bytes recived by the client fixes (to some extent, still gettin a little bit of garbage) this issue, these are my modifications to ChatServer:
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1, 177 };
byte gateway[] = { 192,168,1, 1 };
byte subnet[] = { 255, 255, 0, 0 };
// telnet defaults to port 23
Server server(23);
boolean gotAMessage = false; // whether or not you got a message from the client yet
void setup() {
// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);
// start listening for clients
server.begin();
// open the serial port
Serial.begin(9600);
}
void loop() {
// wait for a new client:
Client client = server.available();
// when the client sends the first byte, say hello:
if (client) {
if (!gotAMessage) {
Serial.println("We have a new client");
client.println("Hello, client!");
gotAMessage = true;
}
char availableChars = client.available();
if (availableChars > 0) {
Serial.print("got: ");
Serial.println(availableChars, DEC);
}
while (availableChars) {
// read the bytes incoming from the client:
char thisChar = client.read();
// echo the bytes back to the client:
server.write(thisChar);
// echo the bytes to the server as well:
Serial.print(thisChar);
availableChars--;
}
}
}
My serial terminal outpus:
We have a new client
got: 27
ÿý
ÿû
ÿû
ÿû ÿû!ÿû"ÿû'ÿý
ÿû#got: 27
ÿû
ÿþ
ÿþ
ÿþ ÿþ!ÿþ"ÿþ'ÿü
ÿþ#got: 1
hgot: 1
ogot: 1
lgot: 1
a
And my terminal outputs:
mig@laptop ~/files/arduino-0021 $ telnet 192.168.1.177
Trying 192.168.1.177...
Connected to 192.168.1.177.
Escape character is '^]'.
Hello, client!
hhoollaa
As you can see, I am using the char type for availableChars, this is "wrong" because the available method in Client class must output an integer, but if I use an integer I still get a lot of garbage (bad recived size) before I send something in the terminal.
Diggin furter in the code I see that the W5100 has a 16 bit register to store the size of the recived buffer. ¿Whats the deal here? My intuition tells me that there is some problem when the Ethernet library reads the SnRX_RSR register in the W5100, the datasheet (version 1.1.6) says the SnRX_RSR register is unsigned... but before diggin more i like to ask
Thanks a lot.