0

I have an Arduino XBee shield and I have a Sparkfun XBee USB explorer. I would like to send data (temperature sensor) that comes from the Ardunio XBee and receive it in my C# programme.

For now, let's say I want to send 45, 100 to my C# programme.

I don't receive any data that comes from the XBee shield. Am I missing anything with the code?

The below code is the sender from the Arduino XBee shield:

SoftwareSerial mySerial(4,5);
void setup()
{
    mySerial.begin(9600);
}


void loop()
{
    if (mySerial.available() > 0)
    {
        mySerial.write(45);
        mySerial.write(',');
        mySerial.write(100);
        mySerial.write('\n');
    }
}

Receiver code for the USB XBee explorer in C#:

SerialPort port = new SerialPort();

public Form1()
{
    try
        {
            port.PortName = "COM8";
            port.BaudRate = 9600;
            port.DataBits = 8;
            port.Parity = Parity.None;
            port.StopBits = StopBits.One;
            port.Open();
            Console.WriteLine("Opened");
        }
        catch(Exception ex)
        {
            Console.WriteLine("Sorry! " + ex);
        }

        // Handler for receiving data
        port.DataReceived += serialPort1_DataReceived;
    }

    private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        if (port.IsOpen == true)
        {
            string RxString = port.ReadLine();
            Console.WriteLine(RxString);
        }
    }

The XBee configuration:

  • One XBee is: Coordinator AT mode -- connected to USB Sparkfun Explorer
  • Another XBee is: Router AT mode -- Connected to Arduino shield

As tomlogic answered my question in Stack Overflow question XBee two-way communication (sender and receiver) at the same time.

Community
  • 1
  • 1
COLD ICE
  • 840
  • 1
  • 12
  • 31
  • Note: I tried mySerial.print() method but still I can't receive. – COLD ICE Sep 02 '13 at 04:45
  • Is it possible to eliminate the XBee modules from your test setup, and just use a wired serial connection to verify that your serial code is working correctly? You could also connect one (or both) of the XBee modules to X-CTU on a PC and verify that they're joined to the same network and passing data back and forth. Try to isolate the possible cause of the problem so you can focus your troubleshooting efforts. – tomlogic Sep 03 '13 at 16:41

2 Answers2

1

I got it working. The problem was from my void loop() method. The mySerial should be like

mySerial.println(temperature);
  • However, you must check mySerial Rx,Tx pin that they are right one
COLD ICE
  • 840
  • 1
  • 12
  • 31
0

Your XBee shield uses pins 0 and 1 on the Arduino. Softwareserial is not needed, just use:

Serial.begin(9600); // In void setup() routine

To send the temperature, use this in function loop:

Serial.print(temperature); // Need a variable 'temperature' of course...

Test the Arduino code with the built-in terminal in the Arduino IDE to see if the port actually can receive and send (remove the XBee shield first). After that works, test out the XBee communication.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user2019047
  • 749
  • 7
  • 10