這段C++Builder如何轉Delphi,謝謝! ( 积分: 200 )

L

lixin38

Unregistered / Unconfirmed
GUEST, unregistred user!
#include "SPComm.hpp"

// 通訊命令
#define HD_SETTIME 0x01 // 校時
#define HD_SEARCHDM 0x03 // 單機尋機
#define HD_RECVRECORD 0x10 // 取定長記錄
#define HD_RECVDELRECORD 0x1c // 刪除方式取記錄
#define HD_RECVDELRECORDRLT 0x1d // 刪除取記錄確認幀


//-------------------------實時門禁命令字--------------------------
#define HD_UPKAOQING 0xf0 // 實時上傳考勤記錄
#define HD_UPMENJING 0xf1 // 實時上傳門禁記錄
#define HD_UPBAOJING 0xf2 // 實時上傳報警信號
#define HD_UPDEL_RECORD 0xf3 // 實時上傳記錄確認
//------------------------------------------------------------------

// 返回信息代碼
#define HDA_SUCCESS 0x08 // 命令執行成功
#define HDA_FAIL 0x09 // 命令執行失敗

#define CF_BOARDADR 0xaaaa // 通訊廣播地址
#define CF_BOOT 0x0f // 通訊通訊引導字節
#define CF_START 0xab // 通訊幀前導字節

// 通訊收發狀態表
#define CS_RECEIVE_STANDBY 0x02 // 接收等待
#define CS_RECEIVE_START2 0x04 // 接收幀頭
#define CS_RECEIVE_TAGADR1 0x06 // 接收目的地址
#define CS_RECEIVE_TAGADR2 0x00 // 接收目的地址
#define CS_RECEIVE_SRCADR1 0x05 // 接收來源地址
#define CS_RECEIVE_SRCADR2 0x09 // 接收來源地址
#define CS_RECEIVE_LOF1 0x07 // 接收幀長度字節1
#define CS_RECEIVE_LOF2 0x08 // 接收幀長度字節2
#define CS_RECEIVE_FRAME 0x0a // 接收幀內容
#define CS_RECEIVE_CHKSUM 0x0c // 接收校驗和
#define CS_RECEIVE_PARITYERR 0x0e // 接收校驗錯

struct ATTGUARDREC // 考勤記錄結構
{
char strPersonID[10];// 工號
//int nCardID
// 卡號
int nYear, nMonth, nDay, nHour, nMinute, nSecond
// 年,月,日,時,分,秒
BOOL bOnDuty, bBC
// 上下班標志(bOnDuty):0-上班,1-下班; 黑名單標志(bBC):0-非黑名單,1-黑名單
int nDMAdr
// 機具地址
};

// 自定義ASCII字符表
static unsigned char acChar[64] =
{
0x20, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a,
0x5f, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d,
0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a
};

//------------------------------------------------------------------
class THDLC : public TObject
{
private:
TComm *m_pComm;
// TTimer *Timer;

public:
int nTagAddress, nMyAddress, nCommand;
int nSrcAddress;// 源地址
ATTGUARDREC AttGuardRecord[100];
BYTE CF_COMMAND;// 接收通訊命令字

public:
BOOL __fastcall HDLC_Send(TComm *, const String&);
String __fastcall HDLC_Receive(BYTE);
int __fastcall GetAttGuardRecord(const String&);
BOOL __fastcall DelRecord(TComm*, bool);
void __fastcall DriveMessage();

int __fastcall myStrToInt(String, bool);
char __fastcall myatoi(char);
int __fastcall StrToHex(String);
int __fastcall StrToHex4(String);
String __fastcall Hex4ToStr(int);
String __fastcall HexToStr(int);
int __fastcall IntToBCD(int);

__fastcall THDLC();
__fastcall ~THDLC();

protected:
};

//---------------------------------------------------------------------------
__fastcall THDLC::THDLC()
{ CF_COMMAND = 0 ;
nMyAddress = 256;
}
//---------------------------------------------------------------------------
__fastcall THDLC::~THDLC()
{
m_pComm = NULL;
}
//---------------------------------------------------------------------------
// 發送幀
BOOL __fastcall THDLC::HDLC_Send(TComm *pComm, const String&amp
sData)
{
String str;

if (!pComm)
return FALSE;
m_pComm = pComm;

// 發送引導段
str += (char)CF_BOOT;
str += (char)CF_BOOT;
// 發送幀頭
str += (char)CF_START;
str += (char)CF_START;
// 發送目的地址
str += (char)(nTagAddress >> 8);
str += (char)nTagAddress;
BYTE bChkSum = (BYTE)str[5];
bChkSum ^= (BYTE)str[6];
// 發送來源地址
str += (char)(nMyAddress >> 8);
str += (char)nMyAddress;
bChkSum ^= (BYTE)str[7];
bChkSum ^= (BYTE)str[8];
// 發送幀長度
int nLengthOfFrame = sData.Length() + 9;
str += (char)(nLengthOfFrame >> 8);
str += (char)nLengthOfFrame;
bChkSum ^= (BYTE)str[9];
bChkSum ^= (BYTE)str[10];
// 發送通訊密碼
for(int i=0
i<8
i++)
{
str += (char)0;
// bChkSum ^= (BYTE)0;
}
// 發送命令字
str += (char)nCommand;
bChkSum ^= (BYTE)str[19];
// 發送幀內容,校驗和
for(int i=1
i<=sData.Length()
i++)
{
str += sData;
bChkSum ^= (BYTE)sData;
}
str += (char)bChkSum;

m_pComm->WriteCommData(str.c_str(), str.Length());
return true;

}
//---------------------------------------------------------------------------
// 接收字節
String __fastcall THDLC::HDLC_Receive(BYTE bByte)
{
String sRecvData = &quot;&quot;;
static String sCommBuf;
static int nCommunState=CS_RECEIVE_STANDBY, nTranCount;
static BYTE bChkSum, nTagAdr, nSrcAdr;
DriveMessage();
switch (nCommunState)
{
case CS_RECEIVE_STANDBY: // 接收等待
nCommunState = (bByte==CF_START) ? CS_RECEIVE_START2 : CS_RECEIVE_STANDBY;
break;
case CS_RECEIVE_START2: // 接收幀頭
nCommunState = (bByte==CF_START) ? CS_RECEIVE_TAGADR1 : CS_RECEIVE_STANDBY;
break;
case CS_RECEIVE_TAGADR1: // 接收目的地址
bChkSum = bByte;
nTagAdr = bByte << 8;
nCommunState = CS_RECEIVE_TAGADR2;
break;
case CS_RECEIVE_TAGADR2: // 接收目的地址
bChkSum ^= bByte;
nTagAdr += bByte;
nCommunState = CS_RECEIVE_SRCADR1;
break;
case CS_RECEIVE_SRCADR1: // 接收來源地址
bChkSum ^= bByte;
nSrcAdr = bByte << 8;
nCommunState = CS_RECEIVE_SRCADR2;
break;
case CS_RECEIVE_SRCADR2: // 接收來源地址
bChkSum ^= bByte;
nSrcAdr += bByte;
nSrcAddress = nSrcAdr;
nCommunState = CS_RECEIVE_LOF1;
break;
case CS_RECEIVE_LOF1: // 接收幀長度字節1
bChkSum ^= bByte;
nTranCount = bByte << 8;
nCommunState = CS_RECEIVE_LOF2;
break;
case CS_RECEIVE_LOF2: // 接收幀長度字節2
bChkSum ^= bByte;
nTranCount += bByte;
//nTranCount = 0x0a;
///////////////////////// // 限制幀長
if (nTranCount > 6000)
nCommunState = CS_RECEIVE_STANDBY;
/////////////////////////
sCommBuf = &quot;&quot;;
nCommunState = CS_RECEIVE_FRAME;
break;
case CS_RECEIVE_FRAME: // 接收幀內容
bChkSum ^= bByte;
sCommBuf += (char)bByte;
if (--nTranCount==0)
nCommunState = CS_RECEIVE_CHKSUM;
break;
case CS_RECEIVE_CHKSUM: // 接收校驗和
nCommunState = CS_RECEIVE_STANDBY;
if (bChkSum==bByte)
{
sRecvData = sCommBuf;
CF_COMMAND = sRecvData[1];
}
break
////////
default:
nCommunState = CS_RECEIVE_STANDBY;
break
///////
}
return sRecvData;
}
//---------------------------------------------------------------------------
// 考勤門禁記錄字符串->結構
int __fastcall THDLC::GetAttGuardRecord(const String&amp
sData)
{
int nCount = (sData.Length() - 1) / 9;
int nRemain = (sData.Length() - 1) % 9;
BYTE bTemp, bTemp2;

if (nRemain != 0)
return 0;

for (int i = 0
i < nCount
i ++)
{
//-----解析工號成ASCII碼----------------------------------
BYTE bTmp1[4];
BYTE bTmp[10];
char strTmp;
int iLoop,jLoop;
int m,n;
bTmp1[0]=sData[2];
bTmp1[1]=sData[3];
bTmp1[2]=sData[4];
bTmp1[3]=sData[5];
// bTmp1[0]=5;
// bTmp1[1]=0;
// bTmp1[2]=0;
// bTmp1[3]=0;
iLoop= bTmp1[0]*256*256*256+ bTmp1[1]*256*256 + bTmp1[2]*256+bTmp1[3];
//iLoop = 2;
bTmp[0]=iLoop/1000000000;
jLoop = iLoop%1000000000;
bTmp[1]=jLoop/100000000;
iLoop = jLoop%100000000;
bTmp[2]=iLoop/10000000;
jLoop = iLoop%10000000;
bTmp[3]=jLoop/1000000;
iLoop = jLoop%1000000;
bTmp[4]=iLoop/100000;
jLoop = iLoop%100000;
bTmp[5]=jLoop/10000;
iLoop = jLoop%10000;
bTmp[6]=iLoop/1000;
jLoop = iLoop%1000;
bTmp[7]=jLoop/100;
iLoop = jLoop%100;
bTmp[8]=iLoop/10;
jLoop = iLoop%10;
bTmp[9]=jLoop;

for(m=0;m<=9;m++)
AttGuardRecord.strPersonID[m] = bTmp[m]+'0';
//AttGuardRecord.nCardID = myStrToInt(sData.SubString(3 + 9 * i, 4), true);
AttGuardRecord.nYear = 2000 + myStrToInt(sData.SubString(6 + 9 * i, 1), true);
bTemp = myStrToInt(sData.SubString(7 + 9 * i, 1), true);
AttGuardRecord.nMonth = (int)(bTemp >> 4);
bTemp2 = myStrToInt(sData.SubString(8 + 9 * i, 1), true);
AttGuardRecord.nDay = (int)(((bTemp &amp
0x0f) << 1) | (bTemp2 >> 7));
AttGuardRecord.nHour = (int)((bTemp2 &amp
0x7f) >> 2);
bTemp = myStrToInt(sData.SubString(9 + 9 * i, 1), true);
AttGuardRecord.nMinute = (int)(((bTemp2 &amp
0x03) << 4) | (bTemp >> 4));
bTemp2 = myStrToInt(sData.SubString(10 + 9 * i, 1), true);
AttGuardRecord.nSecond = (int)(((bTemp &amp
0x0f) << 2) | (bTemp2 >> 6));

bTemp = myStrToInt(sData.SubString(10 + 9 * i, 1), true);
if ((bTemp &amp
0x20) == 0)
AttGuardRecord.bOnDuty = false;
else
AttGuardRecord.bOnDuty = true;

if ((bTemp &amp
0x10) == 0)
AttGuardRecord.bBC = false;
else
AttGuardRecord.bBC = true;
AttGuardRecord.nDMAdr = nSrcAddress;
}

return nCount;
}
//---------------------------------------------------------------------------
// 刪除記錄
BOOL __fastcall THDLC::DelRecord(TComm* pComm, bool bReal)
{
nTagAddress = nSrcAddress;
if (bReal)
nCommand = HD_UPDEL_RECORD;
else
nCommand = HD_RECVDELRECORDRLT;
String sCmd = &quot;&quot;;//(char)HDA_SUCCESS;
return HDLC_Send(pComm, sCmd);
}
//---------------------------------------------------------------------------
// 消息驅動
void __fastcall THDLC::DriveMessage()
{
MSG msg;
while (PeekMessage(&amp;msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
return;
TranslateMessage(&amp;msg);
DispatchMessage(&amp;msg);
}
}
//---------------------------------------------------------------------------
// String to Int function
// bool bUnsigned: 最高位是否為符號位
int __fastcall THDLC::myStrToInt(String sData, bool bUnsigned)
{
int nTemp = 0;
if (bUnsigned)
{
for (int i = 1
i < sData.Length() + 1
i ++)
nTemp = nTemp * 256 + (BYTE)sData;
}
else
{
if (((BYTE)sData[1] &amp
0x80) == 0x80)// 返回負數
{
nTemp = (BYTE)sData[1] - 0x80;
for (int i = 2
i < sData.Length() + 1
i ++)
nTemp = nTemp * 256 + (BYTE)sData;
nTemp = - nTemp;
}
else// 返回正數
{
for (int i = 1
i < sData.Length() + 1
i ++)
nTemp = nTemp * 256 + (BYTE)sData;
}
}
return nTemp;
}
//---------------------------------------------------------------------------
char __fastcall THDLC::myatoi(char chChar)
{
if (chChar>='0' &amp;&amp
chChar<='9')
return (char)(chChar - '0');
else if (chChar>='a' &amp;&amp
chChar<='f')
return (char)(chChar - 'a' + 10);
else if (chChar>='A' &amp;&amp
chChar<='F')
return (char)(chChar - 'A' + 10);
else
return 0;
}
//---------------------------------------------------------------------------
int __fastcall THDLC::StrToHex(String sSrc)
{
if (sSrc.Length()<2)
return 0;
else
return myatoi(sSrc[1]) * 0x10 + myatoi(sSrc[2]);
}
//---------------------------------------------------------------------------
int __fastcall THDLC::StrToHex4(String sSrc)
{
if (sSrc.Length()<4)
return 0;
else
return myatoi(sSrc[1]) * 0x1000 + myatoi(sSrc[2]) * 0x100
+ myatoi(sSrc[3]) * 0x10 + myatoi(sSrc[4]);
}
//---------------------------------------------------------------------------
String __fastcall THDLC::Hex4ToStr(int n)
{
String str;
char ch[5];
wsprintf(ch, &quot;%02x&quot;, (BYTE)(n / 0x100));
str = ch;
wsprintf(ch, &quot;%02x&quot;, (BYTE)n);
str += ch;
return str;
}
//---------------------------------------------------------------------------
String __fastcall THDLC::HexToStr(int n)
{
String str;
char ch[10];
wsprintf(ch, &quot;%02x&quot;, (BYTE)n);
str = ch;
return str;
}
//---------------------------------------------------------------------------
int __fastcall THDLC::IntToBCD(int nSrc)
{
return nSrc / 10 * 16 + nSrc % 10;
}
//---------------------------------------------------------------------------
 
#include &quot;SPComm.hpp&quot;

// 通訊命令
#define HD_SETTIME 0x01 // 校時
#define HD_SEARCHDM 0x03 // 單機尋機
#define HD_RECVRECORD 0x10 // 取定長記錄
#define HD_RECVDELRECORD 0x1c // 刪除方式取記錄
#define HD_RECVDELRECORDRLT 0x1d // 刪除取記錄確認幀


//-------------------------實時門禁命令字--------------------------
#define HD_UPKAOQING 0xf0 // 實時上傳考勤記錄
#define HD_UPMENJING 0xf1 // 實時上傳門禁記錄
#define HD_UPBAOJING 0xf2 // 實時上傳報警信號
#define HD_UPDEL_RECORD 0xf3 // 實時上傳記錄確認
//------------------------------------------------------------------

// 返回信息代碼
#define HDA_SUCCESS 0x08 // 命令執行成功
#define HDA_FAIL 0x09 // 命令執行失敗

#define CF_BOARDADR 0xaaaa // 通訊廣播地址
#define CF_BOOT 0x0f // 通訊通訊引導字節
#define CF_START 0xab // 通訊幀前導字節

// 通訊收發狀態表
#define CS_RECEIVE_STANDBY 0x02 // 接收等待
#define CS_RECEIVE_START2 0x04 // 接收幀頭
#define CS_RECEIVE_TAGADR1 0x06 // 接收目的地址
#define CS_RECEIVE_TAGADR2 0x00 // 接收目的地址
#define CS_RECEIVE_SRCADR1 0x05 // 接收來源地址
#define CS_RECEIVE_SRCADR2 0x09 // 接收來源地址
#define CS_RECEIVE_LOF1 0x07 // 接收幀長度字節1
#define CS_RECEIVE_LOF2 0x08 // 接收幀長度字節2
#define CS_RECEIVE_FRAME 0x0a // 接收幀內容
#define CS_RECEIVE_CHKSUM 0x0c // 接收校驗和
#define CS_RECEIVE_PARITYERR 0x0e // 接收校驗錯

struct ATTGUARDREC // 考勤記錄結構
{
char strPersonID[10];// 工號
//int nCardID
// 卡號
int nYear, nMonth, nDay, nHour, nMinute, nSecond
// 年,月,日,時,分,秒
BOOL bOnDuty, bBC
// 上下班標志(bOnDuty):0-上班,1-下班; 黑名單標志(bBC):0-非黑名單,1-黑名單
int nDMAdr
// 機具地址
};

// 自定義ASCII字符表
static unsigned char acChar[64] =
{
0x20, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a,
0x5f, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d,
0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a
};

//------------------------------------------------------------------
class THDLC : public TObject
{
private:
TComm *m_pComm;
// TTimer *Timer;

public:
int nTagAddress, nMyAddress, nCommand;
int nSrcAddress;// 源地址
ATTGUARDREC AttGuardRecord[100];
BYTE CF_COMMAND;// 接收通訊命令字

public:
BOOL __fastcall HDLC_Send(TComm *, const String&amp;);
String __fastcall HDLC_Receive(BYTE);
int __fastcall GetAttGuardRecord(const String&amp;);
BOOL __fastcall DelRecord(TComm*, bool);
void __fastcall DriveMessage();

int __fastcall myStrToInt(String, bool);
char __fastcall myatoi(char);
int __fastcall StrToHex(String);
int __fastcall StrToHex4(String);
String __fastcall Hex4ToStr(int);
String __fastcall HexToStr(int);
int __fastcall IntToBCD(int);

__fastcall THDLC();
__fastcall ~THDLC();

protected:
};

//---------------------------------------------------------------------------
__fastcall THDLC::THDLC()
{ CF_COMMAND = 0 ;
nMyAddress = 256;
}
//---------------------------------------------------------------------------
__fastcall THDLC::~THDLC()
{
m_pComm = NULL;
}
//---------------------------------------------------------------------------
// 發送幀
BOOL __fastcall THDLC::HDLC_Send(TComm *pComm, const String&amp
sData)
{
String str;

if (!pComm)
return FALSE;
m_pComm = pComm;

// 發送引導段
str += (char)CF_BOOT;
str += (char)CF_BOOT;
// 發送幀頭
str += (char)CF_START;
str += (char)CF_START;
// 發送目的地址
str += (char)(nTagAddress >> 8);
str += (char)nTagAddress;
BYTE bChkSum = (BYTE)str[5];
bChkSum ^= (BYTE)str[6];
// 發送來源地址
str += (char)(nMyAddress >> 8);
str += (char)nMyAddress;
bChkSum ^= (BYTE)str[7];
bChkSum ^= (BYTE)str[8];
// 發送幀長度
int nLengthOfFrame = sData.Length() + 9;
str += (char)(nLengthOfFrame >> 8);
str += (char)nLengthOfFrame;
bChkSum ^= (BYTE)str[9];
bChkSum ^= (BYTE)str[10];
// 發送通訊密碼
for(int i=0
i<8
i++)
{
str += (char)0;
// bChkSum ^= (BYTE)0;
}
// 發送命令字
str += (char)nCommand;
bChkSum ^= (BYTE)str[19];
// 發送幀內容,校驗和
for(int i=1
i<=sData.Length()
i++)
{
str += sData;
bChkSum ^= (BYTE)sData;
}
str += (char)bChkSum;

m_pComm->WriteCommData(str.c_str(), str.Length());
return true;

}
//---------------------------------------------------------------------------
// 接收字節
String __fastcall THDLC::HDLC_Receive(BYTE bByte)
{
String sRecvData = &quot;&quot;;
static String sCommBuf;
static int nCommunState=CS_RECEIVE_STANDBY, nTranCount;
static BYTE bChkSum, nTagAdr, nSrcAdr;
DriveMessage();
switch (nCommunState)
{
case CS_RECEIVE_STANDBY: // 接收等待
nCommunState = (bByte==CF_START) ? CS_RECEIVE_START2 : CS_RECEIVE_STANDBY;
break;
case CS_RECEIVE_START2: // 接收幀頭
nCommunState = (bByte==CF_START) ? CS_RECEIVE_TAGADR1 : CS_RECEIVE_STANDBY;
break;
case CS_RECEIVE_TAGADR1: // 接收目的地址
bChkSum = bByte;
nTagAdr = bByte << 8;
nCommunState = CS_RECEIVE_TAGADR2;
break;
case CS_RECEIVE_TAGADR2: // 接收目的地址
bChkSum ^= bByte;
nTagAdr += bByte;
nCommunState = CS_RECEIVE_SRCADR1;
break;
case CS_RECEIVE_SRCADR1: // 接收來源地址
bChkSum ^= bByte;
nSrcAdr = bByte << 8;
nCommunState = CS_RECEIVE_SRCADR2;
break;
case CS_RECEIVE_SRCADR2: // 接收來源地址
bChkSum ^= bByte;
nSrcAdr += bByte;
nSrcAddress = nSrcAdr;
nCommunState = CS_RECEIVE_LOF1;
break;
case CS_RECEIVE_LOF1: // 接收幀長度字節1
bChkSum ^= bByte;
nTranCount = bByte << 8;
nCommunState = CS_RECEIVE_LOF2;
break;
case CS_RECEIVE_LOF2: // 接收幀長度字節2
bChkSum ^= bByte;
nTranCount += bByte;
//nTranCount = 0x0a;
///////////////////////// // 限制幀長
if (nTranCount > 6000)
nCommunState = CS_RECEIVE_STANDBY;
/////////////////////////
sCommBuf = &quot;&quot;;
nCommunState = CS_RECEIVE_FRAME;
break;
case CS_RECEIVE_FRAME: // 接收幀內容
bChkSum ^= bByte;
sCommBuf += (char)bByte;
if (--nTranCount==0)
nCommunState = CS_RECEIVE_CHKSUM;
break;
case CS_RECEIVE_CHKSUM: // 接收校驗和
nCommunState = CS_RECEIVE_STANDBY;
if (bChkSum==bByte)
{
sRecvData = sCommBuf;
CF_COMMAND = sRecvData[1];
}
break
////////
default:
nCommunState = CS_RECEIVE_STANDBY;
break
///////
}
return sRecvData;
}
//---------------------------------------------------------------------------
// 考勤門禁記錄字符串->結構
int __fastcall THDLC::GetAttGuardRecord(const String&amp
sData)
{
int nCount = (sData.Length() - 1) / 9;
int nRemain = (sData.Length() - 1) % 9;
BYTE bTemp, bTemp2;

if (nRemain != 0)
return 0;

for (int i = 0
i < nCount
i ++)
{
//-----解析工號成ASCII碼----------------------------------
BYTE bTmp1[4];
BYTE bTmp[10];
char strTmp;
int iLoop,jLoop;
int m,n;
bTmp1[0]=sData[2];
bTmp1[1]=sData[3];
bTmp1[2]=sData[4];
bTmp1[3]=sData[5];
// bTmp1[0]=5;
// bTmp1[1]=0;
// bTmp1[2]=0;
// bTmp1[3]=0;
iLoop= bTmp1[0]*256*256*256+ bTmp1[1]*256*256 + bTmp1[2]*256+bTmp1[3];
//iLoop = 2;
bTmp[0]=iLoop/1000000000;
jLoop = iLoop%1000000000;
bTmp[1]=jLoop/100000000;
iLoop = jLoop%100000000;
bTmp[2]=iLoop/10000000;
jLoop = iLoop%10000000;
bTmp[3]=jLoop/1000000;
iLoop = jLoop%1000000;
bTmp[4]=iLoop/100000;
jLoop = iLoop%100000;
bTmp[5]=jLoop/10000;
iLoop = jLoop%10000;
bTmp[6]=iLoop/1000;
jLoop = iLoop%1000;
bTmp[7]=jLoop/100;
iLoop = jLoop%100;
bTmp[8]=iLoop/10;
jLoop = iLoop%10;
bTmp[9]=jLoop;

for(m=0;m<=9;m++)
AttGuardRecord.strPersonID[m] = bTmp[m]+'0';
//AttGuardRecord.nCardID = myStrToInt(sData.SubString(3 + 9 * i, 4), true);
AttGuardRecord.nYear = 2000 + myStrToInt(sData.SubString(6 + 9 * i, 1), true);
bTemp = myStrToInt(sData.SubString(7 + 9 * i, 1), true);
AttGuardRecord.nMonth = (int)(bTemp >> 4);
bTemp2 = myStrToInt(sData.SubString(8 + 9 * i, 1), true);
AttGuardRecord.nDay = (int)(((bTemp &amp
0x0f) << 1) | (bTemp2 >> 7));
AttGuardRecord.nHour = (int)((bTemp2 &amp
0x7f) >> 2);
bTemp = myStrToInt(sData.SubString(9 + 9 * i, 1), true);
AttGuardRecord.nMinute = (int)(((bTemp2 &amp
0x03) << 4) | (bTemp >> 4));
bTemp2 = myStrToInt(sData.SubString(10 + 9 * i, 1), true);
AttGuardRecord.nSecond = (int)(((bTemp &amp
0x0f) << 2) | (bTemp2 >> 6));

bTemp = myStrToInt(sData.SubString(10 + 9 * i, 1), true);
if ((bTemp &amp
0x20) == 0)
AttGuardRecord.bOnDuty = false;
else
AttGuardRecord.bOnDuty = true;

if ((bTemp &amp
0x10) == 0)
AttGuardRecord.bBC = false;
else
AttGuardRecord.bBC = true;
AttGuardRecord.nDMAdr = nSrcAddress;
}

return nCount;
}
//---------------------------------------------------------------------------
// 刪除記錄
BOOL __fastcall THDLC::DelRecord(TComm* pComm, bool bReal)
{
nTagAddress = nSrcAddress;
if (bReal)
nCommand = HD_UPDEL_RECORD;
else
nCommand = HD_RECVDELRECORDRLT;
String sCmd = &quot;&quot;;//(char)HDA_SUCCESS;
return HDLC_Send(pComm, sCmd);
}
//---------------------------------------------------------------------------
// 消息驅動
void __fastcall THDLC::DriveMessage()
{
MSG msg;
while (PeekMessage(&amp;msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
return;
TranslateMessage(&amp;msg);
DispatchMessage(&amp;msg);
}
}
//---------------------------------------------------------------------------
// String to Int function
// bool bUnsigned: 最高位是否為符號位
int __fastcall THDLC::myStrToInt(String sData, bool bUnsigned)
{
int nTemp = 0;
if (bUnsigned)
{
for (int i = 1
i < sData.Length() + 1
i ++)
nTemp = nTemp * 256 + (BYTE)sData;
}
else
{
if (((BYTE)sData[1] &amp
0x80) == 0x80)// 返回負數
{
nTemp = (BYTE)sData[1] - 0x80;
for (int i = 2
i < sData.Length() + 1
i ++)
nTemp = nTemp * 256 + (BYTE)sData;
nTemp = - nTemp;
}
else// 返回正數
{
for (int i = 1
i < sData.Length() + 1
i ++)
nTemp = nTemp * 256 + (BYTE)sData;
}
}
return nTemp;
}
//---------------------------------------------------------------------------
char __fastcall THDLC::myatoi(char chChar)
{
if (chChar>='0' &amp;&amp
chChar<='9')
return (char)(chChar - '0');
else if (chChar>='a' &amp;&amp
chChar<='f')
return (char)(chChar - 'a' + 10);
else if (chChar>='A' &amp;&amp
chChar<='F')
return (char)(chChar - 'A' + 10);
else
return 0;
}
//---------------------------------------------------------------------------
int __fastcall THDLC::StrToHex(String sSrc)
{
if (sSrc.Length()<2)
return 0;
else
return myatoi(sSrc[1]) * 0x10 + myatoi(sSrc[2]);
}
//---------------------------------------------------------------------------
int __fastcall THDLC::StrToHex4(String sSrc)
{
if (sSrc.Length()<4)
return 0;
else
return myatoi(sSrc[1]) * 0x1000 + myatoi(sSrc[2]) * 0x100
+ myatoi(sSrc[3]) * 0x10 + myatoi(sSrc[4]);
}
//---------------------------------------------------------------------------
String __fastcall THDLC::Hex4ToStr(int n)
{
String str;
char ch[5];
wsprintf(ch, &quot;%02x&quot;, (BYTE)(n / 0x100));
str = ch;
wsprintf(ch, &quot;%02x&quot;, (BYTE)n);
str += ch;
return str;
}
//---------------------------------------------------------------------------
String __fastcall THDLC::HexToStr(int n)
{
String str;
char ch[10];
wsprintf(ch, &quot;%02x&quot;, (BYTE)n);
str = ch;
return str;
}
//---------------------------------------------------------------------------
int __fastcall THDLC::IntToBCD(int nSrc)
{
return nSrc / 10 * 16 + nSrc % 10;
}
//---------------------------------------------------------------------------
 
这么多,看了就晕.转换没什么困难.太多了.懒得转.
 
9494,太多,我就是用C++ Builder,它资料太少,就经常看Delphi的资料,二者差不太多!
 
楼上的两位能否帮助转换一下呢?很急啊,谢谢!
 
自由界面和报表的完美解决方案!
http://www.anylib.com
 
都不知道啊?不是吧
 
小弟虽然学了点B++Builder但是楼主的问题解决不了,
帮楼主顶一下,也跟着学学
 
bChkSum := Byte(Str[5]);
bChkSum := bChkSum xor Byte(Str[6]);
 
一般懂的人都是很少废话[:D]
 
你的最后的FreeMem(pRecvByte);因为动态数组是自动管理的,把它去掉。只用使用GetMen分配的内存采用FreeMem释放。如果你想手动释放动态数组,可以用Finalize过程
 
大致是这样的,另外说一句,不要等别人回答后改变问题内容

pRecvByte : PByte;
p : PByte;
j : Integer;

pRecvByte := GetMem(BufferLength);
p := PRecvByte;
CopyMemory(pRecvByte, Buffer, BufferLength);
for j = 0 to BufferLength-1 do
begin
m_strRecvData = m_pHDLC^.HDLC_Receive(p^);
Inc(p);
end;

FreeMem(pRecvByte);
 
謝謝tseug的解答!
 
顶部