Skip to content

Commit dd8565f

Browse files
committed
Updated readme and comments
1 parent e672c22 commit dd8565f

3 files changed

Lines changed: 73 additions & 24 deletions

File tree

BACnetServerExample/BACnetServerExample/BACnetServerExampleDatabase.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
using BACnetStackDLLServerCSharpExample;
1+
/*
2+
* Windows BACnet Server Example Proprietary Property Sharp
3+
* ----------------------------------------------------------------------------
4+
* BACnetServerExampleDatabase.cs
5+
*
6+
* A BACnet server example that shows how to add proprietary property to an object
7+
*
8+
* More information https://github.com/chipkin/BACnetServerExampleProprietaryPropertyCSharp
9+
*
10+
* Created by: Steven Smethurst
11+
* Created on: June 24, 2019
12+
* Last updated: June 24, 2019
13+
*/
14+
15+
using BACnetStackDLLServerCSharpExample;
216
using System;
317
using System.Collections.Generic;
418
using System.Text;

BACnetServerExample/BACnetServerExample/Program.cs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
/**
1+
/*
22
* Windows BACnet Server Example Proprietary Property Sharp
33
* ----------------------------------------------------------------------------
4+
* Program.cs
5+
*
46
* A BACnet server example that shows how to add proprietary property to an object
57
*
68
* More information https://github.com/chipkin/BACnetServerExampleProprietaryPropertyCSharp
79
*
810
* Created by: Steven Smethurst
911
* Created on: June 24, 2019
1012
* Last updated: June 24, 2019
11-
*/
13+
*/
1214

1315

1416
using BACnetStackDLLServerCSharpExample;
@@ -22,19 +24,21 @@ namespace BACnetServerExample
2224
{
2325
class Program
2426
{
27+
// Main function
2528
static void Main(string[] args)
2629
{
2730
BACnetServer bacnetServer = new BACnetServer();
2831
bacnetServer.Run();
2932
}
3033

34+
// BACnet Server Object
3135
unsafe class BACnetServer
3236
{
3337
// UDP
3438
UdpClient udpServer;
3539
IPEndPoint RemoteIpEndPoint;
3640

37-
// Settings
41+
// Set up the BACnet port
3842
const UInt16 SETTING_BACNET_PORT = 47808;
3943

4044
// A Database to hold the current state of the
@@ -43,16 +47,20 @@ unsafe class BACnetServer
4347
// Version
4448
const string APPLICATION_VERSION = "0.0.1";
4549

50+
// Server setup and main loop
4651
public void Run()
4752
{
48-
Console.WriteLine("Starting Windows-BACnetServerExampleProprietaryPropertyCSharp version{0}.{1}", APPLICATION_VERSION, CIBuildVersion.CIBUILDNUMBER);
53+
Console.WriteLine("Starting Windows-BACnetServerExampleProprietaryPropertyCSharp version {0}.{1}", APPLICATION_VERSION, CIBuildVersion.CIBUILDNUMBER);
4954
Console.WriteLine("https://github.com/chipkin/BACnetServerExampleProprietaryPropertyCSharp");
5055
Console.WriteLine("FYI: BACnet Stack version: {0}.{1}.{2}.{3}",
5156
CASBACnetStackAdapter.GetAPIMajorVersion(),
5257
CASBACnetStackAdapter.GetAPIMinorVersion(),
5358
CASBACnetStackAdapter.GetAPIPatchVersion(),
5459
CASBACnetStackAdapter.GetAPIBuildVersion());
5560

61+
// 1. Setup the callbacks
62+
// ---------------------------------------------------------------------------
63+
5664
// Send/Recv callbacks.
5765
CASBACnetStackAdapter.RegisterCallbackSendMessage(SendMessage);
5866
CASBACnetStackAdapter.RegisterCallbackReceiveMessage(RecvMessage);
@@ -64,10 +72,13 @@ public void Run()
6472
// Set Datatype Callbacks
6573
CASBACnetStackAdapter.RegisterCallbackSetPropertyCharacterString(CallbackSetPropertyCharacterString);
6674

75+
// 2. Setup the BACnet device
76+
// ---------------------------------------------------------------------------
6777

78+
// Initialize database
6879
this.database.Setup();
6980

70-
// Add the device.
81+
// Add the device
7182
CASBACnetStackAdapter.AddDevice(this.database.Device.instance);
7283
CASBACnetStackAdapter.SetProprietaryProperty(this.database.Device.instance, CASBACnetStackAdapter.OBJECT_TYPE_DEVICE, this.database.Device.instance, 512 + 1, false, false, CASBACnetStackAdapter.DATA_TYPE_CHARACTER_STRING, false, false, false);
7384
CASBACnetStackAdapter.SetProprietaryProperty(this.database.Device.instance, CASBACnetStackAdapter.OBJECT_TYPE_DEVICE, this.database.Device.instance, 512 + 2, true, true, CASBACnetStackAdapter.DATA_TYPE_CHARACTER_STRING, false, false, false);
@@ -77,22 +88,26 @@ public void Run()
7788
CASBACnetStackAdapter.SetServiceEnabled(database.Device.instance, CASBACnetStackAdapter.SERVICE_WRITE_PROPERTY, true);
7889
CASBACnetStackAdapter.SetServiceEnabled(database.Device.instance, CASBACnetStackAdapter.SERVICE_WRITE_PROPERTY_MULTIPLE, true);
7990

80-
// All done with the BACnet setup.
91+
// All done with the BACnet setup
8192
Console.WriteLine("FYI: CAS BACnet Stack Setup, successfuly");
8293

83-
// Open the BACnet port to recive messages.
94+
// 3. Open the BACnet port to receive messages
95+
// ---------------------------------------------------------------------------
8496
this.udpServer = new UdpClient(SETTING_BACNET_PORT);
8597
this.RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
8698

87-
// Main loop.
99+
// 4. Main loop
100+
// ---------------------------------------------------------------------------
88101
Console.WriteLine("FYI: Starting main loop");
89102
for (; ; )
90103
{
91-
CASBACnetStackAdapter.Loop();
92-
database.Loop(); // Just for this example
104+
CASBACnetStackAdapter.Loop(); // CAS BACnet stack loop
105+
106+
database.Loop(); // Update database values
93107
}
94108
}
95109

110+
// Not used
96111
private void DoUserInput()
97112
{
98113
if (Console.KeyAvailable)
@@ -113,12 +128,14 @@ private void DoUserInput()
113128
}
114129
}
115130

116-
131+
// Callback used by the BACnet Stack to get the current time
117132
public ulong CallbackGetSystemTime()
118133
{
119134
// https://stackoverflow.com/questions/9453101/how-do-i-get-epoch-time-in-c
120135
return (ulong)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
121136
}
137+
138+
// Callback used by the BACnet Stack to send a BACnet message
122139
public UInt16 SendMessage(System.Byte* message, UInt16 messageLength, System.Byte* connectionString, System.Byte connectionStringLength, System.Byte networkType, Boolean broadcast)
123140
{
124141
if (connectionStringLength < 6 || messageLength <= 0)
@@ -148,6 +165,8 @@ public UInt16 SendMessage(System.Byte* message, UInt16 messageLength, System.Byt
148165

149166
return 0;
150167
}
168+
169+
// Callback used by the BACnet Stack to check if there is a message to process
151170
public UInt16 RecvMessage(System.Byte* message, UInt16 maxMessageLength, System.Byte* receivedConnectionString, System.Byte maxConnectionStringLength, System.Byte* receivedConnectionStringLength, System.Byte* networkType)
152171
{
153172
try
@@ -181,9 +200,10 @@ public UInt16 RecvMessage(System.Byte* message, UInt16 maxMessageLength, System.
181200
return 0;
182201
}
183202

184-
private UInt32 UpdateStringAndReturnSize(System.Char* value, UInt32 maxElementCount, string stringAsVallue)
203+
// Update a string and return its size
204+
private UInt32 UpdateStringAndReturnSize(System.Char* value, UInt32 maxElementCount, string stringAsValue)
185205
{
186-
byte[] nameAsBuffer = ASCIIEncoding.ASCII.GetBytes(stringAsVallue);
206+
byte[] nameAsBuffer = ASCIIEncoding.ASCII.GetBytes(stringAsValue);
187207
UInt32 valueElementCount = maxElementCount;
188208
if (nameAsBuffer.Length < valueElementCount)
189209
{
@@ -192,13 +212,16 @@ private UInt32 UpdateStringAndReturnSize(System.Char* value, UInt32 maxElementCo
192212
Marshal.Copy(nameAsBuffer, 0, (IntPtr)value, Convert.ToInt32(valueElementCount));
193213
return valueElementCount;
194214
}
215+
216+
// Read string from char pointer
195217
private string GetStringFromCharPointer(System.Char* value, UInt32 maxElementCount, Byte encodingType)
196218
{
197219
byte[] nameAsBuffer = new Byte[maxElementCount];
198220
Marshal.Copy((IntPtr)value, nameAsBuffer, 0, Convert.ToInt32(maxElementCount));
199221
return ASCIIEncoding.ASCII.GetString(nameAsBuffer);
200222
}
201223

224+
// Callback used by the BACnet Stack to set Charstring property values to the user
202225
public bool CallbackGetPropertyCharString(UInt32 deviceInstance, UInt16 objectType, UInt32 objectInstance, UInt32 propertyIdentifier, System.Char* value, UInt32* valueElementCount, UInt32 maxElementCount, System.Byte encodingType, bool useArrayIndex, UInt32 propertyArrayIndex)
203226
{
204227
Console.WriteLine("FYI: Request for CallbackGetPropertyCharString. objectType={0}, objectInstance={1}, propertyIdentifier={2}, propertyArrayIndex={3}", objectType, objectInstance, propertyIdentifier, propertyArrayIndex);
@@ -251,6 +274,7 @@ public bool CallbackGetPropertyCharString(UInt32 deviceInstance, UInt16 objectTy
251274
return false; // Could not handle this request.
252275
}
253276

277+
// Callback used by the BACnet Stack to set Charstring property values to the user
254278
bool CallbackSetPropertyCharacterString(UInt32 deviceInstance, UInt16 objectType, UInt32 objectInstance, UInt32 propertyIdentifier, char* value, UInt32 length, Byte encodingType, [In, MarshalAs(UnmanagedType.I1)] bool useArrayIndex, UInt32 propertyArrayIndex, System.Byte priority, UInt32* errorCode)
255279
{
256280
Console.WriteLine("FYI: Request for CallbackSetPropertyCharacterString. objectType={0}, objectInstance={1}, propertyIdentifier={2}, propertyArrayIndex={3}", objectType, objectInstance, propertyIdentifier, propertyArrayIndex);

README.md

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
1-
# BACnet Server Example Proprietary Property CSharp
1+
# BACnet Server Example Proprietary Property C#
22

3-
A basic BACnet IP server example written in CSharp using the [CAS BACnet Stack](https://www.bacnetstack.com/). In this example we show how to add a proprietary property to a device object.
3+
A basic BACnet IP server example written in C# using the [CAS BACnet Stack](https://store.chipkin.com/services/stacks/bacnet-stack). We show how to add a proprietary property to a device object in this example.
44

5+
## Releases
6+
7+
Build versions of this example can be downloaded from the [Releases](https://github.com/chipkin/BACnetServerExampleProprietaryPropertyCSharp/releases) page.
8+
9+
## Installation
10+
11+
Download the latest release zip file on the [Releases](https://github.com/chipkin/BACnetServerExampleProprietaryPropertyCSharp/releases) page.
12+
13+
## Usage
14+
Pre-configured with the following example BACnet device and properties
515
- device: 389001 (Device name Rainbow)
616
- Property 513 = "Proprietary property 513 readonly" (CharString)
717
- Property 514 = "Proprietary property 514 read/write" (CharString)
818

9-
## Example output
19+
## Build
20+
21+
A [Visual Studio 2019](https://visualstudio.microsoft.com/downloads/) project is included with this project. This project also auto built using [Gitlab CI](https://docs.gitlab.com/ee/ci/) on every commit.
1022

23+
1. Copy *CASBACnetStack_x64_Debug.dll*, *CASBACnetStack_x64_Debug.lib*, *CASBACnetStack_x64_Release.dll*, and *CASBACnetStack_x64_Release.lib* from the [CAS BACnet Stack](https://store.chipkin.com/services/stacks/bacnet-stack) project into the /bin/netcoreapp2.1/ folder.
24+
2. Use [Visual Studio 2019](https://visualstudio.microsoft.com/vs/) to build the project. The solution can be found in the */BACnetServerExampleProprietaryPropertyCSharp/BACnetServerExample/* folder.
25+
26+
27+
## Example output
1128
```txt
1229
Starting Windows-BACnetServerExampleProprietaryPropertyCSharp version0.0.1.0
1330
https://github.com/chipkin/BACnetServerExampleProprietaryPropertyCSharp
@@ -27,10 +44,4 @@ FYI: Request for CallbackGetPropertyCharString. objectType=8, objectInstance=389
2744
FYI: Sending 340 bytes to 192.168.1.26:47808
2845
FYI: Recving 19 bytes from 192.168.1.26:47808
2946
FYI: Sending 25 bytes to 192.168.1.26:47808
30-
```
31-
32-
## Building
33-
34-
A [Visual studio 2019](https://visualstudio.microsoft.com/downloads/) project is included with this project.
35-
36-
This project also auto built using [Gitlab CI](https://docs.gitlab.com/ee/ci/) on every commit.
47+
```

0 commit comments

Comments
 (0)