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

Netduino serial data eventhandler

netduino xbee c# byte arrays

  • Please log in to reply
9 replies to this topic

#1 leke_lion

leke_lion

    New Member

  • Members
  • Pip
  • 7 posts

Posted 29 April 2013 - 01:33 AM

Hi,  

 

Can anyone help troubleshoot this code? I'm building a wireless temperature sensor, and reading the values from the receiver end of a xbee into the digital input of a netduino. I found some examples on reading hex values and looping through a byte array online and essentially integrated them into this. Specifically, I'm getting an error about delegate not matching overload. It's the bold and underlined part of the code. Any help is greatly appreciated! Also, if there's a more efficient way to read the hex values coming through the COM2 port then please explain how. THANKS!

 

error:

"no overload for 'serialPort_DataReceived' matches delegate 'SerialDataReceivedEventHandler'"

 

 

...................................................................................
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;
using System.Text;
using LXIT.Hardware;
 
namespace NetduinoApplication1
{
    public class Program
    {
        static SerialPort serialPort;
        const int Max_Command_Length = 3;
        static byte[] commandString = new Byte[Max_Command_Length];
        static int commandStringIndex = 0;
         
       
        public static void Main()
        {
            // Create buffer for data from xbee
            serialPort = new SerialPort("COM2",9600);
            serialPort.Open();
            serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
            Thread.Sleep(Timeout.Infinite);
 
            // 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);
        }
 
        static void serialPort_DataReceived(object sender, SerialErrorReceivedEventArgs e)
        {
            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)
            {
                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;
 
                while (true)
                {
                    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);
                }
 
                // Wait 2 seconds
                //Thread.Sleep(2000);
 
                // Turn the LCD back on
                //smallLcd.SetDisplay(SerLCD.Status.On);
            }
            else
            { }
 
        }
    }
}
 


#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 29 April 2013 - 02:02 AM

Hi leke_lion, Can you zip up your project file really quick and attach it here? If we can pull it up in Visual Studio, we can help explain what Visual Studio is being grumpy about :) Chris

#3 leke_lion

leke_lion

    New Member

  • Members
  • Pip
  • 7 posts

Posted 29 April 2013 - 02:14 AM

Thanks! 

 

I've attached it.

Attached Files



#4 idlegoose007

idlegoose007

    Member

  • Members
  • PipPip
  • 11 posts

Posted 29 April 2013 - 05:17 AM

Looking at your Main() you have an infinite timeout before you finish your initialization of your LCD. is this intended?



#5 leke_lion

leke_lion

    New Member

  • Members
  • Pip
  • 7 posts

Posted 29 April 2013 - 04:00 PM

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



#6 Kem

Kem

    Member

  • Members
  • PipPip
  • 17 posts

Posted 29 April 2013 - 08:17 PM

Hi,

 

I've had the same problem a while ago, turns out you need to add a reference to Microsoft.SPOT.Hardware.dll. The SerialDataReceivedEventHandler seems to live there :).

 

Cheers,

 

Kem 



#7 Kem

Kem

    Member

  • Members
  • PipPip
  • 17 posts

Posted 29 April 2013 - 08:19 PM

O and it might be a good idea to change the parameters of your serialPort_DataReceived method :)

 

 

 

 

void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) 

 



#8 leke_lion

leke_lion

    New Member

  • Members
  • Pip
  • 7 posts

Posted 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



#9 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 29 April 2013 - 08:33 PM

your also using an infinite lopp in your event handler.

 

you might want to rething what a eventhandler is.

 

it gets called whenever data is ready, all the time -

you have than to care about reading it and storing it (it can be any ammount of the data, lets say you send

100 bytes, its very likly that only 20 bytes have arrived when the event fires, so you store them till you got the whole ammount)



#10 leke_lion

leke_lion

    New Member

  • Members
  • Pip
  • 7 posts

Posted 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
                { }
             
            }
        }
}






Also tagged with one or more of these keywords: netduino, xbee, c#, byte, arrays

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.