using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace BasicReadWrite
{
//
// A very simple read/write client code based on Yabe code
//
class Program
{
static BacnetClient bacnet_client;
// All the present Bacnet Device List
static List<BacNode> DevicesList = new List<BacNode>();
/*****************************************************************************************/
static void Main(string[] args)
{
[Link](new ConsoleTraceListener());
try
{
StartActivity();
[Link]("Started");
[Link](1000); // Wait a fiew time for WhoIs responses (managed in
handler_OnIam)
ReadWriteExample();
}
catch { }
[Link]();
}
/******************************************************************************************
**/
static void StartActivity()
{
// Bacnet on UDP/IP/Ethernet
bacnet_client = new BacnetClient(new BacnetIpUdpProtocolTransport(0xBAC0,
false));
// or Bacnet Mstp on COM4 38400 bps, own master id 8
// m_bacnet_client = new BacnetClient(new BacnetMstpProtocolTransport("COM4",
38400, 8);
// Or Bacnet Ethernet
// bacnet_client = new BacnetClient(new
BacnetEthernetProtocolTransport("Connexion au rseau local"));
// Or Bacnet on IPV6
// bacnet_client = new BacnetClient(new
BacnetIpV6UdpProtocolTransport(0xBAC0));
bacnet_client.Start(); // go
// Send WhoIs in order to get back all the Iam responses :
bacnet_client.OnIam += new [Link](handler_OnIam);
bacnet_client.WhoIs();
/* Optional Remote Registration as A Foreign Device on a BBMD at @[Link]
on the default 0xBAC0 port
bacnet_client.RegisterAsForeignDevice("[Link]", 60);
[Link](20);
bacnet_client.RemoteWhoIs("[Link]");
*/
}
/*****************************************************************************************/
static void ReadWriteExample()
{
BacnetValue Value;
bool ret;
// Read Present_Value property on the object ANALOG_INPUT:0 provided by the
device 12345
// Scalar value only
ret = ReadScalarValue(12345, new
BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_INPUT, 0),
BacnetPropertyIds.PROP_PRESENT_VALUE, out Value);
if (ret == true)
{
[Link]("Read value : " + [Link]());
// Write Present_Value property on the object ANALOG_OUTPUT:0 provided by
the device 4000
BacnetValue newValue = new BacnetValue([Link]([Link])); //
expect it's a float
ret = WriteScalarValue(4000, new
BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_OUTPUT, 0),
BacnetPropertyIds.PROP_PRESENT_VALUE, newValue);
[Link]("Write feedback : " + [Link]());
}
else
[Link]("Error somewhere !");
}
/*****************************************************************************************/
static void handler_OnIam(BacnetClient sender, BacnetAddress adr, uint device_id,
uint max_apdu, BacnetSegmentations segmentation, ushort vendor_id)
{
lock (DevicesList)
{
// Device already registred ?
foreach (BacNode bn in DevicesList)
if ([Link](device_id) != null) return; // Yes
// Not already in the list
[Link](new BacNode(adr, device_id)); // add it
}
}
/*****************************************************************************************/
static bool ReadScalarValue(int device_id, BacnetObjectId BacnetObjet,
BacnetPropertyIds Propriete, out BacnetValue Value)
{
BacnetAddress adr;
IList<BacnetValue> NoScalarValue;
Value = new BacnetValue(null);
// Looking for the device
adr = DeviceAddr((uint)device_id);
if (adr == null) return false; // not found
// Property Read
if (bacnet_client.ReadPropertyRequest(adr, BacnetObjet, Propriete, out
NoScalarValue) == false)
return false;
Value = NoScalarValue[0];
return true;
}
/*****************************************************************************************/
static bool WriteScalarValue(int device_id, BacnetObjectId BacnetObjet,
BacnetPropertyIds Propriete, BacnetValue Value)
{
BacnetAddress adr;
// Looking for the device
adr = DeviceAddr((uint)device_id);
if (adr == null) return false; // not found
// Property Write
BacnetValue[] NoScalarValue = { Value };
if (bacnet_client.WritePropertyRequest(adr, BacnetObjet, Propriete,
NoScalarValue) == false)
return false;
return true;
}
/*****************************************************************************************/
static BacnetAddress DeviceAddr(uint device_id)
{
BacnetAddress ret;
lock (DevicesList)
{
foreach (BacNode bn in DevicesList)
{
ret = [Link](device_id);
if (ret != null) return ret;
}
// not in the list
return null;
}
}
}
class BacNode
{
BacnetAddress adr;
uint device_id;
public BacNode(BacnetAddress adr, uint device_id)
{
[Link] = adr;
this.device_id = device_id;
}
public BacnetAddress getAdd(uint device_id)
{
if (this.device_id == device_id)
return adr;
else
return null;
}
}
}