From ef80e662b3dff0b021fac61eef5e7aed0c041d68 Mon Sep 17 00:00:00 2001 From: skarg Date: Tue, 26 Jul 2011 22:11:20 +0000 Subject: [PATCH] Cleaned up compile on Win32 for MS/TP datalink. Still not reliable yet. --- bacnet-stack/demo/mstpcap/main.c | 19 ++++++------ bacnet-stack/lib/Makefile | 2 ++ bacnet-stack/ports/win32/dlmstp.c | 44 ++++----------------------- bacnet-stack/ports/win32/rs485.c | 50 +++++++++++++++++++------------ 4 files changed, 49 insertions(+), 66 deletions(-) diff --git a/bacnet-stack/demo/mstpcap/main.c b/bacnet-stack/demo/mstpcap/main.c index 96286082..d32314db 100644 --- a/bacnet-stack/demo/mstpcap/main.c +++ b/bacnet-stack/demo/mstpcap/main.c @@ -244,7 +244,7 @@ static void packet_statistics( old_tv.tv_usec = tv->tv_usec; } -static void packet_statistics_save( +static void packet_statistics_print( void) { unsigned i; /* loop counter */ @@ -500,7 +500,7 @@ static void write_global_header( fflush(pFile); fprintf(stdout, "mstpcap: saving capture to %s\n", filename); } else { - fprintf(stderr, "mstpcap: failed to open %s: %s\n", filename, + fprintf(stderr, "mstpcap[header]: failed to open %s: %s\n", filename, strerror(errno)); } if (pipe_enable) { @@ -552,7 +552,7 @@ static void write_received_packet( (void) data_write((char *) &mstp_port->DataCRCActualLSB, 1, 1); } } else { - fprintf(stderr, "mstpcap: failed to open %s: %s\n", Capture_Filename, + fprintf(stderr, "mstpcap[packet]: failed to open %s: %s\n", Capture_Filename, strerror(errno)); } } @@ -623,7 +623,7 @@ static bool test_global_header( return false; } } else { - fprintf(stderr, "mstpcap: failed to open %s: %s\n", filename, + fprintf(stderr, "mstpcap[scan]: failed to open %s: %s\n", filename, strerror(errno)); return false; } @@ -715,7 +715,7 @@ static bool read_received_packet( static void cleanup( void) { - packet_statistics_save(); + packet_statistics_print(); if (pFile) { fflush(pFile); /* stream pointer */ fclose(pFile); /* stream pointer */ @@ -764,6 +764,7 @@ void filename_create_new( if (pFile) { fclose(pFile); } + pFile = NULL; filename_create(&Capture_Filename[0]); write_global_header(&Capture_Filename[0]); } @@ -825,7 +826,7 @@ int main( fprintf(stdout, "\r%hu packets", packet_count); } if (packet_count) { - packet_statistics_save(); + packet_statistics_print(); } } else { fprintf(stderr, "File header does not match.\n"); @@ -839,12 +840,12 @@ int main( if (argc > 2) { my_baud = strtol(argv[2], NULL, 0); } - RS485_Set_Baud_Rate(my_baud); + atexit(cleanup); RS485_Initialize(); timer_init(); + RS485_Set_Baud_Rate(my_baud); fprintf(stdout, "mstpcap: Using %s for capture at %ld bps.\n", RS485_Interface(), (long) RS485_Get_Baud_Rate()); - atexit(cleanup); #if defined(_WIN32) SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_PROCESSED_INPUT); SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlCHandler, TRUE); @@ -879,7 +880,7 @@ int main( Invalid_Frame_Count); } if (packet_count >= 65535) { - packet_statistics_save(); + packet_statistics_print(); packet_statistics_clear(); filename_create_new(); packet_count = 0; diff --git a/bacnet-stack/lib/Makefile b/bacnet-stack/lib/Makefile index c39d2b64..19afb665 100644 --- a/bacnet-stack/lib/Makefile +++ b/bacnet-stack/lib/Makefile @@ -130,6 +130,7 @@ PORT_ARCNET_SRC = \ PORT_MSTP_SRC = \ $(BACNET_PORT_DIR)/rs485.c \ $(BACNET_PORT_DIR)/dlmstp.c \ + $(BACNET_PORT_DIR)/timer.c \ $(BACNET_CORE)/ringbuf.c \ $(BACNET_CORE)/fifo.c \ $(BACNET_CORE)/mstp.c \ @@ -148,6 +149,7 @@ PORT_ALL_SRC = \ $(BACNET_PORT_DIR)/arcnet.c \ $(BACNET_PORT_DIR)/dlmstp.c \ $(BACNET_PORT_DIR)/rs485.c \ + $(BACNET_PORT_DIR)/timer.c \ $(BACNET_CORE)/ringbuf.c \ $(BACNET_CORE)/fifo.c \ $(BACNET_CORE)/mstp.c \ diff --git a/bacnet-stack/ports/win32/dlmstp.c b/bacnet-stack/ports/win32/dlmstp.c index fb4a3e91..7eee7655 100644 --- a/bacnet-stack/ports/win32/dlmstp.c +++ b/bacnet-stack/ports/win32/dlmstp.c @@ -22,6 +22,7 @@ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * *********************************************************************/ + #include #include #include @@ -39,6 +40,7 @@ #include "npdu.h" #include "bits.h" #include "ringbuf.h" +#include "timer.h" #define WIN32_LEAN_AND_MEAN #define STRICT 1 @@ -70,33 +72,16 @@ static uint16_t Treply_timeout = 260; static uint8_t Tusage_timeout = 50; /* Timer that indicates line silence - and functions */ -static uint16_t SilenceTime; -#define INCREMENT_AND_LIMIT_UINT16(x) {if (x < 0xFFFF) x++;} -static uint16_t Timer_Silence( +static uint32_t Timer_Silence( void) { - return SilenceTime; + return timer_milliseconds(TIMER_SILENCE); } static void Timer_Silence_Reset( void) { - SilenceTime = 0; -} - -static void dlmstp_millisecond_timer( - void) -{ - INCREMENT_AND_LIMIT_UINT16(SilenceTime); -} - -static void dlmstp_reinit( - void) -{ - /*RS485_Reinit(); */ - dlmstp_set_mac_address(DEFAULT_MAC_ADDRESS); - dlmstp_set_max_info_frames(DEFAULT_MAX_INFO_FRAMES); - dlmstp_set_max_master(DEFAULT_MAX_MASTER); + timer_reset(TIMER_SILENCE); } void dlmstp_cleanup( @@ -230,18 +215,6 @@ static void dlmstp_master_fsm_task( } } -static void dlmstp_millisecond_task( - void *pArg) -{ - (void) pArg; - (void) SetThreadPriority(GetCurrentThread(), - THREAD_PRIORITY_TIME_CRITICAL); - for (;;) { - dlmstp_millisecond_timer(); - Sleep(1); - } -} - void dlmstp_fill_bacnet_address( BACNET_ADDRESS * src, uint8_t mstp_address) @@ -622,7 +595,7 @@ bool dlmstp_init( exit(1); } /* initialize hardware */ - /* initialize hardware */ + timer_init(); if (ifname) { RS485_Set_Interface(ifname); #if PRINT_ENABLED @@ -672,11 +645,6 @@ bool dlmstp_init( fprintf(stderr, "MS/TP Max_Info_Frames: %u\n", (unsigned) MSTP_Port.Nmax_info_frames); #endif - /* start the threads */ - hThread = _beginthread(dlmstp_millisecond_task, 4096, &arg_value); - if (hThread == 0) { - fprintf(stderr, "Failed to start timer task\n"); - } hThread = _beginthread(dlmstp_receive_fsm_task, 4096, &arg_value); if (hThread == 0) { fprintf(stderr, "Failed to start recive FSM task\n"); diff --git a/bacnet-stack/ports/win32/rs485.c b/bacnet-stack/ports/win32/rs485.c index de48c1d0..cb5e674f 100644 --- a/bacnet-stack/ports/win32/rs485.c +++ b/bacnet-stack/ports/win32/rs485.c @@ -33,12 +33,12 @@ ------------------------------------------- ####COPYRIGHTEND####*/ -/* The module handles sending data out the RS-485 port */ -/* and handles receiving data from the RS-485 port. */ -/* Customize this file for your specific hardware */ +/** @file win32/rs485.c Provides Windows-specific functions for RS-485 */ + /* Suggested USB to RS485 devices: B&B Electronics USOPTL4 SerialGear USB-COMi-SI-M + USB-RS485-WE-1800-BT */ #include @@ -54,6 +54,7 @@ #define STRICT 1 #include #include "rs485.h" +#include "fifo.h" /* details from Serial Communications in Win32 at MSDN */ @@ -83,6 +84,11 @@ static DWORD RS485_DTRControl = DTR_CONTROL_DISABLE; RTS_CONTROL_ENABLE, RTS_CONTROL_DISABLE, RTS_CONTROL_HANDSHAKE, RTS_CONTROL_TOGGLE */ static DWORD RS485_RTSControl = RTS_CONTROL_DISABLE; +/* Ring buffer for incoming bytes, in order to speed up the receiving. */ +static FIFO_BUFFER Rx_FIFO; +/* buffer size needs to be a power of 2 */ +static uint8_t Rx_Buffer[1 << 12]; + /**************************************************************************** * DESCRIPTION: Change the characters in a string to uppercase @@ -99,8 +105,6 @@ static void strupper( } } -#pragma - /**************************************************************************** * DESCRIPTION: Initializes the RS485 hardware and variables, and starts in * receive mode. @@ -211,6 +215,26 @@ static void RS485_Configure_Status( } } +/**************************************************************************** +* DESCRIPTION: Cleans up any handles that were created at startup. +* RETURN: none +* ALGORITHM: none +* NOTES: none +*****************************************************************************/ +static void RS485_Cleanup( + void) +{ + if (!EscapeCommFunction(RS485_Handle, CLRDTR)) { + RS485_Print_Error(); + } + + if (!SetCommTimeouts(RS485_Handle, &RS485_Timeouts)) { + RS485_Print_Error(); + } + + CloseHandle(RS485_Handle); +} + /**************************************************************************** * DESCRIPTION: Initializes the RS485 hardware and variables, and starts in * receive mode. @@ -236,23 +260,11 @@ void RS485_Initialize( } RS485_Configure_Status(); + atexit(RS485_Cleanup); + return; } -void RS485_Cleanup( - void) -{ - if (!EscapeCommFunction(RS485_Handle, CLRDTR)) { - RS485_Print_Error(); - } - - if (!SetCommTimeouts(RS485_Handle, &RS485_Timeouts)) { - RS485_Print_Error(); - } - - CloseHandle(RS485_Handle); -} - /**************************************************************************** * DESCRIPTION: Returns the baud rate that we are currently running at * RETURN: none