Skip to content

Commit e779905

Browse files
authored
Merge pull request #4 from chipkin/readability
Updated readme and comments
2 parents cf0cf78 + 64af864 commit e779905

File tree

7 files changed

+107
-54
lines changed

7 files changed

+107
-54
lines changed

BACnetServerMSTPExample/BACnetServerMSTPExample.cpp

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1-
// BACnetServerMSTPExample.cpp : This file contains the 'main' function. Program execution begins and ends there.
2-
//
1+
/*
2+
* BACnet Server MSTP Example C++
3+
* ----------------------------------------------------------------------------
4+
* BACnetServerMSTPExample.cpp
5+
*
6+
* In this CAS BACnet Stack example, we create a minimal BACnet MSTP server.
7+
*
8+
* More information https://github.com/chipkin/BACnetServerMSTPExampleCPP
9+
*
10+
* This file contains the 'main' function. Program execution begins and ends there.
11+
*
12+
* Created by: Steven Smethurst
13+
*/
14+
315

416
#include "CASBACnetStackAdapter.h"
517
#include "CASBACnetStackExampleConstants.h"
@@ -67,12 +79,13 @@ void APDUCallBack(unsigned char* buffer, uint16_t length, unsigned char source);
6779
void MSTPFrameCallBack(uint8_t* buffer, uint16_t length);
6880
void MSTPDebugLog(const char* message);
6981

70-
// Helper
82+
// Helper functions
7183
void DebugMSTPFrame(uint8_t* buffer, uint16_t length);
7284

7385
std::map<std::string, uint32_t> g_statCounter;
7486
void printMSTPStats();
7587

88+
// MSTP Frame
7689
struct MSTPAPDUFrame : public MSTPFrame
7790
{
7891
MSTPAPDUFrame(uint8_t destination, uint8_t* buffer, uint16_t length) {
@@ -131,8 +144,6 @@ int main(int argc, char* argv[])
131144
}
132145

133146

134-
135-
136147
// 1. Load the CAS BACnet stack functions
137148
// ---------------------------------------------------------------------------
138149
std::cout << "FYI: Loading CAS BACnet Stack functions... ";
@@ -143,6 +154,7 @@ int main(int argc, char* argv[])
143154
std::cout << "OK" << std::endl;
144155
std::cout << "FYI: CAS BACnet Stack version: " << fpGetAPIMajorVersion() << "." << fpGetAPIMinorVersion() << "." << fpGetAPIPatchVersion() << "." << fpGetAPIBuildVersion() << std::endl;
145156

157+
146158
// 2. Connect the serial resource
147159
// ---------------------------------------------------------------------------
148160
std::cout << "FYI: Connecting serial resource to serial port. baudrate=[" << g_database.device.baudRate << "], ... ";
@@ -153,21 +165,24 @@ int main(int argc, char* argv[])
153165
}
154166
std::cout << "OK" << std::endl;
155167

156-
// Configure the highspeed timer.
168+
// Configure the highspeed timer
157169
CHiTimer_Init();
158170

159171
// Configure the MSTP thread
160172
if (!MSTP_Init(RecvByte, SendByte, ThreadSleep, APDUCallBack, TimerReset, TimerDifference, g_database.device.macAddress, MSTPFrameCallBack, MSTPDebugLog)) {
161173
std::cerr << "Error: Failed to start MSTP stack" << std::endl;
162174
return 0;
163175
}
176+
177+
// Set the baud rate
164178
MSTP_SetBaudRate(g_database.device.baudRate);
165179
std::cout << "OK, Connected to port" << std::endl;
166180

167181
// Set the MSTP stack's max master
168182
MSTP_SetMaxMaster(g_database.device.maxMaster);
169183

170-
// 3. Setup the callbacks.
184+
185+
// 3. Setup the callbacks
171186
// ---------------------------------------------------------------------------
172187
std::cout << "FYI: Registering the callback Functions with the CAS BACnet Stack" << std::endl;
173188

@@ -186,7 +201,8 @@ int main(int argc, char* argv[])
186201
// Set Property Callback Functions
187202
fpRegisterCallbackSetPropertyReal(CallbackSetPropertyReal);
188203

189-
// 4. Setup the BACnet device.
204+
205+
// 4. Setup the BACnet device
190206
// ---------------------------------------------------------------------------
191207
std::cout << "Setting up server device. device.instance=[" << g_database.device.instance << "]" << std::endl;
192208

@@ -195,13 +211,16 @@ int main(int argc, char* argv[])
195211
std::cerr << "Failed to add Device." << std::endl;
196212
return false;
197213
}
214+
215+
// Add AnalogValue object to device
198216
if (!fpAddObject(g_database.device.instance, CASBACnetStackExampleConstants::OBJECT_TYPE_ANALOG_VALUE, g_database.analogValue.instance)) {
199217
std::cerr << "Failed to add Analog value(2)." << std::endl;
200218
return false;
201219
}
202220
std::cout << "Created Device." << std::endl;
203221

204222
// Enable services
223+
// Enable write property
205224
std::cout << "Enabling WriteProperty service... ";
206225
if (!fpSetServiceEnabled(g_database.device.instance, CASBACnetStackExampleConstants::SERVICE_WRITE_PROPERTY, true)) {
207226
std::cerr << "Failed to enabled the WriteProperty" << std::endl;
@@ -223,12 +242,14 @@ int main(int argc, char* argv[])
223242
std::cout << "FYI: Sending I-AM broadcast" << std::endl;
224243
uint8_t connectionString[1] = { 0xFF };
225244
if (!fpSendIAm(g_database.device.instance, connectionString, 1, CASBACnetStackExampleConstants::NETWORK_TYPE_MSTP, true, 65535, NULL, 0)) {
226-
std::cerr << "Unable to send IAm broadcast" << std::endl ;
245+
std::cerr << "Unable to send I-Am broadcast" << std::endl;
227246
return false;
228247
}
229248

249+
// Clear the RX TX Buffer
230250
RS232_flushRXTX(g_database.device.serialPort);
231251

252+
232253
// 5. Start the main loop
233254
// ---------------------------------------------------------------------------
234255
std::cout << "FYI: Entering main loop..." << std::endl;
@@ -266,12 +287,12 @@ uint16_t CallbackReceiveMessage(uint8_t* message, const uint16_t maxMessageLengt
266287
if (g_incomingFrame.size() <= 0) {
267288
// Call Sleep to give some time back to the system
268289
Sleep(1);
269-
return 0; // Nothing recived.
290+
return 0; // Nothing received
270291
}
271292

272293
for (std::list<MSTPAPDUFrame>::iterator itr = g_incomingFrame.begin(); itr != g_incomingFrame.end(); itr++) {
273294
if ((*itr).length > maxMessageLength) {
274-
// This is too big to fit in the message buffer. it probably never will be able to be used. lets get ride of it
295+
// This is too big to fit in the message buffer. It probably never will be able to be used, so get rid of it.
275296
g_incomingFrame.erase(itr);
276297
return 0;
277298
}
@@ -541,8 +562,8 @@ bool SendByte(unsigned char* byte, uint16_t length) {
541562
}
542563

543564
// The MSTP thread does not need attention for the next "length" amount of time.
544-
// This function can do nothing, and the MSTP thread will consumed as much proccessing power
545-
// that you will give it.
565+
// This function can do nothing, and the MSTP thread will consume as much processing power
566+
// as you give it.
546567
void ThreadSleep(uint32_t length) {
547568
if (length < 1) {
548569
return;
@@ -555,7 +576,7 @@ void TimerReset() {
555576
CHiTimer_Reset();
556577
}
557578

558-
// Find the differen in time between the last time that the TimerReset was called and now.
579+
// Find the difference in time between the last time that the TimerReset was called and now.
559580
uint32_t TimerDifference() {
560581
return CHiTimer_DiffTimeMicroSeconds();
561582
}
@@ -573,6 +594,7 @@ void MSTPDebugLog(const char* message) {
573594
#endif // MSTP_DEBUG
574595
}
575596

597+
// Displays MSTP Frame Data
576598
void DebugMSTPFrame(uint8_t* buffer, uint16_t length) {
577599
if (length < 3) {
578600
return;

BACnetServerMSTPExample/CASBACnetStackExampleConstants.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
/*
2+
* BACnet Server MSTP Example C++
3+
* ----------------------------------------------------------------------------
4+
* CASBACnetStackExampleConstants.h
5+
*
6+
* This is a fully static class that contains all the constants used by the example
7+
* This includes Object Types, Property Identifiers, etc
8+
*
9+
* Created by: Steven Smethurst
10+
*/
11+
112
#ifndef __CASBACnetStackExampleConstants_h__
213
#define __CASBACnetStackExampleConstants_h__
314

415
/*
5-
This is a fully static class that contains all the constants used by the example
6-
This includes Object Types, Property Identifiers, etc
16+
717
*/
818
class CASBACnetStackExampleConstants {
919

BACnetServerMSTPExample/CASBACnetStackExampleDatabase.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/*
2+
* BACnet Server MSTP Example C++
3+
* ----------------------------------------------------------------------------
4+
* CASBACnetStackExampleDatabase.cpp
5+
*
6+
* Sets up object names and properties in the database.
7+
*
8+
* Created by: Steven Smethurst
9+
*/
10+
111
#include "CASBACnetStackExampleDatabase.h"
212

313
#include <time.h> // time()

BACnetServerMSTPExample/CASBACnetStackExampleDatabase.h

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
#ifndef __CASBACnetStackExampleDatabase_h__
2-
#define __CASBACnetStackExampleDatabase_h__
3-
41
/*
5-
The CASBACnetStackExampleDatabase is a data store that contains the
6-
some example data used in the BACnetStackDLLExample.
2+
* BACnet Server MSTP Example C++
3+
* ----------------------------------------------------------------------------
4+
* CASBACnetStackExampleDatabase.h
5+
*
6+
* The CASBACnetStackExampleDatabase is a data storage that contains
7+
* some example data used in the server example. This data is represented by BACnet
8+
* objects for this server example. There will be one object of each type currently
9+
* supported by the CASBACnetStack.
10+
*
11+
* The database will include the following:
12+
* - present value
13+
* - name
14+
* - for outputs priority array (bool and value)
15+
*
16+
* Created by: Steven Smethurst
17+
*/
718

8-
This data is represented by BACnet objects for this server example.
9-
There will be one object of each type currently supported by the CASBACnetStack.
1019

11-
The database will include the following:
12-
- present value
13-
- name
14-
- for outputs priority array (bool and value)
15-
*/
20+
#ifndef __CASBACnetStackExampleDatabase_h__
21+
#define __CASBACnetStackExampleDatabase_h__
1622

1723
#include <string>
1824
#include <vector>

BACnetServerMSTPExample/CHiTimer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Written by: Steven Smethust
66
Email: funvill@funvill.com
77
8-
No warrantee of any kind, express or implied, is included with this
8+
No warranty of any kind, express or implied, is included with this
99
software; use at your own risk
1010
1111
If you find this code useful, credits would be nice!
@@ -16,7 +16,7 @@
1616
1717
Versions
1818
1.00aA 25 Nov 02 SWS Created
19-
1.00aB 11 Ded 06 SWS Added Licence
19+
1.00aB 11 Ded 06 SWS Added License
2020
2121
********************************************************************************/
2222

BACnetServerMSTPExample/CHiTimer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Written by: Steven Smethust
66
Email: funvill@funvill.com
77
8-
No warrantee of any kind, express or implied, is included with this
8+
No warranty of any kind, express or implied, is included with this
99
software; use at your own risk
1010
1111
If you find this code useful, credits would be nice!
@@ -16,7 +16,7 @@
1616
1717
Versions
1818
1.00aA 25 Nov 02 SWS Created
19-
1.00aB 11 Ded 06 SWS Added Licence
19+
1.00aB 11 Ded 06 SWS Added License
2020
2121
********************************************************************************/
2222
#ifndef __CHiTimer_h__

README.md

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,42 @@
1-
# BACnet Server MSTP Example CPP
1+
# BACnet Server MSTP Example C++
22

3-
A minumal BACnet MSTP server example written with C++ using the [CAS BACnet Stack](https://www.bacnetstack.com/). For a full featured BACnet Server example see the [BACnet Server Example CPP](https://github.com/chipkin/BACnetServerExampleCPP) project.
3+
A minimal BACnet MSTP server example written in C++ using the [CAS BACnet Stack](https://store.chipkin.com/services/stacks/bacnet-stack). For a full featured BACnet Server example see the [BACnet Server Example CPP](https://github.com/chipkin/BACnetServerExampleCPP) project.
44

5-
Device tree:
5+
## Releases
66

7-
- Device: 389999 (Device Rainbow)
8-
- analog_value: 2 (AnalogValue Diamond)
7+
Build versions of this example can be downloaded from the [Releases](https://github.com/chipkin/BACnetServerMSTPExampleCPP/releases) page.
8+
9+
## Installation
10+
11+
Download the latest release zip file on the [Releases](https://github.com/chipkin/BACnetServerMSTPExampleCPP/releases) page.
912

10-
## Cli
13+
## Usage
1114

12-
The serial port, baud rate, and MAC address of this BACnet MSTP server can be configured using command line parameters.
15+
The serial port, baud rate, and MAC address of this BACnet MSTP server can be configured using command line parameters:
1316

1417
```bash
1518
BACnetServerMSTPExampleCPP [serial port] [baud rate] [mac address]
1619
BACnetServerMSTPExampleCPP COM5 9600 1
1720
BACnetServerMSTPExampleCPP ttyS6 19200 25
1821
```
1922

20-
## Output
23+
Device tree:
24+
25+
- Device: 389999 (Device Rainbow)
26+
- analog_value: 2 (AnalogValue Diamond)
27+
28+
## Build
29+
30+
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.
2131

22-
The application outputs all BYTES sent or recived. BYTES with underscore after them are recived bytes. BYTES with a space after them are sent bytes. When the user presses the "enter" key, the MSTP stats, states, and events are printed for reference.
32+
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/ folder.
33+
2. Use [Visual Studio 2019](https://visualstudio.microsoft.com/vs/) to build the project. The solution can be found in the */BACnetServerMSTPExample/* folder.
34+
35+
A makefile that uses gcc is included for making executables on linux.
36+
37+
## Example Output
38+
39+
The application outputs all BYTES sent or received. BYTES with underscore after them are received bytes. BYTES with a space after them are sent bytes. When the user presses the "enter" key, the MSTP stats, states, and events are printed for reference.
2340

2441
```txt
2542
CAS BACnet Stack Server MSTP Example v0.0.1.0
@@ -86,16 +103,4 @@ MSTP Events:
86103
NextStationUnknown: 1ReceivedDataNeedingReply: 1 ReceivedInvalidFrame: 0
87104
ReceivedPFM: 1 ReceivedReplyToPFM: 1 ResetMaintenancePFM: 2
88105
RetrySendToken: 0 SendCustomFrame: 1 SendMaintenancePFM: 1
89-
```
90-
91-
## Building
92-
93-
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.
94-
95-
A makefile that uses gcc is included for making excultables on linux.
96-
97-
## Releases
98-
99-
Build versions of this example can be downloaded from the releases page:
100-
101-
[https://github.com/chipkin/BACnetServerMSTPExampleCPP/releases](https://github.com/chipkin/BACnetServerMSTPExampleCPP/releases)
106+
```

0 commit comments

Comments
 (0)