📄 contread.c
字号:
// ****************************************************************************
//
// File Name: contread.c
//
// Description: Preform the Continuous Block Read mode actions.
// This is a state machine to handle receiving the response from
// reader/writer with the tag data. It is a state machine so
// the reception of the reader/writer response is done one byte at
// a time. This allows a command to be checked for while receiving
// the packet.
//
// The routines in this module assume that ContRead_file has
// been initilized. It must be NULL or there is an open file.
//
// ORIGINATOR: Escort Memory Systems
//
// HISTORY
// who when what
// -------- --------- ----------------------------------------------------
//
// ****************************************************************************
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h> // Lower case conversion and toupper.
#include <dos.h>
#include "typedef.h" // General typedef and defines.
#include "handheld.h"
#include "comm.h"
#include "hms800.h"
#include "preform.h"
#include "contread.h"
#define DEBUG_DUMP FALSE // Should unexpected bytes be printed?
#define MAX_INTERBYTE 2 // Maximum Data response inter byte time (seconds).
#define COL_WIDTH 70 // Maximum display width, to start number of any format.
#define ONE_BYTE 1 // One, to request one byte form com_rec_buf()
#define SOMETHING 2 // Some number of seconds. This number is not relevant
// when comm_avail() is called before com_rec_buf(), but must have something.
#if TEST_MUX32
// The following values precede the message to indicate what format is being used.
// After the below value us the MUX address, that is ignored.
#define STD_MUX_HEAD 0xAA // Precede MUX32 address when using ABx Standard.
#define FAST_CHK_HEAD 0x9A // Precede MUX32 address when using ABx Fast,
#define FAST_NO_CHK 0xBA // when using ABx Fast with a checksum.
#define ASCII_CHK_HEAD 0x5A // Precede MUX32 address when using ABx ASCII,
#define ASCII_NO_CHK 0x7A // when using ABx ASCII without a checksum.
#define THESE_MSG_HEAD STD_MUX_HEAD
#endif
typedef void (*Fnct_ptr)(void); // FuNCtion PoinTeR, for state functions.
static Fnct_ptr Cont_read_rsp; // State of the Continuous Block Read data response.
BYTE ContRead_fname[13]; // Contigous Block Read response data.
FILE *ContRead_file = NULL; // Point to Contigous Block Read response file.
static long start_time;
static BYTE cur_col; // Current column on the display, for data response.
#if TEST_MUX32
static BYTE Mux_address; // Hold what should be the MUX32 address byte.
static void precede(void);
#endif
static void header_byte(void); // Forword state definition.
static void zero_cont_data(void); // Forword state definition.
//*****************************************************************************
// Utility routine to print a carriage return at the end of display lines.
//
// Input: The byte value to be printed.
// Return: NONE.
// Side effects: NONE.
static void print_newline(void)
{
if (cur_col >= COL_WIDTH)
{
if (ContRead_file NE NULL)
fprintf(ContRead_file, "\n");
else
printf("\n");
cur_col = 0;
}
}
//*****************************************************************************
// Utility routine to print, to the output device, unexpected bytes, in
// hexadecimal. This is to print bytes that are received, but are not in a
// data response packet form the reader/writer.
//
// Input: The byte value to be printed.
// Return: NONE.
// Side effects: NONE.
static void print_the_byte(BYTE this_byte)
{
print_newline(); // New line now?
#if DEBUG_DUMP
if (ContRead_file NE NULL)
fprintf(ContRead_file, " [%02X]", this_byte);
else
printf(" [%02X]", this_byte);
cur_col = cur_col + 5;
#else
this_byte = this_byte; // Have something so the complier does not produce
#endif // warnings.
}
//*****************************************************************************
// Utility routine to save the base time for timing.
//
// Input: NONE.
// Return: NONE.
// Side effects: NONE.
static void init_time(void)
{
start_time = time((long *)NULL); // Start timing fresh for the next byte.
}
//*****************************************************************************
// Has the maximum inter byte time been exceeded?
// This assumes start_time has been set when the last byte was received.
//
// Input: NONE, start_time Implied global.
// Return: BOOLEAN - TRUE maximum inter byte time has expired.
// FALSE the maximum inter byte time has not expired.
// Side effects: If inter byte time has expired, then a state change happens.
static BYTE interbyte_toolong(void)
{
WORD elapsed; // For time casting.
elapsed = time((long *)NULL) - start_time;
if (elapsed > MAX_INTERBYTE)
{
#if DEBUG_DUMP
if (ContRead_file NE NULL)
fprintf(ContRead_file, "[Inter byte time exceeded!]\n");
else
printf("[Inter byte time exceeded!]\n");
#endif
cur_col = 0; // Start next packet on a new display line.
#if TEST_MUX32
Cont_read_rsp = precede; // Start fresh looking for data response.
#else
Cont_read_rsp = header_byte; // Start fresh looking for data response.
#endif
return(TRUE);
}
return(FALSE);
}
//*****************************************************************************
// Handle the continuous Block Read data response..
//
// Input: NONE.
// Return: NONE.
// Side effects: NONE.
static void cont_data_read(void)
{
BYTE data_byte; // Hold what should be the header byte.
BYTE ii; // Locla bit loop counter.
if (NOT interbyte_toolong())
if (comm_avail())
{
print_newline(); // Got somethign to display. New line now?
if (NOT com_rec_buf(&data_byte, ONE_BYTE, SOMETHING))
return; // This cannot happen, but check anyway.
init_time(); // Got another byte of data, so start timing again.
switch (display_type)
{
case HEX:
if (ContRead_file NE NULL)
fprintf(ContRead_file, " %02X", data_byte);
else
printf(" %02X", data_byte);
cur_col = cur_col + 3;
break;
case DEC:
if (ContRead_file NE NULL)
fprintf(ContRead_file, " %3d", data_byte);
else
printf(" %3d", data_byte);
cur_col = cur_col + 4;
break;
case ASCII:
if (ContRead_file NE NULL)
fprintf(ContRead_file, "%c", data_byte);
else
printf("%c", data_byte);
cur_col = cur_col + 1;
break;
case BIN:
if (ContRead_file NE NULL)
fprintf(ContRead_file, " ");
else
printf(" "); // Space between numbers.
for (ii = 0x80; ii; ii = ii >> 1)
if (ii & data_byte)
{
if (ContRead_file NE NULL)
fputc('1', ContRead_file);
else
putch('1');
} else
{
if (ContRead_file NE NULL)
fputc('0', ContRead_file);
else
putch('0');
}
cur_col = cur_col + 9;
break;
}
Cont_read_rsp = zero_cont_data; // Now get the Command echo.
}
}
//*****************************************************************************
// Final termination byte in ABx standard
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -