Cleaned up compile on Win32 for MS/TP datalink. Still not reliable yet.
This commit is contained in:
@@ -244,7 +244,7 @@ static void packet_statistics(
|
|||||||
old_tv.tv_usec = tv->tv_usec;
|
old_tv.tv_usec = tv->tv_usec;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void packet_statistics_save(
|
static void packet_statistics_print(
|
||||||
void)
|
void)
|
||||||
{
|
{
|
||||||
unsigned i; /* loop counter */
|
unsigned i; /* loop counter */
|
||||||
@@ -500,7 +500,7 @@ static void write_global_header(
|
|||||||
fflush(pFile);
|
fflush(pFile);
|
||||||
fprintf(stdout, "mstpcap: saving capture to %s\n", filename);
|
fprintf(stdout, "mstpcap: saving capture to %s\n", filename);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "mstpcap: failed to open %s: %s\n", filename,
|
fprintf(stderr, "mstpcap[header]: failed to open %s: %s\n", filename,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
if (pipe_enable) {
|
if (pipe_enable) {
|
||||||
@@ -552,7 +552,7 @@ static void write_received_packet(
|
|||||||
(void) data_write((char *) &mstp_port->DataCRCActualLSB, 1, 1);
|
(void) data_write((char *) &mstp_port->DataCRCActualLSB, 1, 1);
|
||||||
}
|
}
|
||||||
} else {
|
} 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));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -623,7 +623,7 @@ static bool test_global_header(
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "mstpcap: failed to open %s: %s\n", filename,
|
fprintf(stderr, "mstpcap[scan]: failed to open %s: %s\n", filename,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -715,7 +715,7 @@ static bool read_received_packet(
|
|||||||
static void cleanup(
|
static void cleanup(
|
||||||
void)
|
void)
|
||||||
{
|
{
|
||||||
packet_statistics_save();
|
packet_statistics_print();
|
||||||
if (pFile) {
|
if (pFile) {
|
||||||
fflush(pFile); /* stream pointer */
|
fflush(pFile); /* stream pointer */
|
||||||
fclose(pFile); /* stream pointer */
|
fclose(pFile); /* stream pointer */
|
||||||
@@ -764,6 +764,7 @@ void filename_create_new(
|
|||||||
if (pFile) {
|
if (pFile) {
|
||||||
fclose(pFile);
|
fclose(pFile);
|
||||||
}
|
}
|
||||||
|
pFile = NULL;
|
||||||
filename_create(&Capture_Filename[0]);
|
filename_create(&Capture_Filename[0]);
|
||||||
write_global_header(&Capture_Filename[0]);
|
write_global_header(&Capture_Filename[0]);
|
||||||
}
|
}
|
||||||
@@ -825,7 +826,7 @@ int main(
|
|||||||
fprintf(stdout, "\r%hu packets", packet_count);
|
fprintf(stdout, "\r%hu packets", packet_count);
|
||||||
}
|
}
|
||||||
if (packet_count) {
|
if (packet_count) {
|
||||||
packet_statistics_save();
|
packet_statistics_print();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "File header does not match.\n");
|
fprintf(stderr, "File header does not match.\n");
|
||||||
@@ -839,12 +840,12 @@ int main(
|
|||||||
if (argc > 2) {
|
if (argc > 2) {
|
||||||
my_baud = strtol(argv[2], NULL, 0);
|
my_baud = strtol(argv[2], NULL, 0);
|
||||||
}
|
}
|
||||||
RS485_Set_Baud_Rate(my_baud);
|
atexit(cleanup);
|
||||||
RS485_Initialize();
|
RS485_Initialize();
|
||||||
timer_init();
|
timer_init();
|
||||||
|
RS485_Set_Baud_Rate(my_baud);
|
||||||
fprintf(stdout, "mstpcap: Using %s for capture at %ld bps.\n",
|
fprintf(stdout, "mstpcap: Using %s for capture at %ld bps.\n",
|
||||||
RS485_Interface(), (long) RS485_Get_Baud_Rate());
|
RS485_Interface(), (long) RS485_Get_Baud_Rate());
|
||||||
atexit(cleanup);
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_PROCESSED_INPUT);
|
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_PROCESSED_INPUT);
|
||||||
SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlCHandler, TRUE);
|
SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlCHandler, TRUE);
|
||||||
@@ -879,7 +880,7 @@ int main(
|
|||||||
Invalid_Frame_Count);
|
Invalid_Frame_Count);
|
||||||
}
|
}
|
||||||
if (packet_count >= 65535) {
|
if (packet_count >= 65535) {
|
||||||
packet_statistics_save();
|
packet_statistics_print();
|
||||||
packet_statistics_clear();
|
packet_statistics_clear();
|
||||||
filename_create_new();
|
filename_create_new();
|
||||||
packet_count = 0;
|
packet_count = 0;
|
||||||
|
|||||||
@@ -130,6 +130,7 @@ PORT_ARCNET_SRC = \
|
|||||||
PORT_MSTP_SRC = \
|
PORT_MSTP_SRC = \
|
||||||
$(BACNET_PORT_DIR)/rs485.c \
|
$(BACNET_PORT_DIR)/rs485.c \
|
||||||
$(BACNET_PORT_DIR)/dlmstp.c \
|
$(BACNET_PORT_DIR)/dlmstp.c \
|
||||||
|
$(BACNET_PORT_DIR)/timer.c \
|
||||||
$(BACNET_CORE)/ringbuf.c \
|
$(BACNET_CORE)/ringbuf.c \
|
||||||
$(BACNET_CORE)/fifo.c \
|
$(BACNET_CORE)/fifo.c \
|
||||||
$(BACNET_CORE)/mstp.c \
|
$(BACNET_CORE)/mstp.c \
|
||||||
@@ -148,6 +149,7 @@ PORT_ALL_SRC = \
|
|||||||
$(BACNET_PORT_DIR)/arcnet.c \
|
$(BACNET_PORT_DIR)/arcnet.c \
|
||||||
$(BACNET_PORT_DIR)/dlmstp.c \
|
$(BACNET_PORT_DIR)/dlmstp.c \
|
||||||
$(BACNET_PORT_DIR)/rs485.c \
|
$(BACNET_PORT_DIR)/rs485.c \
|
||||||
|
$(BACNET_PORT_DIR)/timer.c \
|
||||||
$(BACNET_CORE)/ringbuf.c \
|
$(BACNET_CORE)/ringbuf.c \
|
||||||
$(BACNET_CORE)/fifo.c \
|
$(BACNET_CORE)/fifo.c \
|
||||||
$(BACNET_CORE)/mstp.c \
|
$(BACNET_CORE)/mstp.c \
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*
|
*
|
||||||
*********************************************************************/
|
*********************************************************************/
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
@@ -39,6 +40,7 @@
|
|||||||
#include "npdu.h"
|
#include "npdu.h"
|
||||||
#include "bits.h"
|
#include "bits.h"
|
||||||
#include "ringbuf.h"
|
#include "ringbuf.h"
|
||||||
|
#include "timer.h"
|
||||||
|
|
||||||
#define WIN32_LEAN_AND_MEAN
|
#define WIN32_LEAN_AND_MEAN
|
||||||
#define STRICT 1
|
#define STRICT 1
|
||||||
@@ -70,33 +72,16 @@ static uint16_t Treply_timeout = 260;
|
|||||||
static uint8_t Tusage_timeout = 50;
|
static uint8_t Tusage_timeout = 50;
|
||||||
|
|
||||||
/* Timer that indicates line silence - and functions */
|
/* Timer that indicates line silence - and functions */
|
||||||
static uint16_t SilenceTime;
|
static uint32_t Timer_Silence(
|
||||||
#define INCREMENT_AND_LIMIT_UINT16(x) {if (x < 0xFFFF) x++;}
|
|
||||||
static uint16_t Timer_Silence(
|
|
||||||
void)
|
void)
|
||||||
{
|
{
|
||||||
return SilenceTime;
|
return timer_milliseconds(TIMER_SILENCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Timer_Silence_Reset(
|
static void Timer_Silence_Reset(
|
||||||
void)
|
void)
|
||||||
{
|
{
|
||||||
SilenceTime = 0;
|
timer_reset(TIMER_SILENCE);
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void dlmstp_cleanup(
|
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(
|
void dlmstp_fill_bacnet_address(
|
||||||
BACNET_ADDRESS * src,
|
BACNET_ADDRESS * src,
|
||||||
uint8_t mstp_address)
|
uint8_t mstp_address)
|
||||||
@@ -622,7 +595,7 @@ bool dlmstp_init(
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
/* initialize hardware */
|
/* initialize hardware */
|
||||||
/* initialize hardware */
|
timer_init();
|
||||||
if (ifname) {
|
if (ifname) {
|
||||||
RS485_Set_Interface(ifname);
|
RS485_Set_Interface(ifname);
|
||||||
#if PRINT_ENABLED
|
#if PRINT_ENABLED
|
||||||
@@ -672,11 +645,6 @@ bool dlmstp_init(
|
|||||||
fprintf(stderr, "MS/TP Max_Info_Frames: %u\n",
|
fprintf(stderr, "MS/TP Max_Info_Frames: %u\n",
|
||||||
(unsigned) MSTP_Port.Nmax_info_frames);
|
(unsigned) MSTP_Port.Nmax_info_frames);
|
||||||
#endif
|
#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);
|
hThread = _beginthread(dlmstp_receive_fsm_task, 4096, &arg_value);
|
||||||
if (hThread == 0) {
|
if (hThread == 0) {
|
||||||
fprintf(stderr, "Failed to start recive FSM task\n");
|
fprintf(stderr, "Failed to start recive FSM task\n");
|
||||||
|
|||||||
@@ -33,12 +33,12 @@
|
|||||||
-------------------------------------------
|
-------------------------------------------
|
||||||
####COPYRIGHTEND####*/
|
####COPYRIGHTEND####*/
|
||||||
|
|
||||||
/* The module handles sending data out the RS-485 port */
|
/** @file win32/rs485.c Provides Windows-specific functions for RS-485 */
|
||||||
/* and handles receiving data from the RS-485 port. */
|
|
||||||
/* Customize this file for your specific hardware */
|
|
||||||
/* Suggested USB to RS485 devices:
|
/* Suggested USB to RS485 devices:
|
||||||
B&B Electronics USOPTL4
|
B&B Electronics USOPTL4
|
||||||
SerialGear USB-COMi-SI-M
|
SerialGear USB-COMi-SI-M
|
||||||
|
USB-RS485-WE-1800-BT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
@@ -54,6 +54,7 @@
|
|||||||
#define STRICT 1
|
#define STRICT 1
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include "rs485.h"
|
#include "rs485.h"
|
||||||
|
#include "fifo.h"
|
||||||
|
|
||||||
/* details from Serial Communications in Win32 at MSDN */
|
/* 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_ENABLE, RTS_CONTROL_DISABLE,
|
||||||
RTS_CONTROL_HANDSHAKE, RTS_CONTROL_TOGGLE */
|
RTS_CONTROL_HANDSHAKE, RTS_CONTROL_TOGGLE */
|
||||||
static DWORD RS485_RTSControl = RTS_CONTROL_DISABLE;
|
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
|
* 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
|
* DESCRIPTION: Initializes the RS485 hardware and variables, and starts in
|
||||||
* receive mode.
|
* 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
|
* DESCRIPTION: Initializes the RS485 hardware and variables, and starts in
|
||||||
* receive mode.
|
* receive mode.
|
||||||
@@ -236,23 +260,11 @@ void RS485_Initialize(
|
|||||||
}
|
}
|
||||||
RS485_Configure_Status();
|
RS485_Configure_Status();
|
||||||
|
|
||||||
|
atexit(RS485_Cleanup);
|
||||||
|
|
||||||
return;
|
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
|
* DESCRIPTION: Returns the baud rate that we are currently running at
|
||||||
* RETURN: none
|
* RETURN: none
|
||||||
|
|||||||
Reference in New Issue
Block a user