Added unit test to rs485 windows port to allow sending a string of ascii hex out the rs485 port to inject messages onto the wire.
This commit is contained in:
@@ -667,6 +667,17 @@ static void MSTP_Receive_Frame_FSM(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static MSTP_MASTER_STATE Master_State_Log[128];
|
||||||
|
static unsigned master_state_log_index = 0;
|
||||||
|
void log_master_state(MSTP_MASTER_STATE state)
|
||||||
|
{
|
||||||
|
Master_State_Log[master_state_log_index] = state;
|
||||||
|
master_state_log_index++;
|
||||||
|
if (master_state_log_index > 128) {
|
||||||
|
master_state_log_index = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* returns true if we need to transition immediately */
|
/* returns true if we need to transition immediately */
|
||||||
static bool MSTP_Master_Node_FSM(
|
static bool MSTP_Master_Node_FSM(
|
||||||
void)
|
void)
|
||||||
@@ -704,6 +715,7 @@ static bool MSTP_Master_Node_FSM(
|
|||||||
next_poll_station = (Poll_Station + 1) % (Nmax_master + 1);
|
next_poll_station = (Poll_Station + 1) % (Nmax_master + 1);
|
||||||
next_this_station = (This_Station + 1) % (Nmax_master + 1);
|
next_this_station = (This_Station + 1) % (Nmax_master + 1);
|
||||||
next_next_station = (Next_Station + 1) % (Nmax_master + 1);
|
next_next_station = (Next_Station + 1) % (Nmax_master + 1);
|
||||||
|
log_master_state(Master_State);
|
||||||
switch (Master_State) {
|
switch (Master_State) {
|
||||||
case MSTP_MASTER_STATE_INITIALIZE:
|
case MSTP_MASTER_STATE_INITIALIZE:
|
||||||
/* DoneInitializing */
|
/* DoneInitializing */
|
||||||
|
|||||||
@@ -46,8 +46,8 @@ static uint32_t Baud_Rate = 9600;
|
|||||||
/* turnaround_time_milliseconds = (Tturnaround*1000UL)/Baud_Rate; */
|
/* turnaround_time_milliseconds = (Tturnaround*1000UL)/Baud_Rate; */
|
||||||
|
|
||||||
/* buffer for storing received bytes - size must be power of two */
|
/* buffer for storing received bytes - size must be power of two */
|
||||||
static uint8_t Receive_Buffer_Data[128];
|
uint8_t Receive_Buffer_Data[128];
|
||||||
static FIFO_BUFFER Receive_Buffer;
|
FIFO_BUFFER Receive_Buffer;
|
||||||
|
|
||||||
static void rs485_rts_init(
|
static void rs485_rts_init(
|
||||||
void)
|
void)
|
||||||
|
|||||||
@@ -356,6 +356,10 @@ void RS485_Check_UART_Data(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef TEST_RS485
|
#ifdef TEST_RS485
|
||||||
|
|
||||||
|
#include "mstpdef.h"
|
||||||
|
|
||||||
|
|
||||||
static void test_transmit_task(
|
static void test_transmit_task(
|
||||||
void *pArg)
|
void *pArg)
|
||||||
{
|
{
|
||||||
@@ -368,25 +372,88 @@ static void test_transmit_task(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
|
static BOOL WINAPI CtrlCHandler(
|
||||||
|
DWORD dwCtrlType)
|
||||||
|
{
|
||||||
|
dwCtrlType = dwCtrlType;
|
||||||
|
exit(0);
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static int ascii_hex_to_int(char ch)
|
||||||
|
{
|
||||||
|
int rv = -1;
|
||||||
|
|
||||||
|
if ((ch >= '0') && (ch <= '9')) {
|
||||||
|
rv = ch - '0';
|
||||||
|
} else if ((ch >= 'a') && (ch <= 'f')) {
|
||||||
|
rv = 10 + ch - 'a';
|
||||||
|
} else if ((ch >= 'A') && (ch <= 'F')) {
|
||||||
|
rv = 10 + ch - 'a';
|
||||||
|
}
|
||||||
|
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
int main(
|
int main(
|
||||||
void)
|
int argc,
|
||||||
|
char *argv[])
|
||||||
{
|
{
|
||||||
unsigned long hThread = 0;
|
unsigned long hThread = 0;
|
||||||
uint32_t arg_value = 0;
|
uint32_t arg_value = 0;
|
||||||
char lpBuf[1];
|
char lpBuf[1];
|
||||||
DWORD dwRead = 0;
|
DWORD dwRead = 0;
|
||||||
unsigned i = 0;
|
unsigned i = 0, len = 0, count = 0;
|
||||||
|
char hex_pair[5] = "0xff";
|
||||||
|
char ch = ' ';
|
||||||
|
int lsb = 0, msb = 0;
|
||||||
|
long my_baud = 38400;
|
||||||
|
uint8_t buffer[501] = {0};
|
||||||
|
|
||||||
RS485_Set_Interface("COM4");
|
if (argc > 1) {
|
||||||
RS485_Set_Baud_Rate(38400);
|
RS485_Set_Interface(argv[1]);
|
||||||
|
}
|
||||||
|
if (argc > 2) {
|
||||||
|
my_baud = strtol(argv[2], NULL, 0);
|
||||||
|
}
|
||||||
|
RS485_Set_Baud_Rate(my_baud);
|
||||||
RS485_Initialize();
|
RS485_Initialize();
|
||||||
#if 0
|
#if defined(_WIN32)
|
||||||
/* create a task for synchronous transmit */
|
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_PROCESSED_INPUT);
|
||||||
hThread = _beginthread(test_transmit_task, 4096, &arg_value);
|
SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlCHandler, TRUE);
|
||||||
if (hThread == 0) {
|
#endif
|
||||||
fprintf(stderr, "Failed to start transmit task\n");
|
#ifdef TEST_RS485_TRANSMIT
|
||||||
|
/* read a stream of characters from stdin or argument */
|
||||||
|
if (argc > 3) {
|
||||||
|
len = strlen(argv[3]);
|
||||||
|
for (i = 0; i < len; i++) {
|
||||||
|
/* grab pairs of hex characters, skip spaces */
|
||||||
|
ch = argv[3][i];
|
||||||
|
if (ch == ' ') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
msb = ascii_hex_to_int(ch);
|
||||||
|
if (msb >= 0) {
|
||||||
|
i++;
|
||||||
|
ch = argv[3][i];
|
||||||
|
lsb = ascii_hex_to_int(ch);
|
||||||
|
if (lsb >= 0) {
|
||||||
|
buffer[count] = msb << 4 | lsb;
|
||||||
|
} else {
|
||||||
|
buffer[count] = msb;
|
||||||
|
}
|
||||||
|
count++;
|
||||||
|
if (count >= sizeof(buffer)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
RS485_Send_Frame(NULL, buffer, count);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef TEST_RS485_RECEIVE
|
||||||
/* receive task */
|
/* receive task */
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (!ReadFile(RS485_Handle, lpBuf, sizeof(lpBuf), &dwRead, NULL)) {
|
if (!ReadFile(RS485_Handle, lpBuf, sizeof(lpBuf), &dwRead, NULL)) {
|
||||||
@@ -403,5 +470,7 @@ int main(
|
|||||||
dwRead = 0;
|
dwRead = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
#endif /* TEST_ABORT */
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ PRODUCT = rs485
|
|||||||
PRODUCT_EXE = $(PRODUCT).exe
|
PRODUCT_EXE = $(PRODUCT).exe
|
||||||
|
|
||||||
# Choose the Data Link Layer to Enable
|
# Choose the Data Link Layer to Enable
|
||||||
DEFINES = -DBACDL_MSTP=1;TEST_RS485
|
DEFINES = -DBACDL_MSTP=1;TEST_RS485;TEST_RS485_TRANSMIT
|
||||||
|
|
||||||
SRCS = rs485.c
|
SRCS = rs485.c
|
||||||
OBJS = $(SRCS:.c=.obj)
|
OBJS = $(SRCS:.c=.obj)
|
||||||
@@ -32,7 +32,7 @@ TLIB = $(BORLAND_DIR)\bin\tlib
|
|||||||
# Include directories
|
# Include directories
|
||||||
#
|
#
|
||||||
CC_DIR = $(BORLAND_DIR)\BIN
|
CC_DIR = $(BORLAND_DIR)\BIN
|
||||||
BACNET_INCL = ..\..\;..\..\demo\handler\;..\..\demo\object\;.
|
BACNET_INCL = ..\..\include;.
|
||||||
INCL_DIRS = -I$(BORLAND_DIR)\include;$(BACNET_INCL)
|
INCL_DIRS = -I$(BORLAND_DIR)\include;$(BACNET_INCL)
|
||||||
|
|
||||||
CFLAGS = $(INCL_DIRS) $(CS_FLAGS) $(DEFINES)
|
CFLAGS = $(INCL_DIRS) $(CS_FLAGS) $(DEFINES)
|
||||||
|
|||||||
Reference in New Issue
Block a user