I'm trying to create a windows form app with which i plan (i.e. hope) to control my N+ through a Cross-over ethernet cable (static IP). So far I've managed to get my program to send a command using socket.send()to my N+ in order to turn on/off the on-board LED, and amazingly (I'm no coder as you'll probably notice from my code) it works! But only once before the software on my N+ throws an "System.ObjectDisposedException" related to the socket.Accept() function.
To be honest, I have limited understanding on what I'm doing. My code contains bits and pieces I've collected on this forum and other internet resources and I've just recently started to grasp system.net.socket functions (the basic).
Most of the code here is based on an project by Bill french(on this forum to whom I owe many thanks)
Anyone care to have a look comment on what I might be lacking/doing wrong? Many thanks in advance! =)
The code for my Windows Form seems to work fine; simple program:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Net; using System.Diagnostics; namespace WindowsFormNetduinoPlusLED { public partial class ONOFFLedForm : Form { string NetduinoIP = "192.168.5.100"; public int Port = 80; public ONOFFLedForm() { InitializeComponent(); } public void SendLEDCommand(String LEDCommand) { if (NetduinoIP != null && Port > 0) { using (System.Net.Sockets.Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { IPAddress hostAddress = System.Net.IPAddress.Parse("192.168.5.100"); IPEndPoint endpoint = new IPEndPoint(hostAddress, 8000); try { socket.Connect(endpoint); socket.Send(Encoding.UTF8.GetBytes(LEDCommand)); socket.Close(); } catch (SocketException se) { Debug.Print("Socket Exception! Probably no server or bad ip?"); Debug.Print(se.StackTrace); } } } } private void ONButton_Click(object sender, EventArgs e) { SendLEDCommand("LEDON"); } private void OFFButton_Click(object sender, EventArgs e) { SendLEDCommand("LEDOFF"); } } }
This is my N+ program which fails after receiving the first command and turning on/off the led... =/
using System; using System.Net; using System.Net.Sockets; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.NetduinoPlus; using Microsoft.SPOT.Net.NetworkInformation; using System.Text; namespace ListenForLedCommand { public class Program { static bool SocketConnected(Socket s) { bool part1 = s.Poll(1000, SelectMode.SelectRead); bool part2 = (s.Available == 0); if (part1 & part2) {//connection is closed return false; } return true; } public static void Main() { OutputPort led = new OutputPort(Pins.ONBOARD_LED, true); //Socket socket = null; //String NetduinoIP = "192.168.5.100"; Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Bind(new IPEndPoint(IPAddress.Parse("192.168.5.100"), 8000)); socket.Listen(10); String receivedStr = ""; while (true) { socket = socket.Accept(); using (socket) { while (SocketConnected(socket)) { int availablebytes = socket.Available; byte[] buffer = new byte[availablebytes]; socket.Receive(buffer); if (buffer.Length > 0) { receivedStr = new String(Encoding.UTF8.GetChars(buffer)); } } } if (receivedStr == "LEDON") { led.Write(true); } if (receivedStr == "LEDOFF") { led.Write(false); } } } } }