Added MS/TP crc calculating demo application to use when writing articles that include example MS/TP code.
This commit is contained in:
@@ -0,0 +1,59 @@
|
|||||||
|
#Makefile to build BACnet Application for the Linux Port
|
||||||
|
|
||||||
|
# tools - only if you need them.
|
||||||
|
# Most platforms have this already defined
|
||||||
|
# CC = gcc
|
||||||
|
|
||||||
|
# Executable file name
|
||||||
|
TARGET = mstpcrc
|
||||||
|
|
||||||
|
TARGET_BIN = ${TARGET}$(TARGET_EXT)
|
||||||
|
|
||||||
|
# This demo seems to be a little unique
|
||||||
|
BACNET_INCLUDE_DIR = ../../include
|
||||||
|
BACNET_SOURCE_DIR = ../../src
|
||||||
|
|
||||||
|
#libraries used
|
||||||
|
LIBRARIES=-lgcc,-lm
|
||||||
|
|
||||||
|
#build for release (default) or debug
|
||||||
|
DEBUGGING =
|
||||||
|
OPTIMIZATION = -Os
|
||||||
|
ifeq (${BUILD},debug)
|
||||||
|
OPTIMIZATION = -O0
|
||||||
|
DEBUGGING = -g
|
||||||
|
endif
|
||||||
|
|
||||||
|
# search order for included libraries
|
||||||
|
INCLUDES = -I$(BACNET_INCLUDE_DIR)
|
||||||
|
|
||||||
|
# any special defines
|
||||||
|
DEFINES =
|
||||||
|
|
||||||
|
# put all the flags together
|
||||||
|
CFLAGS = -Wall $(DEBUGGING) $(OPTIMIZATION) $(INCLUDES) $(DEFINES)
|
||||||
|
LFLAGS = -Wl,$(LIBRARIES)
|
||||||
|
|
||||||
|
SRCS = main.c \
|
||||||
|
${BACNET_SOURCE_DIR}/crc.c
|
||||||
|
|
||||||
|
OBJS = ${SRCS:.c=.o}
|
||||||
|
|
||||||
|
all: Makefile ${TARGET_BIN}
|
||||||
|
|
||||||
|
${TARGET_BIN}: ${OBJS} Makefile
|
||||||
|
${CC} ${PFLAGS} ${OBJS} ${LFLAGS} -o $@
|
||||||
|
size $@
|
||||||
|
cp $@ ../../bin
|
||||||
|
|
||||||
|
.c.o:
|
||||||
|
${CC} -c ${CFLAGS} $*.c -o $@
|
||||||
|
|
||||||
|
depend:
|
||||||
|
rm -f .depend
|
||||||
|
${CC} -MM ${CFLAGS} *.c >> .depend
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f core ${TARGET_BIN} ${OBJS} $(TARGET).map
|
||||||
|
|
||||||
|
include: .depend
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
@echo off
|
||||||
|
echo Build with MinGW and MSYS: mingw.sourceforge.net
|
||||||
|
rem set PATH=C:\MinGW\msys\1.0\bin;C:\MinGW\bin
|
||||||
|
rem assumes rm, cp, size are already in path
|
||||||
|
set CC=gcc
|
||||||
|
set AR=ar
|
||||||
|
set MAKE=make
|
||||||
|
set TARGET_EXT=.exe
|
||||||
|
make clean all
|
||||||
@@ -0,0 +1,182 @@
|
|||||||
|
/*####COPYRIGHTBEGIN####
|
||||||
|
-------------------------------------------
|
||||||
|
Copyright (C) 2012 Steve Karg
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU General Public License
|
||||||
|
as published by the Free Software Foundation; either version 2
|
||||||
|
of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program; if not, write to:
|
||||||
|
The Free Software Foundation, Inc.
|
||||||
|
59 Temple Place - Suite 330
|
||||||
|
Boston, MA 02111-1307
|
||||||
|
USA.
|
||||||
|
|
||||||
|
As a special exception, if other files instantiate templates or
|
||||||
|
use macros or inline functions from this file, or you compile
|
||||||
|
this file and link it with other works to produce a work based
|
||||||
|
on this file, this file does not by itself cause the resulting
|
||||||
|
work to be covered by the GNU General Public License. However
|
||||||
|
the source code for this file must still be made available in
|
||||||
|
accordance with section (3) of the GNU General Public License.
|
||||||
|
|
||||||
|
This exception does not invalidate any other reasons why a work
|
||||||
|
based on this file might be covered by the GNU General Public
|
||||||
|
License.
|
||||||
|
-------------------------------------------
|
||||||
|
####COPYRIGHTEND####*/
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
/* local includes */
|
||||||
|
#include "crc.h"
|
||||||
|
#include "version.h"
|
||||||
|
|
||||||
|
#ifndef max
|
||||||
|
#define max(a,b) (((a) (b)) ? (a) : (b))
|
||||||
|
#define min(a,b) (((a) < (b)) ? (a) : (b))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* buffer needed by CRC functions */
|
||||||
|
static uint8_t CRC_Buffer[1512];
|
||||||
|
static unsigned CRC_Buffer_Len = 0;
|
||||||
|
/* flags needed for options */
|
||||||
|
static bool ASCII_Decimal = false;
|
||||||
|
static unsigned CRC_Size = 8;
|
||||||
|
|
||||||
|
/******************************************************************
|
||||||
|
* DESCRIPTION: Takes one of the arguments passed by the main function
|
||||||
|
* and sets flags if it matches one of the predefined args.
|
||||||
|
* PARAMETERS: argc (IN) number of arguments.
|
||||||
|
* argv (IN) an array of arguments in string form.
|
||||||
|
* RETURN: number of arguments parsed
|
||||||
|
* NOTES: none
|
||||||
|
******************************************************************/
|
||||||
|
static void Parse_Arguments(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
long long_value = 0;
|
||||||
|
|
||||||
|
for (i=1;i<argc;i++) {
|
||||||
|
if (argv[i][0] == '-') {
|
||||||
|
/* numeric arguments */
|
||||||
|
if (isdigit(argv[i][1])) {
|
||||||
|
long_value = strtol(&argv[i][1], NULL, 10);
|
||||||
|
/* dash arguments */
|
||||||
|
switch(long_value) {
|
||||||
|
case 8:
|
||||||
|
CRC_Size = 8;
|
||||||
|
break;
|
||||||
|
case 16:
|
||||||
|
CRC_Size = 16;
|
||||||
|
break;
|
||||||
|
case 32:
|
||||||
|
CRC_Size = 32;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* dash arguments */
|
||||||
|
switch(argv[i][1]) {
|
||||||
|
case 'h':
|
||||||
|
case 'H':
|
||||||
|
ASCII_Decimal = false;
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
case 'D':
|
||||||
|
ASCII_Decimal = true;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
/* should be number values here */
|
||||||
|
if (ASCII_Decimal) {
|
||||||
|
long_value = strtol(argv[i], NULL, 10);
|
||||||
|
} else {
|
||||||
|
long_value = strtol(argv[i], NULL, 16);
|
||||||
|
}
|
||||||
|
CRC_Buffer[CRC_Buffer_Len] = (uint8_t)long_value;
|
||||||
|
CRC_Buffer_Len++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* simple program to CRC the data and print the CRC */
|
||||||
|
int main(
|
||||||
|
int argc,
|
||||||
|
char *argv[])
|
||||||
|
{
|
||||||
|
/* accumulates the crc value */
|
||||||
|
uint8_t crc8 = 0xff;
|
||||||
|
uint16_t crc16 = 0xffff;
|
||||||
|
unsigned i = 0;
|
||||||
|
|
||||||
|
/* initialize our interface */
|
||||||
|
if ((argc > 1) && (strcmp(argv[1], "--help") == 0)) {
|
||||||
|
printf("mstpcrc [options] <00 00 00 00...>\r\n"
|
||||||
|
"perform MS/TP CRC on data bytes.\r\n"
|
||||||
|
"options:\r\n"
|
||||||
|
"[-x] interprete the arguments as ascii hex (default)\r\n"
|
||||||
|
"[-d] interprete the argument as ascii decimal\r\n"
|
||||||
|
"[-8] calculate the MS/TP 8-bit Header CRC (default)\r\n"
|
||||||
|
"[-16] calculate the MS/TP 16-bit Data CRC\r\n"
|
||||||
|
"[-32] calculate the MS/TP 32-bit Extended Frame CRC\r\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if ((argc > 1) && (strcmp(argv[1], "--version") == 0)) {
|
||||||
|
printf("mstpcap %s\r\n", BACNET_VERSION_TEXT);
|
||||||
|
printf("Copyright (C) 2012 by Steve Karg\r\n"
|
||||||
|
"This is free software; see the source for copying conditions.\r\n"
|
||||||
|
"There is NO warranty; not even for MERCHANTABILITY or\r\n"
|
||||||
|
"FITNESS FOR A PARTICULAR PURPOSE.\r\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
Parse_Arguments(argc, argv);
|
||||||
|
if (CRC_Buffer_Len) {
|
||||||
|
for (i = 0; i < CRC_Buffer_Len; i++) {
|
||||||
|
if (CRC_Size == 8) {
|
||||||
|
crc8 = CRC_Calc_Header(CRC_Buffer[i], crc8);
|
||||||
|
} else if (CRC_Size == 16) {
|
||||||
|
crc16 = CRC_Calc_Data(CRC_Buffer[i], crc16);
|
||||||
|
}
|
||||||
|
if (ASCII_Decimal) {
|
||||||
|
printf("%u\r\n", (unsigned)CRC_Buffer[i]);
|
||||||
|
} else {
|
||||||
|
printf("0x%02X\r\n", CRC_Buffer[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (CRC_Size == 8) {
|
||||||
|
crc8 = ~crc8;
|
||||||
|
if (ASCII_Decimal) {
|
||||||
|
printf("%u Header CRC\r\n", (unsigned)crc8);
|
||||||
|
} else {
|
||||||
|
printf("0x%02X Header CRC\r\n", crc8);
|
||||||
|
}
|
||||||
|
} else if (CRC_Size == 16) {
|
||||||
|
crc16 = ~crc16;
|
||||||
|
if (ASCII_Decimal) {
|
||||||
|
printf("%u Data CRC\r\n", (unsigned)(crc16 & 0xFF));
|
||||||
|
printf("%u Data CRC\r\n", (unsigned)(crc16 >> 8));
|
||||||
|
} else {
|
||||||
|
printf("0x%02X Data CRC\r\n", (crc16 & 0xFF));
|
||||||
|
printf("0x%02X Data CRC\r\n", (crc16 >> 8));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
@@ -0,0 +1,139 @@
|
|||||||
|
#
|
||||||
|
# Simple makefile to build an executable for Win32
|
||||||
|
#
|
||||||
|
# This makefile assumes Borland bcc32 development environment
|
||||||
|
# on Windows NT/9x/2000/XP
|
||||||
|
#
|
||||||
|
|
||||||
|
!ifndef BORLAND_DIR
|
||||||
|
BORLAND_DIR_Not_Defined:
|
||||||
|
@echo .
|
||||||
|
@echo You must define environment variable BORLAND_DIR to compile.
|
||||||
|
!endif
|
||||||
|
|
||||||
|
# target
|
||||||
|
PRODUCT = mstpcrc
|
||||||
|
PRODUCT_EXE = $(PRODUCT).exe
|
||||||
|
|
||||||
|
# tools
|
||||||
|
CC = $(BORLAND_DIR)\bin\bcc32
|
||||||
|
MAKE=$(BORLAND_DIR)\bin\make.exe
|
||||||
|
#LINK = $(BORLAND_DIR)\bin\tlink32
|
||||||
|
LINK = $(BORLAND_DIR)\bin\ilink32
|
||||||
|
|
||||||
|
BACNET_LIB_DIR = ..\..\lib
|
||||||
|
BACNET_LIB = $(BACNET_LIB_DIR)\bacnet.lib
|
||||||
|
|
||||||
|
# directories
|
||||||
|
BACNET_PORT = ..\..\ports\win32
|
||||||
|
BACNET_INCLUDE = ..\..\include
|
||||||
|
BACNET_OBJECT = ..\object
|
||||||
|
BACNET_HANDLER = ..\handler
|
||||||
|
INCLUDES = \
|
||||||
|
-I$(BACNET_INCLUDE) \
|
||||||
|
-I$(BACNET_PORT) \
|
||||||
|
-I$(BACNET_OBJECT) \
|
||||||
|
-I$(BACNET_HANDLER) \
|
||||||
|
-I$(BORLAND_DIR)\include
|
||||||
|
|
||||||
|
#
|
||||||
|
BACNET_DEFINES = -DPRINT_ENABLED=1 -DBACAPP_ALL
|
||||||
|
#BACDL_DEFINE=-DBACDL_MSTP=1
|
||||||
|
BACDL_DEFINE=-DBACDL_BIP=1 -DUSE_INADDR=1
|
||||||
|
DEFINES = $(BACNET_DEFINES) $(BACDL_DEFINE)
|
||||||
|
|
||||||
|
SRCS = main.c \
|
||||||
|
|
||||||
|
OBJS = $(SRCS:.c=.obj)
|
||||||
|
|
||||||
|
#
|
||||||
|
# Compiler definitions
|
||||||
|
#
|
||||||
|
BCC_CFG = bcc32.cfg
|
||||||
|
|
||||||
|
#
|
||||||
|
# Include directories
|
||||||
|
#
|
||||||
|
CFLAGS = $(INCLUDES) $(DEFINES)
|
||||||
|
|
||||||
|
#
|
||||||
|
# Libraries
|
||||||
|
#
|
||||||
|
C_LIB_DIR = $(BORLAND_DIR)\lib
|
||||||
|
|
||||||
|
LIBS = $(BACNET_LIB) \
|
||||||
|
$(C_LIB_DIR)\IMPORT32.lib \
|
||||||
|
$(C_LIB_DIR)\CW32MT.lib \
|
||||||
|
|
||||||
|
#
|
||||||
|
# Main target
|
||||||
|
#
|
||||||
|
# This should be the first one in the makefile
|
||||||
|
|
||||||
|
all : $(BACNET_LIB) $(BCC_CFG) $(OBJS) $(PRODUCT_EXE)
|
||||||
|
del $(BCC_CFG)
|
||||||
|
|
||||||
|
install: $(PRODUCT_EXE)
|
||||||
|
copy $(PRODUCT_EXE) ..\..\bin\$(PRODUCT_EXE)
|
||||||
|
|
||||||
|
# Linker specific: the link below is for BCC linker/compiler. If you link
|
||||||
|
# with a different linker - please change accordingly.
|
||||||
|
#
|
||||||
|
|
||||||
|
# need a temp response file (@&&| ... |) because command line is too long
|
||||||
|
# $** lists each dependency
|
||||||
|
# $< target name
|
||||||
|
# $* target name without extension
|
||||||
|
$(PRODUCT_EXE) : $(OBJS)
|
||||||
|
@echo Running Linker for $(PRODUCT_EXE)
|
||||||
|
$(LINK) -L$(C_LIB_DIR) -L$(BACNET_LIB_DIR) -m -c -s -v @&&|
|
||||||
|
$(BORLAND_DIR)\lib\c0x32.obj $**
|
||||||
|
$<
|
||||||
|
$*.map
|
||||||
|
$(LIBS)
|
||||||
|
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# Utilities
|
||||||
|
|
||||||
|
clean :
|
||||||
|
del $(OBJS)
|
||||||
|
del $(PRODUCT_EXE)
|
||||||
|
del $(PRODUCT).map
|
||||||
|
del $(PRODUCT).ilc
|
||||||
|
del $(PRODUCT).ild
|
||||||
|
del $(PRODUCT).ilf
|
||||||
|
del $(PRODUCT).ils
|
||||||
|
del $(PRODUCT).tds
|
||||||
|
del $(BCC_CFG)
|
||||||
|
|
||||||
|
#
|
||||||
|
# Generic rules
|
||||||
|
#
|
||||||
|
.SUFFIXES: .cpp .c .sbr .obj
|
||||||
|
|
||||||
|
#
|
||||||
|
# cc generic rule
|
||||||
|
#
|
||||||
|
.c.obj:
|
||||||
|
$(CC) +$(BCC_CFG) -o$@ $<
|
||||||
|
|
||||||
|
# Compiler configuration file
|
||||||
|
$(BCC_CFG) :
|
||||||
|
Copy &&|
|
||||||
|
$(CFLAGS)
|
||||||
|
-c
|
||||||
|
-y #include line numbers in OBJ's
|
||||||
|
-v #include debug info
|
||||||
|
-w+ #turn on all warnings
|
||||||
|
-Od #disable all optimizations
|
||||||
|
#-a4 #32 bit data alignment
|
||||||
|
#-M # generate link map
|
||||||
|
#-ls # linker options
|
||||||
|
#-WM- #not multithread
|
||||||
|
-WM #multithread
|
||||||
|
-w-aus # ignore warning assigned a value that is never used
|
||||||
|
-w-sig # ignore warning conversion may lose sig digits
|
||||||
|
| $@
|
||||||
|
|
||||||
|
# EOF: makefile
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
BACnet MS/TP CRC Calculator
|
||||||
|
|
||||||
|
This tool receives MS/TP bytes and generates a CRC for those bytes
|
||||||
|
|
||||||
|
mstpcrc [options] <00 00 00 00...>
|
||||||
|
perform MS/TP CRC on data bytes.
|
||||||
|
options:
|
||||||
|
[-x] interprete the arguments as ascii hex (default)
|
||||||
|
[-d] interprete the argument as ascii decimal
|
||||||
|
[-8] calculate the MS/TP 8-bit Header CRC (default)
|
||||||
|
[-16] calculate the MS/TP 16-bit Data CRC
|
||||||
|
|
||||||
|
Here is a sample of the tool running (use CTRL-C to quit):
|
||||||
|
D:\code\bacnet-stack\demo\mstpcrc>mstpcrc 06 ff 01 00 15
|
||||||
|
0x06
|
||||||
|
0xFF
|
||||||
|
0x01
|
||||||
|
0x00
|
||||||
|
0x15
|
||||||
|
0x8E Header CRC
|
||||||
Reference in New Issue
Block a user