⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 preform.c

📁 LP830 无线识别卡 读写程序demo
💻 C
📖 第 1 页 / 共 5 页
字号:
// ****************************************************************************
//
// File Name:    preform.c
//
// Description: Preform a selected function.  Request information necessary 
// for the given command and then execute it.
//
//
//  ORIGINATOR: Escort Memory Systems
//
//  HISTORY
//    who     when     what
// -------- --------- ----------------------------------------------------
//
// ****************************************************************************

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
#include <time.h>      // For down count display.
#include <ctype.h>     // Lower case conversion.
#include <mem.h>
#include "typedef.h"   // General typedef and defines.
#include "handheld.h"
#include "preform.h"
#include "contread.h"
#include "hms800.h"
#include "comm.h"


#define HMS_HEX_LINES   20 // Bytes displayed on a line in Hexadecimal format.
#define HMS_DEC_LINES   15 // Bytes displayed on a line in decimal format.
#define HMS_ASCII_LNS   70 // Bytes displayed on a line in ASCII format.
#define HMS_BIN_LINES    8 // Bytes displayed on a line in Hexadecimal format.
#define DISPLAY_LINES   24 // Number of data lines to display on one screen full.
#define DISPLAY_WIDTH   80 // Maximum Number of characters on one line if display.

#define HS200_HEX_LNS   25 // Bytes displayed on a line in Hexadecimal format.
#define HS200_DEC_LNS   15 // Bytes displayed on a line in decimal format.
#define HS200_ASCII     70 // Bytes displayed on a line in ASCII format.
#define HS200_BIN_LNS    8 // Bytes displayed on a line in Hexadecimal format.

#define HS208_HEX_LNS   20 // Bytes displayed on a line in Hexadecimal format.
#define HS208_DEC_LNS   15 // Bytes displayed on a line in decimal format.
#define HS208_ASCII     70 // Bytes displayed on a line in ASCII format.
#define HS208_BIN_LNS    8 // Bytes displayed on a line in Hexadecimal format.

#define HS232_HEX_LNS   20 // Bytes displayed on a line in Hexadecimal format.
#define HS232_DEC_LNS   15 // Bytes displayed on a line in decimal format.
#define HS232_ASCII     70 // Bytes displayed on a line in ASCII format.
#define HS232_BIN_LNS    8 // Bytes displayed on a line in Hexadecimal format.

#define DOWN_DISPLAY   FALSE 
#define TAG_START        15 // Initial down count value when waiting for a tag.
#define TAG_FILLING       3 // After tag found, every tag fill command for hole tag.
#define TAG_CONTINUAL     1 // down count timeout value when in continual read.
#define MAX_HEX_DIG       2 // Maximum number of hexadecimal digits in a BYTE.
#define MAX_BIN_DIG       8 // Maximum number of binary digits in a BYTE.
#define MAX_DEC_DIGITS    5 // More then maximum number of decimal digits input.

#define TOO_MANY_ERRORS   10   // Too many read errors on this tag.

#define NUM_REGIONS        1   // One region block.
#define LAST_PROT_OFF   0x0A   // Offset value of last protected byte to hide in file.

#define HS_ID_START        1   // Tag address of first BYTE of HS tag ID.
#define HS_ID_END          2   // Seconds and last Tag ID byte address.

#define OUT_LIMIT        128  // Outut buffer array size.
#define MAX_FILENAME       8  // Number of characters in the base part of a file name.

#if TEST_MUX32
#ifdef LRP820
#define MAX_MUX_DATA      48  // Maximum data size in an LRP tag..
#else
#define MAX_MUX_DATA      55  // Maximum data size in a MUX32 data replay.
#endif  
#endif

#define CON_READ_LEN       0  // Array index of parsed COM read response length
#define CON_READ_STAT      1  // Array index of parsed COM read stats byte.
#define CON_READ_DATA      2  // Array index of parsed COM read data bytes.

#define TIME_FOR_SLOWEST_PACKET 3 // Seconds 'should' be long enough for 1200 baud.

enum data_type display_type;   // Display format chosen.
enum ant_type antenna_type = NOT_VALID; // "flag" value for antenna type selected.
WORD mem_size = LRP_MEM_SIZE;         // Size of selected antenna type tag.
BYTE max_data_pkt = HMS827_MAX_DATA;  // Maximum data bytes size for selected antanna.

static __far BYTE tag_io_buf[HMS_MEM_SIZE]; // Working IO buffer for all tag BYTEs.

static time_t last_time;            // Value returned by time() last time.
static int display_val;             // Down count  display value.
static BYTE finished;               // Local BOOLEAN Not finished with mifare command.

// Copy, hold and write an entire tag working variables.
static BYTE tag_buf[HMS_MEM_SIZE];  // Hold all BYTEs from a tag.
WORD start_adr = TOO_BIG_VAL;// Begin coping from this address.
WORD stop_adr = TOO_BIG_VAL; // Last address byte to be coping.

// Below working variables for non-contiguous I/O routines.
static BYTE *address_ptr;   // Index the output buffer to build command packet.
static WORD address_value;  // Hold the input address.
static WORD nc_read_size;   // Number of bytes in for Non-contiguous Read.

// ****************************************************************************
// Preform a getche(), but also keep looking at the COM port for a Continuous
// Block read data response.
//
// Input:        None.
// Return:       Character input from the keyboard..
// Side effects: None.
BYTE getche_cont(void)
{
   while(NOT kbhit())
      cont_read_mode();   // Handle any Contiguous Block Read data response. 
   return(getche());   
}

// ****************************************************************************
// Preform a getch(), but also keep looking at the COM port for a Continuous
// Block read data response.
//
// Input:        None.
// Return:       Character input from the keyboard..
// Side effects: None.
BYTE getch_cont(void)
{
   while(NOT kbhit())
      cont_read_mode();   // Handle any Contiguous Block Read data response. 
   return(getch());   
}

// ****************************************************************************
// Input a data byte in the current format.. 
// Input:        NONE.
// Return:       BYTE value returned or escapint status value of
//                    TOO_BIG_VAL or ENTER_ALONE
// Side effects: None.
static WORD inp_data_byte(void)
{
   WORD value;     // Value (or ESC flag) entered by user to be filled.
   BYTE status;    // Tag IO return status.

   if (display_type EQ ASCII)
   {
      value = ENTER_ALONE; // Nothing entered yet.
      printf("Enter an ASCII character: ");
      do
      {
         if ((status = getche_cont()) EQ ESC) // This echos the character.
            return(TOO_BIG_VAL);              // But ESC was entered. Exit.
         if (status EQ BACK_SPACE)
         {
            putch(' ');                  // Step back up the data entry space.
            putch(' ');                  // Erase from screen last character.
            value = ENTER_ALONE;         // Back space erased the character.
         } else if (status NE CR)
            value = status;              // Got a character input.
         putch(BACK_SPACE);              // Stay in first column, for 1 char.
      } while (status NE CR);
   } else if (display_type EQ HEX)
   {
      printf("Enter the byte in Hexadecimal: ");
      value = inp_hex(); // Enter a number in hexadecimal format.
   } else if (display_type EQ BIN)
   {
      printf("Enter the byte in Binary: ");
      value = inp_bin(); // Enter a number in hexadecimal format.
   } else
   {
      printf("Enter the byte in decimal [0..255]: ");
      value = inp_num(BYTE_VALUE);
   }
   return(value);
}

// ****************************************************************************
// Preform a Block Read command. 
// Input:        Begin reading at this start address in the tag.
//               Number of bytes to be read from tag.
//               Where to place the received data.
// Return:       Read STATUS.
// Side effects: None.
static BYTE Read_command(WORD start_add, WORD data_size, BYTE *rx_data)
{
   BYTE  status;     // Mifare tag interface status value.

   BLReadTx(start_add, data_size, MIFARE_TIME_OUT); 
   status = BLReadRx(rx_data, data_size);
   return(status);
}

// ****************************************************************************
// Preform a Block Write command. 
// Input:        Begin write at this start address in the tag.
//               Number of bytes to be read from tag.
//               Pointer to the bytes to be written.
//               BOOLEAN - TRUE, using proteced mode block writes.
//                       - FALSE, normal block write.
// Return:       Read STATUS.
// Side effects: None.
static BYTE Write_command(WORD start_add, WORD data_size, BYTE *tx_data, BYTE prt_mode)
{
   BYTE  status;     // Mifare tag interface status value.

   if (prt_mode)
   {
      pro_BLWriteTx(start_add, data_size, MIFARE_TIME_OUT, tx_data);
      status = pro_BLWriteRx();
   } else
   {
      BLWriteTx(start_add, data_size, MIF_WRITE_TI, tx_data);
      status = BLWriteRx();
   }
   return(status);
}

// ****************************************************************************
// What is the value of the decimal number in the digit string pointed to?
// That value is placed in the WORD pointed to by value parameter.
// The decimal number can be terminated with spaces (' ') or a carriage return.
// If the digits overflow a WORD, then what *value points to is set to zero (0)
// and this function returns FALSE.
//
// Input:        Pointer to where digit string is located.
//               Pointer to a WORD where the value from the string is place,
//               unless there is an overflow and FALSE is returned.
// Return:       BOOLEAN - TRUE,  Valid decimal number input, with no overflow.
//                         FALSE, digits in the string will overflow a WORD. 
// Side effects: The WORD value points to will be altered.
static BYTE dec_word_val(BYTE* digit_string, WORD *value) 
{
   unsigned long int incom_value;

   *value = incom_value = 0;
   while ((*digit_string >= '0') AND (*digit_string <= '9'))
   {
      incom_value = (incom_value * 10) + *digit_string - '0'; // Add in new value.
      if (incom_value > TOO_BIG_VAL)  // This string overflowed.
         return(FALSE);
      digit_string++;        // Count another valid digit entered.
   }
   *value = (WORD)incom_value;
   return(TRUE);
}

// ****************************************************************************
// Is the string pointed to by the passed pointer all eight digits for a 
// binary BYTE?  Assuming the string was pre-fille with '.' or ' ', those
// eight (8) characters should be replaced with 1 or 0 before this function
// is called.  This is to check if all eight (8) binary digits have been entered.
//
// Input:        Pointer to a string of binary digits.
// Return:       BOOLEAN - TRUE,  all eight (8) binary digits are in the string.
//                         FALSE, not all eight (8) binary digits.
// Side effects: None.
static WORD eight_binary_digits(BYTE *binary_string)
{
   WORD ii;       // Local loop counter.
   
   for (ii = MAX_BIN_DIG; ii; ii--)
   {
      if ((*binary_string < '0') OR (*binary_string > '1'))
         return(FALSE);     // Non-binary digit found.  Thus not all binary.
      binary_string++;      // Look at the next digt.
   }
   return(TRUE);            // Must be all binary.
}

// ****************************************************************************
// For use with the Del key when entering number on the screen.  The first
// digit in the digit string pointed to by the passed parameter is deleted.
// The remaining digits are shifted to the left none character space.
// These remaining digit are then printed on the screen and then the cursor is
// backed up over them, to leave the cursor in the same position.
//
// Input:        Pointer to the string of digits.
// Return:       None
// Side effects: String pointed to by the digit string pointer looses a digit.
static void del_digit(BYTE *dig_str)
{
   WORD num_digits;     // Number of digits after the deleted one to be shifted.
   WORD prt_count;      // Loop counter for printing the shifted digits.
   BYTE *dig_ptr;       // Used to index the digit string.
   
   num_digits = 0;      // May not shift any digits.
   dig_ptr = dig_str;
   while ((*dig_ptr >= '0') AND (*dig_ptr <= '9')) // While in the digits
   {                                               // Shift the next one down.
      *dig_ptr = *(dig_ptr + 1);
      dig_ptr++;
      num_digits = num_digits + 1;  // Another digit shifted.
   }
   dig_ptr = dig_str;
   for (prt_count = num_digits; prt_count; prt_count--)
      if (*dig_ptr NE CR)           // Last digit handled?
         putch(*dig_ptr++);         // No, echo the digit.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -