Using System
Using System
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.BACnet;
using System.Threading;
using System.Diagnostics;
namespace BasicReadWrite
{
//
// A very simple read/write client code based on Yabe code
//
class Program
{
static BacnetClient bacnet_client;
Trace.Listeners.Add(new ConsoleTraceListener());
try
{
StartActivity();
Console.WriteLine("Started");
ReadWriteExample();
}
catch { }
Console.ReadKey();
}
/******************************************************************************************
**/
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
bacnet_client.WhoIs();
bacnet_client.RegisterAsForeignDevice("192.168.1.1", 60);
Thread.Sleep(20);
bacnet_client.RemoteWhoIs("192.168.1.1");
*/
}
/*****************************************************************************************/
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)
{
Console.WriteLine("Read value : " + Value.Value.ToString());
// 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;
// Property Write
BacnetValue[] NoScalarValue = { Value };
if (bacnet_client.WritePropertyRequest(adr, BacnetObjet, Propriete,
NoScalarValue) == false)
return false;
return true;
}
/*****************************************************************************************/
lock (DevicesList)
{
foreach (BacNode bn in DevicesList)
{
ret = bn.getAdd(device_id);
if (ret != null) return ret;
}
// not in the list
return null;
}
}
}
class BacNode
{
BacnetAddress adr;
uint device_id;