RFID reader programming

Feb 29, 2016 at 5:53am
I think anyone of you can help me if iam clear in my questions.I need to write a dll for M18 long range RFID reader.Iam not clear about serial programming.I know to start to communicate with a hardware i need to open a port.In the code below i have opened com port,set timeout,created a log file to know what is happening..
iam able to open com port,set communication timeout,close com port.

next function is to read reader's factory serial no...

please check this link http://www.rfidshop....85-protocol.pdf

how to read from the serial port...please help me

#include "stdafx.h"
#include "prox_reader.h"
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>

#define ERR -1
#define OK 1
#define ONEPARITY 1
#define ID 0x31 TO 0x38
#define READ_TIMEOUT 30000
#define SOH 0x09
#define END 0x0D
#define TYPE 0x0A
#define FC 0x0F
#define NULL 0

using namespace std;

wchar_t * __stdcall getstring(char* appname)
{
wchar_t* wString = new wchar_t[4096];
MultiByteToWideChar(CP_ACP, 0, appname, -1, wString, 4096);
return wString;
}
//Get the Error Message where the function blocks
void ProxReader_Log_Message(char *name)
{
FILE *pFile;
time_t t = time(0);
struct tm* lt = localtime(&t);
char date[9];
char directory[80] = "C:\\Client\\";

sprintf(date, "%02d%02d%02d", lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday);

strcat(directory, date);
strcat(directory, "_Readerlog.txt");

pFile = fopen(directory, "ab");
if (pFile != NULL)
{
fputs(name, pFile);
fclose(pFile);
}
}

/* Open Serial Port for the PROXIMITY*/
RFIDreader_API int CommOpen(char* Port, HANDLE &m_portHandle)
{
ProxReader_Log_Message("from function CommOpen!!!\n");
ProxReader_Log_Message("Trying to open: ");
ProxReader_Log_Message(Port);
ProxReader_Log_Message("\n");
wchar_t* portname = getstring(Port);
m_portHandle = CreateFile(portname,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
NULL,
0);
if (m_portHandle == INVALID_HANDLE_VALUE)
{
ProxReader_Log_Message("Error Opening com port!!!\n");
return ERR;
}
else
{
ProxReader_Log_Message("Success Opening com port!!!\n");
return OK;
}
}

/* Configure COM Port */
static int ConfigurePort(DWORD BaudRate, BYTE ByteSize, BYTE Parity, HANDLE m_portHandle)
{
BOOL m_portStatus;
DCB dcb;

if ((m_portStatus = GetCommState(m_portHandle, &dcb)) == 0){
CloseHandle(m_portHandle);
return ERR;
}
dcb.BaudRate = 19200; //BaudRate;
dcb.ByteSize = 8;
dcb.Parity = ONEPARITY;
m_portStatus = SetCommState(m_portHandle, &dcb);
if (m_portStatus == 0)
{
ProxReader_Log_Message("Error Configuring com port!!!\n");
CloseHandle(m_portHandle);
return ERR;
}
ProxReader_Log_Message("Success Configuring com port!!!\n");
return OK;
}
/* Close COM Port */
RFIDreader_API int CommClose(HANDLE m_portHandle)
{
ProxReader_Log_Message("Closing comm port.\n");
CloseHandle(m_portHandle);
return OK;
}

/* Set Communication Timeouts */
int SetCommunicationTimeouts(int timeout_len, HANDLE m_portHandle)//(DWORD ReadIntervalTimeout, DWORD ReadTotalTimeoutMultiplier, DWORD ReadTotalTimeoutConstant, DWORD WriteTotalTimeoutMultiplier, DWORD WriteTotalTimeoutConstant)
{
if ((m_portStatus = GetCommTimeouts(m_portHandle, &m_CommTimeouts)) == 0)
return false;
m_CommTimeouts.ReadIntervalTimeout = 1;
m_CommTimeouts.ReadTotalTimeoutConstant = READ_TIMEOUT;
m_CommTimeouts.ReadTotalTimeoutMultiplier = 1;
m_CommTimeouts.WriteTotalTimeoutConstant = 1;
m_CommTimeouts.WriteTotalTimeoutMultiplier = 1;
m_portStatus = SetCommTimeouts(m_portHandle, &m_CommTimeouts);
if (m_portStatus == 0)
{
ProxReader_Log_Message("Set Timeout\n");
//CloseHandle(m_portHandle);
return ERR;
}
return OK;
}

int CommSetup(char* Port, char* BaudRate, HANDLE &m_portHandle)
{
ProxReader_Log_Message("trying to open port\n");
int result;
result = CommOpen(Port, m_portHandle);
ConfigurePort(atoi(BaudRate), 8, 1, m_portHandle);
SetCommunicationTimeouts(35, m_portHandle);
ProxReader_Log_Message("finish open port\n");
return result;
}

void readConfig(char** Baudrate, char** Readerport)
{
fstream clientINI;
string line;
string port;
unsigned int found;
string variable = "PROXIMITYREADERCOMPort=";
clientINI.open("C:\\Client\\INI\\client.ini");
if (clientINI.is_open())
{
*Baudrate = "19200";
while (getline(clientINI, line))
{
found = line.find(variable);
if (found != string::npos)
{
port = line.substr(found + variable.length()).c_str();
char *cstr = new char[port.length() + 1];
strcpy(cstr, port.c_str());
*Readerport = cstr;
break;
}
}
clientINI.close();
}
else
{
*Baudrate = "19200";
*Readerport = "COM1";
}
}

int read(char *buffer, int buffLen, HANDLE m_portHandle, bool nullTerminate)
{
DWORD numRead;
if (nullTerminate)
{
--buffLen;
}

BOOL ret = ReadFile(m_portHandle, buffer, buffLen, &numRead, NULL);

if (!ret)
{
return 0;
}

if (nullTerminate)
{
buffer[numRead] = '\0';
}

return numRead;
}

#define FLUSH_BUFFSIZE 10

void flush(HANDLE m_portHandle)
{
char buffer[FLUSH_BUFFSIZE];
int numBytes = read(buffer, FLUSH_BUFFSIZE, m_portHandle, false);
while (numBytes != 0)
{
ProxReader_Log_Message("Flushing\n");
numBytes = read(buffer, FLUSH_BUFFSIZE, m_portHandle, false);
}
}



/* Write Data to Serial Port
int Write_To_Serial(BYTE *msg, DWORD msgLength, HANDLE m_portHandle)
{
ProxReader_Log_Message("trying to write port\n");
DWORD written;
int ch = 0;
if (!WriteFile(m_portHandle, msg, msgLength, &written, NULL))
ProxReader_Log_Message("writing data to port\n");

if (written != msgLength)
ProxReader_Log_Message("not all data written to port\n");

return 0;
}
/*Read from Serial Port */
int Read_From_Serial(BYTE resp[], HANDLE m_portHandle)
{
DWORD dwBytesTransferred = 0;
char byte[200];
int k = 0, result = 0;
unsigned int i;
memset(byte, 0, sizeof(byte));
memset(resp, 0, sizeof(resp));

if (ReadFile(m_portHandle, &byte, sizeof(byte), &dwBytesTransferred, NULL))
{
if (dwBytesTransferred > 0)
{
ProxReader_Log_Message("Managed to read something\n");
ProxReader_Log_Message(byte);
ProxReader_Log_Message("\n");

}
else
{
ProxReader_Log_Message("Error reading from file \n");
return ERR;
}
}

}
Feb 29, 2016 at 8:52am
The problem that you need to be aware of: You might not read the complete data. So you need to call ReadFile(...) until the required number of bytes are read.
Mar 10, 2016 at 4:59am
thank you for your reply
Topic archived. No new replies allowed.