demoDlg.cpp
资源名称:PPP0208.rar [点击查看]
上传用户:karykuang
上传日期:2010-02-26
资源大小:103k
文件大小:40k
源码类别:
TCP/IP协议栈
开发平台:
Visual C++
- // demoDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "demo.h"
- #include "demoDlg.h"
- #include "Ip.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /*typedef struct t_TREADDATA
- {
- HWND hWnd;
- HANDLE CommDev;
- }THREADDATA;
- UINT MyThreadProc(LPVOID pParam);
- int NEAR ReadCommBlock(HANDLE,LPSTR,int);*/
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- BYTE tempdata[32]=
- {
- 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
- 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70,
- 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x61,
- 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69
- };
- //IP_HEADER IP_header;
- /////////////////////////////////////////////////////////////////////////////
- // CDemoDlg dialog
- CDemoDlg::CDemoDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CDemoDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CDemoDlg)
- m_baud = _T("19200");
- m_called = _T("9163");
- m_com = _T("COM1");
- m_password = _T("163");
- m_userid = _T("163");
- // m_ipad[0] = _T("211");
- // m_ipad[1] = _T("100");
- // m_ipad[2] = _T("31");
- // m_ipad[3] = _T("131");
- //m_ipOut[0] = _T("211"); //www.263.net
- //m_ipOut[1] = _T("100");
- //m_ipOut[2] = _T("31");
- //m_ipOut[3] = _T("131");
- m_ipOut0 = _T("216"); //www.google.com
- m_ipOut1 = _T("239");
- m_ipOut2 = _T("33");
- m_ipOut3 = _T("100");
- m_senddata = _T("AT");
- m_srcport = _T("0");
- m_desport = _T("0");
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CDemoDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CDemoDlg)
- DDX_Control(pDX, IDC_EVENT, m_event);
- DDX_CBString(pDX, IDC_BAUD, m_baud);
- DDX_Text(pDX, IDC_CALLED, m_called);
- DDX_CBString(pDX, IDC_COM, m_com);
- DDX_Text(pDX, IDC_PASSWORD, m_password);
- DDX_Text(pDX, IDC_USERID, m_userid);
- DDX_Text(pDX, IDC_IPAD0, m_ipad0);
- DDX_Text(pDX, IDC_IPAD1, m_ipad1);
- DDX_Text(pDX, IDC_IPAD2, m_ipad2);
- DDX_Text(pDX, IDC_IPAD3, m_ipad3);
- DDX_Text(pDX, IDC_IPAD4, m_ipOut0);
- DDX_Text(pDX, IDC_IPAD5, m_ipOut1);
- DDX_Text(pDX, IDC_IPAD6, m_ipOut2);
- DDX_Text(pDX, IDC_IPAD7, m_ipOut3);
- DDX_Text(pDX, IDC_RICHTEXTCTRL1, m_senddata);
- DDX_Text(pDX, IDC_SRCPORT, m_srcport);
- DDX_Text(pDX, IDC_DESPORT, m_desport);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CDemoDlg, CDialog)
- //{{AFX_MSG_MAP(CDemoDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_INICOM, OnInicom)
- ON_WM_TIMER()
- ON_BN_CLICKED(IDC_DIAL, OnDial)
- ON_MESSAGE(CN_SEND, OnSend)
- ON_BN_CLICKED(IDC_PING, OnPing)
- ON_BN_CLICKED(IDC_SendA, OnSendA)
- ON_BN_CLICKED(IDC_CloseA, OnCloseA)
- ON_BN_CLICKED(IDC_UDP, OnUdp)
- ON_BN_CLICKED(IDC_TCP, OnTcp)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- BEGIN_EVENTSINK_MAP(CDemoDlg, CDialog)
- //{{AFX_EVENTSINK_MAP(CDemoDlg)
- ON_EVENT(CDemoDlg, IDC_SendBox, -603 /* KeyPress */, OnKeyPressSendBox, VTS_PI2)
- //}}AFX_EVENTSINK_MAP
- END_EVENTSINK_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CDemoDlg message handlers
- BOOL CDemoDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Add "About..." menu item to system menu.
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- m_ipOut=&m_ipOut0;
- m_ipad=&m_ipad0;
- framehead=FALSE;
- frameend=FALSE;
- memset(szPacketTx,0x00,256);
- memset(szPacketRx,0x00,256);
- // SetTimer(ID_CLOCK3,50,NULL);
- //OnInicom();
- GetDlgItem(IDC_DIAL)->SetFocus();
- /* 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};
- BYTE teststring1[256]={0x7E,0x80,0x21,0x01,0x07,0x00,0x0a,0x03,0x06,0x3d,0x8b,0x06,0x08,0xdd,0x58,0x7e};
- BYTE teststring2[256]={0x7E,0x80,0x21,0x02,0x07,0x00,0x0a,0x03,0x06,0x3d,0x8b,0x06,0x08,0xdd,0x58,0x7e};
- BYTE teststring3[256]={0x7E,0x80,0x21,0x01,0x08,0x00,0x0a,0x03,0x06,0x3d,0x8b,0x06,0x08,0xdd,0x58,0x7e};
- recv=TRUE;
- Display(recv,teststr);
- // 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};//
- // Display(recv,teststr);
- m_packet.compress=2;
- m_packet.checkpacket(teststr);
- recv=FALSE;
- Display(recv,m_packet.bPacketTx);
- recv=TRUE;
- Display(recv,teststring1);
- m_packet.checkpacket(teststring1);
- recv=FALSE;
- Display(recv,m_packet.bPacketTx);
- recv=TRUE;
- Display(recv,teststring2);
- m_packet.checkpacket(teststring2);
- recv=FALSE;
- Display(recv,m_packet.bPacketTx);
- recv=TRUE;
- Display(recv,teststring3);
- m_packet.checkpacket(teststring3);
- recv=FALSE;
- Display(recv,m_packet.bPacketTx);
- /* BYTE teststr1[64]={0x0a,0x11,0x7E,0xFF,0x7d,0x23,0xc0,0x21};
- 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};
- BYTE teststr3[64]={0x20,0x7E,0xFF,0x7d,0x23,0xc0,0x21,0x00,0xff};
- BYTE teststr4[64]={0x97,0x23,0x7E,0x00,0xff};
- testcat(teststr1);
- testcat(teststr2);
- testcat(teststr3);
- testcat(teststr4);
- // sprintf(teststr,"x7ExFFx03xC0x21x01x01x00x0cx02x06x00x0ax00x00x03x04xc0x23");
- /* THREADDATA m_threaddata;
- m_threaddata.CommDev=hCommDev;
- m_threaddata.CommDev=GetSafeHwnd();
- AfxBeginThread(MyThreadProc,&m_threaddata,THREAD_PRIORITY_NORMAL);
- */
- return TRUE; // return TRUE unless you set the focus to a control
- }
- /*UINT MyThreadProc(LPVOID param)
- {
- DWORD dwEvtMask;
- int nLength;
- BYTE abIn[1024];
- THREADDATA *pData=(THREADDATA*)param;
- while (1)
- { dwEvtMask=0 ;
- WaitCommEvent(pData->CommDev,&dwEvtMask,NULL);
- if ((dwEvtMask&EV_RXCHAR)==EV_RXCHAR)
- {
- do
- {
- if(nLength=ReadCommBlock(pData->CommDev,(LPSTR)abIn,1024))
- {
- //WriteCommBlock((LPSTR)abIn,nLength );
- *(abIn+nLength)=0;
- ::SendMessage(pData->hWnd,CN_SEND,nLength,(LONG)(LPSTR)abIn);
- } //end if
- } while (nLength>0); //end do
- } //end if
- } //end while
- }
- */
- void CDemoDlg::OnSend(LPARAM lParam)
- {
- int len;
- // BYTE InBuf[1024];
- len=(int)lParam;
- //InBuf=(BYTE)wParam;
- // tempstr.Format("receiced %d",len);
- // m_event.AddString(tempstr);
- }
- void CDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CDemoDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CDemoDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- /*typedef struct _DCB {
- DWORD DCBlength; /* sizeof(DCB)
- DWORD BaudRate; /* Baudrate at which running
- DWORD fBinary: 1; /* Binary Mode (skip EOF check)
- DWORD fParity: 1; /* Enable parity checking
- DWORD fOutxCtsFlow:1; /* CTS handshaking on output
- DWORD fOutxDsrFlow:1; /* DSR handshaking on output
- DWORD fDtrControl:2; /* DTR Flow control
- DWORD fDsrSensitivity:1; /* DSR Sensitivity /
- DWORD fTXContinueOnXoff: 1; /* Continue TX when Xoff sent
- DWORD fOutX: 1; /* Enable output X-ON/X-OFF /
- DWORD fInX: 1; /* Enable input X-ON/X-OFF /
- DWORD fErrorChar: 1; /* Enable Err Replacement /
- DWORD fNull: 1; /* Enable Null stripping /
- DWORD fRtsControl:2; /* Rts Flow control /
- DWORD fAbortOnError:1; /* Abort all reads and writes on Error /
- DWORD fDummy2:17; /* Reserved /
- WORD wReserved; /* Not currently used /
- WORD XonLim; /* Transmit X-ON threshold /
- WORD XoffLim; /* Transmit X-OFF threshold /
- BYTE ByteSize; /* Number of bits/byte, 4-8 /
- BYTE Parity; /* 0-4=None,Odd,Even,Mark,Space /
- BYTE StopBits; /* 0,1,2 = 1, 1.5, 2 /
- char XonChar; /* Tx and Rx X-ON character /
- char XoffChar; /* Tx and Rx X-OFF character /
- char ErrorChar; /* Error replacement char /
- char EofChar; /* End of Input character /
- char EvtChar; /* Received Event character /
- WORD wReserved1; /* Fill for now. /
- } DCB, *LPDCB;
- */
- void CDemoDlg::OnInicom()
- {
- // TODO: Add your control notification handler code here
- memset(szPacketRx,0x0,256);
- memset(szPacketTx,0x0,256);
- ATState=0;
- MyReadFile_Flag=false;
- MyReadFile_Flag1=false;
- m_event.ResetContent();
- UpdateData(TRUE);
- DCB dcb;
- hCommDev=CreateFile(m_com,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);//打开串口
- // hCommDev=CreateFile(m_com,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);//打开串口
- if (hCommDev==(HANDLE)-1)
- {
- tempstr.Format(":initiate modem error");
- m_event.AddString(m_com+tempstr);
- writelog(m_com+tempstr);
- }
- else
- {
- tempstr.Format(":initiate modem successfully");
- m_event.AddString(m_com+tempstr);
- writelog(m_com+tempstr);
- }
- /*
- //<wcy
- oCommDev.hEvent = CreateEvent(
- NULL, // no security attributes
- FALSE, // auto reset event
- FALSE, // not signaled
- NULL // no name
- );
- if (SetCommMask(hCom, EV_RXFLAG))
- {
- tempstr.Format(":Set Mask successfully!");
- m_event.AddString(m_com+tempstr);
- m_event.AddString(m_com+tempstr);
- }
- else
- {
- tempstr.Format(":Set Mask Error!");
- m_event.AddString(m_com+tempstr);
- m_event.AddString(m_com+tempstr);
- }
- //wcy>
- */
- COMMTIMEOUTS CommTimeOuts;
- CommTimeOuts.ReadIntervalTimeout=MAXDWORD;
- CommTimeOuts.ReadTotalTimeoutMultiplier=0;
- CommTimeOuts.ReadTotalTimeoutConstant=0;
- CommTimeOuts.WriteTotalTimeoutMultiplier=0;
- CommTimeOuts.WriteTotalTimeoutConstant=0;
- BuildCommDCB(m_com+":"+m_baud+",n,8,1",&dcb);//配置串口 8位数据位,1位停止位,物理层
- dcb.fRtsControl=RTS_CONTROL_DISABLE;
- dcb.fDtrControl=DTR_CONTROL_ENABLE; // DTR flow control type 2
- //*
- //dcb.fDsrSensitivity=1; // DSR sensitivity
- //dcb.fTXContinueOnXoff=0; // XOFF continues Tx
- //dcb.fOutX=0; // XON/XOFF out flow control
- //dcb.fInX=0; // XON/XOFF in flow control
- //dcb.fNull=0; // enable null stripping
- //dcb.ErrorChar=ErrorChar;
- // */
- SetCommState(hCommDev,&dcb);
- SetCommTimeouts(hCommDev,&CommTimeOuts);
- PurgeComm(hCommDev,PURGE_TXCLEAR);//清发送缓冲区
- PurgeComm(hCommDev,PURGE_RXCLEAR);//清接收缓冲区
- char strOutMsg[20];
- DWORD dwBytesWritten;
- sprintf(strOutMsg,"ATr");
- WriteFile(hCommDev,strOutMsg,strlen(strOutMsg),&dwBytesWritten,NULL);//初始化MODEM
- tempstr.Format("发:%s",strOutMsg);
- m_event.AddString(tempstr);
- writelog(m_com+tempstr);
- ini=TRUE;
- PurgeComm(hCommDev,PURGE_TXCLEAR);//清发送缓冲区
- PurgeComm(hCommDev,PURGE_RXCLEAR);//清接收缓冲区
- /*if (!SetCommMask(hCommDev,EV_RXCHAR))
- {
- tempstr.Format(":SetCommMask error!");
- m_event.AddString(m_com+tempstr);
- writelog(m_com+tempstr);
- }*/
- memset(temp,0x00,256);
- SetTimer(ID_CLOCK1,50,NULL);
- GetDlgItem(IDC_INICOM)->EnableWindow(false);
- GetDlgItem(IDC_SendA)->EnableWindow(false);
- GetDlgItem(IDC_CloseA)->EnableWindow(true);
- }
- bool CDemoDlg::OnTimer(UINT nIDEvent)
- {
- BYTE* strCommRecvMsg=new BYTE[256];
- DWORD dwBytesWritten;
- memset(strCommRecvMsg,0x00,256);
- bool bReturn;
- DWORD dwBytesRead;
- int k,l,start=0;
- int mm;
- if(nIDEvent==5)//send icmp ping packet
- {
- KillTimer(ID_CLOCK5);
- PurgeComm(hCommDev,PURGE_TXCLEAR);//清发送缓冲区
- m_packet.makepacket(IPCP,REQ,temp);
- DWORD dwBytesWritten;
- m_packet.IPCP_s=0;
- if (WriteFile(hCommDev,m_packet.bPacketTx,m_packet.TxLen,&dwBytesWritten,NULL)<1)
- {
- m_event.AddString("send error.");
- writelog("send error");
- return false;
- }
- recv=FALSE;
- Display(recv,m_packet.bPacketTx1);
- memset(szPacketRx,0x00,256);
- SetTimer(ID_CLOCK1,50,NULL);
- return true;
- }
- else if(nIDEvent==4)
- {
- KillTimer(ID_CLOCK4);
- SetDlgItemText(IDC_RICHTEXTCTRL1,"");
- GetDlgItem(IDC_RICHTEXTCTRL1)->EnableWindow(true);
- GetDlgItem(IDC_RICHTEXTCTRL1)->SetFocus();
- return true;
- }
- else if(nIDEvent==3)//send icmp ping packet
- {
- KillTimer(ID_CLOCK3);
- return true;
- memset(temp,0x00,256);
- PurgeComm(hCommDev,PURGE_TXCLEAR);
- temp[0]=atoi(m_ipOut[0]);//destination address
- temp[1]=atoi(m_ipOut[1]);
- temp[2]=atoi(m_ipOut[2]);
- temp[3]=atoi(m_ipOut[3]);
- temp[4]=strlen(m_senddata)>>8;
- //temp[4]=32;
- temp[5]=strlen(m_senddata)&0x00ff;
- mm=strlen(m_senddata)+6;
- l=0;
- for (k=6;k<mm;k++)
- {
- //temp[k]=tempdata[l++];
- temp[k]=m_senddata[l++];
- }
- //format of temp for udp:
- //destination address 4bytes
- //source port 2bytes msb+lsb
- //destination port 2bytes msb+lsb
- //udp data length 2bytes msb +lsb
- //udp data
- //m_packet.makepacket(IP,PING,temp);
- m_packet.IP_header.protocol=IP_ICMP;
- m_packet.makeIPpacket(IP_ICMP,ICMP_PING,temp);
- DWORD dwBytesWritten;
- if (WriteFile(hCommDev,m_packet.bPacketTx,m_packet.TxLen,&dwBytesWritten,NULL)<1)
- {
- m_event.AddString("send error.");
- writelog("send error");
- return false;
- }
- recv=FALSE;
- Display(recv,m_packet.bPacketTx1);
- memset(szPacketRx,0x00,256);
- //SetTimer(ID_CLOCK3,6000,NULL);//SEND IP PACKET
- SetTimer(ID_CLOCK1,50,NULL);
- return true;
- }
- else if(nIDEvent==2)
- {
- KillTimer(ID_CLOCK2);
- PurgeComm(hCommDev,PURGE_TXCLEAR);//清发送缓冲区
- m_packet.makepacket(LCP,ACK,temp);
- DWORD dwBytesWritten;
- if (WriteFile(hCommDev,m_packet.bPacketTx,m_packet.TxLen,&dwBytesWritten,NULL)<1)
- {
- m_event.AddString("send error.");
- writelog("send error");
- return false;
- }
- recv=FALSE;
- Display(recv,m_packet.bPacketTx1);
- memset(szPacketRx,0x00,256);
- SetTimer(ID_CLOCK1,50,NULL);
- return true;
- }
- else if (nIDEvent==1)
- {
- UpdateData(TRUE);
- /* BYTE LastLeftBuf[256];
- static bool LastLeft=false;
- if (LastLeft){
- int jj;
- for(jj=0;jj<256;jj++) {
- strCommRecvMsg[jj]=LastLeftBuf[jj];
- if (LastLeftBuf[jj]=' ') break;
- }
- bReturn=ReadFile(hCommDev,strCommRecvMsg+jj,100,&dwBytesRead,NULL);
- LastLeft=false;
- }
- */
- // else {
- bool bReturn=false;
- if (MyReadFile_Flag==false) bReturn=ReadFile(hCommDev,strCommRecvMsg,150,&dwBytesRead,NULL);
- else
- {
- // bReturn=ReadFile(hCommDev,strCommRecvMsg,1,&dwBytesRead,NULL);
- DWORD dwBytesRead1;
- int ii=0;
- dwBytesRead=0;
- //for(;1;) {
- for(ii=0;ii<256;ii++) {
- ReadFile(hCommDev,strCommRecvMsg+ii,1,&dwBytesRead1,NULL);
- if (dwBytesRead1==1){
- if (dwBytesRead==0) KillTimer(ID_CLOCK1);
- bReturn=true;
- dwBytesRead++;
- //if ((strCommRecvMsg[ii]==0x7E) && (dwBytesRead!=1)) break;
- if (strCommRecvMsg[ii]==0x7E) break;
- else
- {
- if ((framehead==FALSE)&&(dwBytesRead==1)&&MyReadFile_Flag1==TRUE)
- {
- strCommRecvMsg[1]=strCommRecvMsg[0];
- strCommRecvMsg[0]=0x7E;
- dwBytesRead++;
- break;
- }
- }//ii++;
- }
- else break;
- }
- }
- /* 读一个字符
- 判断该字符是否7E
- 如果是则停止读数据
- 如果不是则继续读数
- */
- // }
- //BOOL aReturn=WaitCommEvent(hCommDev,EV_RXFLAG,NULL);
- if (dwBytesRead<1)//无新数据,返回
- {
- delete strCommRecvMsg;
- return false;
- }
- if (!bReturn)//失败,返回
- {
- tempstr.Format(":接收失败");
- m_event.AddString(m_com+tempstr);
- writelog(m_com+tempstr);
- delete strCommRecvMsg;
- return false;
- }
- else
- {
- UpdateData(TRUE);
- char* strOutMsg=new char[256];
- memset(strOutMsg,0x00,256);
- CString findstr;
- findstr=strCommRecvMsg;
- tempstr.Format("收:%s",strCommRecvMsg);
- m_event.AddString(tempstr);
- writelog(tempstr);
- if (ATState<8)
- {
- if (findstr.Find("OK",NULL)!=-1 && ini )
- {
- //ini=FALSE;
- switch (ATState)
- {
- case 0:
- sprintf(strOutMsg,"ATE0V1r");
- ATState++;
- break;
- case 1:
- sprintf(strOutMsg,"ATr");
- ATState++;
- break;
- case 2:
- sprintf(strOutMsg,"ATS0=0r");
- ATState++;
- break;
- case 3:
- sprintf(strOutMsg,"ATr");
- ATState++;
- break;
- case 4:
- sprintf(strOutMsg,"ATE0V1r");
- ATState++;
- GetDlgItem(IDC_DIAL)->EnableWindow(TRUE);
- break;
- }
- if (ATState<5)
- {
- WriteFile(hCommDev,strOutMsg,strlen(strOutMsg),&dwBytesWritten,NULL);
- tempstr.Format("发:%s",strOutMsg);
- m_event.AddString(tempstr);
- writelog(m_com+tempstr);
- }
- }
- else if ((findstr.Find("gin",NULL)!=-1) || (findstr.Find("ame",NULL)!=-1) )
- {
- //memset(strOutMsg,0x00,256);
- strcpy(strOutMsg,m_userid);
- strcat(strOutMsg,"r");
- WriteFile(hCommDev,strOutMsg,strlen(strOutMsg),&dwBytesWritten,NULL);//初始化MODEM
- tempstr.Format("发:%s",strOutMsg);
- m_event.AddString(tempstr);
- writelog(tempstr);
- //wcy 2002-
- }
- else if (findstr.Find("rd",NULL)!=-1)
- {
- //memset(strOutMsg,0x00,256);
- strcpy(strOutMsg,m_password);
- strcat(strOutMsg,"r");
- DWORD dwBytesWritten;
- WriteFile(hCommDev,strOutMsg,strlen(strOutMsg),&dwBytesWritten,NULL);//初始化MODEM
- tempstr.Format("发:%s",strOutMsg);
- m_event.AddString(tempstr);
- writelog(tempstr);
- iLen=0;
- MyReadFile_Flag=true;
- }
- else SetTimer(ID_CLOCK1,50,NULL);
- }
- //else SetTimer(ID_CLOCK1,50,NULL);
- if (findstr.Find('~',NULL)!=-1)
- {
- MyReadFile_Flag1=true;
- if (!framehead)
- {
- memset(RecvMsgtemp,0x00,256);
- iLen=0;
- framehead=TRUE;
- for(k=0;k<256;k++)
- {
- if(strCommRecvMsg[k]=='~')
- {
- //if (k==0) break;
- if (strCommRecvMsg[k+1]=='~') k++;
- for(l=0;l<256,k<256;l++,k++)
- {
- if(strCommRecvMsg[k]!=' ')
- RecvMsgtemp[l]=strCommRecvMsg[k];
- else
- break;
- }
- break;
- }
- }
- bytecat(RecvMsgtemp);
- }else//if(!framehead)
- {
- framehead=FALSE;
- frameend=TRUE;
- for(k=0;k<256;k++)
- {
- if(strCommRecvMsg[k]=='~')
- {
- if (strCommRecvMsg[k+1]=='~')
- {
- framehead=FALSE;
- frameend=FALSE;
- }
- else
- {
- for(int l=0;l<=k;l++)
- {
- RecvMsgtemp[l]=strCommRecvMsg[l];// modified by wcy
- }
- }
- break;
- }
- }
- bytecat(RecvMsgtemp);
- BYTE select;
- select=0x00;
- for(int i=0;i<256;i++)
- {
- if (szPacketRx[i]==0x7d)
- {
- if(szPacketRx[i+1]==0x5e)//0x7e
- {
- select=0x7e;
- szPacketRx[i+1]=select;
- }else if(szPacketRx[i+1]==0x5d)//0x7d
- {
- select=0x7d;
- szPacketRx[i+1]=select;
- }else//<0x20
- {
- select=szPacketRx[i+1]-0x20;
- szPacketRx[i+1]=select;
- }
- for(int j=i;j<256;j++)
- {
- szPacketRx[j]=szPacketRx[j+1];
- }
- }//end if (szPacketRx[i]==0x7d)
- }//end for
- framehead=FALSE;
- frameend=FALSE;
- KillTimer(ID_CLOCK1);
- if (!m_packet.checkpacket(szPacketRx))
- {
- writelog("FCS check error");
- m_event.AddString("FCS check error");
- return false;
- }
- recv=TRUE;
- Display(recv,szPacketRx);
- //if(WriteFile(hCommDev,m_packet.bPacketTx,iLen,&dwBytesWritten,NULL))
- if(WriteFile(hCommDev,m_packet.bPacketTx,m_packet.TxLen,&dwBytesWritten,NULL))
- {
- recv=FALSE;
- //Display(recv,m_packet.bPacketTx);
- //Display(recv,&(m_packet.bPacketTx1[0]));
- if ( m_packet.TxLen!=0 ) Display(recv,m_packet.bPacketTx1);
- }
- int address=0;
- CString tempad;
- switch(m_packet.bState)
- {
- case LCPOPEN:
- GetDlgItem(IDC_STATE)->SetWindowText("正在建立连接...");
- for(i=0;i<256;i++)
- temp[i]=szPacketRx[i];
- SetTimer(ID_CLOCK2,500,NULL);
- break;
- case IPCPOPEN:
- GetDlgItem(IDC_STATE)->SetWindowText("正在注册计算机...");
- for(i=0;i<256;i++)
- {
- temp[i]=szPacketRx[i];
- }
- SetTimer(ID_CLOCK5,500,NULL);
- break;
- case IPCPOK:
- //m_packet.bState=IPSTART;
- tempad.Format("%u",m_packet.bIpaddress[0]);//首位
- m_ipad[0]=tempad;
- tempad.Format("%u",m_packet.bIpaddress[1]);//第二位
- m_ipad[1]=tempad;
- tempad.Format("%u",m_packet.bIpaddress[2]);//第三位
- m_ipad[2]=tempad;
- tempad.Format("%u",m_packet.bIpaddress[3]);//第四位
- m_ipad[3]=tempad;
- UpdateData(FALSE);
- GetDlgItem(IDC_STATE)->SetWindowText("连接已建立");
- //SetTimer(ID_CLOCK3,6000,NULL);//SEND IP PACKET
- //*
- if (m_packet.IPCP_s==IPCPREOPEN)
- {
- for(i=0;i<256;i++)
- {
- temp[i]=szPacketRx[i];
- }
- SetTimer(ID_CLOCK5,500,NULL);
- KillTimer(ID_CLOCK1);
- }
- //* /
- memset(szPacketRx,0x00,256);
- SetTimer(ID_CLOCK1,50,NULL);
- GetDlgItem(IDC_PING)->EnableWindow(true);
- GetDlgItem(IDC_UDP)->EnableWindow(true);
- GetDlgItem(IDC_TCP)->EnableWindow(true);
- //tempstr.Format("Local IP Adress: %s . %s . %s . %s",m_ipad[0],m_ipad[1],m_ipad[2],m_ipad[3]);
- //m_event.AddString(tempstr);
- //writelog(tempstr);
- break;
- default:
- memset(szPacketRx,0x00,256);
- SetTimer(ID_CLOCK1,50,NULL);
- break;
- }//end switch
- }//end if(framehead)
- }//end if(findstr.Find('~',NULL)!=-1)
- memset(RecvMsgtemp,0x0,256);
- if (framehead && (findstr.Find('~',NULL)==-1))
- {
- bytecat(strCommRecvMsg);
- }
- delete strOutMsg;
- }//receive valid data
- PurgeComm(hCommDev,PURGE_TXCLEAR);//清发送缓冲区
- // PurgeComm(hCommDev,PURGE_RXCLEAR);//清接收缓冲区
- delete strCommRecvMsg;
- return true;
- }// ID_CLOCK1
- return true;
- CDialog::OnTimer(nIDEvent);
- }
- void CDemoDlg::bytecat(BYTE* strCommRecvMsg)
- {
- int k;
- for(k=0;k<256;k++)
- {
- if (strCommRecvMsg[k]!=' ')
- {
- szPacketRx[k+iLen]=strCommRecvMsg[k];
- }
- else
- {
- iLen+=k;
- break;
- }//end if
- }//end for
- }
- void CDemoDlg::OnDial()
- {
- // TODO: Add your control notification handler code here
- UpdateData(TRUE);
- char* strOutMsg=new char[256];
- memset(strOutMsg,0x00,256);
- strcpy(strOutMsg,"ATDT");
- strcat(strOutMsg,m_called);
- strcat(strOutMsg,"r");
- DWORD dwBytesWritten;
- WriteFile(hCommDev,strOutMsg,strlen(strOutMsg),&dwBytesWritten,NULL);//初始化MODEM
- tempstr.Format("发:%s",strOutMsg);
- m_event.AddString(tempstr);
- writelog(tempstr);
- GetDlgItem(IDC_STATE)->SetWindowText("正在拨号...");
- delete strOutMsg;
- GetDlgItem(IDC_DIAL)->EnableWindow(false);
- // GetDlgItem(IDC_LOGIN)->EnableWindow(TRUE);
- }
- /*
- void CDemoDlg::OnLogin()
- {
- DWORD dwBytesWritten;
- char* strOutMsg=new char[256];
- UpdateData(TRUE);
- memset(strOutMsg,0x00,256);
- strcpy(strOutMsg,m_password);
- strcat(strOutMsg,"r");
- WriteFile(hCommDev,strOutMsg,strlen(strOutMsg),&dwBytesWritten,NULL);//初始化MODEM
- tempstr.Format("发:%s",strOutMsg);
- m_event.AddString(tempstr);
- writelog(tempstr);
- }
- */
- void CDemoDlg::writelog(CString temp)
- {
- CStdioFile file;
- CString filename;
- CTime time;
- time=CTime::GetCurrentTime();
- filename=time.Format("%Y%m%d");
- file.Open(filename+".log",CFile::modeNoTruncate|CFile::modeCreate|CFile::modeWrite|CFile::typeText);
- file.SeekToEnd();
- temp=time.Format("%Y.%m.%d %H:%M:%S ")+temp+"rn";
- file.Write(temp,temp.GetLength());
- file.Close();
- }
- /*int NEAR ReadCommBlock(HANDLE COMDEV,LPSTR lpszBlock,int nMaxLength)
- {
- BOOL fReadStat ;
- COMSTAT ComStat ;
- DWORD dwErrorFlags;
- DWORD dwLength;
- // DWORD dwError;
- //if (!npGPSInfo) return(FALSE);
- ClearCommError(COMDEV,&dwErrorFlags,&ComStat);
- dwLength=min((DWORD)nMaxLength,ComStat.cbInQue);
- if (dwLength>0)
- {
- fReadStat=ReadFile(COMDEV,lpszBlock,dwLength,&dwLength,NULL);
- if (!fReadStat)
- {
- if (GetLastError()==ERROR_IO_PENDING)
- {
- OutputDebugString("nrIO Pending");
- /* while(!GetOverlappedResult(COMDEV(npGPSInfo),&READ_OS(npGPSInfo),&dwLength,TRUE))
- {
- dwError=GetLastError();
- if(dwError == ERROR_IO_INCOMPLETE)
- continue;
- }*/ //end while
- /* }
- else
- {
- dwLength=0;
- ClearCommError(COMDEV,&dwErrorFlags,&ComStat);
- }//end if
- }//end if
- }
- return ( dwLength ) ;
- }
- */
- /*void CDemoDlg::OnReadfile()
- {
- CStdioFile file;
- CString filename;
- CTime time;
- char* content=new char[256];
- /* time=CTime::GetCurrentTime();
- filename=time.Format("%Y%m%d");
- file.Open(filename+".log",CFile::modeNoTruncate|CFile::modeCreate|CFile::modeRead|CFile::typeText);
- file.SeekToBegin();
- file.Read(content,256);
- int len=strlen(content);
- for (int i=0;i<len;i++)
- {
- tempstr.Format(": %x",content[i]);
- m_event.AddString(tempstr);
- // writelog(m_com+tempstr);
- }*/
- /* BYTE tempstr[256];
- memset(tempstr,0x00,256);
- DWORD dwBytesWritten;
- m_packet.makepacket(PAP,REQ,tempstr);
- if( WriteFile(hCommDev,m_packet.bPacketTx,m_packet.TxLen,&dwBytesWritten,NULL))
- {
- recv=FALSE;
- Display(recv,m_packet.bPacketTx);
- }
- }*/
- void CDemoDlg::Display(BOOL recv,BYTE* pDispstr)
- {
- char szTemp[256];
- char tempbuf[100];
- CString tempsrcip[4];
- CString tempdesip[4];
- BYTE bAscii;
- BYTE mm;
- 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"};
- char keybytes[11][20]={
- "7E FF 03 C0 21 01", //0LCP_REQ
- "7E FF 03 C0 21 04", //1LCP_REJ
- "7E FF 03 C0 21 02", //2LCP_ACK
- "7E FF 03 C0 23 01", //3PAP_REQ
- "7E FF 03 C0 23 02", //4PAP_ACK
- "7E 80 21 01", //5IPCP_REQ
- "7E 80 21 02", //6IPCP_ACK
- "7E 80 21 03", //7IPCP_NAK
- "7E 80 21 04", //8IPCP_REJ
- "7E 00 21 45", //9PINGOUT
- "7E 21 45" //10PINGIN
- };
- memset(szTemp,0x00,256);
- for(int i=0;i<MAXLENGTH;i++)
- {
- // if(pDispstr[i]!=' ')
- {
- bAscii=pDispstr[i];
- itoa(bAscii,szDisplay,16);
- //if (bAscii=='0')
- // sprintf(szDisplay,"0");
- if (szDisplay[0]<='f' && szDisplay[0]>='0'&& szDisplay[1]==' ')
- {
- szDisplay[1]=szDisplay[0];
- szDisplay[0]='0';
- }
- for(int j=0;j<3;j++)
- {
- if(szDisplay[j]>='a' && szDisplay[j]<'z')
- szDisplay[j]-=32;
- }
- strcat(szTemp,szDisplay);
- strcat(szTemp," ");
- }//end if
- }
- if (recv)
- {
- for(int kk=0;kk<11;kk++)
- {
- if (strncmp(keybytes[kk], szTemp, strlen(keybytes[kk]))==0)
- {
- if ( kk==9 || kk==10)
- {
- for (mm=0;mm<4;mm++)
- tempsrcip[mm].Format("%u",m_packet.IP_header.SrcIP[mm]);
- for (mm=0;mm<4;mm++)
- tempdesip[mm].Format("%u",m_packet.IP_header.DesIP[mm]);
- if (m_packet.IP_header.protocol==IP_ICMP)
- {
- sprintf(tempbuf," :In -->IP --- %-10s from:%3s.%3s.%3s.%3s to:%3s.%3s.%3s.%3s",
- keystr[11],
- tempsrcip[0],tempsrcip[1],tempsrcip[2],tempsrcip[3],
- tempdesip[0],tempdesip[1],tempdesip[2],tempdesip[3]
- );
- }
- else if (m_packet.IP_header.protocol==IP_UDP)
- {
- sprintf(tempbuf," :In -->IP --- %-10s from:%3s.%3s.%3s.%3s to:%3s.%3s.%3s.%3s",
- keystr[12],
- tempsrcip[0],tempsrcip[1],tempsrcip[2],tempsrcip[3],
- tempdesip[0],tempdesip[1],tempdesip[2],tempdesip[3]
- );
- }
- else
- {
- sprintf(tempbuf," :In -->IP Unkown Type from:%3s.%3s.%3s.%3s to:%3s.%3s.%3s.%3s",
- //keystr[12],
- tempsrcip[0],tempsrcip[1],tempsrcip[2],tempsrcip[3],
- tempdesip[0],tempdesip[1],tempdesip[2],tempdesip[3]
- );
- }
- //strcat(tempbuf, tempdesip[0]
- m_event.AddString(tempbuf);
- writelog(tempbuf);
- }
- else
- {
- sprintf(tempbuf," :In -->%s",keystr[kk]);
- m_event.AddString(tempbuf);
- writelog(tempbuf);
- }
- break;
- }
- }
- }
- else
- {
- for(int kk=0;kk<11;kk++)
- {
- if (strncmp(keybytes[kk], szTemp, strlen(keybytes[kk]))==0)
- {
- if ( kk==9 || kk==10 && (
- m_packet.IP_header.protocol==IP_ICMP ||
- m_packet.IP_header.protocol==IP_UDP
- ))
- {
- for (mm=0;mm<4;mm++)
- tempsrcip[mm].Format("%u",m_packet.IP_header.SrcIP[mm]);
- for (mm=0;mm<4;mm++)
- tempdesip[mm].Format("%u",m_packet.IP_header.DesIP[mm]);
- if (m_packet.IP_header.protocol==IP_ICMP)
- {
- sprintf(tempbuf," :Out-->IP --- %-10s from:%3s.%3s.%3s.%3s to:%3s.%3s.%3s.%3s",
- keystr[11],
- tempsrcip[0],tempsrcip[1],tempsrcip[2],tempsrcip[3],
- tempdesip[0],tempdesip[1],tempdesip[2],tempdesip[3]
- );
- }
- else if (m_packet.IP_header.protocol==IP_UDP)
- {
- sprintf(tempbuf," :Out-->IP --- %-10s from:%3s.%3s.%3s.%3s to:%3s.%3s.%3s.%3s",
- keystr[12],
- tempsrcip[0],tempsrcip[1],tempsrcip[2],tempsrcip[3],
- tempdesip[0],tempdesip[1],tempdesip[2],tempdesip[3]
- );
- }
- else
- {
- sprintf(tempbuf," :Out-->IP Unkown Type from:%3s.%3s.%3s.%3s to:%3s.%3s.%3s.%3s",
- //keystr[12],
- tempsrcip[0],tempsrcip[1],tempsrcip[2],tempsrcip[3],
- tempdesip[0],tempdesip[1],tempdesip[2],tempdesip[3]
- );
- }
- m_event.AddString(tempbuf);
- writelog(tempbuf);
- }
- else
- {
- sprintf(tempbuf," :Out<--%s",keystr[kk]);
- m_event.AddString(tempbuf);
- writelog(tempbuf);
- }
- break;
- }
- }
- }
- m_event.AddString(szTemp);
- if (recv) tempstr.Format("收:%s",szTemp);
- else tempstr.Format("发:%s",szTemp);
- writelog(tempstr);
- }
- void CDemoDlg::testcat(BYTE* strCommRecvMsg)
- {
- BYTE *RecvMsgtemp1=new BYTE[256];
- memset(RecvMsgtemp1,0x00,256);
- int l,k;
- CString findstr;
- findstr=strCommRecvMsg;
- if (findstr.Find('~',NULL)!=-1)
- {
- if (!framehead)
- {
- iLen=0;
- framehead=TRUE;
- for(k=0;k<256;k++)
- {
- if(strCommRecvMsg[k]=='~')
- {
- // if (k==0) break;
- for(l=0;l<256,k<256;l++,k++)
- {
- if(strCommRecvMsg[k]!=' ')
- RecvMsgtemp1[l]=strCommRecvMsg[k];
- else
- break;
- }
- bytecat(RecvMsgtemp1);
- break;
- }//end if(strCommRecvMsg[k]='~')
- }//end for
- }else
- {
- framehead=FALSE;
- frameend=TRUE;
- for(k=0;k<256;k++)
- {
- if(strCommRecvMsg[k]=='~')
- {
- for(int l=0;l<=k;l++)
- {
- if(strCommRecvMsg[l]!=' ')
- RecvMsgtemp1[l]=strCommRecvMsg[l];
- else
- break;
- }//end for
- bytecat(RecvMsgtemp1);
- break;
- }//end if
- }//end for
- BYTE* select=new BYTE[256];
- memset(select,0x00,256);
- for(int i=0;i<256;i++)
- {
- if (szPacketRx[i]==0x7d)
- {
- if(szPacketRx[i+1]==0x5e)//0x7e
- {
- select[i]=0x7e;
- szPacketRx[i+1]=select[i];
- }else if(szPacketRx[i+1]==0x5d)//0x7d
- {
- select[i]=0x7d;
- szPacketRx[i+1]=select[i];
- }else//<0x20
- {
- select[i]=szPacketRx[i+1]-0x20;
- szPacketRx[i+1]=select[i];
- }
- for(int j=i;j<256;j++)
- {
- szPacketRx[j]=szPacketRx[j+1];
- }//end for
- }//end if
- }//end for
- delete select;
- recv=TRUE;
- Display(recv,szPacketRx);
- memset(szPacketRx,0x00,256);
- // m_packet.checkpacket(szPacketRx);
- // recv=FALSE;
- // Display(recv,m_packet.bPacketTx);
- }//end if(!framehead)
- }//end if(findstr.Find('~',NULL)!=-1)
- delete RecvMsgtemp1;
- if (framehead && (findstr.Find('~',NULL)==-1))
- {
- bytecat(strCommRecvMsg);
- }
- }
- void CDemoDlg::OnPing()
- {
- int k,l,mm;
- memset(temp,0x00,256);
- PurgeComm(hCommDev,PURGE_TXCLEAR);
- //PurgeComm(hCommDev,PURGE_RXCLEAR);
- UpdateData(TRUE);
- temp[0]=atoi(m_ipOut[0]);//destination address
- temp[1]=atoi(m_ipOut[1]);
- temp[2]=atoi(m_ipOut[2]);
- temp[3]=atoi(m_ipOut[3]);
- //m_packet.bID=0;
- temp[4]=strlen(m_senddata)>>8;
- //temp[4]=32;
- temp[5]=strlen(m_senddata)&0x00ff;
- mm=strlen(m_senddata)+6;
- l=0;
- for (k=6;k<mm;k++)
- {
- //temp[k]=tempdata[l++];
- temp[k]=m_senddata[l++];
- }
- //m_packet.makepacket(IP,PING,temp);
- m_packet.IP_header.protocol=IP_ICMP;
- m_packet.makeIPpacket(IP_ICMP,ICMP_PING,temp);
- DWORD dwBytesWritten;
- if (WriteFile(hCommDev,m_packet.bPacketTx,m_packet.TxLen,&dwBytesWritten,NULL)<1)
- {
- m_event.AddString("send error.");
- writelog("send error");
- }
- recv=FALSE;
- Display(recv,m_packet.bPacketTx1);
- memset(szPacketRx,0x00,256);
- SetTimer(ID_CLOCK1,50,NULL);
- }
- void CDemoDlg::OnSendA()
- {
- // TODO: Add your control notification handler code here
- memset(szPacketRx,0x0,256);
- memset(szPacketTx,0x0,256);
- ATState=10;
- MyReadFile_Flag=false;
- MyReadFile_Flag1=false;
- m_event.ResetContent();
- UpdateData(TRUE);
- DCB dcb;
- hCommDev=CreateFile(m_com,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);//打开串口
- // hCommDev=CreateFile(m_com,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);//打开串口
- if (hCommDev==(HANDLE)-1)
- {
- tempstr.Format(":initiate modem error");
- m_event.AddString(m_com+tempstr);
- writelog(m_com+tempstr);
- }
- else
- {
- tempstr.Format(":initiate modem successfully");
- m_event.AddString(m_com+tempstr);
- writelog(m_com+tempstr);
- }
- /*
- //<wcy
- oCommDev.hEvent = CreateEvent(
- NULL, // no security attributes
- FALSE, // auto reset event
- FALSE, // not signaled
- NULL // no name
- );
- if (SetCommMask(hCom, EV_RXFLAG))
- {
- tempstr.Format(":Set Mask successfully!");
- m_event.AddString(m_com+tempstr);
- m_event.AddString(m_com+tempstr);
- }
- else
- {
- tempstr.Format(":Set Mask Error!");
- m_event.AddString(m_com+tempstr);
- m_event.AddString(m_com+tempstr);
- }
- //wcy>
- */
- COMMTIMEOUTS CommTimeOuts;
- CommTimeOuts.ReadIntervalTimeout=MAXDWORD;
- CommTimeOuts.ReadTotalTimeoutMultiplier=0;
- CommTimeOuts.ReadTotalTimeoutConstant=0;
- CommTimeOuts.WriteTotalTimeoutMultiplier=0;
- CommTimeOuts.WriteTotalTimeoutConstant=0;
- BuildCommDCB(m_com+":"+m_baud+",n,8,1",&dcb);//配置串口 8位数据位,1位停止位,物理层
- dcb.fRtsControl=RTS_CONTROL_DISABLE;
- dcb.fDtrControl=DTR_CONTROL_ENABLE; // DTR flow control type 2
- //*
- //dcb.fDsrSensitivity=1; // DSR sensitivity
- //dcb.fTXContinueOnXoff=0; // XOFF continues Tx
- //dcb.fOutX=0; // XON/XOFF out flow control
- //dcb.fInX=0; // XON/XOFF in flow control
- //dcb.fNull=0; // enable null stripping
- //dcb.ErrorChar=ErrorChar;
- // */
- SetCommState(hCommDev,&dcb);
- SetCommTimeouts(hCommDev,&CommTimeOuts);
- PurgeComm(hCommDev,PURGE_TXCLEAR);//清发送缓冲区
- PurgeComm(hCommDev,PURGE_RXCLEAR);//清接收缓冲区
- ini=TRUE;
- PurgeComm(hCommDev,PURGE_TXCLEAR);//清发送缓冲区
- PurgeComm(hCommDev,PURGE_RXCLEAR);//清接收缓冲区
- memset(temp,0x00,256);
- SetTimer(ID_CLOCK1,50,NULL);
- GetDlgItem(IDC_INICOM)->EnableWindow(false);
- GetDlgItem(IDC_SendA)->EnableWindow(false);
- GetDlgItem(IDC_CloseA)->EnableWindow(true);
- }
- void CDemoDlg::OnCloseA()
- {
- // TODO: Add your control notification handler code here
- KillTimer(ID_CLOCK1);
- KillTimer(ID_CLOCK2);
- KillTimer(ID_CLOCK3);
- KillTimer(ID_CLOCK4);
- KillTimer(ID_CLOCK5);
- DeleteFile(m_com);
- UpdateData(TRUE);
- //CloseHandle(hCommDev);
- if (CloseHandle(hCommDev))
- {
- tempstr.Format(":Close successfully");
- m_event.AddString(m_com+tempstr);
- writelog(m_com+tempstr);
- }
- else
- {
- tempstr.Format(":Close error");
- m_event.AddString(m_com+tempstr);
- writelog(m_com+tempstr);
- }
- GetDlgItem(IDC_INICOM)->EnableWindow(true);
- GetDlgItem(IDC_SendA)->EnableWindow(true);
- GetDlgItem(IDC_CloseA)->EnableWindow(false);
- GetDlgItem(IDC_DIAL)->EnableWindow(false);
- GetDlgItem(IDC_PING)->EnableWindow(false);
- GetDlgItem(IDC_UDP)->EnableWindow(false);
- GetDlgItem(IDC_TCP)->EnableWindow(false);
- m_packet.InitPacket();
- }
- void CDemoDlg::OnKeyPressSendBox(short FAR* KeyAscii)
- {
- // TODO: Add your control notification handler code here
- if (*KeyAscii==0x0d || *KeyAscii==0x0a)
- {
- char strOutMsg[100];
- UpdateData(TRUE);
- strcpy(strOutMsg,m_senddata);
- if (strlen(strOutMsg)>0)
- {
- strcat(strOutMsg,"r");
- DWORD dwBytesWritten;
- WriteFile(hCommDev,strOutMsg,strlen(strOutMsg),&dwBytesWritten,NULL);//初始化MODEM
- tempstr.Format("发:%s",strOutMsg);
- m_event.AddString(tempstr);
- writelog(m_com+tempstr);
- }
- SetTimer(ID_CLOCK4,1,NULL); //timer to clear richtext
- GetDlgItem(IDC_RICHTEXTCTRL1)->EnableWindow(FALSE);
- }
- }
- void CDemoDlg::OnUdp()
- {
- // TODO: Add your control notification handler code here
- int k,l,j;
- memset(temp,0x00,256);
- PurgeComm(hCommDev,PURGE_TXCLEAR);
- //PurgeComm(hCommDev,PURGE_RXCLEAR);
- UpdateData(TRUE);
- temp[0]=atoi(m_ipOut[0]); //destination address
- temp[1]=atoi(m_ipOut[1]);
- temp[2]=atoi(m_ipOut[2]);
- temp[3]=atoi(m_ipOut[3]);
- temp[4]=atoi(m_srcport)>>8; //source port msb
- temp[5]=atoi(m_srcport)&0x00ff; //source port lsb
- temp[6]=atoi(m_desport)>>8; //destination port msb
- temp[7]=atoi(m_desport)&0x00ff; //destination port lsb
- j=strlen(m_senddata)+8;
- temp[8]=j>>8; //the length of send udp data's msb
- temp[9]=j&0x00ff; //the length of send udp data's lsb
- temp[10]=0;
- temp[11]=0;
- m_packet.bID=0;
- l=0;
- for (k=12;k<j+4;k++)
- {
- temp[k]=m_senddata[l++];
- }
- //format of temp for udp:
- //destination address
- //source port
- //destination port
- //udp data length
- //udp check sum 2bytes msb +lsb
- //udp data
- m_packet.IP_header.protocol=IP_UDP;
- m_packet.makeIPpacket(IP_UDP,NULL,temp);
- DWORD dwBytesWritten;
- if (WriteFile(hCommDev,m_packet.bPacketTx,m_packet.TxLen,&dwBytesWritten,NULL)<1)
- {
- m_event.AddString("send error.");
- writelog("send error");
- }
- recv=FALSE;
- Display(recv,m_packet.bPacketTx1);
- memset(szPacketRx,0x00,256);
- SetTimer(ID_CLOCK1,50,NULL);
- }
- void CDemoDlg::OnTcp()
- {
- // TODO: Add your control notification handler code here
- }