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);
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.