Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

odich

Member Since 16 May 2014
Offline Last Active Jun 01 2014 06:28 PM
-----

Posts I've Made

In Topic: water quality monitoring system using wireless sensor

26 May 2014 - 05:07 PM

hello everyone, am trying to read bytes from the using the serial port of my netduino and turn on the onboard led after time data arrives but nothing has happened, all i get is the error message unhandled exception of the type system.exception occured in microsoft.spot.hardware.dll.

 

below is my code and am using pins D0 and D1 for rx and tx respectively

 

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.IO.Ports;
 
namespace NetduinoApplication1
{
    public class Program
    {
        static SerialPort serial;
 
        public static void Main()
        {
            // initialize the serial port for COM1 (using D0 & D1)
 
            serial = new SerialPort(SerialPorts.COM1, 2400, Parity.None, 8, StopBits.One);
 
            OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
          // open the serial-port, so we can send & receive data
            try
 
            {
            serial.Open();
            }
 
            catch
            {
                led.Write(true);
            }
            // add an event-handler for handling incoming data
 
            serial.DataReceived += new SerialDataReceivedEventHandler(serial_DataReceived);
            
            for (int i = 0; i < 3; i++)
            {
                led.Write(true); // turn on the LED
 
                Thread.Sleep(250); // sleep for 250ms
 
                led.Write(false); // turn off the LED
 
                Thread.Sleep(250); // sleep for 250ms
 
            }
 
            // wait forever...
            Thread.Sleep(Timeout.Infinite);
        }
 
        static void serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
 
            // create a byte array
 
            byte[] bytes = new byte[serial.BytesToRead];
 
            // as long as there is data waiting to be read
            while (serial.BytesToRead > 0)
            {
                // read a single byte
                serial.Read(bytes, 0, bytes.Length);
 
                // send the same byte back
                Thread.Sleep(2000);
 
                serial.Write(bytes, 0, bytes.Length);
 
                OutputPort led1 = new OutputPort(Pins.ONBOARD_LED, false);
 
                led1.Write(true); // turn on the LED
 
                Thread.Sleep(250); // sleep for 250ms
                led1.Write(false); // turn off the LED
 
                Thread.Sleep(250); // sleep for 250ms
 
            }
 
        }
 
    }
}
 
thanks a lot

In Topic: Netduino Serial Port Code Review

26 May 2014 - 04:54 PM

hello everyone am new to netduino and am trying to receive data inform of bytes from the serial port, everytime data appears the onboard led should be turned on but am only getting the error message unhandled exception of the type system.exception occurred in microsoft.spot.hardware.dll, please anyone out there help me. this is part of my school project as a computer engineer.

 

below is my code, am using pins D0 and D1 for serial connection

 

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.IO.Ports;
 
namespace NetduinoApplication1
{
    public class Program
    {
        static SerialPort serial;
 
        public static void Main()
        {
            // initialize the serial port for COM1 (using D0 & D1)
            serial = new SerialPort(SerialPorts.COM1, 2400, Parity.None, 8, StopBits.One);
 
            OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
          // open the serial-port, so we can send & receive data
            try
 
            { serial.Open(); }
 
            catch
            {
                led.Write(true);
            }
            // add an event-handler for handling incoming data
            serial.DataReceived += new SerialDataReceivedEventHandler(serial_DataReceived);
            
            for (int i = 0; i < 3; i++)
            {
                led.Write(true); // turn on the LED
                Thread.Sleep(250); // sleep for 250ms
                led.Write(false); // turn off the LED
                Thread.Sleep(250); // sleep for 250ms
 
            }
 
            // wait forever...
            Thread.Sleep(Timeout.Infinite);
        }
 
        static void serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
 
            // create a byte array
            byte[] bytes = new byte[serial.BytesToRead];
 
            // as long as there is data waiting to be read
            while (serial.BytesToRead > 0)
            {
                // read a single byte
                serial.Read(bytes, 0, bytes.Length);
                // send the same byte back
                Thread.Sleep(2000);
                serial.Write(bytes, 0, bytes.Length);
                OutputPort led1 = new OutputPort(Pins.ONBOARD_LED, false);
                led1.Write(true); // turn on the LED
                Thread.Sleep(250); // sleep for 250ms
                led1.Write(false); // turn off the LED
                Thread.Sleep(250); // sleep for 250ms
 
            }
 
        }
 
    }
}

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