⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trans.cpp

📁 VC环境下电力系统故障录波数据管理系统
💻 CPP
📖 第 1 页 / 共 4 页
字号:
 
 while(MyTTY.npTTYInfo->HeadPtr==MyTTY.npTTYInfo->TailPtr && TimeOut<30)
  {
  	if( MissConnected() )
	  {
		 AfxMessageBox("线路中断!");
		 TransStatus = 0; 
		 free(buf);	return (-1);
	  }	
  	TimeOut = (GetCurrentTime()-OldTime)/1000;
  }
 free(buf);
 if(TimeOut >= 30)
 {
 	if(SENDTIME<3)
	{
		SENDTIME++;
		Sleep(100);
		goto NEXTTIME;
	}
 }
 if(MyTTY.npTTYInfo->Queue[MyTTY.npTTYInfo->HeadPtr-1]==0x90)
    {
	 Sleep(10);
	 if(ReceiveFile())
		 {
		  remove("modem.d");
		  WinExec("arj e send.arj -y",SW_HIDE);
          Sleep(1000);
          AddDirList();
		 }
      TransStatus = 0; 
	  return (0);
	 }
 else
  {
	if(SENDTIME<3)
	{
		SENDTIME++;
		Sleep(100);
		goto NEXTTIME;
	}
	AfxMessageBox("出错: 等待远方的握手信号出错!",MB_OK);
	TransStatus = 0; 
	return (-1);
  }
}

//////////////////// WyySendFile ///////////////////////////
//
int CTrans::WyySendFile(void)
{
 DWORD OldTime, TimeOut,OldTwo;
 
 int i;
 BYTE far *buf;
 BYTE ch;
 int SENDTIME=0;

NEXTTIME:
 TransStatus = SENDING;
 lstrcpy(StatusStr, "正在准备传送,请稍等...");
 InvalidateRect(&m_StatusRect);
 UpdateWindow();
 if( MissConnected() )
	{ AfxMessageBox("调制解调器没有连通!"); TransStatus = 0; return (-1); }

 buf = (BYTE far *)malloc(512);
 if(buf==NULL)
	{ AfxMessageBox("No enough memory.",MB_OK); TransStatus = 0; return (-1);}
 buf[0]=0x81; buf[1]=0x00; buf[2]=0x01; buf[3] = 0x02;
 buf[4]='W';  buf[5]='M';  buf[7]=3;
 
 if(strlen(szName)==0){	free(buf);  TransStatus = 0; return (-1); }
 
 OFSTRUCT ffp;
 char Temp[_MAX_DIR + 20];
 OpenFile("Send.arj", &ffp, OF_DELETE);

 sprintf(Temp, "Arj a -y send.arj %s",szName);
 WinExec(Temp, SW_HIDE);
 Sleep(2000);
 lstrcpy((char *)(buf+8),szName);
 buf[8+strlen(szName)+1]=-2; buf[8+strlen(szName)+2]=-2;
 ch=0;
 for(i=7;i<512;i++)ch+=buf[i];
 buf[6]=ch;
 
 WriteCommBlock( MyTTY.npTTYInfo, buf , 512);
 
 MyTTY.npTTYInfo->HeadPtr = 0;
 MyTTY.npTTYInfo->TailPtr = 0;
 
 OldTwo = TimeOut = 0;  OldTime = GetCurrentTime();
 
 while(MyTTY.npTTYInfo->HeadPtr == 0 && TimeOut<60)
  {
	TimeOut = (GetCurrentTime()-OldTime)/1000;
	if( MissConnected() )
	  {
		 AfxMessageBox("线路中断!");
		 TransStatus = 0;
		 free(buf);	return (-1);
	  }	
  }
 free(buf);
 if(MyTTY.npTTYInfo->Queue[MyTTY.npTTYInfo->TailPtr]==0x90)
	{
	 int ii = SendFile(0);
	 TransStatus = 0;	 
     return (ii); 
	}
 else
  {
	if(SENDTIME<3)
	{
		SENDTIME++;
		Sleep(100);
		goto NEXTTIME;
	}
	AfxMessageBox("等待远方的握手信号出错!", MB_OK);
	TransStatus = 0;
	return (-1);
  }
}

////////////////////////////////////////////////////
//                      SendFile
////////////////////////////////////////////////////
int CTrans::SendFile(int flag)
{
	return 1;
}
/*
	UINT ReSendTimes=0;
 BYTE SendCh;
 long FileLength, offset;
 char LJH= '\0';
 int BlockDataLength=0, kb=0;
 DWORD OldTime, TimeOut;
 int LastBlockLength = 0, BlockCount=0,ib=0;
 unsigned int   SendCount=0;

 
 CProgressCtrl * MyProgCtrl =  ((CProgressCtrl *)GetDlgItem(IDC_PROGRESS));
 MyProgCtrl->SetRange(0,100);
 
 if(MissConnected())
	{ AfxMessageBox("调制解调器没有连通!"); return (-1); }

 FileHead.Head.StartChar = SOH;

 if(flag==1)
	 lstrcpy(FileHead.Head.Name, "send.arj");
 else
	 lstrcpy(FileHead.Head.Name, szName);

 if((rfp=fopen("send.arj","rb"))==NULL)  //FileHead.Head.Name
   { AfxMessageBox("错 误: 打开不文件" ,MB_OK); return (-1) ;}

 fseek(rfp,0L,SEEK_END);
 if( (FileLength = ftell(rfp)) ==-1)
	{ AfxMessageBox("移动文件指针出错!",MB_OK);  return (-1); }

 ltoa ( FileLength,FileHead.Head.FileLength,10);
 FileHead.Head.LJH = '\0';
 for(kb=0;kb < HEADSIZE-2 ;kb++)
	 FileHead.Head.LJH += FileHead.HeadBuffer[kb];
 FileHead.Head.EndChar=ETX;

 if( FileLength < BLOCKSIZE )
  { BlockCount=1; LastBlockLength = (int)(FileLength%BLOCKSIZE);}
 else if( (FileLength%BLOCKSIZE) == 0)
	 { BlockCount = (int)(FileLength/BLOCKSIZE);LastBlockLength=BLOCKSIZE;}
 else
	{  BlockCount = (int)(FileLength/BLOCKSIZE)+1;
		LastBlockLength = (int)(FileLength%BLOCKSIZE);
	 }
 lstrcpy(StatusStr,"等待接收方的SOH"); 
 InvalidateRect(&m_StatusRect);
  UpdateWindow(); 
RepeHead:

   //等待接收方的SOH信号
 MyTTY.npTTYInfo->HeadPtr = 0;
 MyTTY.npTTYInfo->TailPtr = 0;
 memset(MyTTY.npTTYInfo->Queue,0x20,sizeof(MyTTY.npTTYInfo->Queue));
 TimeOut=0;
 OldTime = GetCurrentTime();
 do{
	 while((MyTTY.npTTYInfo->HeadPtr == MyTTY.npTTYInfo->TailPtr) &&  TimeOut<60)
		 {
		  TimeOut = (unsigned long)((GetCurrentTime()-OldTime)/1000.);
		   if(MissConnected())
             { AfxMessageBox("调制解调器没有连通!"); goto end_send; }
		 }
	  if(TimeOut >= 60)
		 { AfxMessageBox("错误: 传送文件头超时!",MB_OK); goto end_send; }
	  if(MyTTY.npTTYInfo->HeadPtr >= 1 )
	  {
		  if( MyTTY.npTTYInfo->Queue[MyTTY.npTTYInfo->HeadPtr-1]==SOH)
		 {
           SendCh = ACK;
		   WriteCommBlock(MyTTY.npTTYInfo, &SendCh, 1);
		   Sleep(2);
		   WriteCommBlock(MyTTY.npTTYInfo, &SendCh, 1);
		   Sleep(2);
		   WriteCommBlock(MyTTY.npTTYInfo, &SendCh, 1);
		   Sleep(200);
		   break;
		 }
		else
		{
		  AfxMessageBox("对方超时退出!",MB_OK); goto end_send; 
		}
	  }
	}while(1);

 lstrcpy(StatusStr,"传送文件头");
 InvalidateRect(&m_StatusRect);
 UpdateWindow(); 
 SendCount = 0;
 
 WriteCommBlock(MyTTY.npTTYInfo, FileHead.HeadBuffer, HEADSIZE);
 MyTTY.npTTYInfo->HeadPtr = 0;
 MyTTY.npTTYInfo->TailPtr = 0;
 TimeOut=0; OldTime = GetCurrentTime();
//Get the ACK about the head
  lstrcpy(StatusStr,"等待文件头的回应消息...");
 InvalidateRect(&m_StatusRect);
 UpdateWindow(); 
 do{
	 while( (MyTTY.npTTYInfo->HeadPtr==MyTTY.npTTYInfo->TailPtr)   && TimeOut<60)
		{
		   if(MissConnected())
             { AfxMessageBox("调制解调器没有连通!"); goto end_send; }
	       TimeOut = (unsigned long )((GetCurrentTime()-OldTime)/1000.);
		}
	  if(TimeOut >= 60)
		 { AfxMessageBox("错误: 传送文件头超时!",MB_OK); goto end_send; }
	 if(MyTTY.npTTYInfo->Queue[MyTTY.npTTYInfo->HeadPtr - 1]==ACK)
		{
		 if ( MyTTY.npTTYInfo->TailPtr >= 4999 )MyTTY.npTTYInfo->TailPtr=0;
		 else MyTTY.npTTYInfo->TailPtr++;
		 break;
		}
	 else if(MyTTY.npTTYInfo->Queue[MyTTY.npTTYInfo->HeadPtr - 1] == NAK)
		{
	     if(ReSendTimes < 5)
		 {
		   ReSendTimes ++;
		   goto RepeHead;
		 }
		  SendCh = NAK;
		  WriteCommBlock(MyTTY.npTTYInfo, &SendCh, 1);
		  AfxMessageBox("传送文件头出错!" ,MB_OK);
		  goto end_send;
		}	
	 else 
	 {
		  for(int endi=0; endi<BLOCKSIZE; endi++)
		  {
		   SendCh = NAK;
		   WriteCommBlock(MyTTY.npTTYInfo, &SendCh, 1);
		  }
		   AfxMessageBox("传送文件头出错!" ,MB_OK);
		  goto end_send;
	}
	}while(1);
 fclose(rfp);
 Sleep(10);
 lstrcpy(FileHead.Head.Name, "send.arj");
 MyTTY.npTTYInfo->TailPtr =  MyTTY.npTTYInfo->HeadPtr = 0;
 ReSendTimes = 0;

 for( ib=1; ib<=BlockCount; ib++ )
	 {
	  repsend:
	  if( MissConnected())
		 {		AfxMessageBox("调制解调器没有连通!");		goto end_send;	 }
      MyTTY.npTTYInfo->HeadPtr = 0;
      MyTTY.npTTYInfo->TailPtr = 0;

	  if((rfp=fopen(FileHead.Head.Name,"rb"))==NULL)
		 { AfxMessageBox("打不开文件!", MB_OK); goto end_send; }
	  offset = (ib-1)*BLOCKSIZE;
	  if(fseek(rfp,offset,SEEK_SET) != 0)
	     { AfxMessageBox("移动文件指针出错!" , MB_OK); goto end_send; }
	  SendBuffer[0] = ib%256;
	  SendBuffer[1] = ib/256;
	  
	  if (ib < BlockCount) BlockDataLength = BLOCKSIZE+4;
	  if(ib == BlockCount) BlockDataLength = LastBlockLength+4;
	  if(fread(SendBuffer+2,BlockDataLength-4,1,rfp) != 1 )
		 {  AfxMessageBox("读文件出错!",MB_OK);	goto end_send; }
	  fclose(rfp);
	  LJH = '\0';
	  for(kb=0;kb<BlockDataLength-2;kb++)
	       LJH += SendBuffer[kb];
	  SendBuffer[BlockDataLength-2] = LJH;
	  SendBuffer[BlockDataLength-1] = ETX;
       
	  SendCount = 0; 
	  MyTTY.npTTYInfo->HeadPtr = 0;
	  MyTTY.npTTYInfo->TailPtr = 0;
	  TimeOut=0;  OldTime = GetCurrentTime();

      sprintf(StatusStr,"传送第%d块", ib);
      InvalidateRect(&m_StatusRect);
      UpdateWindow(); 	  

	  WriteCommBlock(MyTTY.npTTYInfo, SendBuffer, BlockDataLength);
      sprintf(StatusStr,"等待本块的应答信号!", ib);
      InvalidateRect(&m_StatusRect);
      UpdateWindow(); 	  
	  
	  MyTTY.npTTYInfo->HeadPtr = 0;
	  MyTTY.npTTYInfo->TailPtr = 0;

	  //Get the ACK about the block
	  TimeOut=0;	OldTime = GetCurrentTime();
	  do{
		 while( (MyTTY.npTTYInfo->HeadPtr == MyTTY.npTTYInfo->TailPtr) && TimeOut < 60 )
		      {
		       if(MissConnected())
			   { AfxMessageBox("调制解调器没有连通!"); goto end_send; }

			    TimeOut = (unsigned long)((GetCurrentTime()-OldTime)/1000.);
		       }
		  if(TimeOut >= 60)
		     {  AfxMessageBox("错误: 传送文件超时!",MB_OK); goto end_send; }
		  else if(MyTTY.npTTYInfo->Queue[MyTTY.npTTYInfo->HeadPtr-1] == ACK)
		     {
			  if ( MyTTY.npTTYInfo->TailPtr >= 4999 )MyTTY.npTTYInfo->TailPtr=0;
			  else MyTTY.npTTYInfo->TailPtr++;
			  break;
		    }
		  else if(MyTTY.npTTYInfo->Queue[MyTTY.npTTYInfo->HeadPtr-1] == NAK)
		    {
			  ::MessageBeep(-1);
			  ReSendTimes++;
			  if(ReSendTimes >= 10)
			   { ::MessageBeep(-1);
				 AfxMessageBox("线路故障,通信中断!",MB_OK);
				 goto end_send;
			   }
             sprintf(StatusStr,"重传本块 %d", ib);
             InvalidateRect(&m_StatusRect);
             UpdateWindow(); 	  
			 goto repsend;
		    }
		  else
		  {
		    for(int endi=0; endi<BLOCKSIZE; endi++)
			{
				SendCh = NAK;
				WriteCommBlock(MyTTY.npTTYInfo, &SendCh, 1);
			}
			AfxMessageBox("传送文件出错!" ,MB_OK);
			goto end_send;
		  }
	     }while(1);

	  Percent = ((double)ib)/((float)(BlockCount))*100 ;
	  sprintf(m_PerStr, "%d%%", (int)(Percent));
	  MyProgCtrl->SetPos((int)(Percent));
	  InvalidateRect(&m_StatusRect);
	  UpdateWindow();
	  ReSendTimes=0;
	  Sleep(5);
	 }
 ClearMessage();
 return (1);
 end_send:
 ClearMessage();
 fclose(rfp);
 return (-1);
}
*/

void CTrans::OnPaint() 
{
    char m_strShow[256];
	CPaintDC dc(this); // device context for painting
	CShowWyy MyShowWyy;
    MyShowWyy.ShowBMP(dc, "send.bmp", m_StatusRect.left+2, m_StatusRect.top+10);
	
	dc.SetBkMode(TRANSPARENT);
	dc.TextOut(m_StatusRect.left+10, m_StatusRect.top+40, StatusStr);
	sprintf(m_strShow, "文件名: %s", szName);
	dc.TextOut(m_StatusRect.left+10, m_StatusRect.top+20, m_strShow);
	dc.TextOut(m_StatusRect.left+415, m_StatusRect.top+40, m_PerStr);	
	sprintf(m_strShow, "文件压缩后的大小: %s", FileHead.Head.FileLength);
	dc.TextOut(m_StatusRect.left+10, m_StatusRect.top+60, m_strShow);	
	// Do not call CDialog::OnPaint() for painting messages
}


//=========================ReceiveFile=======================================
int CTrans::ReceiveFile(void)
{
 return 1;
}
/*
 WORD RepeatTimes=0;
 char *nameptr;
 BYTE SendCh;
 long FileLength;
 char FileName[_MAX_DIR];
 unsigned char LJH= '\0';
 long  BlockDataLength=0;
 int  ib=0,ibb;
 long kb=0;
 long LastBlockLength = 0;
 long BlockCount=0, ReceCount=0;
 
 DWORD OldTime,TimeOut=0;
  BYTE SendWyyCh[3];

    SendWyyCh[0] = ACK;   
	SendWyyCh[1] = ACK;
	SendWyyCh[2] = 0x00;
 
 CProgressCtrl * MyProgCtrl =  ((CProgressCtrl *)GetDlgItem(IDC_PROGRESS));
 MyProgCtrl->SetRange(0,100);
 
 if(MissConnected())
	{ AfxMessageBox("调制解调器没有连通!"); return (-1); }

 TimeOut=0;    OldTime = GetCurrentTime();
 lstrcpy(StatusStr, "等待超时或接收到字符");   
 InvalidateRect(&m_StatusRect);       UpdateWindow();

                     //  1.  清缓冲区 
 MyTTY.npTTYInfo->HeadPtr = 0;
 MyTTY.npTTYInfo->TailPtr = 0;
 memset(MyTTY.npTTYInfo->Queue,0x20, 1024);
                     //  2. 等待超时或接收到字符
 int ii=0;
 while(MyTTY.npTTYInfo->HeadPtr == MyTTY.npTTYInfo->TailPtr && TimeOut<60)
 {
	if(MissConnected())     //调制解调器没有连通, 送 NAK 并退出
		{ AfxMessageBox("调制解调器没有连通!");   goto end_rece; }
	TimeOut = (GetCurrentTime()-OldTime)/1000;
    sprintf(StatusStr, "等待超时或接收到字符%d", ii);   
    TimeOut = (GetCurrentTime()-OldTime)/1000;
	ii++;
	InvalidateRect(&m_StatusRect);       UpdateWindow();
	SendCh = SOH;
	WriteCommBlock(MyTTY.npTTYInfo, &SendCh, 1);
	Sleep(10);             // 延时 100 ms
	
 }  //end of while
 Sleep(100);
 MyTTY.npTTYInfo->TailPtr = MyTTY.npTTYInfo->HeadPtr;
 
 if ( TimeOut>=60)           // 超时, 送 NAK 并退出
		{ AfxMessageBox("传送握手信号超时!",MB_OK);  goto end_rece; }
 if (MyTTY.npTTYInfo->Queue[MyTTY.npTTYInfo->HeadPtr-1] != ACK)
		                    // 没收到ACK

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -