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

📄 at125lib.cpp

📁 附件采用EM4095 读头IC来读取Atmel的Tag芯片T5557
💻 CPP
字号:

#include "AT125Lib.h"

#include "Function_basic.h"
#include "Const.h"




//----------------------------------------------

AT125Lib::AT125Lib()
{
   timeout=2000;

    uart_in_read = 0;
    uart_in_write= 0;
    uart_in_end = 0;
    uart_state = UART_EMPTY;
    uart_in_error=0;
    uart_in_overflow=0;

}

//----------------------------------------------

AT125Lib::~AT125Lib()
{

}

//----------------------------------------------

bool AT125Lib::OpenComPort(int port)
{

    this->InitPort(port,38400,'N',8,1);

    return OpenPort();
}

//----------------------------------------------

void AT125Lib::CloseComPort()
{
   this->ClosePort();
}

//----------------------------------------------

int AT125Lib::T5557RegularRead(unsigned char *ptr,int len)
{
GinAutoMutex sync(&mutex);
unsigned char buf[12];

        buf[0]=COMMAND;
        buf[1]=CMD_T5557;
        buf[2]=CMD_T5557_REGULAR_READ;
        buf[3]=(unsigned char)len&0xff;

        if(AccessControlLayer(buf,4)==0)
        {
            return CheckResponseT5557RegularRead(ptr,len,3000);
        }

return -1;
}

//----------------------------------------------

int AT125Lib::T5557ReadTID(unsigned char *ptr)
{
GinAutoMutex sync(&mutex);
unsigned char buf[12];

        buf[0]=COMMAND;
        buf[1]=CMD_T5557;
        buf[2]=CMD_T5557_READ_TID;

        if(AccessControlLayer(buf,3)==0)
        {
            return CheckT5557ReadTID(ptr,2000);
        }

return -1;
}

//----------------------------------------------

int AT125Lib::T5557ReadBlock(unsigned char *ptr,int addr)
{
GinAutoMutex sync(&mutex);
unsigned char buf[12];

        buf[0]=COMMAND;
        buf[1]=CMD_T5557;
        buf[2]=CMD_T5557_READ_BLOCK;
        buf[3]=addr&0xff;

        if(AccessControlLayer(buf,4)==0)
        {
            return CheckT5557ReadBlock(ptr,addr,2000);
        }

return -1;
}

//----------------------------------------------

int AT125Lib::T5557WriteBlock(unsigned char *data,int addr)
{
GinAutoMutex sync(&mutex);
unsigned char buf[12];
unsigned char status;

        buf[0]=COMMAND;
        buf[1]=CMD_T5557;
        buf[2]=CMD_T5557_WRITE_BLOCK;
        buf[3]=addr&0xff;
        buf[4]=data[0];
        buf[5]=data[1];
        buf[6]=data[2];
        buf[7]=data[3];


        if(AccessControlLayer(buf,8)==0)
        {
            return CheckResponseStatus(status,2000);
        }

return -1;
}

//----------------------------------------------

int AT125Lib::T5557WriteConfig(unsigned char *config)
{
GinAutoMutex sync(&mutex);
unsigned char buf[12];
unsigned char status;

        buf[0]=COMMAND;
        buf[1]=CMD_T5557;
        buf[2]=CMD_T5557_WRITE_CONFIG;
        buf[3]=config[0];
        buf[4]=config[1];
        buf[5]=config[2];
        buf[6]=config[3];

        if(AccessControlLayer(buf,7)==0)
        {
            return CheckResponseStatus(status,2000);
        }

return -1;
}

//----------------------------------------------

int AT125Lib::T5557WritePassword(unsigned char *password)
{
GinAutoMutex sync(&mutex);
unsigned char buf[12];
unsigned char status;

        buf[0]=COMMAND;
        buf[1]=CMD_T5557;
        buf[2]=CMD_T5557_WRITE_BLOCK;
        buf[3]=password[0];
        buf[4]=password[1];
        buf[5]=password[2];
        buf[6]=password[3];
        buf[7]=0x07&0xff;

        if(AccessControlLayer(buf,8)==0)
        {
            return CheckResponseStatus(status,2000);
        }

return -1;
}

//----------------------------------------------

int AT125Lib::AccessControlLayer(unsigned char *data,unsigned char len,int to)
{
unsigned char buf[64];  //don't large than each node's temp_buffer
         buf[0]=0x01;
         buf[1]=len;
         memcpy(&buf[2],data,len);
         buf[len+2]=GetChecksum(buf,len+2);
         buf[len+3]=0xFF;

return SendData(buf,len+4,to);
}

//----------------------------------------------

unsigned char AT125Lib::GetChecksum(unsigned char *data,int len)
{
   unsigned char crc = 0x00;
   while (len-->0)
   {
    crc = crc ^ *data++;
   }
   return ( crc );

}

//----------------------------------------------

int AT125Lib::SendData(unsigned char *data,int len,int to)
{
   if(this->IsOpened())
   {
        TComm::FlushPort();
   if(!TComm::WritePort(data,len,to?to:timeout))
        return -1;

   return 0;
   }

   return -1;

}

//----------------------------------------------

int AT125Lib::ReadData(unsigned char *data,int len,int to)
{
int l;

   if(this->IsOpened())
   {
        l=TComm::ReadPort(data,len,to?to:timeout,true);
        return l;
   }

   return -1;

}

//----------------------------------------------

int AT125Lib::CheckResponse(unsigned char *data,unsigned char *len,int to)
{
unsigned short ptr;
unsigned char result;
unsigned char c,byte;

    uart_in_read = 0;
    uart_in_write= 0;
    uart_in_end = 0;
    uart_state = UART_EMPTY;
    uart_in_error=0;
    uart_in_overflow=0;

    ptr=0;

    while ( (uart_state != UART_WAIT_ERROR_SENT) && (uart_state != UART_VALID))
    {
        if(ReadData(&temp_buffer[ptr],1,to)==1)
        {
          c=temp_buffer[ptr];
        }
        else
        {
          //return -1;
           c=0;
          break;
        }

        switch (uart_state)
        {
        case UART_EMPTY :                //detect STX
            if (c == 0x00)
            {               //zero byte is ignored
            }
            else if (c == 0x01)
            {
                uart_state = UART_READ_SIZE;
                byte=c;
            }
            else
            {
                uart_state = UART_WAIT_ERROR_SENT;
                result = ERR_UART_ERROR_FLAG;
            }

            break;
            
        case UART_READ_SIZE :            //capture size byte

                uart_read_bytes = c;
                uart_read_msg_bytes = c;
                uart_state = UART_READ_BYTES;
                byte ^= c;

            break;
            
        case UART_READ_BYTES :           //read specified size bytes to allow
            
            if (uart_read_bytes == 0)
            {
                //checksum
                if (byte == c)
                {
                    uart_state = UART_END;
                }
                else
                {
                    result = ERR_UART_BAD_CRC;
                    uart_state = UART_WAIT_ERROR_SENT;
                }

            }
            else
            {
              uart_read_bytes--;
              byte ^= c;
            }

            break;

        case UART_END :
                //last byte - ETX expected
                //check ETX
                if (c == 0xFF)
                {
                    result = UART_MESSAGE_OK;
                    uart_in_end = ptr;
                    uart_state = UART_VALID;
                }
                else
                {
                    result = ERR_UART_NO_ETX;
                    uart_state = UART_WAIT_ERROR_SENT;
                }

             break;
        };

        ptr++;
 
    }


    if (uart_state == UART_WAIT_ERROR_SENT)
    {
        uart_in_read = uart_in_write;      //dropped =async

        //return result;
        return -1;
    }
    
    if (uart_state == UART_VALID)
    {
        if(*len<temp_buffer[1])
                return -2;

         *len=temp_buffer[1];
          memcpy(data,&temp_buffer[2],*len);
         return (ptr);

    }

    return 0;


}

//----------------------------------------------

int AT125Lib::CheckResponseStatus(unsigned char &status,int to)
{
unsigned char buf[128];
unsigned char result,len;
int count;
unsigned char rs;

        len=128;
        count=0;
        while((result=CheckResponse(buf,&len,to))>0)
        {
                if(result>0)
                {
                        if(buf[0]==RESPONSE_DATA)// && buf[1]==0x01)
                        {
                                //if(buf[2]==0x03)
                                {
                                        rs=buf[3];
                                        if(buf[3]==0x00)
                                                count=1;

                                }
                                break;

                        }
                        else if(buf[0]==0x03 && buf[1]==0x02)
                        {
                                 break;
                        }
                }
        }

return count;
}


//----------------------------------------------

int AT125Lib::CheckResponseT5557RegularRead(unsigned char *ptr,int length,int to)
{
unsigned char buf[128];
unsigned char result,len;
int count;

        len=128;
        count=0;
        while((result=CheckResponse(buf,&len,to))>0)
        {
          if(result>0)
          {
              if(buf[0]==RESPONSE_DATA && buf[1]==CMD_T5557 && buf[2]==CMD_T5557_REGULAR_READ)
              {
                if( buf[3]==0)
                {
                        count++;
                        memcpy(ptr,&buf[4],length);
                }
                break;
              }
              else if(buf[0]==RESPONSE_END && buf[1]==CMD_T5557)
              {
                  break;
              }
          }
        }


return count;
}

//----------------------------------------------

int AT125Lib::CheckT5557ReadTID(unsigned char *ptr,int to)
{
unsigned char buf[128];
unsigned char result,len;
int count;

        len=128;
        count=0;
        while((result=CheckResponse(buf,&len,to))>0)
        {
          if(result>0)
          {
              if(buf[0]==RESPONSE_DATA && buf[1]==CMD_T5557 && buf[2]==CMD_T5557_READ_TID)
              {
                if( buf[3]==0)
                {
                        count++;
                        memcpy(ptr,&buf[4],8);
                }
                break;
              }
              else if(buf[0]==RESPONSE_END && buf[1]==CMD_T5557)
              {
                  break;
              }
          }
        }


return count;
}

//----------------------------------------------

int AT125Lib::CheckT5557ReadBlock(unsigned char *ptr,int addr,int to)
{
unsigned char buf[128];
unsigned char result,len;
int count;

        len=128;
        count=0;
        while((result=CheckResponse(buf,&len,to))>0)
        {
          if(result>0)
          {
              if(buf[0]==RESPONSE_DATA && buf[1]==CMD_T5557 && buf[2]==CMD_T5557_READ_BLOCK)
              {
                if( buf[3]==0)
                {
                        count++;
                        memcpy(ptr,&buf[4],4);
                }
                break;
              }
              else if(buf[0]==RESPONSE_END && buf[1]==CMD_T5557)
              {
                  break;
              }
          }
        }
        
return count;
}

⌨️ 快捷键说明

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