I'm new in this forum and I've bought my first NETduino plus a few weeks ago (I have a little experience with c# and other uCPU but I'm totally newbie with Arduino/NETduino).
I' writing 2 projects:
1) a server (on NETduino) waiting for a TCP connection and able to manage 2 simple commands: LED ON and LED OFF.
2) a client (on my PC) to establish a TCP connection with the server and able to send the same 2 commands.
This is the code on the server:
public void Listerer() { bool Loop = true; bool RxLoop = true; using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { socket.Bind(new IPEndPoint(IPAddress.Any, mConfig.Port)); Debug.Print("Waiting connection..."); socket.Listen(5); while (Loop == true) { using (Socket commSocket = socket.Accept()) { RxLoop = true; while (RxLoop == true) { if (commSocket.Poll(1, SelectMode.SelectError) == true) { Debug.Print("SOCKET ERROR DETECTED"); } if (commSocket.Poll(1, SelectMode.SelectRead) == true) { if (commSocket.Available == 0) { // Disconnected! Debug.Print("Disconnected"); RxLoop = false; } else { int BuffSize = commSocket.Available; byte[] bytes = new byte[BuffSize]; int count = commSocket.Receive(bytes); if (count > BuffSize) { Debug.Print("ERROR! : Buffer overflow"); PowerState.RebootDevice(false); } if (count == 0) { Debug.Print("ERROR! : Rx empty string"); continue; } string s = new String(Encoding.UTF8.GetChars(bytes)); [here I clean the RX buffer: trim, remove <space>, <newline>, etc etc...] if (s == "LEDON") { [Onboard LED ON] commSocket.Send(StrToByteArray("OK" + NewLine)); } else if (s == "LEDOFF") { [Onboard LED OFF] commSocket.Send(StrToByteArray("OK" + NewLine)); } } } } } } } }
and here the code for the client:
client = new TcpClient(sIPAddress, iPort); stream = client.GetStream(); do { [TX] bBuffer = System.Text.Encoding.ASCII.GetBytes("LEDON"); stream.Write(bBuffer, 0, bBuffer.Length); stream.Flush(); [RX] Int32 bytes = stream.Read(bBuffer, 0, bBuffer.Length); [TX] bBuffer = System.Text.Encoding.ASCII.GetBytes("LEDOFF"); stream.Write(bBuffer, 0, bBuffer.Length); stream.Flush(); [RX] Int32 bytes = stream.Read(bBuffer, 0, bBuffer.Length); } while(1);
I was expecting to see the LED flashing very fast but actually it flashes once per second! Why so slow?
Here what is going on the LAN (thanks Wireshark!):
No. Time Source Destination Protocol Length Info 7 0.225345 192.168.1.141 192.168.1.21 TCP 54 57189 > http [ACK] Seq=9 Ack=9 Win=65384 Len=0 8 0.503487 192.168.1.141 192.168.1.21 HTTP 63 Continuation or non-HTTP traffic 9 0.515869 192.168.1.21 192.168.1.141 TCP 60 http > 57189 [ACK] Seq=9 Ack=18 Win=1007 Len=0 10 0.518783 192.168.1.21 192.168.1.141 HTTP 63 Continuation or non-HTTP traffic 11 0.725376 192.168.1.141 192.168.1.21 TCP 54 57189 > http [ACK] Seq=18 Ack=18 Win=65375Len=0 12 1.005504 192.168.1.141 192.168.1.21 HTTP 62 Continuation or non-HTTP traffic 13 1.009187 192.168.1.21 192.168.1.141 TCP 60 http > 57189 [ACK] Seq=18 Ack=26 Win=999 Len=0 14 1.011983 192.168.1.21 192.168.1.141 HTTP 62 Continuation or non-HTTP traffic 192.168.1.141: PC 192.168.1. 21: NETduino
Replay is very fast but seems each command is split in 2 part and it took about 300ms between the 1st and 2nd part.
I have attached the whole projects (there is a Scan function to search all NETduino boards on the network, maybe someone could be interested in it...).
Please help!
Rusko