I’ll certainly give him a try. I haven’t even taken the cover of the source from the toolbox and really think I need a little more experience with netmf before I do. I might install VS2012 from my MSDN subscription and give your code a try.
Does anyone have any VS2010 winform code for a socket server that they’ve sucessfully used to talk to thie Roving xBee WiFly hardware?
I’ve built the client and server from the link you sent me and they are talking to each other.
The next step is to see if I can get the socket client on the netmf side to talk to this server.
I installed VS2012 but many of the methods you used in your blog post are only available if the client is Windows 8. If I struggle getting the socket server/client to work I’ll get a Windows 8 image built somewhere. I seem so close and am not about to give up now. I’ll keep you all posted.
Yes, the WinRT API for sockets is quite a bit simpler than what we’ve had before. I find it really easy to use. You can still do a socket client (or server) for the desktop using .NET 4.5 (which supports async/await as well). .NET 4.5 works on Windows 7 and Windows 8.
If you’re using something really old (like XP, which is 11 years old now), you’ll need to use .NET 4 or older.
in fact winRT socket handling is great loving the await and async modifiers. currently running up a demo project to use a spider as a server and then getting a metro app to connect as the client, the reverse of petes great robot example really.
Pete, I’ve really enjoyed your blog post but have hit a serious brick wall with the sockets stuff in VS2010. I’ve looked at the link you sent the other day and whilst it’s shed some light I’m struggling with how to establish the socket and then keep the socket open so that the server can send messages back to the client. Do you have any other examples or web links that might help me buld this using VS2010, or even VS2012 on a Win7 device? Anything you have would be greatly appreciated.
By sockets in VS2010, do you mean the code which goes on the Gadgeteer, or the application you’re creating on the desktop?
Here’s a .NET 4 async socket server:
Here’s an older example
To keep the socket open (server or client) you just need to open it and then make sure the code doesn’t terminate. Otherwise, I’m not quite sure what you’re running into. If a Gadgeteer question, I suggest starting a new post (and link to it here for reference) in the Gadgeteer forum so we can get more eyes on it.
I’m trying to figure out the code for the desktop server app.
My issue is that your server code sends messages to the client but my code (and the example you sent me as well as the MS async server example code) block the server UI and close the socket to complete the reception of return bytes to the client. With this solution I therefore can’t envisage a method of keeping the socket open to allow the server to send commands to the client. Am I missing something fundemental in my understanding of how these examples work? Sorry for such a line of questions but I can’t see my way out of this right now (without building a Win8 machine and copying your code verbatim). Happy to continue this via email or some other forum if we’re getting way off topic.
You mentioned UI…so…
Remember in .net, all ui run on a single thread. Any code running on that same thread would block. What you should do is spawn a new thread that runs asynchronously (this could be your code that receives and/or sends commands), and if/when you need to display some data on the ui, use dispather.invoke to invoke onto the ui thread?
I think, with everyone’s help, I have got something that should work. I have a client and server that can send and receive without blocking the UI and keeping the socket open in the server. Both apps are running on a desktop right now. It needs some tidying up and the code for the Gadgeteer writing (should be abe to use much of Pete’s code as it is) and then I post it all.
Spoke too soon. The desktop client and server work well (enough) and I even move the client toa separate PC to check that firewalls, etc weren’t getting in the way, but try as I might I can’t get the gadgeteer working. My code’s a little untidy right now (so I’m not going to post it all) but I think the important parts of the server are
public static void StartListening()
{
byte[] bytes = new Byte[1024];
IPAddress ipAddress = IPAddress.Parse("192.168.1.65");
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 5051);
_socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
try
{
_socket.Bind(localEndPoint);
_socket.Listen(100);
while (true)
{
allDone.Reset();
Console.WriteLine("Waiting for a connection...");
_socket.BeginAccept(new AsyncCallback(AcceptCallback),_socket);
// Wait until a connection is made before continuing.
allDone.WaitOne();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine("\nPress ENTER to continue...");
Console.Read();
}
the desktop client - which works
// Establish the remote endpoint for the socket.
IPAddress ipAddress = IPAddress.Parse("192.168.1.65");
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
_socket.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), _socket);
connectDone.WaitOne();
and the Gadgeteer - which doesn’t.
SimpleSocket _socket;
private void StartCommunications()
{
_wifi.DebugMode = false;
Debug.Print("Initialising WiFi");
DisplayStatus(0, "Initialising WiFi");
_wifi.EnableDHCP();
_wifi.JoinNetwork("htid");
bool noIP = true;
while (noIP)
{
noIP = _wifi.LocalIP == "0.0.0.0";
Thread.Sleep(250);
}
Debug.Print("WiFly IP : " + _wifi.LocalIP);
Debug.Print("WiFly MAC : " + _wifi.MacAddress);
DisplayStatus(0, "IP: " + _wifi.LocalIP);
EnableSockets();
}
private void EnableSockets()
{
Debug.Print("Enabling Sockets");
string name = "192.168.1.65";
ushort port = 5051;
DisplayStatus(1, ">>" + name + ":" + port);
_socket = new WiFlySocket(name, port, _wifi);
Debug.Print("Connecting...");
try
{
_socket.Connect(); // Exception raised here...
_socket.Send("helo<EOF");
}
catch (Exception ex)
{
Debug.Print("Connection Failed.");
Debug.Print(ex.ToString());
DisplayStatus(1,"Connection Failed");
}
var t = new Thread(new ThreadStart(() =>
{
var cmd = _socket.Receive();
if (cmd.Length != 0)
ProcessCommand(cmd);
}));
t.Start();
Debug.Print("Awaiting commands");
}
It never connects to the socket, even though it seems to connect to the WiFi ok. Anyone had issues with this before? Anything I should take a look at?
I’m not sure it’s the code at the moment as I am getting a hardware error when I try to deply. I continue but I’m pretty sure this are not working properly. I can create an deploy a simple (and new app) and this seems fine. I think this error might be some of the problem. I’m getting. I’ve explained the observed issues below and I will update once I have sorted out the deployment error. If can have any thoughts on that I’d appreciate them. If I can’t get them sorted I’ll start a new forum thread.
This is the output I’m getting
Started communications thread
WiFly IP : 192.168.0.30
WiFly MAC : 00:06:66:71:d7:ce
The thread '<No Name>' (0x4) has exited with code 0 (0x0).
Enabling Sockets
Connecting...
A first chance exception of type 'System.ApplicationException' occurred in Toolbox.NETMF.Hardware.WiFlyGSX
Connection Failed.
System.ApplicationException: Connection timed out
The thread '<No Name>' (0x6) has exited with code 0 (0x0).
Awaiting commands
The thread '<No Name>' (0x5) has exited with code 0 (0x0).
It connects to the WiFi and is assigned an IP and MAC address. Then it tries to connect the client socket (see below) to the server and times out (after the 5000ms set in the WiFiSocket code written by the netmftookbox guys). I’ve tried extending this
Short update. Having created a new project and moved my code over to it the solution compiles and deplos to my Cerbuino just fine, and better yet the WiFi and Sockets is working great. Just need to tidy up a little and get the hardware wired up. I’ll be back to post some of this over the next few days. Thanks to you all for you help.
Thats why you should always start with a fresh project when you upgrade minor or makor SDK versions. Old references ! They will cause you lots of pain. Anyone reading this, tell us when you are using an old project or have recently upgraded SDK versions
Well I finally found and hour to wire it all up and see how it behaved. After writing the code to handle the commands the robot did a pretty good job of moving forwards, backwards, left and right. It even stopped (usually) when commanded. Not perfect yet since the stop command seems to only work straight after a forward command, but I’ll get them. Codes since messy and I’m sure the async stuff could be improved and commented up. I’ll get round to that soon and post some stuff here.
Funny you say that, I had code that worked earlier and now my bot won’t stop after I give it a forward command, but it will stop if I issue a 0 after telling it to go backwards. I assume it was the motor controller itself, but sounds like that may not be the case.
I had no issues with getting it to stop, until I demonstrated it at thatConference. That was the first time it failed to stop. I had assumed it was a communication error, but now I think maybe it was something else.
Setting a motor to speed of 0 should stop it. Not sure what’s up with that, if it’s my code, or the motor driver, or what.