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.
Photo

water quality monitoring system using wireless sensor


  • Please log in to reply
4 replies to this topic

#1 odich

odich

    New Member

  • Members
  • Pip
  • 3 posts

Posted 17 May 2014 - 03:54 PM

hello, am currently working on a final year project- water quality monitoring system using the netduino. the system uses RF transceivers for data transmission, the receiving node receives the data and sends it to the computer using the serial port. Am using the prolific USB-to-TTL converter.

The problem is that i can't see anything displayed in my richtextbox because am using charp to build the desktop application, but whenever i use hyperterminal the values are  displayed correctly, below is my code for the netduino and the application. please anybody out there who can help me figure out this problem, I deeply appreciate. thanks

 

 

///the program on the netduino

 

 

using System;
using System.Threading;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.IO.Ports;

namespace flash_led
{
    public class Program
    {
        private const int MaximumValue = 1023;  

        private const float analogReference=5.0f;

        static SerialPort myserial;

        public static void Main()
        {

           myserial = new SerialPort(SerialPorts.COM1, 9600, Parity.None, 8, StopBits.One);  // create a serial port
            
            myserial.Open();  // open serial port for writing

            OutputPort led = new OutputPort(Pins.GPIO_PIN_D7, false);

            OutputPort led2 = new OutputPort(Pins.GPIO_PIN_D4, false);

            AnalogInput adcPort = new AnalogInput(Pins.GPIO_PIN_A0);

            int digitalValue;

            float analogvalue;

            double turbidity;

            while (true)
            {
               
                led2.Write(true);

                Thread.Sleep(1000);

                digitalValue = adcPort.Read();

                analogvalue = (float)digitalValue / MaximumValue * analogReference;

                turbidity = (20.83 * analogvalue - 0.6);

                byte[] final_value = System.Text.Encoding.UTF8.GetBytes(turbidity.ToString());

                led.Write(true);

                Thread.Sleep(50);

                myserial.Write(final_value, 0, final_value.Length);

                led.Write(false);

                Thread.Sleep(2000);

                analogvalue = 0;
               
                Thread.Sleep(1000);

            }
        
       }

    }
}
 

 

// and for my desktop application

 

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.IO.Ports;

namespace water_quality
{
    public partial class Form1 : Form
    {
        SerialPort sp = new SerialPort();


        string line;


        public Form1()
        {
            InitializeComponent();

            sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived); //
        }

        private void connect_button_Click(object sender, EventArgs e)
        {
            try
            {
                sp.PortName = "COM2";
                sp.StopBits = StopBits.One;
                sp.DataBits = 8;
                sp.BaudRate = 9600;
                sp.Parity = Parity.None;

                sp.Open();

                           }
            catch
            {
                MessageBox.Show(" the serialport " + sp.PortName + " could not be found" );
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        public void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            System.Threading.Thread.Sleep(100);

            byte[] incoming = new byte[sp.BytesToRead];

            sp.Read(incoming, 0, incoming.Length);

            line = System.Text.Encoding.UTF8.GetString(incoming);


            if (sp.BytesToRead > 0)
            {
                this.Invoke(new EventHandler(display));
            }

        }

         

         public void display(object sender, EventArgs e)
        {
            this.turbidity.Text= line; // turbidity is the richtextbox name
        }

    }
}
 



#2 jrlyman3

jrlyman3

    Advanced Member

  • Members
  • PipPipPip
  • 67 posts
  • LocationNorth St Paul. MN

Posted 18 May 2014 - 12:41 AM

odich,

 

Try adding the statement "this.turbidity.Refresh();" after you set the Text value.

 

John



#3 odich

odich

    New Member

  • Members
  • Pip
  • 3 posts

Posted 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


#4 jrlyman3

jrlyman3

    Advanced Member

  • Members
  • PipPipPip
  • 67 posts
  • LocationNorth St Paul. MN

Posted 06 June 2014 - 02:27 AM

This is a lot of code to try and guess what caused the exception, especially since you didn't give us the entire output.  I would suggest using Debug.Print("Some message"); at different points in the program to pin down where the exception is occurring.  One guess is that maybe you need to add your event handler before you open the serial port.  All of the examples show it that way (and it works for me), but the API doc (http://msdn.microsof...y/hh401405.aspx) doesn't have much to say.



#5 Spiked

Spiked

    Advanced Member

  • Members
  • PipPipPip
  • 129 posts

Posted 06 June 2014 - 01:07 PM

Just some wild guesses.

 

You create a new Led port in main, then eventually fall through to a infinite wait (never exit). The object reference may never be garbage collected.

 

In your loop you create another Led port (same pins), over and over, never disposing of it, although I guess it might self dispose in the 250ms wait, but in any case this is bad practice.  Allocate it once, in main, and refer to the same object everywhere.

 

You also allocate a buffer, the size of bytes to be read whenever bytes are available, then read that amount. It is probably safe to assume that a single read will do the trick in that case, no need to 'while (serial.BytesToRead > 0)'

 

Your comment

 // read a single byte
serial.Read(bytes, 0, bytes.Length);
 
does NOT match your code.
 
Is Com1 valid if USB is in use by visual studio / debugger?  I have always used Com2; digital pins 2-3: UART 2 RX, TX
 
I ran your code, but I do not have anything on a serial port at the moment. It did not get an exception, so that tells me it must be in the receive delegate.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

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.