📄 serialappdlg.cpp
字号:
if(nLength>0)
{
start=GetTickCount();
memcpy(bufp,abIn,nLength);
bufp+=(long)nLength;
for(ci=0;ci<nLength;ci++)
check+=abIn[ci]+(abIn[ci]&0x80?0xff00:0);
readlen+=(long)nLength;
}
if((GetTickCount()-start)>TIMELIMIT)
{
// SendCommMsg(CSM_TIMEOUT,0);
// return FALSE;
::AfxMessageBox("RSDTIME TOO SHORT");
bRet=FALSE;
goto clearrub;
}
}while(readlen<templen);
m_Port.m_bBlockRead=FALSE;
check-=(*(bufp-1)+(*(bufp-1)&0x80?0xff00:0));
check-=(*(bufp-2)+(*(bufp-2)&0x80?0xff00:0));
if((readlen!=templen)||(check>>8)!=*(bufp-2)||(check&0xff)!=*(bufp-1))
{
bufp=old_bufp;
step=3;
if (ReSendSum>0)
{
ReSendSum--;
WriteCommBlock((unsigned char*) RESEND,1);
}
else
{
// AfxMessageBox("Fail");,0);
// return FALSE;
char error[100];
sprintf(error,"readlen %d\n",readlen);
::AfxMessageBox(error);
bRet=FALSE;
goto clearrub;
}
break;
}
fwrite(old_bufp,1,templen-2,RSF);
WriteCommBlock((unsigned char*) ACK, 1);
break;
case 4:
Sleep(20);
nLength = ReadCommBlock( abIn, MAXBLOCK );
if((nLength==3)&&(abIn[0]==6)&&(abIn[1]==0)&&(abIn[2]==6))
{
start=GetTickCount();
step=5;
WriteCommBlock( (unsigned char*)ACK, 1);
};
break;
default:
break;
};
bTimeout = (GetTickCount()-start>=SYNTIME);
}while((step<5)&&(!bTimeout));
clearrub:
m_Port.m_bBlockRead=FALSE;
free(buf);
// if(step!=5)
// fwrite("\0\0\0\x1\0\x1",1,6,RSF);
fclose(RSF);
return bRet;
}
int CSerialAppDlg::WriteCommBlock(unsigned char *pBlock, int nLength)
{
int count=0;
m_Port.m_nWriteBufferSize=nPackSize+2;
if(m_Port.m_szWriteBuffer)
delete m_Port.m_szWriteBuffer;
m_Port.m_szWriteBuffer=new BYTE[m_Port.m_nWriteBufferSize];
while(nLength > m_Port.m_nWriteBufferSize)
{
m_Port.WriteToPort(pBlock+count,m_Port.m_nWriteBufferSize);
nLength-=m_Port.m_nWriteBufferSize;
count+=m_Port.m_nWriteBufferSize;
// Sleep(10);
}
m_Port.WriteToPort(pBlock+count,nLength);
fwrite(pBlock,1,count+nLength,fsend);
return 0;
}
//DEL void CSerialAppDlg::OnFiletransfer()
//DEL {
//DEL // TODO: Add your command handler code here
//DEL CWinThread* sendThread=AfxBeginThread(CSerialAppDlg::SendThread,this);
//DEL }
UINT CSerialAppDlg::ReceiveThread(LPVOID pParam)
{
DWORD start=::GetTickCount();
CSerialAppDlg* pDlg=(CSerialAppDlg*)pParam;
CWnd* pWnd=pDlg->GetDlgItem(IDC_MSG);
pWnd->EnableWindow(FALSE);
BOOL bSuccess=pDlg->ReceiveFile();
CSerialAppDlg::file=0;
if(bSuccess)
{
AfxMessageBox("Receive File Complete");
char temp[100];
sprintf(temp,"Receive time:%d",::GetTickCount()-start);
pDlg->m_wndShow.ShowMessage("Status: ",CString(temp));
}
else
AfxMessageBox("Receive File Fail");
pDlg->m_ProgressCtrl.SetPos(0);
pWnd->EnableWindow(TRUE);
return 0;
}
UINT CSerialAppDlg::SendThread(LPVOID pParam)
{
DWORD start=::GetTickCount();
file=1;
CSerialAppDlg* pDlg=(CSerialAppDlg*)pParam;
CWnd* pWnd=pDlg->GetDlgItem(IDC_MSG);
pWnd->EnableWindow(FALSE);
BOOL bSuccess=pDlg->SendFile();
file=0;
if(bSuccess)
{
AfxMessageBox("Send File Complete");
char temp[100];
sprintf(temp,"Send time:%d",::GetTickCount()-start);
pDlg->m_wndShow.ShowMessage("Status: ",CString(temp));
}
else
AfxMessageBox("Send File Fail");
pDlg->m_ProgressCtrl.SetPos(0);
pWnd->EnableWindow(TRUE);
// pDlg->m_btnSend.EnableWindow(TRUE);
return 0;
}
BOOL CSerialAppDlg::SendFile()
{
CString path;
CWnd* pWnd = GetDlgItem(IDC_EDIT_SENDFILE);
pWnd->GetWindowText(path);
if(path=="")
{
CFileDialog dlg(TRUE);
if(dlg.DoModal()==IDOK)
{
path = dlg.GetPathName();
CWnd* pWnd = GetDlgItem(IDC_EDIT_SENDFILE);
pWnd->SetWindowText(path);
}
}
if(path=="") return FALSE;
int step,old_step;
BYTE *buf,*bufp,*old_bufp;
BOOL bTimeout;
BOOL bRet=TRUE;
WORD RecordNum=0;
WORD totalRec=0;
FILE *RSF;
DWORD start;
int nLength;
const int MAXBLOCK=nPackSize+10;
BYTE abIn[ MAXBLOCK];
long filelen,templen,totallen;//,readlen;
// WORD check,ci;
RSF=fopen(path,"rb");
if(!RSF)
{
AfxMessageBox("Fail");
return FALSE;
}
filelen=_filelength(_fileno(RSF));
buf=new BYTE[filelen];
if(!buf)
{
AfxMessageBox("Fail");
fclose(RSF);
return FALSE;
}
fread(buf,1,filelen,RSF);
fclose(RSF);
totalRec=filelen/nPackSize+1;
m_ProgressCtrl.SetRange(0,totalRec);
buf=FormatPack(buf,filelen);
step=0;
bufp=buf;
old_step=step;
old_bufp=bufp;
totallen=0;
start=GetTickCount();
nLength=0;
bTimeout=FALSE;
while((nLength==0)&&(!bTimeout))
{
WriteCommBlock((unsigned char*)SYN,1);
Sleep(50);
nLength=m_strReceived.GetLength();
bTimeout = (GetTickCount()-start>=SYNTIME);
}
if (bTimeout) {
// SendCommMsg(CSM_TIMEOUT,0);
AfxMessageBox("TimeOut 1");
return FALSE;
}
do{
bTimeout=FALSE;
nLength=0;
start=GetTickCount();
while((nLength==0)&&(!bTimeout))
{
nLength=m_strReceived.GetLength();
bTimeout = (GetTickCount()-start>=SYNTIME);
}
if (bTimeout) {
// SendCommMsg(CSM_TIMEOUT,0);
AfxMessageBox("TimeOut 2");
return FALSE;
}
nLength = ReadCommBlock( (unsigned char*) abIn, MAXBLOCK);
start=GetTickCount();
if((abIn[0]==RESEND[0]) && (step!=0))
{
bufp=old_bufp;
step=old_step;
}
if((abIn[0]==ACK[0])||(abIn[0]==RESEND[0]))
{
switch(step)
{
case 0: //发帧头
old_step=step;
old_bufp=bufp;
templen=(((long)bufp[2]<<8)+bufp[3])+2;
TRACE("bufp:%d",bufp);
// check=bufp[2]+(bufp[2]&0x80?0xff00:0)+bufp[3]+(bufp[3]&0x80?0xff00:0);
// bufp[4]=check>>8;
// bufp[5]=check&0xff;
WriteCommBlock((unsigned char*)bufp,6);
bufp+=6;
start=GetTickCount();
step=1;
if((templen==2) && (old_bufp[1]==1))
step=4; //ASK
else if((old_bufp[0]==0) && (old_bufp[1]==1))
step=0;
break;
case 1: //发数据,包括校验和
old_step=step;
old_bufp=bufp;
// check=0;
if(templen>0)
{
WriteCommBlock(bufp,templen);
// for(ci=0;ci<templen-2;ci++)
// check+=bufp[ci]+(bufp[ci]&0x80?0xff00:0);
}
bufp+=templen;
// abIn[0]=check>>8;
// abIn[1]=check&0xff;
// WriteCommBlock((unsigned char*)abIn,2);
start=GetTickCount();
step=2;
break;
//设置该状态已便于计算进程
case 2:
old_step=step;
old_bufp=bufp;
RecordNum++;
//TO ADD : REPORT THE PROGRESS
this->m_ProgressCtrl.SetPos(RecordNum);
//? SendCommMsg(CSM_RUNNING,RecordNum);
start=GetTickCount();
nLength=0;
bTimeout=FALSE;
while((nLength==0)&&(!bTimeout))
{
WriteCommBlock((unsigned char*)SYN,1);
Sleep(50);
nLength=m_strReceived.GetLength();
bTimeout = (GetTickCount()-start>=SYNTIME);
}
if (bTimeout) {
// SendCommMsg(CSM_TIMEOUT,0);
AfxMessageBox("TimeOut 3");
return FALSE;
}
step=0;
break;
case 4:
old_step=step;
old_bufp=bufp;
WriteCommBlock((unsigned char*)BYE,3);
step=5;
break;
case 5:
break;
default:
break;
};
}
else
{
WriteCommBlock((unsigned char*)ACK,1);
}
if((GetTickCount()-start)>MAXTIME)
{
// SendCommMsg(CSM_TIMEOUT,0);
AfxMessageBox("TimeOut 4");
bRet=FALSE;
goto finish;
}
}while((step<5)&&(bRet));
finish:
delete buf;
if(step==5)
{
bRet=TRUE;
}
else
bRet=FALSE;
return bRet;
}
void CSerialAppDlg::OnSendfileRadio()
{
// TODO: Add your control notification handler code here
CButton* pButton = (CButton*)GetDlgItem(IDC_RECFILE_RADIO);
pButton->SetCheck(BST_UNCHECKED);
}
void CSerialAppDlg::OnRecfileRadio()
{
// TODO: Add your control notification handler code here
CButton* pButton = (CButton*)GetDlgItem(IDC_SENDFILE_RADIO);
pButton->SetCheck(BST_UNCHECKED);
}
void CSerialAppDlg::OnFilestart()
{
// TODO: Add your control notification handler code here
if(file==1)
return;
CButton* pWnd = ( CButton*) GetDlgItem(IDC_SENDFILE_RADIO);
BOOL bSend = pWnd->GetCheck();
if(bSend)
{
CWinThread* sendThread=AfxBeginThread(CSerialAppDlg::SendThread,this,THREAD_PRIORITY_HIGHEST);
// m_btnSend.EnableWindow(FALSE);
// m_btnSend.SetWindowText("stop");
}
// else
// CWinThread* recThread=AfxBeginThread(CSerialAppDlg::ReceiveThread,this);
}
void CSerialAppDlg::OnSendfilechoose()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(TRUE);
if(dlg.DoModal()==IDOK)
{
CWnd* pWnd = GetDlgItem(IDC_EDIT_SENDFILE);
pWnd->SetWindowText(dlg.GetPathName());
}
}
void CSerialAppDlg::OnRecfilechoose()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(FALSE);
if(dlg.DoModal()==IDOK)
{
CWnd* pWnd = GetDlgItem(IDC_EDIT_RECFILE);
pWnd->SetWindowText(dlg.GetPathName());
}
}
//DEL void CSerialAppDlg::OnDetails()
//DEL {
//DEL // TODO: Add your control notification handler code here
//DEL int i=3;
//DEL }
//DEL void CSerialAppDlg::FormatFile(CString &path)
//DEL {
//DEL CString newpath=path+"SerialAppTempFormat";
//DEL CopyFile(path,newpath,FALSE);
//DEL char* temp = newpath.GetBuffer(newpath.GetLength()+1);
//DEL FILE* RSF = fopen((const char*)temp,"rwb");
//DEL newpath.ReleaseBuffer();
//DEL int nLength=_filelength(_fileno(RSF));
//DEL int count=0;
//DEL while(nLength>nPackSize)
//DEL {
//DEL
//DEL break;
//DEL };
//DEL
//DEL fclose(RSF);
//DEL }
BYTE* CSerialAppDlg::FormatPack(BYTE* buf,long& buflen)
{
int i=0;
WORD check=0;
BYTE head[6] ;
BYTE end[2] ;
memset(head,0,6);
memset(end,0,2);
// CString str;
long count = buflen;
//加入总包数纪录
WORD packCount= buflen/nPackSize;
if(nPackSize*packCount < buflen)
packCount++;
long newlen = 6+(packCount-1)*(nPackSize+8) + buflen-(packCount-1)*nPackSize+8+6;
BYTE* pnewbuf=new BYTE[newlen];
BYTE* ptemp=pnewbuf;
head[1]=1;
head[2]=HIBYTE(packCount);
head[3]=LOBYTE(packCount);
check=head[2]+(head[2]&0x80?0xff00:0)+head[3]+(head[3]&0x80?0xff00:0);
head[4]=check>>8;
head[5]=check&0xff;
memcpy(ptemp,head,6);
ptemp+=6;
head[1]=0;
//分包
while(count > 0)
{
WORD realsize =(WORD)( count > nPackSize ? nPackSize : count );
//加包头
head[2]=HIBYTE(realsize);
head[3]=LOBYTE(realsize);
check=head[2]+(head[2]&0x80?0xff00:0)+head[3]+(head[3]&0x80?0xff00:0);
head[4]=check>>8;
head[5]=check&0xff;
memcpy(ptemp,head,6);
ptemp+=6;
//添加包中数据
check=0;
for(int ci=0;ci<realsize;ci++)
check+=(buf[buflen-count+ci]+(buf[buflen-count+ci]&0x80?0xff00:0));
memcpy(ptemp,buf+buflen-count,realsize);
ptemp+=realsize;
count-=realsize;
//加校验和
end[0]=check>>8;
end[1]=check&0xff;
memcpy(ptemp,end,2);
ptemp+=2;
TRACE("%d\n",count);
}
head[2]=head[4]=head[3]=head[5]=0;
head[1]=1;
memcpy(ptemp,head,6);
ptemp+=6;
/* i=0;
while(i<6)
{
str.Insert(str.GetLength(),head[i]);
i++;
}
*/
delete buf;
buflen=ptemp-pnewbuf;
ASSERT(newlen==ptemp-pnewbuf);
return pnewbuf;
}
void CSerialAppDlg::WriteMsg(CString str)
{
str.Insert(0,STR[0]);
str.Insert(str.GetLength(),STR[0]);
while(str.GetLength()>m_Port.m_nWriteBufferSize)
{
CString part=str.Left(m_Port.m_nWriteBufferSize);
str.Delete(0,m_Port.m_nWriteBufferSize);
m_Port.WriteToPort(part);
};
m_Port.WriteToPort(str);
}
CString AdjustStr(CString str)
{
return str;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -