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.

leke_lion

Member Since 26 Feb 2013
Offline Last Active Apr 30 2013 12:12 AM
-----

Posts I've Made

In Topic: Netduino serial data eventhandler

29 April 2013 - 09:50 PM

Thanks everyone,

 

I've updated the code with a few revisions as you all suggested and took a look at the serialPort eventhandler API in the link. I modifying my code to use the sp.ReadExisting() method, but I got it couldn't find the reference and got an error... which was wierd. I'm also worried that I'm incorrectly using the serialPort.DataBits method. 

 

http://msdn.microsof...tareceived.aspx

 

 

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
 
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
 
using System.IO.Ports;
using System.Text;
using LXIT.Hardware;
 
namespace NetduinoApplication1
{
    public class Program
    {
        static SerialPort serialPort;
           
        public static void Main()
        {
            // Create instance for a SparkFun SerLCD 16x2
            SerLCD smallLcd = new SerLCD(SerialPorts.COM1);
            // Now start playing with these LCD's 
            smallLcd.ClearDisplay();
            smallLcd.SetCursorPosition(1, 6);
            smallLcd.Write("Helmet");
            smallLcd.SetCursorPosition(2, 6);
            smallLcd.Write("Temps");
            // Wait 2 seconds
            Thread.Sleep(2000);
         
            // data from xbee
            //SerialPort serialPort = new SerialPort("COM2");
            serialPort.BaudRate = 9600;
            serialPort.Parity = Parity.None;
            serialPort.StopBits = StopBits.One;
            serialPort.DataBits = 8;
            serialPort.Handshake = Handshake.None;
 
            serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
            //serialPort.Open();            
            //Thread.Sleep(Timeout.Infinite);
        }
        private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
            {
                //SerialPort sp = (SerialPort)sender;
                //string indata = sp.ReadExisting();
                serialPort.Open();
                int numberOfBytesReceived = serialPort.BytesToRead;
                byte[] serialBuffer = new byte[numberOfBytesReceived];
                serialPort.Read(serialBuffer, 0, numberOfBytesReceived);
                loop();  
            }
 
            static void loop()
            {
                SerLCD smallLcd = new SerLCD(SerialPorts.COM1);
         
                if (serialPort.DataBits == 0x7E)
                {
                    while (true)
                    {
                        for (int i = 1; i < 19; i++)
                        {
                            byte discardByte = (byte)serialPort.DataBits;
                        }
 
                        int firstBit = serialPort.DataBits;
                        int secondBit = serialPort.DataBits;
 
                        int reading = secondBit + (firstBit * 256);
                        double Temp = (reading / 1023) * 1.23;
                        Temp = (Temp - 0.5) / 0.01;
                        string newTemp = Temp.ToString();
                        Thread.Sleep(500);
                        // waits for 500 milliseconds
 
                        // Clear screen
                        smallLcd.ClearDisplay();
                        smallLcd.Write("Player Temp:");
                        smallLcd.SetCursorPosition(2, 6);
                        smallLcd.Write(newTemp + "*C");
 
                        //wait 2 seconds
                        Thread.Sleep(2000);
                    }
                }
                else
                { }
             
            }
        }
}

In Topic: Netduino serial data eventhandler

29 April 2013 - 08:28 PM

Hi Kem, 

 

I'm already referencing it at the top as "Using Microsoft.SPOT.Hardware;" ... Is there another way to reference it?

 

Thanks,

 

Asong


In Topic: Netduino serial data eventhandler

29 April 2013 - 04:00 PM

Good point. Not particularly, the infinite timeout was intended to constantly receive data from the serialport


In Topic: Netduino serial data eventhandler

29 April 2013 - 02:14 AM

Thanks! 

 

I've attached it.


In Topic: Xbee to LCD using Netduino

08 March 2013 - 01:21 AM

Thanks!


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.