📄 demodlg.cpp
字号:
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]!='\0')
{
bAscii=pDispstr[i];
itoa(bAscii,szDisplay,16);
//if (bAscii=='0')
// sprintf(szDisplay,"0");
if (szDisplay[0]<='f' && szDisplay[0]>='0'&& szDisplay[1]=='\0')
{
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]!='\0')
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]!='\0')
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
}
void CDemoDlg::OnState()
{
// TODO: Add your control notification handler code here
}
void CDemoDlg::OnSelchangeEvent()
{
// TODO: Add your control notification handler code here
}
void CDemoDlg::OnChangeSendBox()
{
// TODO: Add your control notification handler code here
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -