demoDlg.cpp
上传用户:karykuang
上传日期:2010-02-26
资源大小:103k
文件大小:40k
源码类别:

TCP/IP协议栈

开发平台:

Visual C++

  1. // demoDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "demo.h"
  5. #include "demoDlg.h"
  6. #include "Ip.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /*typedef struct t_TREADDATA
  13. {
  14. HWND hWnd;
  15. HANDLE CommDev;
  16. }THREADDATA;
  17. UINT MyThreadProc(LPVOID pParam);
  18. int NEAR ReadCommBlock(HANDLE,LPSTR,int);*/
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CAboutDlg dialog used for App About
  21. class CAboutDlg : public CDialog
  22. {
  23. public:
  24. CAboutDlg();
  25. // Dialog Data
  26. //{{AFX_DATA(CAboutDlg)
  27. enum { IDD = IDD_ABOUTBOX };
  28. //}}AFX_DATA
  29. // ClassWizard generated virtual function overrides
  30. //{{AFX_VIRTUAL(CAboutDlg)
  31. protected:
  32. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  33. //}}AFX_VIRTUAL
  34. // Implementation
  35. protected:
  36. //{{AFX_MSG(CAboutDlg)
  37. //}}AFX_MSG
  38. DECLARE_MESSAGE_MAP()
  39. };
  40. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  41. {
  42. //{{AFX_DATA_INIT(CAboutDlg)
  43. //}}AFX_DATA_INIT
  44. }
  45. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  46. {
  47. CDialog::DoDataExchange(pDX);
  48. //{{AFX_DATA_MAP(CAboutDlg)
  49. //}}AFX_DATA_MAP
  50. }
  51. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  52. //{{AFX_MSG_MAP(CAboutDlg)
  53. //}}AFX_MSG_MAP
  54. END_MESSAGE_MAP()
  55. BYTE tempdata[32]=
  56. {
  57. 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  58. 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70,
  59. 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x61,
  60. 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69
  61. };
  62. //IP_HEADER IP_header;
  63. /////////////////////////////////////////////////////////////////////////////
  64. // CDemoDlg dialog
  65. CDemoDlg::CDemoDlg(CWnd* pParent /*=NULL*/)
  66. : CDialog(CDemoDlg::IDD, pParent)
  67. {
  68. //{{AFX_DATA_INIT(CDemoDlg)
  69. m_baud = _T("19200");
  70. m_called = _T("9163");
  71. m_com = _T("COM1");
  72. m_password = _T("163");
  73. m_userid = _T("163");
  74. // m_ipad[0] = _T("211");
  75. // m_ipad[1] = _T("100");
  76. // m_ipad[2] = _T("31");
  77. // m_ipad[3] = _T("131");
  78. //m_ipOut[0] = _T("211"); //www.263.net
  79. //m_ipOut[1] = _T("100");
  80. //m_ipOut[2] = _T("31");
  81. //m_ipOut[3] = _T("131");
  82. m_ipOut0 = _T("216"); //www.google.com
  83. m_ipOut1 = _T("239");
  84. m_ipOut2 = _T("33");
  85. m_ipOut3 = _T("100");
  86. m_senddata = _T("AT");
  87. m_srcport = _T("0");
  88. m_desport = _T("0");
  89. //}}AFX_DATA_INIT
  90. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  91. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  92. }
  93. void CDemoDlg::DoDataExchange(CDataExchange* pDX)
  94. {
  95. CDialog::DoDataExchange(pDX);
  96. //{{AFX_DATA_MAP(CDemoDlg)
  97. DDX_Control(pDX, IDC_EVENT, m_event);
  98. DDX_CBString(pDX, IDC_BAUD, m_baud);
  99. DDX_Text(pDX, IDC_CALLED, m_called);
  100. DDX_CBString(pDX, IDC_COM, m_com);
  101. DDX_Text(pDX, IDC_PASSWORD, m_password);
  102. DDX_Text(pDX, IDC_USERID, m_userid);
  103. DDX_Text(pDX, IDC_IPAD0, m_ipad0);
  104. DDX_Text(pDX, IDC_IPAD1, m_ipad1);
  105. DDX_Text(pDX, IDC_IPAD2, m_ipad2);
  106. DDX_Text(pDX, IDC_IPAD3, m_ipad3);
  107. DDX_Text(pDX, IDC_IPAD4, m_ipOut0);
  108. DDX_Text(pDX, IDC_IPAD5, m_ipOut1);
  109. DDX_Text(pDX, IDC_IPAD6, m_ipOut2);
  110. DDX_Text(pDX, IDC_IPAD7, m_ipOut3);
  111. DDX_Text(pDX, IDC_RICHTEXTCTRL1, m_senddata);
  112. DDX_Text(pDX, IDC_SRCPORT, m_srcport);
  113. DDX_Text(pDX, IDC_DESPORT, m_desport);
  114. //}}AFX_DATA_MAP
  115. }
  116. BEGIN_MESSAGE_MAP(CDemoDlg, CDialog)
  117. //{{AFX_MSG_MAP(CDemoDlg)
  118. ON_WM_SYSCOMMAND()
  119. ON_WM_PAINT()
  120. ON_WM_QUERYDRAGICON()
  121. ON_BN_CLICKED(IDC_INICOM, OnInicom)
  122. ON_WM_TIMER()
  123. ON_BN_CLICKED(IDC_DIAL, OnDial)
  124. ON_MESSAGE(CN_SEND, OnSend)
  125. ON_BN_CLICKED(IDC_PING, OnPing)
  126. ON_BN_CLICKED(IDC_SendA, OnSendA)
  127. ON_BN_CLICKED(IDC_CloseA, OnCloseA)
  128. ON_BN_CLICKED(IDC_UDP, OnUdp)
  129. ON_BN_CLICKED(IDC_TCP, OnTcp)
  130. //}}AFX_MSG_MAP
  131. END_MESSAGE_MAP()
  132. BEGIN_EVENTSINK_MAP(CDemoDlg, CDialog)
  133.     //{{AFX_EVENTSINK_MAP(CDemoDlg)
  134. ON_EVENT(CDemoDlg, IDC_SendBox, -603 /* KeyPress */, OnKeyPressSendBox, VTS_PI2)
  135. //}}AFX_EVENTSINK_MAP
  136. END_EVENTSINK_MAP()
  137. /////////////////////////////////////////////////////////////////////////////
  138. // CDemoDlg message handlers
  139. BOOL CDemoDlg::OnInitDialog()
  140. {
  141. CDialog::OnInitDialog();
  142. // Add "About..." menu item to system menu.
  143. // IDM_ABOUTBOX must be in the system command range.
  144. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  145. ASSERT(IDM_ABOUTBOX < 0xF000);
  146. CMenu* pSysMenu = GetSystemMenu(FALSE);
  147. if (pSysMenu != NULL)
  148. {
  149. CString strAboutMenu;
  150. strAboutMenu.LoadString(IDS_ABOUTBOX);
  151. if (!strAboutMenu.IsEmpty())
  152. {
  153. pSysMenu->AppendMenu(MF_SEPARATOR);
  154. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  155. }
  156. }
  157. // Set the icon for this dialog.  The framework does this automatically
  158. //  when the application's main window is not a dialog
  159. SetIcon(m_hIcon, TRUE); // Set big icon
  160. SetIcon(m_hIcon, FALSE); // Set small icon
  161. m_ipOut=&m_ipOut0;
  162. m_ipad=&m_ipad0;
  163. framehead=FALSE;
  164. frameend=FALSE;
  165. memset(szPacketTx,0x00,256);
  166. memset(szPacketRx,0x00,256);
  167. // SetTimer(ID_CLOCK3,50,NULL);
  168. //OnInicom();
  169. GetDlgItem(IDC_DIAL)->SetFocus();
  170. /* BYTE teststr[256]={0x7E,0x80,0x21,0x01,0x06,0x00,0x10,0x02,0x06,0x00,0x2d,0x0f,0x00,0x03,0x06,0x3d,0x8b,0x06,0x08,0xdd,0x58,0x7e};
  171. BYTE teststring1[256]={0x7E,0x80,0x21,0x01,0x07,0x00,0x0a,0x03,0x06,0x3d,0x8b,0x06,0x08,0xdd,0x58,0x7e};
  172. BYTE teststring2[256]={0x7E,0x80,0x21,0x02,0x07,0x00,0x0a,0x03,0x06,0x3d,0x8b,0x06,0x08,0xdd,0x58,0x7e};
  173. BYTE teststring3[256]={0x7E,0x80,0x21,0x01,0x08,0x00,0x0a,0x03,0x06,0x3d,0x8b,0x06,0x08,0xdd,0x58,0x7e};
  174. recv=TRUE;
  175. Display(recv,teststr);
  176. // BYTE teststr[256]={0x7E,0xFF,0x03,0xc0,0x21,0x01,0x02,0x00,0x1f,0x01,0x04,0x05,0xea,0x02,0x06,0xff,0xff,0xff,0xff,0x05,0x06,0x74,0x8d,0xd1,0x3b,0x07,0x02,0x08,0x02,0x11,0x04,0x05,0xea,0x13,0x03,0x00,0x9f,0x3e,0x7e};//
  177. // Display(recv,teststr);
  178. m_packet.compress=2;
  179. m_packet.checkpacket(teststr);
  180. recv=FALSE;
  181. Display(recv,m_packet.bPacketTx);
  182. recv=TRUE;
  183. Display(recv,teststring1);
  184. m_packet.checkpacket(teststring1);
  185. recv=FALSE;
  186. Display(recv,m_packet.bPacketTx);
  187. recv=TRUE;
  188. Display(recv,teststring2);
  189. m_packet.checkpacket(teststring2);
  190. recv=FALSE;
  191. Display(recv,m_packet.bPacketTx);
  192. recv=TRUE;
  193. Display(recv,teststring3);
  194. m_packet.checkpacket(teststring3);
  195. recv=FALSE;
  196. Display(recv,m_packet.bPacketTx);
  197. /* BYTE teststr1[64]={0x0a,0x11,0x7E,0xFF,0x7d,0x23,0xc0,0x21};
  198. BYTE teststr2[64]={0x7d,0x22,0x7d,0x21,0x7d,0x20,0x7d,0x3f,0x7d,0x21,0x7d,0x24,0x7d,0x25,0x7d,0x5e,0x7d,0x22,0x7d,0x26,0xff,0xff,0xff,0xff,0x7e,0x22,0xff};
  199. BYTE teststr3[64]={0x20,0x7E,0xFF,0x7d,0x23,0xc0,0x21,0x00,0xff};
  200. BYTE teststr4[64]={0x97,0x23,0x7E,0x00,0xff};
  201. testcat(teststr1);
  202. testcat(teststr2);
  203. testcat(teststr3);
  204. testcat(teststr4);
  205. // sprintf(teststr,"x7ExFFx03xC0x21x01x01x00x0cx02x06x00x0ax00x00x03x04xc0x23");
  206. /* THREADDATA m_threaddata;
  207. m_threaddata.CommDev=hCommDev;
  208. m_threaddata.CommDev=GetSafeHwnd();
  209. AfxBeginThread(MyThreadProc,&m_threaddata,THREAD_PRIORITY_NORMAL);
  210. */
  211. return TRUE;  // return TRUE  unless you set the focus to a control
  212. }
  213. /*UINT MyThreadProc(LPVOID param)
  214. {
  215. DWORD dwEvtMask;
  216. int nLength; 
  217. BYTE abIn[1024];
  218. THREADDATA *pData=(THREADDATA*)param;
  219. while (1) 
  220. { dwEvtMask=0 ; 
  221. WaitCommEvent(pData->CommDev,&dwEvtMask,NULL); 
  222. if ((dwEvtMask&EV_RXCHAR)==EV_RXCHAR) 
  223. do 
  224. if(nLength=ReadCommBlock(pData->CommDev,(LPSTR)abIn,1024)) 
  225. //WriteCommBlock((LPSTR)abIn,nLength ); 
  226. *(abIn+nLength)=0; 
  227. ::SendMessage(pData->hWnd,CN_SEND,nLength,(LONG)(LPSTR)abIn); 
  228. } //end if
  229. } while (nLength>0); //end do
  230. } //end if
  231. } //end while
  232. }
  233. */
  234. void CDemoDlg::OnSend(LPARAM lParam)
  235. {
  236. int len;
  237. // BYTE InBuf[1024];
  238. len=(int)lParam;
  239. //InBuf=(BYTE)wParam;
  240. // tempstr.Format("receiced %d",len);
  241. // m_event.AddString(tempstr);
  242. }
  243. void CDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
  244. {
  245. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  246. {
  247. CAboutDlg dlgAbout;
  248. dlgAbout.DoModal();
  249. }
  250. else
  251. {
  252. CDialog::OnSysCommand(nID, lParam);
  253. }
  254. }
  255. // If you add a minimize button to your dialog, you will need the code below
  256. //  to draw the icon.  For MFC applications using the document/view model,
  257. //  this is automatically done for you by the framework.
  258. void CDemoDlg::OnPaint() 
  259. {
  260. if (IsIconic())
  261. {
  262. CPaintDC dc(this); // device context for painting
  263. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  264. // Center icon in client rectangle
  265. int cxIcon = GetSystemMetrics(SM_CXICON);
  266. int cyIcon = GetSystemMetrics(SM_CYICON);
  267. CRect rect;
  268. GetClientRect(&rect);
  269. int x = (rect.Width() - cxIcon + 1) / 2;
  270. int y = (rect.Height() - cyIcon + 1) / 2;
  271. // Draw the icon
  272. dc.DrawIcon(x, y, m_hIcon);
  273. }
  274. else
  275. {
  276. CDialog::OnPaint();
  277. }
  278. }
  279. // The system calls this to obtain the cursor to display while the user drags
  280. //  the minimized window.
  281. HCURSOR CDemoDlg::OnQueryDragIcon()
  282. {
  283. return (HCURSOR) m_hIcon;
  284. }
  285. /*typedef struct _DCB {
  286.     DWORD DCBlength;      /* sizeof(DCB)                     
  287.     DWORD BaudRate;       /* Baudrate at which running       
  288.     DWORD fBinary: 1;     /* Binary Mode (skip EOF check)    
  289.     DWORD fParity: 1;     /* Enable parity checking          
  290.     DWORD fOutxCtsFlow:1; /* CTS handshaking on output       
  291.     DWORD fOutxDsrFlow:1; /* DSR handshaking on output       
  292.     DWORD fDtrControl:2;  /* DTR Flow control                
  293.     DWORD fDsrSensitivity:1; /* DSR Sensitivity              /
  294.     DWORD fTXContinueOnXoff: 1; /* Continue TX when Xoff sent 
  295.     DWORD fOutX: 1;       /* Enable output X-ON/X-OFF        /
  296.     DWORD fInX: 1;        /* Enable input X-ON/X-OFF         /
  297.     DWORD fErrorChar: 1;  /* Enable Err Replacement          /
  298.     DWORD fNull: 1;       /* Enable Null stripping           /
  299.     DWORD fRtsControl:2;  /* Rts Flow control                /
  300.     DWORD fAbortOnError:1; /* Abort all reads and writes on Error /
  301.     DWORD fDummy2:17;     /* Reserved                        /
  302.     WORD wReserved;       /* Not currently used              /
  303.     WORD XonLim;          /* Transmit X-ON threshold         /
  304.     WORD XoffLim;         /* Transmit X-OFF threshold        /
  305.     BYTE ByteSize;        /* Number of bits/byte, 4-8        /
  306.     BYTE Parity;          /* 0-4=None,Odd,Even,Mark,Space    /
  307.     BYTE StopBits;        /* 0,1,2 = 1, 1.5, 2               /
  308.     char XonChar;         /* Tx and Rx X-ON character        /
  309.     char XoffChar;        /* Tx and Rx X-OFF character       /
  310.     char ErrorChar;       /* Error replacement char          /
  311.     char EofChar;         /* End of Input character          /
  312.     char EvtChar;         /* Received Event character        /
  313.     WORD wReserved1;      /* Fill for now.                   /
  314. } DCB, *LPDCB;
  315. */
  316. void CDemoDlg::OnInicom() 
  317. {
  318. // TODO: Add your control notification handler code here
  319. memset(szPacketRx,0x0,256);
  320. memset(szPacketTx,0x0,256);
  321. ATState=0;
  322.     MyReadFile_Flag=false;
  323. MyReadFile_Flag1=false;
  324. m_event.ResetContent();
  325. UpdateData(TRUE);
  326. DCB dcb;
  327. hCommDev=CreateFile(m_com,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);//打开串口
  328. // hCommDev=CreateFile(m_com,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);//打开串口
  329. if (hCommDev==(HANDLE)-1)
  330. {
  331. tempstr.Format(":initiate modem error");
  332. m_event.AddString(m_com+tempstr);
  333. writelog(m_com+tempstr);
  334. }
  335. else
  336. {
  337. tempstr.Format(":initiate modem successfully");
  338. m_event.AddString(m_com+tempstr);
  339. writelog(m_com+tempstr);
  340. }
  341. /*
  342. //<wcy
  343. oCommDev.hEvent = CreateEvent(
  344. NULL,   // no security attributes 
  345. FALSE,  // auto reset event 
  346. FALSE,  // not signaled 
  347. NULL    // no name 
  348. );
  349. if (SetCommMask(hCom, EV_RXFLAG))
  350. {
  351. tempstr.Format(":Set Mask successfully!");
  352. m_event.AddString(m_com+tempstr);
  353. m_event.AddString(m_com+tempstr);
  354. }
  355. else
  356. {
  357. tempstr.Format(":Set Mask Error!");
  358. m_event.AddString(m_com+tempstr);
  359. m_event.AddString(m_com+tempstr);
  360. }
  361. //wcy>
  362. */
  363. COMMTIMEOUTS CommTimeOuts;
  364. CommTimeOuts.ReadIntervalTimeout=MAXDWORD;
  365. CommTimeOuts.ReadTotalTimeoutMultiplier=0;
  366. CommTimeOuts.ReadTotalTimeoutConstant=0;
  367. CommTimeOuts.WriteTotalTimeoutMultiplier=0;
  368. CommTimeOuts.WriteTotalTimeoutConstant=0;
  369. BuildCommDCB(m_com+":"+m_baud+",n,8,1",&dcb);//配置串口 8位数据位,1位停止位,物理层
  370.     dcb.fRtsControl=RTS_CONTROL_DISABLE;
  371. dcb.fDtrControl=DTR_CONTROL_ENABLE;    // DTR flow control type 2
  372. //*
  373. //dcb.fDsrSensitivity=1; // DSR sensitivity 
  374. //dcb.fTXContinueOnXoff=0; // XOFF continues Tx 
  375. //dcb.fOutX=0; // XON/XOFF out flow control 
  376. //dcb.fInX=0;  // XON/XOFF in flow control 
  377. //dcb.fNull=0; // enable null stripping 
  378. //dcb.ErrorChar=ErrorChar;
  379. //  */  
  380. SetCommState(hCommDev,&dcb);
  381. SetCommTimeouts(hCommDev,&CommTimeOuts);
  382. PurgeComm(hCommDev,PURGE_TXCLEAR);//清发送缓冲区
  383. PurgeComm(hCommDev,PURGE_RXCLEAR);//清接收缓冲区
  384. char strOutMsg[20];
  385. DWORD dwBytesWritten;
  386. sprintf(strOutMsg,"ATr");
  387. WriteFile(hCommDev,strOutMsg,strlen(strOutMsg),&dwBytesWritten,NULL);//初始化MODEM
  388. tempstr.Format("发:%s",strOutMsg);
  389. m_event.AddString(tempstr);
  390. writelog(m_com+tempstr);
  391. ini=TRUE;
  392. PurgeComm(hCommDev,PURGE_TXCLEAR);//清发送缓冲区
  393. PurgeComm(hCommDev,PURGE_RXCLEAR);//清接收缓冲区
  394. /*if (!SetCommMask(hCommDev,EV_RXCHAR)) 
  395. {
  396. tempstr.Format(":SetCommMask error!");
  397. m_event.AddString(m_com+tempstr);
  398. writelog(m_com+tempstr);
  399. }*/
  400. memset(temp,0x00,256);
  401. SetTimer(ID_CLOCK1,50,NULL);
  402. GetDlgItem(IDC_INICOM)->EnableWindow(false);
  403. GetDlgItem(IDC_SendA)->EnableWindow(false);
  404. GetDlgItem(IDC_CloseA)->EnableWindow(true);
  405. }
  406. bool CDemoDlg::OnTimer(UINT nIDEvent) 
  407. {
  408. BYTE* strCommRecvMsg=new BYTE[256];
  409. DWORD dwBytesWritten;
  410. memset(strCommRecvMsg,0x00,256);
  411. bool bReturn;
  412. DWORD dwBytesRead;
  413. int k,l,start=0;
  414. int mm;
  415. if(nIDEvent==5)//send icmp ping packet
  416. {
  417. KillTimer(ID_CLOCK5);
  418. PurgeComm(hCommDev,PURGE_TXCLEAR);//清发送缓冲区
  419. m_packet.makepacket(IPCP,REQ,temp);
  420. DWORD dwBytesWritten;
  421. m_packet.IPCP_s=0;
  422. if (WriteFile(hCommDev,m_packet.bPacketTx,m_packet.TxLen,&dwBytesWritten,NULL)<1)
  423. {
  424. m_event.AddString("send error.");
  425. writelog("send error");
  426. return false;
  427. }
  428. recv=FALSE;
  429. Display(recv,m_packet.bPacketTx1);
  430. memset(szPacketRx,0x00,256);
  431. SetTimer(ID_CLOCK1,50,NULL);
  432. return true;
  433. }
  434. else if(nIDEvent==4)
  435. {
  436. KillTimer(ID_CLOCK4);
  437. SetDlgItemText(IDC_RICHTEXTCTRL1,"");
  438. GetDlgItem(IDC_RICHTEXTCTRL1)->EnableWindow(true);
  439. GetDlgItem(IDC_RICHTEXTCTRL1)->SetFocus();
  440. return true;
  441. }
  442. else if(nIDEvent==3)//send icmp ping packet
  443. {
  444. KillTimer(ID_CLOCK3);
  445. return true;
  446. memset(temp,0x00,256);
  447. PurgeComm(hCommDev,PURGE_TXCLEAR);
  448. temp[0]=atoi(m_ipOut[0]);//destination address
  449. temp[1]=atoi(m_ipOut[1]);
  450. temp[2]=atoi(m_ipOut[2]);
  451. temp[3]=atoi(m_ipOut[3]);
  452. temp[4]=strlen(m_senddata)>>8;
  453. //temp[4]=32;
  454. temp[5]=strlen(m_senddata)&0x00ff;
  455. mm=strlen(m_senddata)+6;
  456. l=0;
  457. for (k=6;k<mm;k++)
  458. {
  459. //temp[k]=tempdata[l++];
  460. temp[k]=m_senddata[l++];
  461. }
  462. //format of temp for udp:
  463. //destination address 4bytes
  464. //source port 2bytes msb+lsb
  465. //destination port  2bytes msb+lsb
  466. //udp data length 2bytes msb +lsb
  467. //udp data
  468. //m_packet.makepacket(IP,PING,temp);
  469. m_packet.IP_header.protocol=IP_ICMP;
  470. m_packet.makeIPpacket(IP_ICMP,ICMP_PING,temp);
  471. DWORD dwBytesWritten;
  472. if (WriteFile(hCommDev,m_packet.bPacketTx,m_packet.TxLen,&dwBytesWritten,NULL)<1)
  473. {
  474. m_event.AddString("send error.");
  475. writelog("send error");
  476. return false;
  477. }
  478. recv=FALSE;
  479. Display(recv,m_packet.bPacketTx1);
  480. memset(szPacketRx,0x00,256);
  481. //SetTimer(ID_CLOCK3,6000,NULL);//SEND IP PACKET
  482. SetTimer(ID_CLOCK1,50,NULL);
  483. return true;
  484. }
  485. else if(nIDEvent==2)
  486. {
  487. KillTimer(ID_CLOCK2);
  488. PurgeComm(hCommDev,PURGE_TXCLEAR);//清发送缓冲区
  489. m_packet.makepacket(LCP,ACK,temp);
  490. DWORD dwBytesWritten;
  491. if (WriteFile(hCommDev,m_packet.bPacketTx,m_packet.TxLen,&dwBytesWritten,NULL)<1)
  492. {
  493. m_event.AddString("send error.");
  494. writelog("send error");
  495. return false;
  496. }
  497. recv=FALSE;
  498. Display(recv,m_packet.bPacketTx1);
  499. memset(szPacketRx,0x00,256);
  500. SetTimer(ID_CLOCK1,50,NULL);
  501. return true;
  502. }
  503. else if (nIDEvent==1)
  504. {
  505. UpdateData(TRUE);
  506. /* BYTE LastLeftBuf[256];
  507. static bool LastLeft=false;
  508.         if  (LastLeft){
  509.             int jj;
  510. for(jj=0;jj<256;jj++) {
  511. strCommRecvMsg[jj]=LastLeftBuf[jj];
  512. if (LastLeftBuf[jj]='') break;
  513. }
  514.     bReturn=ReadFile(hCommDev,strCommRecvMsg+jj,100,&dwBytesRead,NULL); 
  515. LastLeft=false;
  516. }
  517. */
  518. // else {
  519.     bool bReturn=false;
  520. if (MyReadFile_Flag==false) bReturn=ReadFile(hCommDev,strCommRecvMsg,150,&dwBytesRead,NULL);
  521. else
  522. {
  523. // bReturn=ReadFile(hCommDev,strCommRecvMsg,1,&dwBytesRead,NULL);
  524. DWORD dwBytesRead1;
  525. int ii=0;
  526. dwBytesRead=0;
  527. //for(;1;) {
  528. for(ii=0;ii<256;ii++) {
  529. ReadFile(hCommDev,strCommRecvMsg+ii,1,&dwBytesRead1,NULL);
  530. if (dwBytesRead1==1){
  531. if (dwBytesRead==0) KillTimer(ID_CLOCK1);
  532. bReturn=true;
  533. dwBytesRead++;
  534. //if ((strCommRecvMsg[ii]==0x7E) && (dwBytesRead!=1)) break;
  535. if (strCommRecvMsg[ii]==0x7E) break;
  536. else
  537. {
  538. if ((framehead==FALSE)&&(dwBytesRead==1)&&MyReadFile_Flag1==TRUE)
  539. {
  540. strCommRecvMsg[1]=strCommRecvMsg[0];
  541. strCommRecvMsg[0]=0x7E;
  542. dwBytesRead++;
  543. break;
  544. }
  545. }//ii++;
  546.    }
  547. else break;
  548.  }
  549.        
  550. }
  551.             /*   读一个字符
  552.    判断该字符是否7E
  553.    如果是则停止读数据
  554.    如果不是则继续读数
  555. */
  556. // }
  557. //BOOL aReturn=WaitCommEvent(hCommDev,EV_RXFLAG,NULL);
  558. if (dwBytesRead<1)//无新数据,返回
  559. {
  560. delete strCommRecvMsg;
  561. return false;
  562. }
  563. if (!bReturn)//失败,返回
  564. {
  565. tempstr.Format(":接收失败");
  566. m_event.AddString(m_com+tempstr);
  567. writelog(m_com+tempstr);
  568. delete strCommRecvMsg;
  569. return false;
  570. }
  571. else
  572. {
  573. UpdateData(TRUE);
  574. char* strOutMsg=new char[256];
  575. memset(strOutMsg,0x00,256);
  576. CString findstr;
  577. findstr=strCommRecvMsg;
  578. tempstr.Format("收:%s",strCommRecvMsg);
  579. m_event.AddString(tempstr);
  580. writelog(tempstr);
  581. if (ATState<8)
  582. {
  583. if (findstr.Find("OK",NULL)!=-1 && ini )
  584. {
  585. //ini=FALSE;
  586. switch (ATState)
  587. {
  588. case 0:
  589. sprintf(strOutMsg,"ATE0V1r");
  590. ATState++;
  591. break;
  592. case 1:
  593. sprintf(strOutMsg,"ATr");
  594. ATState++;
  595. break;
  596. case 2:
  597. sprintf(strOutMsg,"ATS0=0r");
  598. ATState++;
  599. break;
  600. case 3:
  601. sprintf(strOutMsg,"ATr");
  602. ATState++;
  603. break;
  604. case 4:
  605. sprintf(strOutMsg,"ATE0V1r");
  606. ATState++;
  607. GetDlgItem(IDC_DIAL)->EnableWindow(TRUE);
  608. break;
  609. }
  610. if (ATState<5)
  611. {
  612. WriteFile(hCommDev,strOutMsg,strlen(strOutMsg),&dwBytesWritten,NULL);
  613. tempstr.Format("发:%s",strOutMsg);
  614. m_event.AddString(tempstr);
  615. writelog(m_com+tempstr);
  616. }
  617. }
  618. else if ((findstr.Find("gin",NULL)!=-1) || (findstr.Find("ame",NULL)!=-1) )
  619. {
  620. //memset(strOutMsg,0x00,256);
  621. strcpy(strOutMsg,m_userid);
  622. strcat(strOutMsg,"r");
  623. WriteFile(hCommDev,strOutMsg,strlen(strOutMsg),&dwBytesWritten,NULL);//初始化MODEM
  624. tempstr.Format("发:%s",strOutMsg);
  625. m_event.AddString(tempstr);
  626. writelog(tempstr);
  627. //wcy 2002-
  628. }
  629. else if (findstr.Find("rd",NULL)!=-1)
  630. {
  631. //memset(strOutMsg,0x00,256);
  632. strcpy(strOutMsg,m_password);
  633. strcat(strOutMsg,"r");
  634. DWORD dwBytesWritten;
  635. WriteFile(hCommDev,strOutMsg,strlen(strOutMsg),&dwBytesWritten,NULL);//初始化MODEM
  636. tempstr.Format("发:%s",strOutMsg);
  637. m_event.AddString(tempstr);
  638. writelog(tempstr);
  639. iLen=0;
  640. MyReadFile_Flag=true;
  641. }
  642. else SetTimer(ID_CLOCK1,50,NULL);
  643. }
  644. //else SetTimer(ID_CLOCK1,50,NULL);
  645.             
  646. if (findstr.Find('~',NULL)!=-1)
  647. {
  648. MyReadFile_Flag1=true;
  649. if (!framehead) 
  650. {
  651. memset(RecvMsgtemp,0x00,256);
  652. iLen=0;
  653. framehead=TRUE;
  654. for(k=0;k<256;k++)
  655. {
  656. if(strCommRecvMsg[k]=='~')
  657. {
  658. //if (k==0) break;
  659. if (strCommRecvMsg[k+1]=='~') k++;
  660. for(l=0;l<256,k<256;l++,k++)
  661. {
  662. if(strCommRecvMsg[k]!='')
  663. RecvMsgtemp[l]=strCommRecvMsg[k];
  664. else
  665. break;
  666. }
  667. break;
  668. }
  669. }
  670. bytecat(RecvMsgtemp);
  671. }else//if(!framehead) 
  672. {
  673. framehead=FALSE;
  674. frameend=TRUE;
  675. for(k=0;k<256;k++)
  676. {
  677. if(strCommRecvMsg[k]=='~')
  678. {
  679. if (strCommRecvMsg[k+1]=='~')
  680. {
  681. framehead=FALSE;
  682. frameend=FALSE;
  683. }
  684. else
  685. {
  686. for(int l=0;l<=k;l++)
  687. {
  688. RecvMsgtemp[l]=strCommRecvMsg[l];// modified by wcy
  689. }
  690. }
  691. break;
  692. }
  693. }
  694. bytecat(RecvMsgtemp);
  695. BYTE select;
  696. select=0x00;
  697. for(int i=0;i<256;i++)
  698. {
  699. if (szPacketRx[i]==0x7d)
  700. {
  701. if(szPacketRx[i+1]==0x5e)//0x7e
  702. {
  703. select=0x7e;
  704. szPacketRx[i+1]=select;
  705. }else if(szPacketRx[i+1]==0x5d)//0x7d
  706. {
  707. select=0x7d;
  708. szPacketRx[i+1]=select;
  709. }else//<0x20
  710. {
  711. select=szPacketRx[i+1]-0x20;
  712. szPacketRx[i+1]=select;
  713. }
  714. for(int j=i;j<256;j++)
  715. {
  716. szPacketRx[j]=szPacketRx[j+1];
  717. }
  718. }//end if (szPacketRx[i]==0x7d)
  719. }//end for
  720. framehead=FALSE;
  721. frameend=FALSE;
  722. KillTimer(ID_CLOCK1);
  723. if (!m_packet.checkpacket(szPacketRx))
  724. {
  725. writelog("FCS check error");
  726. m_event.AddString("FCS check error");
  727. return false;
  728. }
  729. recv=TRUE;
  730. Display(recv,szPacketRx);
  731. //if(WriteFile(hCommDev,m_packet.bPacketTx,iLen,&dwBytesWritten,NULL))
  732. if(WriteFile(hCommDev,m_packet.bPacketTx,m_packet.TxLen,&dwBytesWritten,NULL))
  733. {
  734.  recv=FALSE;
  735.  //Display(recv,m_packet.bPacketTx);
  736.  //Display(recv,&(m_packet.bPacketTx1[0]));
  737.  if ( m_packet.TxLen!=0 ) Display(recv,m_packet.bPacketTx1);
  738. }
  739. int address=0;
  740. CString tempad;
  741. switch(m_packet.bState)
  742. {
  743. case LCPOPEN:
  744. GetDlgItem(IDC_STATE)->SetWindowText("正在建立连接...");
  745. for(i=0;i<256;i++)
  746. temp[i]=szPacketRx[i];
  747. SetTimer(ID_CLOCK2,500,NULL);
  748. break;
  749. case IPCPOPEN:
  750. GetDlgItem(IDC_STATE)->SetWindowText("正在注册计算机...");
  751. for(i=0;i<256;i++)
  752. {
  753. temp[i]=szPacketRx[i];
  754. }
  755. SetTimer(ID_CLOCK5,500,NULL);
  756. break;
  757. case IPCPOK:
  758. //m_packet.bState=IPSTART;
  759. tempad.Format("%u",m_packet.bIpaddress[0]);//首位
  760. m_ipad[0]=tempad;
  761. tempad.Format("%u",m_packet.bIpaddress[1]);//第二位
  762. m_ipad[1]=tempad;
  763. tempad.Format("%u",m_packet.bIpaddress[2]);//第三位
  764. m_ipad[2]=tempad;
  765. tempad.Format("%u",m_packet.bIpaddress[3]);//第四位
  766. m_ipad[3]=tempad;
  767. UpdateData(FALSE);
  768. GetDlgItem(IDC_STATE)->SetWindowText("连接已建立");
  769. //SetTimer(ID_CLOCK3,6000,NULL);//SEND IP PACKET
  770. //*
  771. if (m_packet.IPCP_s==IPCPREOPEN)
  772. {
  773. for(i=0;i<256;i++)
  774. {
  775. temp[i]=szPacketRx[i];
  776. }
  777. SetTimer(ID_CLOCK5,500,NULL);
  778. KillTimer(ID_CLOCK1);
  779. }
  780. //* /
  781. memset(szPacketRx,0x00,256);
  782. SetTimer(ID_CLOCK1,50,NULL);
  783. GetDlgItem(IDC_PING)->EnableWindow(true);
  784. GetDlgItem(IDC_UDP)->EnableWindow(true);
  785. GetDlgItem(IDC_TCP)->EnableWindow(true);
  786. //tempstr.Format("Local IP Adress: %s . %s . %s . %s",m_ipad[0],m_ipad[1],m_ipad[2],m_ipad[3]);
  787. //m_event.AddString(tempstr);
  788. //writelog(tempstr);
  789. break;
  790. default:
  791. memset(szPacketRx,0x00,256);
  792. SetTimer(ID_CLOCK1,50,NULL);
  793. break;
  794. }//end switch
  795. }//end if(framehead) 
  796. }//end if(findstr.Find('~',NULL)!=-1)
  797. memset(RecvMsgtemp,0x0,256);
  798. if (framehead && (findstr.Find('~',NULL)==-1)) 
  799. {
  800. bytecat(strCommRecvMsg);
  801. }
  802. delete strOutMsg;
  803. }//receive valid data
  804. PurgeComm(hCommDev,PURGE_TXCLEAR);//清发送缓冲区
  805. // PurgeComm(hCommDev,PURGE_RXCLEAR);//清接收缓冲区
  806. delete strCommRecvMsg;
  807. return true;
  808. }// ID_CLOCK1
  809. return true;
  810. CDialog::OnTimer(nIDEvent);
  811. }
  812. void CDemoDlg::bytecat(BYTE* strCommRecvMsg)
  813. {
  814. int k;
  815. for(k=0;k<256;k++)
  816. {
  817. if (strCommRecvMsg[k]!='')
  818. {
  819. szPacketRx[k+iLen]=strCommRecvMsg[k];
  820. }
  821. else
  822. {
  823. iLen+=k;
  824. break;
  825. }//end if
  826. }//end for
  827. }
  828. void CDemoDlg::OnDial() 
  829. {
  830. // TODO: Add your control notification handler code here
  831. UpdateData(TRUE);
  832. char* strOutMsg=new char[256];
  833. memset(strOutMsg,0x00,256);
  834. strcpy(strOutMsg,"ATDT");
  835. strcat(strOutMsg,m_called);
  836. strcat(strOutMsg,"r");
  837. DWORD dwBytesWritten;
  838. WriteFile(hCommDev,strOutMsg,strlen(strOutMsg),&dwBytesWritten,NULL);//初始化MODEM
  839. tempstr.Format("发:%s",strOutMsg);
  840. m_event.AddString(tempstr);
  841. writelog(tempstr);
  842. GetDlgItem(IDC_STATE)->SetWindowText("正在拨号...");
  843. delete strOutMsg;
  844. GetDlgItem(IDC_DIAL)->EnableWindow(false);
  845. // GetDlgItem(IDC_LOGIN)->EnableWindow(TRUE);
  846. }
  847. /*
  848. void CDemoDlg::OnLogin() 
  849. {
  850. DWORD dwBytesWritten;
  851. char* strOutMsg=new char[256];
  852. UpdateData(TRUE);
  853. memset(strOutMsg,0x00,256);
  854. strcpy(strOutMsg,m_password);
  855. strcat(strOutMsg,"r");
  856. WriteFile(hCommDev,strOutMsg,strlen(strOutMsg),&dwBytesWritten,NULL);//初始化MODEM
  857. tempstr.Format("发:%s",strOutMsg);
  858. m_event.AddString(tempstr);
  859. writelog(tempstr);
  860. }
  861. */
  862. void CDemoDlg::writelog(CString temp)
  863. {
  864. CStdioFile file;
  865. CString filename;
  866. CTime time;
  867. time=CTime::GetCurrentTime();
  868. filename=time.Format("%Y%m%d");
  869. file.Open(filename+".log",CFile::modeNoTruncate|CFile::modeCreate|CFile::modeWrite|CFile::typeText);
  870. file.SeekToEnd();
  871. temp=time.Format("%Y.%m.%d %H:%M:%S  ")+temp+"rn";
  872. file.Write(temp,temp.GetLength());
  873. file.Close();
  874. }
  875. /*int NEAR ReadCommBlock(HANDLE COMDEV,LPSTR lpszBlock,int nMaxLength) 
  876. BOOL fReadStat ; 
  877. COMSTAT ComStat ; 
  878. DWORD dwErrorFlags; 
  879. DWORD dwLength; 
  880. // DWORD dwError; 
  881. //if (!npGPSInfo) return(FALSE); 
  882. ClearCommError(COMDEV,&dwErrorFlags,&ComStat); 
  883. dwLength=min((DWORD)nMaxLength,ComStat.cbInQue); 
  884. if (dwLength>0) 
  885. fReadStat=ReadFile(COMDEV,lpszBlock,dwLength,&dwLength,NULL); 
  886. if (!fReadStat) 
  887. if (GetLastError()==ERROR_IO_PENDING) 
  888. OutputDebugString("nrIO Pending"); 
  889. /* while(!GetOverlappedResult(COMDEV(npGPSInfo),&READ_OS(npGPSInfo),&dwLength,TRUE)) 
  890. {
  891. dwError=GetLastError(); 
  892. if(dwError == ERROR_IO_INCOMPLETE)
  893. continue; 
  894. }*/ //end while
  895. /* } 
  896. else 
  897. {
  898. dwLength=0; 
  899. ClearCommError(COMDEV,&dwErrorFlags,&ComStat); 
  900. }//end if 
  901. }//end if 
  902. return ( dwLength ) ; 
  903. */
  904. /*void CDemoDlg::OnReadfile() 
  905. {
  906. CStdioFile file;
  907. CString filename;
  908. CTime time;
  909. char* content=new char[256];
  910. /* time=CTime::GetCurrentTime();
  911. filename=time.Format("%Y%m%d");
  912. file.Open(filename+".log",CFile::modeNoTruncate|CFile::modeCreate|CFile::modeRead|CFile::typeText);
  913. file.SeekToBegin();
  914. file.Read(content,256);
  915. int len=strlen(content);
  916. for (int i=0;i<len;i++)
  917. {
  918. tempstr.Format(": %x",content[i]);
  919. m_event.AddString(tempstr);
  920. // writelog(m_com+tempstr);
  921. }*/
  922. /* BYTE tempstr[256];
  923. memset(tempstr,0x00,256);
  924. DWORD dwBytesWritten;
  925. m_packet.makepacket(PAP,REQ,tempstr);
  926. if( WriteFile(hCommDev,m_packet.bPacketTx,m_packet.TxLen,&dwBytesWritten,NULL))
  927. {
  928. recv=FALSE;
  929. Display(recv,m_packet.bPacketTx);
  930. }
  931. }*/
  932. void CDemoDlg::Display(BOOL recv,BYTE* pDispstr)
  933. {
  934. char szTemp[256];
  935. char tempbuf[100];
  936. CString tempsrcip[4];
  937. CString tempdesip[4];
  938. BYTE bAscii;
  939. BYTE mm;
  940. char keystr[13][10]={"LCP_REQ","LCP_REJ","LCP_ACK","PAP_REQ","PAP_ACK","IPCP_REQ","IPCP_ACK","IPCP_NAK","IPCP_REJ","IP","IP","ICMP","UDP"};
  941.     char keybytes[11][20]={
  942. "7E FF 03 C0 21 01",  //0LCP_REQ
  943. "7E FF 03 C0 21 04",  //1LCP_REJ
  944. "7E FF 03 C0 21 02",  //2LCP_ACK
  945. "7E FF 03 C0 23 01",  //3PAP_REQ
  946. "7E FF 03 C0 23 02",  //4PAP_ACK
  947. "7E 80 21 01",  //5IPCP_REQ
  948. "7E 80 21 02",  //6IPCP_ACK
  949. "7E 80 21 03",  //7IPCP_NAK
  950. "7E 80 21 04",  //8IPCP_REJ
  951. "7E 00 21 45",  //9PINGOUT
  952. "7E 21 45"      //10PINGIN
  953.     };
  954. memset(szTemp,0x00,256);
  955. for(int i=0;i<MAXLENGTH;i++)
  956. {
  957. // if(pDispstr[i]!='')
  958. {
  959. bAscii=pDispstr[i];
  960. itoa(bAscii,szDisplay,16);
  961. //if (bAscii=='0')
  962. // sprintf(szDisplay,"0");
  963. if (szDisplay[0]<='f' && szDisplay[0]>='0'&& szDisplay[1]=='')
  964. {
  965. szDisplay[1]=szDisplay[0];
  966. szDisplay[0]='0';
  967. }
  968. for(int j=0;j<3;j++)
  969. {
  970. if(szDisplay[j]>='a' && szDisplay[j]<'z')
  971. szDisplay[j]-=32;
  972. }
  973. strcat(szTemp,szDisplay);
  974. strcat(szTemp," ");
  975. }//end if
  976. }
  977.     if (recv)
  978. {
  979. for(int kk=0;kk<11;kk++)
  980. {
  981. if (strncmp(keybytes[kk], szTemp, strlen(keybytes[kk]))==0)
  982. {
  983. if ( kk==9 || kk==10)
  984. {
  985. for (mm=0;mm<4;mm++)
  986. tempsrcip[mm].Format("%u",m_packet.IP_header.SrcIP[mm]);
  987. for (mm=0;mm<4;mm++)
  988. tempdesip[mm].Format("%u",m_packet.IP_header.DesIP[mm]);
  989. if (m_packet.IP_header.protocol==IP_ICMP)
  990. {
  991. sprintf(tempbuf,"  :In -->IP --- %-10s  from:%3s.%3s.%3s.%3s  to:%3s.%3s.%3s.%3s",
  992. keystr[11],
  993. tempsrcip[0],tempsrcip[1],tempsrcip[2],tempsrcip[3],
  994. tempdesip[0],tempdesip[1],tempdesip[2],tempdesip[3]
  995. );
  996. }
  997. else if (m_packet.IP_header.protocol==IP_UDP)
  998. {
  999. sprintf(tempbuf,"  :In -->IP --- %-10s  from:%3s.%3s.%3s.%3s  to:%3s.%3s.%3s.%3s",
  1000. keystr[12],
  1001. tempsrcip[0],tempsrcip[1],tempsrcip[2],tempsrcip[3],
  1002. tempdesip[0],tempdesip[1],tempdesip[2],tempdesip[3]
  1003. );
  1004. }
  1005. else
  1006. {
  1007. sprintf(tempbuf,"  :In -->IP     Unkown Type from:%3s.%3s.%3s.%3s  to:%3s.%3s.%3s.%3s",
  1008. //keystr[12],
  1009. tempsrcip[0],tempsrcip[1],tempsrcip[2],tempsrcip[3],
  1010. tempdesip[0],tempdesip[1],tempdesip[2],tempdesip[3]
  1011. );
  1012. }
  1013. //strcat(tempbuf, tempdesip[0]
  1014. m_event.AddString(tempbuf);
  1015. writelog(tempbuf);
  1016. }
  1017. else 
  1018. {
  1019. sprintf(tempbuf,"  :In -->%s",keystr[kk]);
  1020. m_event.AddString(tempbuf);
  1021. writelog(tempbuf);
  1022. }
  1023. break;
  1024. }
  1025. }
  1026. }
  1027. else
  1028. {
  1029. for(int kk=0;kk<11;kk++)
  1030. {
  1031. if (strncmp(keybytes[kk], szTemp, strlen(keybytes[kk]))==0)
  1032. {
  1033. if ( kk==9 || kk==10 && (
  1034. m_packet.IP_header.protocol==IP_ICMP ||
  1035. m_packet.IP_header.protocol==IP_UDP
  1036. ))
  1037. {
  1038. for (mm=0;mm<4;mm++)
  1039. tempsrcip[mm].Format("%u",m_packet.IP_header.SrcIP[mm]);
  1040. for (mm=0;mm<4;mm++)
  1041. tempdesip[mm].Format("%u",m_packet.IP_header.DesIP[mm]);
  1042. if (m_packet.IP_header.protocol==IP_ICMP)
  1043. {
  1044. sprintf(tempbuf,"  :Out-->IP --- %-10s  from:%3s.%3s.%3s.%3s  to:%3s.%3s.%3s.%3s",
  1045. keystr[11],
  1046. tempsrcip[0],tempsrcip[1],tempsrcip[2],tempsrcip[3],
  1047. tempdesip[0],tempdesip[1],tempdesip[2],tempdesip[3]
  1048. );
  1049. }
  1050. else if (m_packet.IP_header.protocol==IP_UDP)
  1051. {
  1052. sprintf(tempbuf,"  :Out-->IP --- %-10s  from:%3s.%3s.%3s.%3s  to:%3s.%3s.%3s.%3s",
  1053. keystr[12],
  1054. tempsrcip[0],tempsrcip[1],tempsrcip[2],tempsrcip[3],
  1055. tempdesip[0],tempdesip[1],tempdesip[2],tempdesip[3]
  1056. );
  1057. }
  1058. else
  1059. {
  1060. sprintf(tempbuf,"  :Out-->IP     Unkown Type from:%3s.%3s.%3s.%3s  to:%3s.%3s.%3s.%3s",
  1061. //keystr[12],
  1062. tempsrcip[0],tempsrcip[1],tempsrcip[2],tempsrcip[3],
  1063. tempdesip[0],tempdesip[1],tempdesip[2],tempdesip[3]
  1064. );
  1065. }
  1066. m_event.AddString(tempbuf);
  1067. writelog(tempbuf);
  1068. }
  1069. else 
  1070. {
  1071. sprintf(tempbuf,"  :Out<--%s",keystr[kk]);
  1072. m_event.AddString(tempbuf);
  1073. writelog(tempbuf);
  1074. }
  1075. break;
  1076. }
  1077. }
  1078. }
  1079. m_event.AddString(szTemp);
  1080. if (recv) tempstr.Format("收:%s",szTemp);
  1081. else tempstr.Format("发:%s",szTemp);
  1082. writelog(tempstr);
  1083. }
  1084. void CDemoDlg::testcat(BYTE* strCommRecvMsg)
  1085. {
  1086. BYTE *RecvMsgtemp1=new BYTE[256];
  1087. memset(RecvMsgtemp1,0x00,256);
  1088. int l,k;
  1089. CString findstr;
  1090. findstr=strCommRecvMsg;
  1091. if (findstr.Find('~',NULL)!=-1)
  1092. {
  1093. if (!framehead) 
  1094. {
  1095. iLen=0;
  1096. framehead=TRUE;
  1097. for(k=0;k<256;k++)
  1098. {
  1099. if(strCommRecvMsg[k]=='~')
  1100. {
  1101. // if (k==0) break;
  1102. for(l=0;l<256,k<256;l++,k++)
  1103. {
  1104. if(strCommRecvMsg[k]!='')
  1105. RecvMsgtemp1[l]=strCommRecvMsg[k];
  1106. else
  1107. break;
  1108. }
  1109. bytecat(RecvMsgtemp1);
  1110. break;
  1111. }//end if(strCommRecvMsg[k]='~')
  1112. }//end for 
  1113. }else
  1114. {
  1115. framehead=FALSE;
  1116. frameend=TRUE;
  1117. for(k=0;k<256;k++)
  1118. {
  1119. if(strCommRecvMsg[k]=='~')
  1120. {
  1121. for(int l=0;l<=k;l++)
  1122. {
  1123. if(strCommRecvMsg[l]!='')
  1124. RecvMsgtemp1[l]=strCommRecvMsg[l];
  1125. else
  1126. break;
  1127. }//end for
  1128. bytecat(RecvMsgtemp1);
  1129. break;
  1130. }//end if 
  1131. }//end for
  1132. BYTE* select=new BYTE[256];
  1133. memset(select,0x00,256);
  1134. for(int i=0;i<256;i++)
  1135. {
  1136. if (szPacketRx[i]==0x7d)
  1137. {
  1138. if(szPacketRx[i+1]==0x5e)//0x7e
  1139. {
  1140. select[i]=0x7e;
  1141. szPacketRx[i+1]=select[i];
  1142. }else if(szPacketRx[i+1]==0x5d)//0x7d
  1143. {
  1144. select[i]=0x7d;
  1145. szPacketRx[i+1]=select[i];
  1146. }else//<0x20
  1147. {
  1148. select[i]=szPacketRx[i+1]-0x20;
  1149. szPacketRx[i+1]=select[i];
  1150. }
  1151. for(int j=i;j<256;j++)
  1152. {
  1153.   szPacketRx[j]=szPacketRx[j+1];
  1154. }//end for 
  1155. }//end if
  1156. }//end for
  1157. delete select;
  1158. recv=TRUE;
  1159. Display(recv,szPacketRx);
  1160. memset(szPacketRx,0x00,256);
  1161. // m_packet.checkpacket(szPacketRx);
  1162. // recv=FALSE;
  1163. // Display(recv,m_packet.bPacketTx);
  1164. }//end if(!framehead) 
  1165. }//end if(findstr.Find('~',NULL)!=-1)
  1166. delete RecvMsgtemp1;
  1167. if (framehead && (findstr.Find('~',NULL)==-1)) 
  1168. {
  1169. bytecat(strCommRecvMsg);
  1170. }
  1171. }
  1172. void CDemoDlg::OnPing() 
  1173. {
  1174. int k,l,mm;
  1175. memset(temp,0x00,256);
  1176. PurgeComm(hCommDev,PURGE_TXCLEAR);
  1177. //PurgeComm(hCommDev,PURGE_RXCLEAR);
  1178. UpdateData(TRUE);
  1179. temp[0]=atoi(m_ipOut[0]);//destination address
  1180. temp[1]=atoi(m_ipOut[1]);
  1181. temp[2]=atoi(m_ipOut[2]);
  1182. temp[3]=atoi(m_ipOut[3]);
  1183. //m_packet.bID=0;
  1184. temp[4]=strlen(m_senddata)>>8;
  1185. //temp[4]=32;
  1186. temp[5]=strlen(m_senddata)&0x00ff;
  1187. mm=strlen(m_senddata)+6;
  1188. l=0;
  1189. for (k=6;k<mm;k++)
  1190. {
  1191. //temp[k]=tempdata[l++];
  1192. temp[k]=m_senddata[l++];
  1193. }
  1194. //m_packet.makepacket(IP,PING,temp);
  1195. m_packet.IP_header.protocol=IP_ICMP;
  1196. m_packet.makeIPpacket(IP_ICMP,ICMP_PING,temp);
  1197. DWORD dwBytesWritten;
  1198. if (WriteFile(hCommDev,m_packet.bPacketTx,m_packet.TxLen,&dwBytesWritten,NULL)<1)
  1199. {
  1200. m_event.AddString("send error.");
  1201. writelog("send error");
  1202. }
  1203. recv=FALSE;
  1204. Display(recv,m_packet.bPacketTx1);
  1205. memset(szPacketRx,0x00,256);
  1206. SetTimer(ID_CLOCK1,50,NULL);
  1207. }
  1208. void CDemoDlg::OnSendA() 
  1209. {
  1210. // TODO: Add your control notification handler code here
  1211. memset(szPacketRx,0x0,256);
  1212. memset(szPacketTx,0x0,256);
  1213. ATState=10;
  1214.     MyReadFile_Flag=false;
  1215. MyReadFile_Flag1=false;
  1216. m_event.ResetContent();
  1217. UpdateData(TRUE);
  1218. DCB dcb;
  1219. hCommDev=CreateFile(m_com,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);//打开串口
  1220. // hCommDev=CreateFile(m_com,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);//打开串口
  1221. if (hCommDev==(HANDLE)-1)
  1222. {
  1223. tempstr.Format(":initiate modem error");
  1224. m_event.AddString(m_com+tempstr);
  1225. writelog(m_com+tempstr);
  1226. }
  1227. else
  1228. {
  1229. tempstr.Format(":initiate modem successfully");
  1230. m_event.AddString(m_com+tempstr);
  1231. writelog(m_com+tempstr);
  1232. }
  1233. /*
  1234. //<wcy
  1235. oCommDev.hEvent = CreateEvent(
  1236. NULL,   // no security attributes 
  1237. FALSE,  // auto reset event 
  1238. FALSE,  // not signaled 
  1239. NULL    // no name 
  1240. );
  1241. if (SetCommMask(hCom, EV_RXFLAG))
  1242. {
  1243. tempstr.Format(":Set Mask successfully!");
  1244. m_event.AddString(m_com+tempstr);
  1245. m_event.AddString(m_com+tempstr);
  1246. }
  1247. else
  1248. {
  1249. tempstr.Format(":Set Mask Error!");
  1250. m_event.AddString(m_com+tempstr);
  1251. m_event.AddString(m_com+tempstr);
  1252. }
  1253. //wcy>
  1254. */
  1255. COMMTIMEOUTS CommTimeOuts;
  1256. CommTimeOuts.ReadIntervalTimeout=MAXDWORD;
  1257. CommTimeOuts.ReadTotalTimeoutMultiplier=0;
  1258. CommTimeOuts.ReadTotalTimeoutConstant=0;
  1259. CommTimeOuts.WriteTotalTimeoutMultiplier=0;
  1260. CommTimeOuts.WriteTotalTimeoutConstant=0;
  1261. BuildCommDCB(m_com+":"+m_baud+",n,8,1",&dcb);//配置串口 8位数据位,1位停止位,物理层
  1262.     dcb.fRtsControl=RTS_CONTROL_DISABLE;
  1263. dcb.fDtrControl=DTR_CONTROL_ENABLE;    // DTR flow control type 2
  1264. //*
  1265. //dcb.fDsrSensitivity=1; // DSR sensitivity 
  1266. //dcb.fTXContinueOnXoff=0; // XOFF continues Tx 
  1267. //dcb.fOutX=0; // XON/XOFF out flow control 
  1268. //dcb.fInX=0;  // XON/XOFF in flow control 
  1269. //dcb.fNull=0; // enable null stripping 
  1270. //dcb.ErrorChar=ErrorChar;
  1271. //  */  
  1272. SetCommState(hCommDev,&dcb);
  1273. SetCommTimeouts(hCommDev,&CommTimeOuts);
  1274. PurgeComm(hCommDev,PURGE_TXCLEAR);//清发送缓冲区
  1275. PurgeComm(hCommDev,PURGE_RXCLEAR);//清接收缓冲区
  1276. ini=TRUE;
  1277. PurgeComm(hCommDev,PURGE_TXCLEAR);//清发送缓冲区
  1278. PurgeComm(hCommDev,PURGE_RXCLEAR);//清接收缓冲区
  1279. memset(temp,0x00,256);
  1280. SetTimer(ID_CLOCK1,50,NULL);
  1281. GetDlgItem(IDC_INICOM)->EnableWindow(false);
  1282. GetDlgItem(IDC_SendA)->EnableWindow(false);
  1283. GetDlgItem(IDC_CloseA)->EnableWindow(true);
  1284. }
  1285. void CDemoDlg::OnCloseA() 
  1286. {
  1287. // TODO: Add your control notification handler code here
  1288. KillTimer(ID_CLOCK1);
  1289. KillTimer(ID_CLOCK2);
  1290. KillTimer(ID_CLOCK3);
  1291. KillTimer(ID_CLOCK4);
  1292. KillTimer(ID_CLOCK5);
  1293. DeleteFile(m_com);
  1294. UpdateData(TRUE);
  1295. //CloseHandle(hCommDev);
  1296. if (CloseHandle(hCommDev))
  1297. {
  1298. tempstr.Format(":Close successfully");
  1299. m_event.AddString(m_com+tempstr);
  1300. writelog(m_com+tempstr);
  1301. }
  1302. else
  1303. {
  1304. tempstr.Format(":Close  error");
  1305. m_event.AddString(m_com+tempstr);
  1306. writelog(m_com+tempstr);
  1307. }
  1308. GetDlgItem(IDC_INICOM)->EnableWindow(true);
  1309. GetDlgItem(IDC_SendA)->EnableWindow(true);
  1310. GetDlgItem(IDC_CloseA)->EnableWindow(false);
  1311. GetDlgItem(IDC_DIAL)->EnableWindow(false);
  1312. GetDlgItem(IDC_PING)->EnableWindow(false);
  1313. GetDlgItem(IDC_UDP)->EnableWindow(false);
  1314. GetDlgItem(IDC_TCP)->EnableWindow(false);
  1315. m_packet.InitPacket();
  1316. }
  1317. void CDemoDlg::OnKeyPressSendBox(short FAR* KeyAscii) 
  1318. {
  1319. // TODO: Add your control notification handler code here
  1320. if (*KeyAscii==0x0d || *KeyAscii==0x0a)
  1321. {
  1322. char strOutMsg[100];
  1323. UpdateData(TRUE);
  1324. strcpy(strOutMsg,m_senddata);
  1325. if (strlen(strOutMsg)>0)
  1326. {
  1327. strcat(strOutMsg,"r");
  1328. DWORD dwBytesWritten;
  1329. WriteFile(hCommDev,strOutMsg,strlen(strOutMsg),&dwBytesWritten,NULL);//初始化MODEM
  1330. tempstr.Format("发:%s",strOutMsg);
  1331. m_event.AddString(tempstr);
  1332. writelog(m_com+tempstr);
  1333. }
  1334. SetTimer(ID_CLOCK4,1,NULL); //timer to clear richtext
  1335. GetDlgItem(IDC_RICHTEXTCTRL1)->EnableWindow(FALSE);
  1336. }
  1337. }
  1338. void CDemoDlg::OnUdp() 
  1339. {
  1340. // TODO: Add your control notification handler code here
  1341. int k,l,j;
  1342. memset(temp,0x00,256);
  1343. PurgeComm(hCommDev,PURGE_TXCLEAR);
  1344. //PurgeComm(hCommDev,PURGE_RXCLEAR);
  1345. UpdateData(TRUE);
  1346. temp[0]=atoi(m_ipOut[0]); //destination address
  1347. temp[1]=atoi(m_ipOut[1]);
  1348. temp[2]=atoi(m_ipOut[2]);
  1349. temp[3]=atoi(m_ipOut[3]);
  1350. temp[4]=atoi(m_srcport)>>8; //source port msb
  1351. temp[5]=atoi(m_srcport)&0x00ff; //source port lsb
  1352. temp[6]=atoi(m_desport)>>8; //destination port msb
  1353. temp[7]=atoi(m_desport)&0x00ff; //destination port lsb
  1354. j=strlen(m_senddata)+8;
  1355. temp[8]=j>>8; //the length of send udp data's msb
  1356. temp[9]=j&0x00ff; //the length of send udp data's lsb
  1357. temp[10]=0;
  1358. temp[11]=0;
  1359. m_packet.bID=0;
  1360. l=0;
  1361. for (k=12;k<j+4;k++)
  1362. {
  1363. temp[k]=m_senddata[l++];
  1364. }
  1365. //format of temp for udp:
  1366. //destination address
  1367. //source port
  1368. //destination port
  1369. //udp data length
  1370. //udp check sum 2bytes msb +lsb
  1371. //udp data
  1372. m_packet.IP_header.protocol=IP_UDP;
  1373. m_packet.makeIPpacket(IP_UDP,NULL,temp);
  1374. DWORD dwBytesWritten;
  1375. if (WriteFile(hCommDev,m_packet.bPacketTx,m_packet.TxLen,&dwBytesWritten,NULL)<1)
  1376. {
  1377. m_event.AddString("send error.");
  1378. writelog("send error");
  1379. }
  1380. recv=FALSE;
  1381. Display(recv,m_packet.bPacketTx1);
  1382. memset(szPacketRx,0x00,256);
  1383. SetTimer(ID_CLOCK1,50,NULL);
  1384. }
  1385. void CDemoDlg::OnTcp() 
  1386. {
  1387. // TODO: Add your control notification handler code here
  1388. }