none
program sensor and xbee in C# RRS feed

  • Question

  • Hi, I need to do a program in C# when the sensor sense something go pass it will tell the slave xbee and the slave xbee will communicate wirelessly to the master xbee which is connected to the pc, and the master xbee will trigger the stopwatch. I need help! does anybody know??? My sensor is a diffuse sensor and I using xbee pro S2.

    The stopwatch coding is done but I don't know how to do a program using sensor.

    Thank you.

    • Moved by Bob_Bao Friday, January 27, 2012 5:12 AM (From:Visual C# Language)
    Monday, January 9, 2012 3:35 AM

All replies

  • Hi HelloKitty,

    Welcome to MSDN Forum.

     

    Since XBee pro S2 is a third party product, you'd better contact its support from the manufacturer, or any other dedicated communities to get some SDK, API, Demo and help document. 

     

    e.g. XBee Support Forum

    http://forums.adafruit.com/viewforum.php?f=40

     

     

    By the way, we can communicate PC with external devices via Serial COM Port communication in C#.

    FAQ: How do I implement Serial COM Port communication in .Net?

     

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/4ebccff7-baf6-4f2d-ae40-bb8733c2c805

     

    Code sample: Receive data from Serial Port and show on UI controls. 

     

    Prerequisites: Drag SerialPort1 and TextBox1 onto Form1. 

     
    public class Form1 
    {
        
        private string inputData = "";
        public event IO.Ports.SerialDataReceivedEventHandler DataReceived;
        
    private void Form1_Load(object sender, System.EventArgs e) 
    {
            // Set values for some properties 
            SerialPort1.PortName = "COM1";
            SerialPort1.BaudRate = 9600;
            SerialPort1.Parity = IO.Ports.Parity.None;
            SerialPort1.DataBits = 8;
            SerialPort1.StopBits = IO.Ports.StopBits.One;
            SerialPort1.Handshake = IO.Ports.Handshake.None;
            SerialPort1.RtsEnable = true;
            SerialPort1.Open();
            // Writes data to the Serial Port output buffer 
            if ((SerialPort1.IsOpen == true)) 
            {
                SerialPort1.Write("MicroCommand");
            }
        }
        
        //  Receive data from the Serial Port
    private void SerialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) 
    {
            inputData = SerialPort1.ReadExisting();
            // or SerialPort1.ReadLine

    this.Invoke((MethodInvoker)delegate {DoUpdate();});

        }
        
        // Show received data on UI controls and do something
    public void DoUpdate() 
    {
            TextBox1.Text = (TextBox1.Text + inputData);
        }
        
    private void Form1_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e) 
    {
            //  Close the Serial Port
            SerialPort1.Close();
        }
    }

    Martin Xie [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, January 11, 2012 2:59 AM
    Moderator
  • Hi Martin, how do I declare sensor and intergrate with this code?? Appreciate your reply.

    Thank you.

    Wednesday, January 11, 2012 3:52 AM
  • Hi,

    The link below shows an Example which connects to the Sensors...using C#

    http://msdn.microsoft.com/en-us/library/bb483043.aspx

    http://www.codeproject.com/KB/recipes/driving_lego_bot.aspx

    http://www.vbforums.com/showthread.php?t=317423


    Don't forget to mark the post as answer or vote as helpful if it does, Regards - Rajasekhar.R
    Wednesday, January 11, 2012 4:03 AM
  • Hi, I got a error on this line "inputData = SerialPort1.ReadExisting;

    The error say " Error 1 Cannot convert method group 'ReadExisting' to non-delegate type 'string' ".

    Can someone help me solve this error? The code is from the top that is given.

     

    Thank you.

    Friday, January 13, 2012 7:36 AM
  • Please change

      inputData = SerialPort1.ReadExisting;

    to

      inputData = SerialPort1.ReadExisting();


    Martin Xie [MSFT]
    MSDN Community Support | Feedback to us
    Friday, January 13, 2012 10:02 AM
    Moderator
  • Hi Martin, the program can't receive any data. I'm not sure where went wrong.
    Monday, January 16, 2012 8:01 AM
  • Hi HelloKitty,

    The above is just a common code sample for COM Port communication in .Net. The detailed solution is depended on the hardware device settings or interfaces.

    Since XBee pro S2 is a third party product, you'd better contact its support or dedicated forum to get some instruction (SDK, API, Demo or help document).


    Martin Xie [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, January 17, 2012 6:33 AM
    Moderator
  • Hi Martin, so the code is for wirelessly received data or from second PC send data to the first PC received data?

    Thank you.

    Wednesday, January 18, 2012 1:53 AM
  • I think it's for wirely receieved data, and provides access to serial driver properties.

     

    The SerialPort class can be used to implement Serial COM Port communication in .Net. It represents a serial port resource. This class provides synchronous and event-driven I/O, access to pin and break states, and access to serial driver properties.

     

    Check this article for some reference

    http://www.innovatic.dk/knowledg/SerialCOM/SerialCOM.htm

     


    Martin Xie [MSFT]
    MSDN Community Support | Feedback to us
    Wednesday, January 18, 2012 8:12 AM
    Moderator
  • Hi Martin, thanks for your information and your help. What can I do once the serial port received data and will trigger the stopwatch without using any button?

    Thank you.

    Thursday, January 19, 2012 3:45 AM
  • The SerialPort.DataReceived Event will handle the data received event of a SerialPort object. The StopWatch is not required.

    //  Receive data from the Serial Port

    private void SerialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) 
    {
            inputData = SerialPort1.ReadExisting();
            // or SerialPort1.ReadLine

                this.Invoke((MethodInvoker)delegate {DoUpdate();});

           }
        
        // Show received data on UI controls and do something
    public void DoUpdate() 
    {
            TextBox1.Text = (TextBox1.Text + inputData);
        }

    Martin Xie [MSFT]
    MSDN Community Support | Feedback to us
    Thursday, January 19, 2012 9:39 AM
    Moderator
  • Hi Martin, I mean what codes should i add in, in order the received data will trigger stopwatch. I need to do once the the data is received through the serial port it will auntomatically trigger the stopwatch.

    Thank you.

    Thursday, January 19, 2012 9:46 AM