Netduino home hardware projects downloads community

Jump to content


Photo

How to use the serial port on Netduino 2?


Best Answer Se3ker, 06 March 2013 - 10:37 PM

Its actually preety easy :D

 

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:

Video

 

 

 

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


  • Please log in to reply
3 replies to this topic

#1 Se3ker

Se3ker

    New Member

  • Members
  • Pip
  • 8 posts

Posted 06 March 2013 - 12:34 PM

Hi,
 
 
Ive got Netduino 2 and I bought Open Source Wireless Inventors Shield for Arduino, they say it can work with Netduino but haven't tested it.

 

Posted Image

 

 

Ive never used Serial on netduino, are there any tutorials on how to start?? Also can someone look at the Manual and help me out?

 

Thanks.

 

 



#2 Se3ker

Se3ker

    New Member

  • Members
  • Pip
  • 8 posts

Posted 06 March 2013 - 10:37 PM   Best Answer

Its actually preety easy :D

 

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:

Video

 

 

 

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);                }            }        }    }}

 



#3 Se3ker

Se3ker

    New Member

  • Members
  • Pip
  • 8 posts

Posted 06 March 2013 - 10:42 PM

Many thanks for the great help with serialports among everything else to NooM !!



#4 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 06 March 2013 - 10:43 PM

well, nice for mentioning all the help you got from me/the chat/the tutorial

 

http://blog.codeblac...with-RS232.aspx

 

- and actually, it wasent easy, it was a pain :P

(it was easy for me :P)

 

none the less, its a cool project, i like wireless stuff






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2010-2014 Secret Labs LLC  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.