全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1943
推到 Plurk!
推到 Facebook!

這段C++Builder如何轉為Delphi啊?謝謝各位大大

答題得分者是:Zard
a185a185
一般會員


發表:4
回覆:4
積分:1
註冊:2005-01-31

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-10-31 16:24:54 IP:221.4.xxx.xxx 未訂閱
#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& 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[i]; bChkSum ^= (BYTE)sData[i]; } str = (char)bChkSum; m_pComm->WriteCommData(str.c_str(), str.Length()); return true; } //--------------------------------------------------------------------------- // 接收字節 String __fastcall THDLC::HDLC_Receive(BYTE bByte) { String sRecvData = ""; 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 = ""; 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& 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 = iLoop00000000; bTmp[1]=jLoop/100000000; iLoop = jLoop0000000; bTmp[2]=iLoop/10000000; jLoop = iLoop000000; bTmp[3]=jLoop/1000000; iLoop = jLoop00000; bTmp[4]=iLoop/100000; jLoop = iLoop0000; bTmp[5]=jLoop/10000; iLoop = jLoop000; bTmp[6]=iLoop/1000; jLoop = iLoop00; bTmp[7]=jLoop/100; iLoop = jLoop0; bTmp[8]=iLoop/10; jLoop = iLoop; bTmp[9]=jLoop; for(m=0;m<=9;m ) AttGuardRecord[i].strPersonID[m] = bTmp[m] '0'; //AttGuardRecord[i].nCardID = myStrToInt(sData.SubString(3 9 * i, 4), true); AttGuardRecord[i].nYear = 2000 myStrToInt(sData.SubString(6 9 * i, 1), true); bTemp = myStrToInt(sData.SubString(7 9 * i, 1), true); AttGuardRecord[i].nMonth = (int)(bTemp >> 4); bTemp2 = myStrToInt(sData.SubString(8 9 * i, 1), true); AttGuardRecord[i].nDay = (int)(((bTemp & 0x0f) << 1) | (bTemp2 >> 7)); AttGuardRecord[i].nHour = (int)((bTemp2 & 0x7f) >> 2); bTemp = myStrToInt(sData.SubString(9 9 * i, 1), true); AttGuardRecord[i].nMinute = (int)(((bTemp2 & 0x03) << 4) | (bTemp >> 4)); bTemp2 = myStrToInt(sData.SubString(10 9 * i, 1), true); AttGuardRecord[i].nSecond = (int)(((bTemp & 0x0f) << 2) | (bTemp2 >> 6)); bTemp = myStrToInt(sData.SubString(10 9 * i, 1), true); if ((bTemp & 0x20) == 0) AttGuardRecord[i].bOnDuty = false; else AttGuardRecord[i].bOnDuty = true; if ((bTemp & 0x10) == 0) AttGuardRecord[i].bBC = false; else AttGuardRecord[i].bBC = true; AttGuardRecord[i].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 = "";//(char)HDA_SUCCESS; return HDLC_Send(pComm, sCmd); } //--------------------------------------------------------------------------- // 消息驅動 void __fastcall THDLC::DriveMessage() { MSG msg; while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) return; TranslateMessage(&msg); DispatchMessage(&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[i]; } else { if (((BYTE)sData[1] & 0x80) == 0x80)// 返回負數 { nTemp = (BYTE)sData[1] - 0x80; for (int i = 2; i < sData.Length() 1 ; i ) nTemp = nTemp * 256 (BYTE)sData[i]; nTemp = - nTemp; } else// 返回正數 { for (int i = 1; i < sData.Length() 1 ; i ) nTemp = nTemp * 256 (BYTE)sData[i]; } } return nTemp; } //--------------------------------------------------------------------------- char __fastcall THDLC::myatoi(char chChar) { if (chChar>='0' && chChar<='9') return (char)(chChar - '0'); else if (chChar>='a' && chChar<='f') return (char)(chChar - 'a' 10); else if (chChar>='A' && 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, "x", (BYTE)(n / 0x100)); str = ch; wsprintf(ch, "x", (BYTE)n); str = ch; return str; } //--------------------------------------------------------------------------- String __fastcall THDLC::HexToStr(int n) { String str; char ch[10]; wsprintf(ch, "x", (BYTE)n); str = ch; return str; } //--------------------------------------------------------------------------- int __fastcall THDLC::IntToBCD(int nSrc) { return nSrc / 10 * 16 nSrc % 10; } //---------------------------------------------------------------------------
Zard
尊榮會員


發表:24
回覆:396
積分:539
註冊:2003-11-26

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-11-02 22:36:06 IP:61.64.xxx.xxx 未訂閱
引言: CB函數中的^=相當於Delphi的哪個函數: BYTE bChkSum = (BYTE)str[5]; bChkSum ^= (BYTE)str[6]; 發表人 - a185a185 於 2005/11/02 20:59:33
A ^= B 就是 A = A ^ B, ^ 是C裡的XOR運算. 所以上面的碼相當於Delphi裡的 bChkSum := bChkSum xor BYTE(str[6]);
a185a185
一般會員


發表:4
回覆:4
積分:1
註冊:2005-01-31

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-11-04 10:31:29 IP:221.4.xxx.xxx 未訂閱
謝謝Zard的回答,再問一個小問題,問題如下: C B代碼如下: unsigned char *pRecvByte = new unsigned char[BufferLength]; CopyMemory(pRecvByte, Buffer, BufferLength); for (int j = 0; j < BufferLength; j ) m_strRecvData = m_pHDLC->HDLC_Receive(*(pRecvByte j)); delete []pRecvByte; 轉為Delphi為何出錯? pRecvByte:array of ^Char; SetLength(pRecvByte,BufferLength); CopyMemory(pRecvByte,Buffer, BufferLength); for J:=0 to BufferLength-1 do begin m_strRecvData := m_pHDLC.HDLC_Receive((pRecvByte[j])^) ; end; FreeMem(pRecvByte);
系統時間:2024-04-26 7:24:59
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!