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:
As tomlogic answered my question in Stack Overflow question XBee two-way communication (sender and receiver) at the same time.
I got it working. The problem was from my void loop() method. The mySerial should be like
mySerial.println(temperature);