谁能把这段C++代码用Delphi写出我,我不懂C++,重谢(200)

  • 主题发起人 主题发起人
  • 开始时间 开始时间

Unregistered / Unconfirmed
GUEST, unregistred user!
DWORD WINAPIdo
ProcThread(LPVOID p ){ CWriteTagDlg *pDlg = (CWriteTagDlg*)p; BYTE pEPCData[200]; int reReadTimes=0; while( !bTheadExit ) { BYTE antenna =1; bExit = false;
TRACE("%s/n","進入等待/n");// if( WAIT_OBJECT_0 == WaitForSingleObject(hRecvSingle,INFINITE) )// { TRACE("%s/n","等待完了/n"); //發送讀卡命令 Sleep(2000); if(!SAAT_6CReadEPCCode(pDlg->m_pHandle,antenna,0x01,1)) { MessageBox(NULL,"發送讀卡命令失敗,請重啟程序!","提示",MB_OK); bTheadExit=true; continue; } TRACE("%s/n","發送讀卡命令成功/n");
//開始讀卡 ZeroMemory(pEPCData,200); BYTE pEPCLength = 200; CString strEpc; bool bWriteState = false; while( !bExit) { strEpc = ""; bWriteState = false;
int readEPCSucceed=SAAT_6CRevEPCMsg(pDlg->m_pHandle,&antenna,pEPCData,&pEPCLength); TRACE("讀卡返回:%d ,長度:%d/n",readEPCSucceed,pEPCLength); if( 1 == readEPCSucceed && pEPCLength == 0 ) { SAAT_PowerOff(pDlg->m_pHandle);// 有異常了 bExit = true; break; } if(1 == readEPCSucceed && pEPCLength != 0 ) { for( int i = 0 i < pEPCLength i ++) { CString strTemp strTemp.Format("%02x",pEPCData); strEpc +=strTemp; } //來一張寫一張 EpcIter iter; //if ((iter=m_mapReadEpcCard.find(strEpc)) != m_mapReadEpcCard.end()) //{ // continue; //}// 註銷掉是否已發卡的判斷2009-11-30 ////新寫入的卡不理// if ((iter=m_mapWriteEpcCard.find(strEpc)) != m_mapWriteEpcCard.end())// { // continue;// }//結束註銷2009-11-30 //Beep(1000,50); TRACE("%s","讀到卡了/n"); //1.讀到一張,停止讀卡 SAAT_PowerOff(pDlg->m_pHandle); TRACE("%s","關閉讀卡/n"); //Sleep(1000); //2.寫卡操作 int nSize = pDlg->m_strNowWriteCard.GetLength(); TRACE("卡號長度:%d/n",nSize); BYTE nType = 0x00; BYTE btPsw[4] ={0,0,0,0}; BYTE btEcp[52]; memcpy(btEcp,(char*)(LPCTSTR)pDlg->m_strNowWriteCard,nSize); int reWriteTimes=0; for(reWriteTimes = 0 reWriteTimes < pDlg->m_nReWriteCount reWriteTimes++ ) { bWriteState = SAAT_6CWriteEPCCode ( pDlg->m_pHandle, 1, nType, btPsw, btEcp, nSize ); if( bWriteState ) { Beep(3500, 50); //Sleep(3000); bool pushcard=SAAT_Auto_Push_OK(pDlg->m_pHandle,antenna); if(pushcard) TRACE("%s/n","發卡成功吐卡成功!/n"); else
TRACE("%s/n","發卡成功吐卡失敗!/n"); break; } Sleep(200); }//判斷是否循環了最大次數,如果是最大次數就說明連續多次發卡失敗,直接吐出卡片 if(reWriteTimes>=pDlg->m_nReWriteCount) { bool pushcard=SAAT_Auto_Push(pDlg->m_pHandle,antenna); TRACE("連續發卡%d次失敗,直接吐出卡片/n",reWriteTimes); if(pushcard) TRACE("%s/n","發卡失敗吐卡成功/n"); else
TRACE("%s/n","發卡失敗吐卡失敗/n"); } //3.完成發消息 SendMessage(pDlg->m_hWnd,WM_INSERT_LIST,(WPARAM)bWriteState,(LPARAM)&strEpc); TRACE("%s/n","操作完,發消息了/n"); bExit = true;// Sleep(1500);}/[= } reReadTimes++; if(0 == readEPCSucceed && reReadTimes>16)//長時間沒讀到卡 { SAAT_PowerOff(pDlg->m_pHandle);// 有異常了 bool checkcard=SAAT_Auto_CheckCard(pDlg->m_pHandle,antenna); if(checkcard)//有卡,是死卡 { bool pushcard=SAAT_Auto_Push(pDlg->m_pHandle,antenna); TRACE("有壞卡,直接吐出卡片/n"); if(pushcard) TRACE("%s/n","有壞卡,吐卡成功"); else
TRACE("%s/n","有壞卡,吐卡失敗"); bExit = true; } else
//沒卡,就檢查卡箱有沒卡 { bool checkcardbox=true; TRACE("%s/n","讀卡位置沒卡,檢查卡箱"); while(checkcardbox) { bool checkcardbox=SAAT_Auto_CheckCardBox(pDlg->m_pHandle,antenna); if(bTheadExit || checkcardbox) { TRACE("%s/n","卡箱有卡或程序要求退出"); bExit = true; break;//卡箱有卡 } if(!checkcardbox) { TRACE("%s/n","卡箱無卡,等待再查詢卡箱"); Sleep(2000); } } } reReadTimes=0; } Sleep(20); } //} Sleep(20); } SetEvent(hThreadExitEvent); TRACE("%s/n","線程退出了..."); return 0;}
 
代码好乱,写代码的人系统开发功力不够哦线程退出还需要设置信号?外部直接等待线程句柄即可。
 
能否帮我用Delphi整理一下,谢谢
 
乱,真乱。。。看了几行,懒得看了不过这就是一个线程函数而已。。。他那个SETEVENT估计是同步用的?
 
期等那位兄弟帮忙,小弟感激不尽
 
无忌老大,看来得请你帮忙了 主要是这一段 SAAT_6CWriteEPCCode (phandle,1,0,pbyte(0000),pbyte('9B1211220123'),pbyte(12))传进去执行失败BYTE nType = 0x00;
BYTE btPsw[4] ={0,0,0,0};
BYTE btEcp[52];
memcpy(btEcp,(char*)(LPCTSTR)pDlg->m_strNowWriteCard,nSize);
int reWriteTimes=0;
for(reWriteTimes = 0 reWriteTimes < pDlg->m_nReWriteCount reWriteTimes++ ) { bWriteState = SAAT_6CWriteEPCCode ( pDlg->m_pHandle, 1, nType, btPsw, btEcp, nSize );
 
pbyte(0000)不就成了空指针了?这样当然出错。看来LZ没有搞清楚左右值的区别。。。var btPsw: array[0..3] of Byte;
nSize: .... .....begin
FillChar(btPsw, SizeOf(btPsw), 0);
nSize:= 12;
.....SAAT_6CWriteEPCCode(phandle, 1, 0, @btPsw[0], ...., @nSize);
 
谢谢 bahamut8348 我明天试一下,让你见笑了,我对C++不熟悉,非常感谢你的QQ或MSN是多少,我们交流一下,我很长时间没搞这个了,一直在研究Oracle
 
以下這樣寫有問題嗎,我改了之後有時會執行成功,大多數失敗是不是問題出在pbyte('9B1211220123')這里, var btPsw: array[0..4] of Byte;
nSize: .... .....begin
FillChar(btPsw, SizeOf(btPsw), 0);
nSize:= 12;
.....SAAT_6CWriteEPCCode(phandle, 1, 0, @btPsw[0], pbyte('9B1211220123'), @nSize);
 
多添加一个变量var s: string;s:= '9B1211220123';SAAT_6CWriteEPCCode(phandle, 1, 0, @btPsw[0], pbyte(@s[1]), @nSize);
 
我定義nsize 為 integer帶入參數@nsize 會報 incompatible types:'byte' and 'pointer' 我改成byte(@nsize)就可以
 
我按你的要求改,但還是會出現有時成功,有時失敗,失敗的機率大,不知為什麼
 
我不知道你最后一个参数到底是什么类型的,BYTE类型的?如果是BYTE类型的话,就不需要取地址了。直接传nsize进去就可以了
 
function _6CWriteEPCCode(pHandle:Pointer;nAntenna:Byte;nType:Byte;pAccessPWD:PByte;pWriteData:PByte;nLen:Byte):Boolean;stdcall;external 'RFIDAPI.dll' name 'SAAT_6CWriteEPCCode';我是這樣調用的,最後一個是byte類型
 
自己去调吧:unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;const WM_INSERT_LIST = WM_USER + 1;type TForm1 = class(TForm) Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private m_strNowWriteCard : string;
m_mapReadEpcCard, m_mapWriteEpcCard : TStringList;
{ Private declarations } public { Public declarations } end;
var Form1: TForm1;
function SAAT_6CReadEPCCode(hParent: THandle;
antenna: Integer;
Operate: Integer;
Flag: Integer): Boolean;
stdcall;external 'RFIDAPI.dll' function SAAT_6CRevEPCMsg( hParent: THandle;
pantenna: PInteger;
pData: PByte;
pLen: PInteger): Integer;
stdcall;
external 'RFIDAPI.dll' procedure SAAT_PowerOff(hParent: THandle);
external 'RFIDAPI.dll' function SAAT_6CWriteEPCCode(hParent:THandle;
antenna:Integer;
nType:Byte;
pAccessPWD:PByte;
pWriteData:PByte;nLen:Integer):Boolean;
stdcall;
external 'RFIDAPI.dll' function SAAT_Auto_Push_OK(hParent: THandle;
antenna: Integer): Boolean;
stdcall;
external 'RFIDAPI.dll' implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);var nSize : Integer;
bWriteState, pushcard: Boolean;
antenna, EPCLength: BYTE;
reReadTimes, readEPCSucceed, i: Integer;
EPCData: array [0..200-1] of Byte;
strEpc, strTemp: string;
nType : BYTE;
btPsw : array [0..4-1] of Byte;
btEcp: array [0..52-1] of Byte;
begin
reReadTimes := 0;
//是一个retry 过程 antenna := 1;
Sleep(2000);
// bad;
if( not SAAT_6CReadEPCCode(Self.Handle, antenna, $01, 1)) then
begin
ShowMessage('發送讀卡命令失敗,請重啟程序!');
Exit;
end;
ZeroMemory(@EPCData, 200);
EPCLength := 200;
bWriteState := false;
strEpc := '';
bWriteState := false;
readEPCSucceed := SAAT_6CRevEPCMsg(Self.Handle, @antenna, @EPCData[0], @EPCLength);
if( 1 <> readEPCSucceed) or (EPCLength = 0 ) then
begin
SAAT_PowerOff(Self.Handle);// 有異常了 Exit;
end;
for i := 0 to EPCLength - 1do
begin
strTemp := IntToHex(EPCData, 16);
strEpc := strEpc + strTemp;
end;
if m_mapReadEpcCard.IndexOf(strEpc) <0 then
begin
Exit;
end;
if m_mapWriteEpcCard.IndexOf(strEpc) <0 then
begin
Exit;
end;
SAAT_PowerOff(Self.Handle);
nSize := Length(m_strNowWriteCard);
nType := $0;
FillMemory(@btPsw[0], SizeOf(btPsw), 0);
CopyMemory(@btEcp[0], @m_strNowWriteCard[1], nSize);
bWriteState := SAAT_6CWriteEPCCode ( Self.Handle, 1, nType, @btPsw[0], @btEcp[0], nSize );
if( bWriteState ) then
begin
pushcard := SAAT_Auto_Push_OK(Self.Handle, antenna);
if(pushcard) then
ShowMessage('發卡成功吐卡成功') else
ShowMessage('發卡成功吐卡失敗');
end;
SendMessage(Self.Handle, WM_INSERT_LIST, Integer(bWriteState), Integer(@strEpc));
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
m_mapReadEpcCard := TStringList.Create;
m_mapWriteEpcCard := TStringList.Create;
end;
end.
 
非常感谢Dstorm ,谢谢你的辛苦,你在那里,有机会请你吃饭,呵呵....我先调调看,有问题再请教[:)]
 
这里真热闹,这个贴一下沉一下了,自己顶一下
 
多人接受答案了。
 
后退
顶部