Knowledge Base Nr: 00192 aegtransp.cpp - http://www.swe-kaiser.de

Downloads:

RFID Transponder AEG Compact Reader ARE12

  
//links:
//- http://www.euroid.com
//- http://www.aegid.de

////////////////////////////////////
//sample
////////////////////////////////////

void CBase::readTransponder(int nTransNo)
{
QString strTxt;
int nValue = hw_readTransponderPosition(nTransNo);
if (nValue<0)
{
LogMessage(LOG_PRIO_ERR, "hw_readTransponderPosition(%d) err:%d!", nTransNo, nValue);
strTxt.sprintf("Error: %d", nValue);
}
else
{
strTxt.sprintf("Value: %08x", nValue);
}

m_pIOStatusDlg->textLabelTrans1->setText(strTxt);
}

////////////////////////////////////
//header
////////////////////////////////////

int hw_readTransponderPosition(int nTransNo); //<0:error 0:kein chip >0:chip-id

int hw_initTransponder(int* hTransp, int nTransNo); //<0:error 0:ok
int hw_cmdTransponder(int hTransp, const char* lpszCmd, char* szAnswer); //<0:error 0:ok
int hw_closeTransponder(int hTransp); //<0:error 0:ok

////////////////////////////////////
//implementierung
////////////////////////////////////

int g_hTransp[MAX_TRANSPONDER];

int hw_init()
{
...
//transponder initialisieren
for (int n=0; n<MAX_TRANSPONDER; n++)
nErr = hw_initTransponder(&g_hTransp[n], n);
...
}

int hw_exit()
{
...
//transponder abmelden
for (int n=0; n<MAX_TRANSPONDER; n++)
hw_closeTransponder(g_hTransp[n]);
...
}


int hw_initTransponder(int* hTransp, int nTransNo)
{
*hTransp = 0;

char szTransponderPort[1024] = {0};

switch (nTransNo)
{
case 0: strcpy(szTransponderPort, "/dev/ttyS1"); break;
case 1: strcpy(szTransponderPort, "/dev/ttyS2"); break;
case 2: strcpy(szTransponderPort, "/dev/ttyS3"); break;
case 3: strcpy(szTransponderPort, "/dev/ttyS4"); break;
default:
LogMessage(LOG_PRIO_ERR, "illegale Transponderschnittstelle: %d", nTransNo);
return -1;
}

int fd = open(szTransponderPort, O_RDWR | O_NONBLOCK | O_NOCTTY, 0);
if (fd < 0)
{
LogMessage(LOG_PRIO_ERR, "Transponderschnittstelle <%s> kann nicht geöffnet werden <%d>", szTransponderPort, fd);
return -2;
}

struct termios termio;
memset(&termio, 0, sizeof(termio));

cfsetospeed(&termio,B19200);
cfsetispeed(&termio,B19200);

termio.c_cflag |= (CLOCAL|CREAD);
termio.c_cflag |= CS8;

tcsetattr(fd, TCSANOW, &termio);

int nErr = 0;
char szCmd[100];
char szAnswer[100];

strcpy(szCmd, "MD 2\r"); //schnittstellengetriggert
nErr = hw_cmdTransponder(fd, szCmd, szAnswer);
LogMessage(LOG_PRIO_MSG, "Transponder <%s> => <%s>", szCmd, szAnswer);
if (nErr < 0)
return -15;

strcpy(szCmd, "BD 2\r"); //baud: 19200
nErr = hw_cmdTransponder(fd, szCmd, szAnswer);
LogMessage(LOG_PRIO_MSG, "Transponder <%s> => <%s>", szCmd, szAnswer);
if (nErr < 0)
return -11;

strcpy(szCmd, "CID 0\r"); //kein filter
nErr = hw_cmdTransponder(fd, szCmd, szAnswer);
LogMessage(LOG_PRIO_MSG, "Transponder <%s> => <%s>", szCmd, szAnswer);
if (nErr < 0)
return -12;

strcpy(szCmd, "CN 1\r"); //noreads
nErr = hw_cmdTransponder(fd, szCmd, szAnswer);
LogMessage(LOG_PRIO_MSG, "Transponder <%s> => <%s>", szCmd, szAnswer);
if (nErr < 0)
return -13;

strcpy(szCmd, "EC 0\r"); //echo off
nErr = hw_cmdTransponder(fd, szCmd, szAnswer);
LogMessage(LOG_PRIO_MSG, "Transponder <%s> => <%s>", szCmd, szAnswer);
if (nErr < 0)
return -14;

strcpy(szCmd, "NID 0\r"); //fehlersicherung leser
nErr = hw_cmdTransponder(fd, szCmd, szAnswer);
LogMessage(LOG_PRIO_MSG, "Transponder <%s> => <%s>", szCmd, szAnswer);
if (nErr < 0)
return -16;

strcpy(szCmd, "TOR 1\r"); //max. lesezeit * 100ms
nErr = hw_cmdTransponder(fd, szCmd, szAnswer);
LogMessage(LOG_PRIO_MSG, "Transponder <%s> => <%s>", szCmd, szAnswer);
if (nErr < 0)
return -17;

strcpy(szCmd, "VSAVE\r"); //in eeprom sichern
nErr = hw_cmdTransponder(fd, szCmd, szAnswer);
LogMessage(LOG_PRIO_MSG, "Transponder <%s> => <%s>", szCmd, szAnswer);
if (nErr < 0)
return -18;

*hTransp = fd;
return 0;
}

int hw_cmdTransponder(int hTransp, const char* lpszCmd, char* szAnswer)
{
*szAnswer = 0;

if (!hTransp)
return -1;

int nCmdLen = strlen(lpszCmd);

LogMessage(LOG_PRIO_MSG, "Transponder schreiben <%d> bytes : <%02x %02x %02x %02x %02x %02x ..."
, nCmdLen, lpszCmd[0], lpszCmd[1], lpszCmd[2], lpszCmd[3], lpszCmd[4], lpszCmd[5]);

int nErr = write(hTransp, lpszCmd, nCmdLen);
if (nErr < 0)
{
LogMessage(LOG_PRIO_ERR, "Transponder Schreibfehler err:<%d> cmd:<%s> len:%d", nErr, lpszCmd, nCmdLen);
return -2;
}

usleep(125*1000); //us //todo

char szBuffer[100] = {0};

errno = 0;
int nRead = read(hTransp, szBuffer, sizeof(szBuffer));
LogMessage(LOG_PRIO_MSG, "Transponder lesen <%d> bytes : <%02x %02x %02x %02x %02x %02x ..."
, nRead, szBuffer[0], szBuffer[1], szBuffer[2], szBuffer[3], szBuffer[4], szBuffer[5]);
LogMessage(LOG_PRIO_INFO, "Transponder lesen (%d bytes) <%s> err:%d - <%s>", nRead, szBuffer, errno, strerror(errno));
if (nRead > 0)
{
LogMessage(LOG_PRIO_MSG, "Transponder <%s> => <%s>", lpszCmd, szBuffer);
strcpy(szAnswer, szBuffer);
}

return 0; //nRead == 0 (keine antwort)
}

int hw_closeTransponder(int hTransp)
{
if (hTransp)
close(hTransp);

return 0;
}

int hw_readTransponderPosition(int nTransNo) //<0:error 0:kein chip >0:chip-id
{
int hTransp = g_hTransp[nTransNo-1];
if (hTransp <= 0)
return 0;

char szCmd[100];
char szAnswer[100];

strcpy(szCmd, "GT\r");
int nErr = hw_cmdTransponder(hTransp, szCmd, szAnswer);
if (nErr < 0)
return nErr;

int nLen = strlen(szAnswer);

if (nLen == 0) //chip-id
{
return 0; //kein chip
}
else if (szAnswer[0] == '#') //gerätefehler
{
return -1;
}
else if (szAnswer[0] == 'X') //kein chip
{
return 0;
}
else if ((szAnswer[0] == '\r') || (szAnswer[0] == '\n')) //kein chip
{
return 0;
}
else if ((nLen == 8) || (nLen == 9)) //chip-id (evtl. mit folgendem cr)
{
int nTransID = 0;
sscanf(szAnswer, "%x", &nTransID);
return nTransID;
}
else if (nLen == 10) //chip-id mit führendem cr
{
int nTransID = 0;
sscanf(szAnswer+1, "%x", &nTransID);
return nTransID;
}

return -200; //illegale antwort
}