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

📄 hms800.c

📁 LP830 无线识别卡 读写程序demo
💻 C
📖 第 1 页 / 共 3 页
字号:
// ****************************************************************************
//
// File Name:    HMS800.c
//
// Description: Preform the HMS commands from here.  Calls are made to
//              routines in comm.c
//
//  ORIGINATOR: Unknown.
//
//  HISTORY
//    who     when     what
// -------- --------- ----------------------------------------------------
//  Geof    08-??-97  Clean up, add marker line at top of every routine.
//  Geof    10-12-97  Extend timeout.  Wait for the device, for HMS814.
//                    Added debug statments for Non-contiguous IO.
//  Geof    01-12-98  Took out calls to malloc and used a static array.  This
//                    was done for timing analysis, less time overhead.
//  Geof    03-24-98  Added now_mux_addr when using MUX32 format. This is so
//                    the MUX address can be changed.
//  Geof    04-06-98  Moved om_buf_clr(), com_rec_buf(), and SendStream() 
//                    from hms800.c to this comm.c.
//  Geof    06-05-98  Moved now_mux_addr declaration to comm.c.
//  Geof    11-13-98  Clean up ABx response (error or OK) must be received.
//  Geof    02-09-99  Added the Get tag Identification Number command.
//  Geof    03-30-99  Added Initiate/cancel Continuous Block Read mode command.
//
// ****************************************************************************

#include <stdlib.h>
#include <string.h>
#include <dos.h>
#include "typedef.h"
#include "handheld.h"
#include "hms800.h"
#include "comm.h"

#define DEBUGGING  FALSE
#if DEBUGGING
#include <stdio.h>
#endif

#if ANTARES
#include "imt24lib.h"  // Intermet Applicatoin Development tools.w
#endif

#define MORE_THAN_ENOUGH  ((HMS_MEM_SIZE * 2) + 4)  // Enough space to build bigest packet.
#define REC_FIRST_BYTE    (MIFARE_TIME_OUT >= 1000 ? ((MIFARE_TIME_OUT / 1000) + 2) : 2)

#define REC_MORE_BYTES      5  // Receiving about 4 bytes.
#define REC_BYTES_TIME      2  // Seconds waiting for bytes in a packet.

#define CONT_READ_CMDSIZE   10 // Initiate/cancel continuous read mode command length.
#define NC_READ_CMDSIZE      6 // Non-contiguous read command size.
#define BAUD_CHANGE_CMDSIZE  6 // Change baud rate command size.

#define COM_READX_SIZE       8 // Byte count in a COM2 ReadX command
#define COM_READLN_SIZE     10 // byte count in a COM2 Read line command.

// Below working variables for non-contiguous I/O routines.
BYTE build_buf[MORE_THAN_ENOUGH];

// ****************************************************************************
// Clear a buffer 
// Input:        Pointer to a buffer to be cleared.
//               Number of bytes in the buffer, to be clared.
// Return:       None.
// Side effects: None.
static void ClearBuf (BYTE *rx_buf, WORD data_size) 
{
   memset(rx_buf, 0x00, data_size);   // Assume all values not valid.
}

// ****************************************************************************
// Transmit Block Write Command 
// Input:        Begin write at this start address in the tag.
//               Number of bytes to be written from tag.
//               Antenna timeout time.
//               Pointer to the bytes to be written.
//               The block write mode, that is normal or protected.
// Return:       None.
// Side effects: None.
static void do_BLWrite(WORD start_add, WORD data_size, WORD timeout, BYTE *data_buf, BYTE mode)
{
    BYTE *tx_buf;
    WORD ii;

    //allocate buffer and initialize them
    tx_buf = build_buf;
    ClearBuf(tx_buf, data_size*2 + 10);

    *tx_buf++ = MSG_BEGIN;
    *tx_buf++ = mode;
    *tx_buf++ = (BYTE)(start_add / 256);
    *tx_buf++ = (BYTE)(start_add % 256);
    *tx_buf++ = (BYTE)(data_size / 256);
    *tx_buf++ = (BYTE)(data_size % 256);
    *tx_buf++ = (BYTE)(timeout / 256);
    *tx_buf++ = (BYTE)(timeout % 256);
    for(ii = 0; ii < data_size; ii++) 
    {
        tx_buf++;
        *tx_buf++ = *data_buf++;
    }
    *tx_buf++ = MSG_TERM;
    *tx_buf = MSG_TERM;
   
#if ANTARES
    com_buf_clr();              // Clear out Anyares COM receive buffer.
#endif    
    SendStream(build_buf, data_size*2 + 10);
}

// ****************************************************************************
// Receive response from block read.
// Input:        where to place the data.
//               Receive no more data than this. Note: BYTE received in a WORD.
//               The read mode, Block read or Continual, BLK_READ or CON_READ_MODE.
// Return:       Command status.
// Side effects: None.
static BYTE do_BLReadRx(BYTE *data_buf, WORD data_size, BYTE read_mode)
{
   BYTE *rx_buf;
   BYTE status;
   WORD ii;

    //allocate buffer and initialize them
   rx_buf = build_buf;
   ClearBuf(rx_buf, data_size*2 + 4);

#if TEST_MUX32
   if(NOT com_rec_buf(rx_buf, 2, REC_FIRST_BYTE)) // Throw out the first two bytes. 
      status = COMM_FAIL;   // They are MUX32 address and stuff.
   else
#endif  
   // first check the first 2 bytes to find out what is expected
   // if even the first 2 bytes are not gotten, serial communication fails
   // if get 0xff in 2nd bytes, receive Error message
   // else may get right message
   if (NOT com_rec_buf(rx_buf, 2, REC_FIRST_BYTE))
       status = COMM_FAIL;
   else if (rx_buf[MSG_FUN_INX] EQ 0xff) 
   {
       if (NOT com_rec_buf(rx_buf + 2, 4, REC_BYTES_TIME))
           status = COMM_ER1;
       else 
           status = rx_buf[3];  // Get status returned form antenna.
   } else 
   {
       if (com_rec_buf(rx_buf + 2, data_size*2 + 2, REC_MORE_BYTES)
           AND (rx_buf[0] EQ MSG_BEGIN) AND (rx_buf[MSG_FUN_INX] EQ read_mode)
           AND (rx_buf[data_size*2 + 2] EQ MSG_TERM)  // Correct terminator?
           AND (rx_buf[data_size*2 + 3] EQ MSG_TERM))
       {
           status = OP_OK;
           for(ii=0; ii < data_size; ii++) 
               *data_buf++ = rx_buf[3 + ii*2];
       } else 
           status = COMM_INV;
   }

   return(status);
}

// ****************************************************************************
// Receive response from COM2 read command.
// Input:        where to place the data.
//               Receive no more data than this. Note: BYTE received in a WORD.
//               Timeout value sent with this command.
//               The read type, Block read or Continual, BLK_READ or CON_READ_MODE.
// Return:       Command status.
// Side effects: None.
static BYTE do_COMReadRx(BYTE *data_buf, WORD data_size, WORD timeout, BYTE read_type)
{
   BYTE *rx_buf;
   BYTE status;
   WORD ii;
   WORD terminated_data;  // byte count for data count and terminator.

   status = OP_OK;                        // Assume a valid command.
   ClearBuf(build_buf, data_size*2 + 4);  // Initialize the buffer

   // first check the first 2 bytes to find out what is expected
   // if even the first 2 bytes are not gotten, serial communication fails
   // if get 0xff in 2nd bytes, receive Error message
   // else may get right message
#if TEST_MUX32  // Throw out the first two bytes.
   if(NOT com_rec_buf(build_buf, 2, 
      (timeout >= 1000 ? ((timeout / 1000) + 2) : REC_BYTES_TIME)))  
      status = COMM_FAIL;   // They are MUX32 address and stuff.
   else if (NOT com_rec_buf(build_buf, 2, REC_BYTES_TIME))
#else
   if (NOT com_rec_buf(build_buf, 2, 
      (timeout >= 1000 ? ((timeout / 1000) + 2) : REC_BYTES_TIME)))
#endif  
       status = COMM_FAIL;
   else if (build_buf[MSG_FUN_INX] NE read_type)
       status = INP_NOTMATCH;
   else 
   {
       if (com_rec_buf(&build_buf[2], 4, REC_MORE_BYTES))
       {
           rx_buf = &build_buf[COM_DATA_LEAD];
           for (ii = build_buf[NUM_DATA]; ii; ii--)
           {
              if (NOT com_rec_buf(rx_buf, 2, REC_BYTES_TIME))
                 return(COMM_FAIL);
              rx_buf = rx_buf + 2;  // Point at next data.
           }
           if (NOT com_rec_buf(rx_buf, 2, REC_BYTES_TIME)) // Get the terminator bytes.
              return(COMM_FAIL);
           if ((*rx_buf NE MSG_TERM) OR (*(rx_buf + 1) NE MSG_TERM))
              return(COMM_FAIL);        // Terminator bytes not found.

           terminated_data = build_buf[NUM_DATA] + LEN_STATUS;
           if (data_size < build_buf[NUM_DATA])
               status = INP_NOTMATCH;
           else      
               for(ii=0; ii < terminated_data; ii++)
                  *data_buf++ = build_buf[NUM_DATA + ii*2];
       } else 
           status = COMM_INV;
   }
   return(status);
}

// ****************************************************************************
// If the received message is short one (4 bytes) without data, use this one
// Input:        The command number that is being echoed as received.
// Return:       BOOLEAN: TRUE  - got expected command echo.
//                        FALSE - did not get expected command echo.
// Side effects: None.
static BYTE RxCheck(BYTE command_no)
{
    BYTE  rx_buf[6], status;

    ClearBuf(rx_buf, 6);

#if TEST_MUX32
    if(NOT com_rec_buf(rx_buf, 2, REC_FIRST_BYTE)) // Throw out the first two bytes. 
       status = COMM_FAIL;   // They are MUX32 address and stuff.
   else
#endif
   // first check the first 2 bytes to find out what is expected
   // if even the first 2 bytes are not gotten, serial communication fails
   // if get 0xFF in 2nd bytes, receive Error message
   // else may get right message
   if (NOT com_rec_buf(rx_buf, 2, REC_FIRST_BYTE))
   {
       status = COMM_FAIL;
   } else if (rx_buf[MSG_FUN_INX] EQ 0xff) 
   {
       if (com_rec_buf(rx_buf+2, 4, REC_BYTES_TIME))
           status = rx_buf[3];
       else
           status = COMM_FAIL;
   } else 
   {
   if (com_rec_buf(rx_buf+2, 2, REC_MORE_BYTES) AND 
       (rx_buf[MSG_BEG_INX] EQ MSG_BEGIN) AND (rx_buf[MSG_FUN_INX] EQ command_no)
       AND (rx_buf[2] EQ MSG_TERM) AND (rx_buf[3] EQ MSG_TERM) ) 
           status = OP_OK;
       else
           status = COMM_INV;
   }
   return(status);
}

           
// ****************************************************************************
// Non-Contiguour Read Command 
//   
// Input:        Time out value of some type.  It is sent in the command packet.
// Return:       None.
// Side effects: A packet is queued to be sent.
void NCReadTx(WORD timeout) 
{
    BYTE tx_buf[6];
    
    tx_buf[MSG_BEG_INX] = MSG_BEGIN;
    tx_buf[MSG_FUN_INX] = NONCO_READ;
    tx_buf[MSG_TIMH_IX] = (BYTE)(timeout / 256);
    tx_buf[MSG_TIML_IX] = (BYTE)(timeout % 256);
    tx_buf[4] = MSG_TERM;
    tx_buf[5] = MSG_TERM;

#if ANTARES
    com_buf_clr();              // Clear out Anyares COM receive buffer.
#endif    
    SendStream(tx_buf, NC_READ_CMDSIZE);
}
     
     
// ****************************************************************************
// Transmit the Non-Contiguour Write Command 
//   
// Input:        Time out value of some type.  It is sent in the command packet.
//               Pointer to the data to be written.
//               Number of data bytes.
// Return:       NONE.
// Side effects: A packet is queued to be sent.
void NCWriteTx(WORD timeout, BYTE *data_buf, 
                      WORD data_size)
{
    WORD i;
    BYTE *address_ptr; // Index the output buffer to build command packet.

    ClearBuf(build_buf, (data_size * 2) + 6);  // initialize buffer.

    build_buf[MSG_BEG_INX] = MSG_BEGIN;
    build_buf[MSG_FUN_INX] = NONCO_WRITE;
    build_buf[MSG_TIMH_IX] = (BYTE)(timeout / 256);
    build_buf[MSG_TIML_IX] = (BYTE)(timeout % 256);
    address_ptr = &build_buf[4];         // Setup for loop index pointer.
    for(i = 0; i < data_size; i++)
    {
        address_ptr++;
        *address_ptr++ = *data_buf++;

⌨️ 快捷键说明

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