Hi friends,
Can anyone help me with this? i don't know how to proceed this code further..showing errors.i need to communicate with a hardware device(Reading card information from RFID reader) and also write dll
my protocol
http://www.rfidshop.com.hk/datasheet/485/485-protocol.pdf
<code>
// longrangeDLL.cpp : Defines the exported functions for the DLL application.
//
//#define _CRT_SECURE_NO_DEPRECATE
#include "stdafx.h"
#include "longrange.h"
#include "cstdint"
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <bitset>
#include <sstream>
#define ERR -1
#define OK 1
#define ONEPARITY 1
#define ID 0x31| 0x32| 0x33|0x34| 0x35| 0x36| 0x37| 0x38
#define READ_TIMEOUT 30000
#define SOH 0x09
#define END 0x0D
#define TYPE 0x0A
#define FC 0x0F
#define NULL 0
using namespace std;
//char mydatachar[16]; //character conversion of data initialization
DWORD dwBytesRead = 0;
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*/
longrangeDLL_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 */
longrangeDLL_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)
{
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);
}
}
//BCC calculation
longrangeDLL_API BYTE calculateBCC(const uint32_t mydata)
{
int BCC;
sprintf(mydatachar, "%lu", mydata); //uint32_t to char conversion
char data[7] = { SOH, TYPE, ID, FC,*mydatachar, BCC, SOH };
unsigned int i = 0;
int k1, k2, k3;
k1 = data[i] ^ data[i + 1];
k2 = k1 ^ data[i + 2];
k3 = k2 ^ data[i + 3];
BCC = k3 ^ mydata;
return BCC; //result will be in byte format
}
//write to serial
int Write_To_Serial(*mydatachar,sizeof(char(mydatachar)), m_portHandle)
{
ProxReader_Log_Message("trying to write port\n");
int BCC = 0;
m_portHandle.Write(mydata, sizeof(mydata), &dwBytesRead);
if (dwBytesRead > 0)
{
calculateBCC(mydata);
return BCC;
cout << "Value of BCC is : " << BCC << endl;
}
return 0;
}
</code>
please help me