Best Answer Se3ker, 06 March 2013 - 10:37 PM
Its actually preety easy
1st Netduino is sending a text command "1" [color=rgb(255,0,0);]every second[/color] and waiting for a text command "2". If it gets "2" from another wireless netduino it will blink its LED briefly.
2nd Netduino is sending text command "2" [color=rgb(255,0,0);]every five seconds [/color]and waiting for a text command "1". If it gets "1" from another wireless netduino it will blink its LED briefly.
Look at the indicators on top of the wireless shields:
Code on 1st Netduino2: WirelessTest1
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.Netduino;using System.IO.Ports;namespace WirelessTest1{ public class Program { static SerialPort serial; static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false); public static void Main() { serial = new SerialPort(SerialPorts.COM1, 9600); serial.Open(); serial.DataReceived += new SerialDataReceivedEventHandler(serial_DataReceived); while (true) { Thread.Sleep(1000); string welcome = "1"; Byte[] bytes = System.Text.Encoding.UTF8.GetBytes(welcome); serial.Write(bytes, 0, bytes.Length); } } static void serial_DataReceived(object sender, SerialDataReceivedEventArgs e) { if (serial.BytesToRead > 0) { Byte[] bytes = new Byte[serial.BytesToRead]; serial.Read(bytes, 0, bytes.Length); Char[] line = System.Text.Encoding.UTF8.GetChars(bytes, 0, bytes.Length); String str = new String(line); Debug.Print(str); if (str == "2") { led.Write(true); Thread.Sleep(100); led.Write(false); } } } }}
Code on 2nd Netduino2 : WirelessTest2
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.Netduino;using System.IO.Ports;namespace WirelessTest2{ public class Program { static SerialPort serial; static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false); public static void Main() { serial = new SerialPort(SerialPorts.COM1, 9600); serial.Open(); serial.DataReceived += new SerialDataReceivedEventHandler(serial_DataReceived); while (true) { Thread.Sleep(5000); string welcome = "2"; Byte[] bytes = System.Text.Encoding.UTF8.GetBytes(welcome); serial.Write(bytes, 0, bytes.Length); } } static void serial_DataReceived(object sender, SerialDataReceivedEventArgs e) { if (serial.BytesToRead > 0) { Byte[] bytes = new Byte[serial.BytesToRead]; serial.Read(bytes, 0, bytes.Length); Char[] line = System.Text.Encoding.UTF8.GetChars(bytes, 0, bytes.Length); String str = new String(line); Debug.Print(str); if (str == "1") { led.Write(true); Thread.Sleep(100); led.Write(false); } } } }}
Go to the full post