Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A .NET library for XBee wireless controllers

License

NotificationsYou must be signed in to change notification settings

jefffhaynes/XBee

Repository files navigation

A.NET package forXBee wireless controllers.

  • Support for Series1, Series 2, 900HP, and Cellular
  • Simple async/await command and query model
  • .NET Rx support for async receive and sampling.

Features

  • Local and remote device discovery
  • Local and remote device configuration
  • Pin configuration
  • Pin control
  • Pin monitoring
  • Digital and analog sample monitoring via events or.NET Rx
  • Data transmit
  • Data receive via events or.NET Rx

UWP Quick Start

Here is a simple example with a coordinator and an arbitrary number of end devices that we're going to configure and monitor for sampling.

Note that for UWP apps you will need to declare a serial communications device capability in your app manifest.

  <Capabilities>    <DeviceCapabilityName="serialcommunication">      <DeviceId="any">        <FunctionType="name:serialPort" />      </Device>    </DeviceCapability>  </Capabilities>
varcontrollers=awaitXBeeController.FindControllersAsync(9600);varcontroller=controllers.First();// setup a simple callback for each time we discover a nodecontroller.NodeDiscovered+=async(sender,args)=>{Console.WriteLine("Discovered {0}",args.Name);// setup some pinsawaitargs.Node.SetInputOutputConfigurationAsync(InputOutputChannel.Channel2,InputOutputConfiguration.DigitalIn);awaitargs.Node.SetInputOutputConfigurationAsync(InputOutputChannel.Channel3,InputOutputConfiguration.AnalogIn);// set sample rateawaitargs.Node.SetSampleRateAsync(TimeSpan.FromSeconds(5));// register callback for sample recieved from this node// TODO: in practice you would want to make sure you only subscribe once (or better yet use Rx)args.Node.SampleReceived+=(node,sample)=>Console.WriteLine("Sample recieved: {0}",sample);}// now discover the network, which will trigger the NodeDiscovered callback for each node foundawait controller.DiscoverNetworkAsync();Console.ReadKey();// wait for the samples to flow in...

.NET Quick Start

Here is a simple example with a coordinator on COM3 and an arbitrary number of end devices that we're going to configure and monitor for sampling.

varcontroller=newXBeeController();// setup a simple callback for each time we discover a nodecontroller.NodeDiscovered+=async(sender,args)=>{Console.WriteLine("Discovered {0}",args.Name);// setup some pinsawaitargs.Node.SetInputOutputConfigurationAsync(InputOutputChannel.Channel2,InputOutputConfiguration.DigitalIn);awaitargs.Node.SetInputOutputConfigurationAsync(InputOutputChannel.Channel3,InputOutputConfiguration.AnalogIn);// set sample rateawaitargs.Node.SetSampleRateAsync(TimeSpan.FromSeconds(5));// register callback for sample recieved from this node// TODO: in practice you would want to make sure you only subscribe once (or better yet use Rx)args.Node.SampleReceived+=(node,sample)=>Console.WriteLine("Sample recieved: {0}",sample);}// open the connection to our coordinatorawait controller.OpenAsync("COM3",9600);// now discover the network, which will trigger the NodeDiscovered callback for each node foundawaitcontroller.DiscoverNetworkAsync();Console.ReadKey();controller.Dispose();// wait for the samples to flow in...

If you don't know a priori what port the XBee will be attached to you can also scan for it:

varcontroller=awaitXBeeController.FindAndOpenAsync(SerialPort.GetPortNames(),9600);if(controller!=null)// ...

Nodes

The XBeeController class represents the local serial attached XBee API. This would typically be a coordinator but could be any device to be controlled via a serial port.

While the controller represents the API, if we want to control the node itself we need to access the local node property.

varlocalNode=controller.Local;// which is the same as calling await controller.GetNodeAsync(); // (address = null)varserialNumber=awaitlocalNode.GetSerialNumberAsync();// etc

This allows us to treat the local node and remote nodes in the same fashion.

varremoteNode=awaitcontroller.GetNodeAsync(address);varserialNumber=awaitremoteNode.GetSerialNumberAsync();

The address for the remote node can be determined in a number of ways. Either connect the remote node physically and use one of the X-CTU utilities (or the above code) or use network discovery.

In some cases, you may have to cast the node to a specific device class to access functions that are not broadly supported. For example:

varremoteNode=(XBeeSeries1)awaitcontroller.GetNodeAsync(address);varpullUpResistorConfig=awaitremoteNode.GetPullUpResistorConfigurationAsync();

Command and Events

XBees are based on a sort of command-event model where the coordinator is either telling the XBee to do something or the XBee is telling the coordinator that something happened.

Commands

The first type of command is what XBee calls AT commands. An example is a command that can be used to configure pins on the XBee, setting pins high or low or reserving them for input.

awaitnode.SetInputOutputConfigurationAsync(InputOutputChannel.Channel4,InputOutputConfiguration.DigitalHigh);

This will force pin DIO4 high. Note that which physical pin this translates to depends on the model.

Below is a table summarizing the commands supported by this library.

AT Commands

DescriptionQueryCommandS1S2Pro900Cellular
HVHardware VersionGetHardwareVersionAsync--
APAPI ModeGetApiModeAsyncSetApiModeAsync
RERestore Defaults--RestoreDefaultsAsync
FRSoft Reset--ResetAsync
BDInterface Data RateGetBaudRateAsyncSetBaudRateAsync
NBParityGetParityAsyncSetParityAsync
SBStop BitsGetStopBitsAsyncSetStopBitsAsync
DBSignal StrengthGetSignalStrengthAsync--
ROPacketization TimeoutGetPacketizationTimeoutAsyncSetPacketizationTimeoutAsync
FTFlow Control ThresholdGetFlowControlThresholdAsyncSetFlowControlThresholdAsync
NINode IdentifierGetNodeIdentifierAsyncSetNodeIdentifierAsync
CNExit Command Mode--ExitCommandModeAsync
WRWrite Command--WriteChangesAsync
SH/SLSerial NumberGetSerialNumberAsync--
DH/DLDestination AddressGetAddressAsyncSetDestinationAddressAsync
MYSource AddressGetAddressAsyncSetSourceAddressAsyncNote 1
SCScan ChannelsGetScanChannelsAsyncSetScanChannelsAsync
SDScan DurationGetScanDurationAsyncSetScanDurationAsync
NDNetwork Discovery--DiscoverNetworkAsync
NTDiscovery TimeoutGetNetworkDiscoveryTimeoutAsyncSetNetworkDiscoveryTimeoutAsync
AIAssociation IndicatorGetAssociationAsync--
A1End Device AssociationGetEndDeviceAssociationOptionsAsyncSetEndDeviceAssociationOptionsAsync
A2Coordinator AssociationGetCoordinatorAssociationOptionsAsyncSetCoordinatorAssociationOptionsAsync
DAForce Disassociation--DisassociateAsync
D(N)DIO ConfigurationGetInputOutputConfigurationAsyncSetInputOutputConfigurationAsync
ICInput ChangeGetChangeDetectionChannelsAsyncSetChangeDetectionChannelsAsync
IRSample RateGetSampleRateAsyncSetSampleRateAsync
ISForce Sample--ForceSampleAsync
IDPAN ID / Module VIDGetPanIdAsync/GetModuleVidAsyncSetPanIdAsync/SetModuleVidAsync
RPRSSI PWM TimerGetRssiPwmTimeAsyncSetRssiPwmTimeAsync
PRPull-up Resistor ConfigGetPullUpResistorConfigurationAsyncSetPullUpResistorConfigurationAsync
SMSleep ModeGetSleepModeAsyncSetSleepModeAsync
SOSleep Mode OptionsGetSleepOptionsAsyncSetSleepOptionsAsync
SPSleep PeriodGetSleepPeriodAsyncSetSleepPeriodAsync
SNSleep Period CountGetSleepPeriodCountAsyncSetSleepPeriodCountAsync
ZSStack ProfileGetStackProfileAsyncSetStackProfileAsync
NJNode Join TimeGetNodeJoinTimeAsyncSetNodeJoinTimeAsync
JVChannel VerificationIsChannelVerificationEnabledAsyncSetChannelVerificationEnabledAsync
NWNetwork WatchdogGetNetworkWatchdogTimeoutAsyncSetNetworkWatchdogTimeoutAsync
JNJoin NotificationIsJoinNotificationEnabledAsyncSetJoinNotificationEnabledAsync
CBCommissioning Button--PushCommissioningButtonAsync
DDDevice Type IdentifierGetDeviceTypeIdentifierAsyncSetDeviceTypeIdentifierAsync
%VSupply VoltageGetSupplyVoltageAsync--
EEEncryption EnableIsEncryptionEnabledAsyncSetEncryptionEnabledAsync
KYEncryption Key--SetEncryptionKeyAsync
CECoordinator EnableIsCoordinatorAsyncSetCoordinatorAsync
CHChannelGetChannelAsyncSetChannelAsync
AIAssociation IndicatorGetAssociationAsyncSetAssociationAsync
NRReset Network--ResetNetworkAsync
PHPhone NumberGetPhoneNumberAsync--
S#ICCIDGetIccidAsync--
IMIMEIGetImeiAsync--
MNNetwork OperatorGetNetworkOperatorAsync--
MVModem Firmware VersionGetModemFirmwareVersionAsync--
IPInternet ProtocolGetInternetProtocolAsyncSetInternetProtocolAsync
TLSSL ProtocolGetSslProtocolAsyncSetSslProtocolAsync
TMClient TimeoutGetTcpClientConnectionTimeoutAsyncSetTcpClientConnectionTimeoutAsync
DODevice OptionGetDeviceOptionAsyncSetDeviceOptionAsync
ANAccess Point NameGetAccessPointNameAsyncSetAccessPointNameAsync

All other commands are currently unsupported but feel free to file an issue if you'd like to see something that isn't here.

Note 1: Use GetIPAddressAsync.

Serial Data

The second type of command involves sending arbitrary serial data to a node. In the simplest case this can act as a transparent passthrough as most XBees will pass the serial data to their local UART. However, in the case of programmable XBees it is possible to intercept the serial data and store it, interpret it, etc.

awaitnode.TransmitDataAsync(Encoding.UTF8.GetBytes("Hello!"));

Events

Somewhat confusingly, XBees have two different mechanisms for asychronously sending data to the coordinator. The first is sampling and the second is serial data. Samples coorespond to our pin example from above and represent the "input" part of GPIO.

As such, we can configure a pin to take and return a sample to the coordinator.

// subscribe to the nodenode.SampleReceived+=(o,eventArgs)=>Console.WriteLine(eventArgs.DigitalSampleState);// configure a pin for digital samplingawaitnode.SetInputOutputConfigurationAsync(InputOutputChannel.Channel5,InputOutputConfiguration.DigitalIn);

At this point the node is set to send samples from pin DIO5 but not necessarily to take samples. There are three ways to trigger a sample: forced, periodic, or change detect.

awaitnode.ForceSampleAsync();// force
awaitnode.SetSampleRateAsync(TimeSpan.FromSeconds(5));// periodic
awaitnode.SetChangeDetectionChannelsAsync(DigitalSampleChannels.Input5);// change detect

The second mechanism for asynchronous remote data transmit is simply the receive side of the transparent serial channel.

node.DataReceived+=(o,eventArgs)=>Console.WriteLine("Received {0} bytes",eventArgs.Data.Length);

Again, this data would either be supplied by the external UART on the remote node or by the microcontroller on the remote node.

About

A .NET library for XBee wireless controllers

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp