Sending and receiving data from the serial interface of the physical XBee device means you are actually communicating with the device (PC, micro-controller, etc.) connected to the XBee module through its serial interface.
Send serial data
The XBee device classes of the Digi IoT Library for .NET MAUI provide the method SendSerialData(byte[]) to send data to the serial interface of the physical XBee device.
| Method | Description |
|---|---|
SendSerialData(byte[]) |
Sends the given data to the XBee serial interface in a User Data Relay frame. Parameter must be a byte array containing the data to send. |
The SendSerialData(byte[]) method is asynchronous, which means that your application does not block during the transmit process. The SendSerialData(byte[]) method may fail for the following reasons:
-
The communication interface with the device is not open, throwing an InterfaceNotOpenException.
-
Other errors are caught as XBeeException.
// Instantiate an XBee device object.
XBeeBLEDevice myXBeeBLEDevice = new XBeeBLEDevice("00:00:00:00:00:00", "password");
// Connect the device.
myXBeeBLEDevice.Connect();
String data = "Serial port, are you there?";
// Send the data to the serial port interface.
myXBeeBLEDevice.SendSerialData(Encoding.UTF8.GetBytes(data));
Receive serial data
You can be notified when new data from the serial interface has been received if you are subscribed to the SerialDataReceived event of the XBee device classes.
// Instantiate an XBee device object.
XBeeBLEDevice myXBeeBLEDevice = new XBeeBLEDevice("00:00:00:00:00:00", "password");
// Connect the device.
myXBeeBLEDevice.Connect();
// Subscribe to serial data received event.
EventHandler<SerialDataReceivedEventArgs> handler = (sender, e) => Console.WriteLine(">> From serial port: " + Encoding.ASCII.GetString(e.Data));
myXBeeBLEDevice.SerialDataReceived += handler;
Whenever serial data is received, the handler callback is executed and provides as parameter the SerialDataReceivedEventArgs object (e), which contains the received serial data.