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

📄 fax.cpp

📁 收发传真的C源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
		  continue;                       // .. continue processing
		  }
	    else if (error)                    // q. error set?
		   {                              // a. yes ..
		   sprintf(fs.f_msg,"传真错误:%s",fax_errors[error]);
		   }

	    else if (rc == -1)                 // q. timeout?
		   sprintf(fs.f_msg,"传真错误:%s",fax_msgs[20]);

	    else if (rc )                  // q. unexpected response?
		   sprintf(fs.f_msg,"传真错误:%s",fax_msgs[21]);
		(stat)(OUT_MSGREC,&fs);
	    if(!error)error=-1;
	    state = ExitFax;                   // .. set new state
	    break;

    case UserCan:                           // user cancelled transmission
         Display_Msg(18);                   // update the status
	    state = ExitFax;                   // .. set new state
	    error=18;
	    break;

    case ExitFax:                           // exit FAX receive
	    if (fileopen)                      // q. fax file open?
		   {
		   if(error==0)
			   {
			   fseek(faxfile, 0L, SEEK_SET);
			   WORD dwAll=*(WORD *)header.page_number;
		       fread(&header,sizeof(FaxHeader),1,faxfile);
			   *(WORD *)header.page_count=dwAll;
			   fseek(faxfile,0L, SEEK_SET);
			   fwrite(&header, sizeof(FaxHeader),1,faxfile);
			   fseek(faxfile,0L,SEEK_END);
			   FaxHeader end_handle;
			   *(WORD *)end_handle.page_count=0;
			   *(WORD *)end_handle.page_number=0;
			   *(long *)end_handle.data_len=0;
			   *(long *)end_handle.off_len=0;
		       fwrite(&end_handle,sizeof(FaxHeader),1,faxfile);
			   fclose(faxfile);               // a. yes .. close the file
			   }
		   else {
			   fclose(faxfile);               // a. yes .. close the file
			   remove(faxname);
			   }
		   }

	    Reset_Modem();                     // reset the 调制解调器

	    loop = FALSE;                      // .. end the loop
	    break;                             // .. and return to caller
    }
   }
   delete pagebuf;                             // free pagebuf memory
   return error;
}

//#define HDLC_ERROR
#define SEND_BUFF_SIZE 256
short Fax::Send(char *faxname,char *telno,BOOL skipDIS)

{
short 	   speed,                              // link speed
	   nextspeed,                          // next link speed
	   /*fileopen = FALSE,*/                   // receive file not open
	   dis_received = FALSE,               // DIS not yet 已被确认
	   i,                                  // work variable
	   error = 0,                          // error number
	   loop = TRUE;                        // loop until complete
FILE   *faxfile;                            // fax file

char    dis[16],                            // DIS 已被确认
	   csi[21],                            // CSI 已被确认
	   c;                                  // work character

//long    st;                                 // work for elapsed time
long    page_count=0,page_size;
short   _count=0,rc=0;
long    oldlen,allPage;
BOOL   reset=FALSE;
	    header=FaxHeader();
enum    SndStates                           // FAX send states
		{
		PhaseA,                           // phase A - make connection
		PhaseB,                           // get HDLC frames
		SetSpeed,                         // set the link speed
		SendTSI,                          // select speed, send ID & DCS
		SendTCF,                          // send the TCF
		GetConfTCF,                       // get TCF confirmation
		PhaseC,                           // send the document
		SendPage,                         // send a page of data
		PhaseD,                           // post message process
		AnotherPage,                      // another page to send
		AllSent,                          // send complete
		PhaseE,                           // disconnect
		SndError,                         // error during send
		UserCan,                          // user cancelled transmission
		ExitFax                           // exit FAX receive
		} state;

state = PhaseA;
faxfile=NULL;

if ((faxfile = fopen(faxname, "rb"))        // q. send file open ok?
				== NULL)
    {                                       // a. no ..
    error = 9;                              // .. set the error code
    state = SndError;                       // .. and declare an error
    }
else
   {
//	fileopen = TRUE;                            // show the file is open
	fread(&header, sizeof(FaxHeader),1,faxfile);
	if(memcmp(header.Title,HeaderTitle,12)!=0)//G3
	    {                                       // a. no ..
	    error = 13;                             // .. show invalid format
	    state = SndError;                       // .. and declare an error
	    }
	allPage=*(WORD *)header.page_count;
	if(*(WORD *)header.fine)dcs_msg[1] |=0x02;
   }
pagebuf=new BYTE[SEND_BUFF_SIZE+1];
while(loop)                                 // top of state machine
  {
  switch(state)                             // perform next state
    {
    case PhaseA:                            // phase A - make connection

	    Display_Msg(1);                    // update the status
	    reset=TRUE;
	    if ( Init_Modem(0) != 0)      // q. 调制解调器 init ok?
		   {
		   error = 1;                     // a. no .. exit now
		   state = SndError;              // .. declare the error
		   continue;
		   }
	    Display_Msg(22);                   // update the status
	    if(telno&&strlen(telno)&&!skipDIS)
		    {
		    cp->Write(FAX_DIAL);               // send the dial command
		    cp->Write(telno);         // .. and the phone number
		    cp->Write('\r');                   // .. and finish the command
		    }
	    else
		    {
		    if(skipDIS)
			  cp->Write("ATA\r");             // send the answer command
		    else
			  cp->Write("ATD\r");             // send the answer command
		    }
	    rc = wait_for1(cp,FAX_CONN,            // wait for a connection
				   FAX_NO_CARR,FAX_BUSY,60);
	    if (--rc)                          // q. any error
		   {                              // a. yes ..
		   if (rc == 1)                   // q. connection fail?
			 error = 10;                 // a. yes .. show connect failed
		   if (rc == 2)                   // q. connection fail?
			 error = 17;                 // a. yes .. show connect failed

		   state = SndError;              // declare an error
		   continue;                      // .. go to next state
		   }

	    connected = TRUE;                  // ... show we are connected
	    state = PhaseB;                    // get frames
	    if(skipDIS)
		    {
		    state = SetSpeed;          // .. and set the speed
		    dis_received = 1;           // show we got DIS
		    memcpy(dis,dis_msg,6);
			delay(nSkipDIS);
		    }
	    else state = PhaseB;
	    break;
    case PhaseB:
	    // get HDLC frames
	    if ((rc = Rcv_Hmsg()) != 0)        // q. any error getting a frame?
		  {
		   if (rc < 0)                    // a. yes .. timeout or ESC?
			  {                          // a. yes ..
			  error = 3;                 // .. error receiving HDLC frame
			  state = SndError;          // .. leave with error.
			  continue;                  // .. continue process
			  }
		   else                           // else .. NO CARRIER
			  {
/*			    cp->Write(FAX_SILENT1);    // wait for 200ms silence

			  rc = wait_for(cp,FAX_OK,      // wait for the OK response
						 FAX_ERR,
						 10) - 1;*/
			  state = SetSpeed;          // .. and set the speed
			  rc=0;
			  }
		   }
	    if (rc)                            // q. any error yet?
		   {                              // a. yes ..
		   state = SndError;              // .. declare an error
		   continue;
		   }

	    switch(hmsg.fax_ctl_fld)           // process based on message type
		   {
		   case FAX_FCF_CSI:              // called subscriber ID
				 Reverse_Bytes(hmsg.data,    // .. reset the bytes to normal
							  20);
				 hmsg.data[20] = 0;          // .. end string in zero
				 strncpy(csi,(char *) hmsg.data,20);
			 csi[20]=0;
			 Display_ID("接收方:",(BYTE *)csi,TRUE);
			 break;

		   case FAX_FCF_DIS:              // digital information signal
			    if(hmsg.len>7)
				 memcpy(dis, hmsg.data,      // .. move the DIS to work area
					   (hmsg.len - 7) >16 ? 16 : (hmsg.len - 7));
				else
				 memcpy(dis, hmsg.data,hmsg.len);
				dis_received = 1;           // show we got DIS
				break;

		   case FAX_FCF_NSF:              // Non-standard facilities
				Display_Msg(8);             // update the status
				break;                      // .. we don't process these
		   }
	    //delay(800);
	    break;

    case SetSpeed:                          // set the link speed
	    if (dis_received == 0)             // q. dis 已被确认?
		   {                              // a. no ..
		   error = 11;                    // .. show the error
		   state = SndError;              // .. and go to error state
		   continue;                      // .. continue with next state
		   }
	    switch (dis[1] & 0x30)             // select initial speed
		   {
		   case 0x30:                     // q. speed 7200?
		   case 0x20:                     // q. speed 9600?
			  nextspeed = 96;            // a. yes .. set speed
//			  nextspeed = 72;            // a. yes .. set speed
			  break;
		   case 0x10:                     // q. speed 4800?
			  nextspeed = 48;            // a. yes .. set speed
			  break;

		   case 0x00:                     // q. speed 2400?
			  nextspeed = 24;            // a. yes . set speed
			  break;
		   }

	    state = SendTSI;                   // next, tell 'em about us
	    break;

    case SendTSI:                           // select speed, send ID & DCS
//	    dcs_msg[1] &= 0xcf;                // set off speed bits
	    speed = nextspeed;
	    switch (speed)         // based on selected speed
		 {
		 case 96:                         // q. speed = 9600?
			 if(speeds & FAX_T9600)
				{
				dcs_msg[1] |= 0x20;          // a. yes .. set speed

				if(_count>0)
					{
					nextspeed = 72;          // a. yes .. next speed = 7200
					_count=0;
					}
				else
					{
					_count++;
					nextspeed = 96;          // a. yes .. next speed = 7200
					}
				i = 41;                      // select speed message
				break;
				}

		 case 72:                         // q. speed = 7200?
			 if(speeds & FAX_T7200)
				{
				 dcs_msg[1] |= 0x30;          // a. yes .. set speed
				if(_count>0)
					{
					nextspeed = 48;          // a. yes .. next speed = 7200
					_count=0;
					}
				else
					{
					_count++;
					nextspeed = 72;
					}
				 i = 42;                      // select speed message
				 break;
				 }
		 case 48:                         // q. speed = 4800?
			 if(speeds & FAX_T4800)
				{
				dcs_msg[1] = 0x51;          // a. yes .. set speed
				if(*(WORD *)header.fine)dcs_msg[1] |=0x02;
				if(_count>0)
					{
					nextspeed = 24;          // a. yes .. next speed = 7200
					_count=0;
					}
				else
					{
					_count++;
					nextspeed = 48;
					}
				i = 43;                      // select speed message
				break;
				}

		 case 24:
			 if(speeds & FAX_T2400)
				{
				dcs_msg[1] = 0x40;          // a. yes .. set speed
				if(*(WORD *)header.fine)dcs_msg[1] |=0x02;
				if(_count>0)
					{
					nextspeed = 12;          // a. yes .. next speed = 7200
					_count=0;
					}
				else
					{
					_count++;
					nextspeed = 24;
					}
				i = 44;                      // select speed message
				break;
				}
		 case 12:
			error = 12;                  // a. yes.. can't connect
			state = SndError;            // .. declare the error
			i = 45;                      // select speed message
			continue;                    // .. continue state machine
		 }
		delay(nSendTSI);
	    Display_Msg(i);
	    rc = Send_TSI();                   // .. send our ID

	    if (rc)                            // q. error?
		   {
		   state = SndError;              // a. yes ..
		   continue;                      // .. declare the error
		   }
	    Display_Msg(26);                   // update status again

		delay(nSendDCS);
	    state = ((rc = Send_DCS()) != 0) ? // q. DCS send go ok?
						   SndError : // a. no .. declare error
						    SendTCF ; // else .. send the check frame
	    break;

    case SendTCF:                           // send the TCF
		delay(nSendTCF);
	    Display_Msg(27);                   // update the 

⌨️ 快捷键说明

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