RFDlg.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // RFDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "RF.h"
  5. #include "RFDlg.h"
  6. #include "mwrf32.h"
  7. #pragma comment(lib,"mwrf32.lib")
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CAboutDlg dialog used for App About
  15. HANDLE icdev=0;
  16. int st;
  17. unsigned char sector;
  18. class CAboutDlg : public CDialog
  19. {
  20. public:
  21. CAboutDlg();
  22. // Dialog Data
  23. //{{AFX_DATA(CAboutDlg)
  24. enum { IDD = IDD_ABOUTBOX };
  25. //}}AFX_DATA
  26. // ClassWizard generated virtual function overrides
  27. //{{AFX_VIRTUAL(CAboutDlg)
  28. protected:
  29. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  30. //}}AFX_VIRTUAL
  31. // Implementation
  32. protected:
  33. //{{AFX_MSG(CAboutDlg)
  34. //}}AFX_MSG
  35. DECLARE_MESSAGE_MAP()
  36. };
  37. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  38. {
  39. //{{AFX_DATA_INIT(CAboutDlg)
  40. //}}AFX_DATA_INIT
  41. }
  42. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  43. {
  44. CDialog::DoDataExchange(pDX);
  45. //{{AFX_DATA_MAP(CAboutDlg)
  46. //}}AFX_DATA_MAP
  47. }
  48. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  49. //{{AFX_MSG_MAP(CAboutDlg)
  50. // No message handlers
  51. //}}AFX_MSG_MAP
  52. END_MESSAGE_MAP()
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CRFDlg dialog
  55. CRFDlg::CRFDlg(CWnd* pParent /*=NULL*/)
  56. : CDialog(CRFDlg::IDD, pParent)
  57. {
  58. //{{AFX_DATA_INIT(CRFDlg)
  59. m_data = _T("");
  60. m_key = _T("");
  61. m_cmd = _T("");
  62. //}}AFX_DATA_INIT
  63. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  64. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  65. }
  66. void CRFDlg::DoDataExchange(CDataExchange* pDX)
  67. {
  68. CDialog::DoDataExchange(pDX);
  69. //{{AFX_DATA_MAP(CRFDlg)
  70. DDX_Control(pDX, IDC_COMBO_SECTOR, m_sector);
  71. DDX_Control(pDX, IDC_COMBO_MODE, m_mode);
  72. DDX_Control(pDX, IDC_COMBO_PORT, m_port);
  73. DDX_Control(pDX, IDC_COMBO_BAUD, m_baud);
  74. DDX_Control(pDX, IDC_LIST1, m_list);
  75. DDX_Text(pDX, IDC_EDIT_M1DATA, m_data);
  76. DDV_MaxChars(pDX, m_data, 16);
  77. DDX_Text(pDX, IDC_EDIT_KEY, m_key);
  78. DDV_MaxChars(pDX, m_key, 12);
  79. DDX_Text(pDX, IDC_EDIT_CMD, m_cmd);
  80. DDV_MaxChars(pDX, m_cmd, 225);
  81. //}}AFX_DATA_MAP
  82. }
  83. BEGIN_MESSAGE_MAP(CRFDlg, CDialog)
  84. //{{AFX_MSG_MAP(CRFDlg)
  85. ON_WM_SYSCOMMAND()
  86. ON_WM_PAINT()
  87. ON_WM_QUERYDRAGICON()
  88. ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
  89. ON_BN_CLICKED(IDC_BUTTON_EXIT, OnButtonExit)
  90. ON_BN_CLICKED(IDC_BUTTON_CARD, OnButtonCard)
  91. ON_BN_CLICKED(IDC_BUTTON_CHECKKEY, OnButtonCheckkey)
  92. ON_BN_CLICKED(IDC_BUTTON_READ, OnButtonRead)
  93. ON_BN_CLICKED(IDC_BUTTON_WRITE, OnButtonWrite)
  94. ON_BN_CLICKED(IDC_BUTTON_READ2, OnButtonRead2)
  95. ON_BN_CLICKED(IDC_BUTTON_CPUCARD, OnButtonCpucard)
  96. ON_BN_CLICKED(IDC_BUTTON_RESET, OnButtonReset)
  97. ON_BN_CLICKED(IDC_BUTTON_CMD, OnButtonCmd)
  98. //}}AFX_MSG_MAP
  99. END_MESSAGE_MAP()
  100. /////////////////////////////////////////////////////////////////////////////
  101. // CRFDlg message handlers
  102. BOOL CRFDlg::OnInitDialog()
  103. {
  104. CDialog::OnInitDialog();
  105. // Add "About..." menu item to system menu.
  106. // IDM_ABOUTBOX must be in the system command range.
  107. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  108. ASSERT(IDM_ABOUTBOX < 0xF000);
  109. CMenu* pSysMenu = GetSystemMenu(FALSE);
  110. if (pSysMenu != NULL)
  111. {
  112. CString strAboutMenu;
  113. strAboutMenu.LoadString(IDS_ABOUTBOX);
  114. if (!strAboutMenu.IsEmpty())
  115. {
  116. pSysMenu->AppendMenu(MF_SEPARATOR);
  117. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  118. }
  119. }
  120. // Set the icon for this dialog. The framework does this automatically
  121. // when the application's main window is not a dialog
  122. SetIcon(m_hIcon, TRUE); // Set big icon
  123. SetIcon(m_hIcon, FALSE); // Set small icon
  124. // TODO: Add extra initialization here
  125. m_port.SetCurSel(0);
  126. m_baud.SetCurSel(0);
  127. m_mode.SetCurSel(0);
  128. m_sector.SetCurSel(0);
  129. m_data="Ã÷»ª°Äººµç×ӿƼ¼";
  130. UpdateData(FALSE);
  131. return TRUE; // return TRUE unless you set the focus to a control
  132. }
  133. void CRFDlg::OnSysCommand(UINT nID, LPARAM lParam)
  134. {
  135. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  136. {
  137. CAboutDlg dlgAbout;
  138. dlgAbout.DoModal();
  139. }
  140. else
  141. {
  142. CDialog::OnSysCommand(nID, lParam);
  143. }
  144. }
  145. // If you add a minimize button to your dialog, you will need the code below
  146. // to draw the icon. For MFC applications using the document/view model,
  147. // this is automatically done for you by the framework.
  148. void CRFDlg::OnPaint()
  149. {
  150. if (IsIconic())
  151. {
  152. CPaintDC dc(this); // device context for painting
  153. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  154. // Center icon in client rectangle
  155. int cxIcon = GetSystemMetrics(SM_CXICON);
  156. int cyIcon = GetSystemMetrics(SM_CYICON);
  157. CRect rect;
  158. GetClientRect(&rect);
  159. int x = (rect.Width() - cxIcon + 1) / 2;
  160. int y = (rect.Height() - cyIcon + 1) / 2;
  161. // Draw the icon
  162. dc.DrawIcon(x, y, m_hIcon);
  163. }
  164. else
  165. {
  166. CDialog::OnPaint();
  167. }
  168. }
  169. // The system calls this to obtain the cursor to display while the user drags
  170. // the minimized window.
  171. HCURSOR CRFDlg::OnQueryDragIcon()
  172. {
  173. return (HCURSOR) m_hIcon;
  174. }
  175. void CRFDlg::OnButtonConnect()
  176. {
  177. // TODO: Add your control notification handler code here
  178. CString str;
  179. int port,baud;
  180. unsigned char ver[20];
  181. GetDlgItemText(IDC_COMBO_PORT,str);
  182. port=atoi(str.Mid(4,1));
  183. baud=GetDlgItemInt(IDC_COMBO_BAUD);
  184. icdev=rf_init(port-1,baud);
  185. if (icdev<=0)
  186. m_list.AddString("Connect failed");
  187. ZeroMemory(ver,20);
  188. st=rf_get_status(icdev,ver);
  189. if (st==0)
  190. m_list.AddString((CHAR*)ver);
  191. else
  192. m_list.AddString("Read the version number failed");
  193. char* key="ffffffffffff";
  194. for(sector=0;sector<16;sector++)
  195. {
  196. st=rf_load_key_hex(icdev,0,sector,key);
  197. if(st!=0)
  198. {
  199. CString str;
  200. str.Format("%d sector load key error!",sector);
  201. m_list.AddString(str);
  202. }
  203. }
  204. rf_beep(icdev,30);
  205. }
  206. void CRFDlg::OnButtonExit()
  207. {
  208. // TODO: Add your control notification handler code here
  209. rf_exit(icdev);
  210. m_list.AddString("Disconnect reader");
  211. }
  212. void CRFDlg::OnButtonCard()
  213. {
  214. // TODO: Add your control notification handler code here
  215. unsigned long snr=0;
  216. unsigned char mode=GetDlgItemInt(IDC_COMBO_MODE);
  217. st=rf_card(icdev,mode,&snr);
  218. if (st!=0)
  219. {
  220. m_list.AddString("Could not find the card");
  221. }
  222. else
  223. {
  224. char CardNumber[10];
  225. ZeroMemory(CardNumber,10);
  226. hex_a((unsigned char*)&snr,CardNumber,sizeof(snr));
  227. CString str="Card Number: ";
  228. str+=CardNumber;
  229. m_list.AddString(str);
  230. }
  231. }
  232. void CRFDlg::OnButtonCheckkey()
  233. {
  234. // TODO: Add your control notification handler code here
  235. sector=GetDlgItemInt(IDC_COMBO_SECTOR);
  236. st=rf_authentication(icdev,0,sector);
  237. if (st!=0)
  238. m_list.AddString("Check password failure");
  239. else
  240. m_list.AddString("Check password ok!");
  241. }
  242. void CRFDlg::OnButtonRead()
  243. {
  244. // TODO: Add your control notification handler code here
  245. unsigned char rdata[17];
  246. ZeroMemory(rdata,17);
  247. st=rf_read(icdev,sector*4+1,rdata);
  248. if(st!=0)
  249. m_list.AddString("Read data error!");
  250. else
  251. m_list.AddString((char*)rdata);
  252. }
  253. void CRFDlg::OnButtonWrite()
  254. {
  255. // TODO: Add your control notification handler code here
  256. UpdateData();
  257. char* wdata=(LPSTR)(LPCTSTR)m_data;
  258. st=rf_write(icdev,sector*4+1,(unsigned char*)wdata);
  259. if (st!=0)
  260. m_list.AddString("Write data error!");
  261. else
  262. m_list.AddString("Write data succeed!");
  263. }
  264. void CRFDlg::OnButtonRead2()
  265. {
  266. // TODO: Add your control notification handler code here
  267. UpdateData();
  268. for (int i=0;i<m_key.GetLength();i++)
  269. {
  270. if(m_key[i]<'0'|| m_key[i]>'f' )
  271. {
  272. MessageBox("Please enter 0 ~ 9, a ~ f between the data!");
  273. return;
  274. }
  275. }
  276. if (m_key.GetLength()<12)
  277. {
  278. MessageBox("Please input 12 data!");
  279. return;
  280. }
  281. char* key=(LPSTR)(LPCTSTR)m_key;
  282. unsigned char wkey[7];
  283. ZeroMemory(wkey,7);
  284. a_hex(key,wkey,12);
  285. st=rf_changeb3(icdev,sector,wkey,0,0,0,1,0,wkey);
  286. if (st!=0)
  287. m_list.AddString("Change key error!");
  288. else
  289. m_list.AddString("Change key ok!");
  290. }
  291. void CRFDlg::OnButtonCpucard()
  292. {
  293. // TODO: Add your control notification handler code here
  294. unsigned long snr=0;
  295. st=rf_card(icdev,0,&snr);
  296. if (st!=0)
  297. {
  298. m_list.AddString("Could not find the card");
  299. }
  300. else
  301. {
  302. char CardNumber[10];
  303. ZeroMemory(CardNumber,10);
  304. hex_a((unsigned char*)&snr,CardNumber,sizeof(snr));
  305. CString str="Card Number: ";
  306. str+=CardNumber;
  307. m_list.AddString(str);
  308. }
  309. }
  310. void CRFDlg::OnButtonReset()
  311. {
  312. // TODO: Add your control notification handler code here
  313. unsigned char resetData[50];
  314. char resetDataHex[100];
  315. ZeroMemory(resetData,50);
  316. ZeroMemory(resetDataHex,100);
  317. st=rf_pro_rst(icdev,resetData);
  318. if (st!=0)
  319. {
  320. m_list.AddString("CPU card reset failed!");
  321. }
  322. else
  323. {
  324. hex_a(resetData,resetDataHex,resetData[0]);
  325. m_list.AddString(resetDataHex);
  326. }
  327. }
  328. void CRFDlg::OnButtonCmd()
  329. {
  330. // TODO: Add your control notification handler code here
  331. UpdateData();
  332. int len=m_cmd.GetLength()/2;
  333. CString str;
  334. str.Format("%x",len);
  335. if (len<=15)
  336. {
  337. str="0000000"+str+m_cmd;
  338. }
  339. else
  340. {
  341. str="000000"+str+m_cmd;
  342. }
  343. char* cmd=(LPSTR)(LPCTSTR)str;
  344. unsigned char cmdhex[225];
  345. ZeroMemory(cmdhex,225);
  346. a_hex(cmd,cmdhex,str.GetLength());
  347. unsigned char returnData[225];
  348. st=rf_pro_trn(icdev,cmdhex,returnData);
  349. if (st!=0)
  350. {
  351. m_list.AddString("CPU card operation failed!");
  352. }
  353. else
  354. {
  355. char returnDatabuff[500];
  356. ZeroMemory(returnDatabuff,0);
  357. hex_a(returnData,returnDatabuff,returnData[3]+4);
  358. m_list.AddString(returnDatabuff);
  359. }
  360. }