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

📄 fax.cpp

📁 收发传真的C源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
#ifdef _WINDOWS
#include <stdafx.h>
#endif
#include <sys/stat.h>
#include <time.h>
#include <fcntl.h>
#include "fax.hpp"
//send
LONG    nSkipDIS=3000;
LONG    nSendTSI  =0;
LONG    nSendDCS  =0;
LONG    nSendTCF  =0;
//receive
LONG    nSendNSF =5000;
LONG    nGetFrame=5000;
char       FaxInitString[20]="ATE0Q0V1";
const char EndString[]={DLE,ETX};
#define XON 17
#define FERROR "\r\n+FCERROR\r\n"
const char	  *fax_msgs[]   = { "%s 传真文件 %s",            // 0
					"初始化调制解调器",                      // 1
					"等待电话",                              // 2
					"回答呼叫",                              // 3
					"传送本机的配置及身份",                  // 4
					"传送(DIS)帧",                           // 5
					"(TSI)帧已被确认",                       // 6
					"(DCS)帧已被确认",                       // 7
					"(NSF)帧已被确认",                       // 8
					"正在接收(TCF)帧",                       // 9
					"已经认同(TCF)帧",                       // 10
					"正在接收传真页...",                     // 11
					"传真页已被确认",                        // 12
					"多页信号(MPS)帧已被确认",               // 13
					"结束信息(EOM)帧已被确认",               // 14
					"结束过程(EOP)帧已被确认",               // 15
					"终止连接已被确认",                      // 16
					"错误 !!!",                              // 17
					"用户放弃传真",                          // 18
					"正在重试(TCF)帧接收",                   // 19
					"等待对方响应超时",                      // 20
					"意外的响应",                            // 21
					"呼叫远方传真机",                        // 22
					"(CSI)帧已被确认",                       // 23
					"(DIS)帧已被确认",                       // 24
					"传送(TSI)帧",                           // 25
					"传送(DCS)帧",                           // 26
					"传送(TCF)帧",                           // 27
					"(TCF)帧已经认同",                       // 28
					"传送数据页",                            // 29
					"(TCF)帧 失败",                          // 30
					"数据页已送出",                          // 31
					"传送多页信号(MPS)帧",                   // 32
					"传送结束过程(EOP)帧",                   // 33
					"传送终止连接(DCN)帧",                   // 34
					"XOFF信号 已被确认",                     // 35
					"                 ",                     // 36
					"调制解调器没有正确响应复位信号",        // 37
					"调制解调器复位",                        // 38
					"调制解调器复位错误",                    // 39
					"文件名: ",                              // 40
					"正在尝试 9600波特率",                   // 41
					"正在尝试 7200波特率",                   // 42
					"正在尝试 4800波特率",                   // 43
					"正在尝试 2400波特率",                   // 44
					"正在尝试 14400波特率",                   // 45
					"正在接收HDLC帧",                   // 46
					"传送(NSF)帧",                           // 47
					0};
const char	  *fax_errors[] = { "没有错误",                              // 0
					"调制解调器初始化错误",                  // 1
					"调制解调器没有连接",                    // 2
					"错误的接收 HDLC 结构",                  // 3
					"没有(DCS)帧被确认",                     // 4
					"不能支持波特率请求",                    // 5
					"非法的(TCF)帧格式",                     // 6
					"(TCF)帧错误的终止",                     // 7
					"错误的(TCF)帧长度",                     // 8
					"不能打开传真文件",                      // 9
					"连接失败, 稍后再次尝试",                // 10
					"没有(DIS)帧被确认",                     // 11
					"不能到建立期间",                        // 12
					"非法的传真文件格式",                    // 13
					"期望的波特率尝试没有被确认",            // 14
					"传送传真数据超时",                       //15
					"意外的响应",                            // 16
					"忙音",                                  // 17
					"BUSY"};
/* ********************************************************************
 *
 *  Fax -- Fax instance constuctor
 *
 * ********************************************************************/
 /*
char    dis_msg[] =                         // DIS frame data
		{ 0,                              // first byte:
								    //   not G1, G2
		0x70,                             // second byte:
								    //   T.4 operation
								    //   9600/7200/4800/2400 bps
								    //   3.85 lines/mm
								    //   one-dimensional coding
		0x02                              // third byte:
		};                                //   1728 pels/215mm
								    //   A4 paper only
								    //   40ms receive time

char    dcs_msg[] =                         // DCS frame data
		{ 0,                              // first byte:
		0x40,                             // second byte:
								    //   T.4 operation
								    //   speed to be set
								    //   3.85 lines/mm
								    //   one-dimensional coding
		0x02                             // third byte:
		};                                //   1728 pels/215mm
								    //   A4 paper only
*/								    //   40ms per line

Fax::Fax(Comm *ci,                          // comm instance
         char *sid,                         // .. our station ID (telno)
	    short (*sr)(short, FuncStat *),   // .. status routine
	    void **w,DWORD aspeed)                           // .. fax status parameter

{
n_speed=aspeed;
strcpy(nameBuffer,"Empty ID");
cp = ci;                                    // save comm instance pointer
stat = sr;                                  // .. and status routine
station = sid;                              // .. and station ID

connected = FALSE;                          // no HDLC connection active

fs.f_parm = w;                              // save fax status parameter
chk_char=1;
memcpy(dis_msg,"\x0\x72\x2\x0\x17\x46",6);
/*dis_msg[0] =0;
dis_msg[1] =0x72;//0x73;
dis_msg[2] =0x02;*/

/*dcs_msg[0] =0x00;
dcs_msg[1] =0x40;
dcs_msg[2] =0x02;*/
memcpy(dcs_msg,"\x0\x40\x2\x0\x22\xf0",6);
}



/* ********************************************************************
 *
 *  ~Fax -- destructor
 *
 * ********************************************************************/

Fax::~Fax(void)                             // Fax destructor

{
}



/* ******************************************************************** *
 *
 *  
 -- initialize the fax 调制解调器
 *
 *  returns: -2 = User pressed ESC
 *           -1 - Timeout
 *            0 = successful; OK response from 调制解调器
 *            1 = ERROR response from 调制解调器
 *
 * ******************************************************************** */
short Fax::Init_Modem(short select_mode)

{
short     rc,                                 // return code
        i;                                  // work counter for loops
/*char    buf[80],                            // work buffer for speeds
	  *c,                                  // work pointer
       *t;                                  // token work pointer

*/
      
oldspeed = cp->GetSpeed();                  // get the old link speed
cp->SetBPS(n_speed);     // set new comm speed

for (rc = i = 0; (rc == 0) && i++ < 3;)     // try three times
	    {
	    cp->Write(FaxInitString);                  // set the 调制解调器 parameters
		cp->Write('\r');
	    rc = wait_for(cp,FAX_OK, FAX_ERR, 3);      // wait for OK response
	    }
if (rc < 0)                                 // q. user cancellation?
    return(rc - 1);                         // a. yes .. return error

speeds = 0;                                 // reset speeds supported
cp->Write(FAX_CLASS1);                      // place 调制解调器 in CLASS 1 mode
rc = wait_for(cp,FAX_OK, FAX_ERR, 10) - 1;      // wait for 调制解调器 response
if (rc)                                     // q. 调制解调器 respond ok?
   return(rc);                              // a. no .. tell the caller

if(select_mode==0)
   {
/*
	cp->Write(FAX_TX_SPD);                      // retrieve transmit speeds

	rc = Get_Line(buf, 5);                      // kill the first CR
	rc = Get_Line(buf, 5);                      // get the line from the mode

	if (rc)                                     // q. any error?
	   return(rc);                              // a. yes.. return w/error

	//cp->Purge(1);                               // kill additional characters

	c = (*buf == '\n') ? buf+1 : buf;           // select start point

	while ((t = strtok(c, ",")) != 0)           // while there are tokens
	    {
	    c = NULL;                               // continue searching buf

	    i = atoi(t);                            // get the token's value

	    switch(i)                               // for various values..
		   {
		   case 24:                            // 2400 found
			   speeds |= FAX_T2400;           // .. set set the speed flag
			   max_sendspeeds=24;
			   break;

		   case 48:                            // 4800 found
			   speeds |= FAX_T4800;           // .. set set the speed flag
			   max_sendspeeds=48;
			   break;

		   case 72:                            // 7200 found
			   speeds |= FAX_T7200;           // .. set set the speed flag
			   max_sendspeeds=72;
			   break;

		   case 96:                            // 9600 found
			   speeds |= FAX_T9600;           // .. set set the speed flag
			   max_sendspeeds=96;
			   break;
		    default:
			   break;
		   }
	    }*/
	   speeds = FAX_T2400 | FAX_T4800 | FAX_T7200 | FAX_T9600;
	   max_sendspeeds=96;
    }
else
   {
/*	cp->Write(FAX_RX_SPD);                      // retrieve receive speeds

	rc = Get_Line(buf, 5);                      // kill the first CR
	rc = Get_Line(buf, 5);                      // get the line from the 调制解调器

	if (rc)                                     // q. any error?
	   return(rc);                              // a. yes.. return w/error

	cp->Purge(1);                               // kill additional characters
	

	c = (*buf == '\n') ? buf+1 : buf;           // select start point

	while ((t = strtok(c, ",")) != 0)           // while there are tokens
	    {
	    c = NULL;                               // continue searching buf

	    i = atoi(t);                            // get the token's value

	    switch(i)                               // for various values..
		   {
		   case 24:                            // 2400 found
			   speeds |= FAX_R2400;           // .. set set the speed flag
			   max_recespeeds=24;
			   break;

		   case 48:                            // 4800 found
			   speeds |= FAX_R4800;           // .. set set the speed flag
			   max_recespeeds=48;
			   break;

		   case 72:                            // 7200 found
			   speeds |= FAX_R7200;           // .. set set the speed flag
			   max_recespeeds=72;
			   break;

		   case 96:                            // 9600 found
			   speeds |= FAX_R9600;           // .. set set the speed flag
			   max_recespeeds=96;
			   break;
		   }
	    }*/
	   speeds = FAX_R2400 | FAX_R4800 | FAX_R7200 | FAX_R9600;
	   max_recespeeds=96;
  }

return(0);                                  // return ok
}

/* ******************************************************************** *
 *
 *  Reset_Modem -- reset 调制解调器 and communications parameters
 *
 * ******************************************************************** */

void Fax::Reset_Modem(void)

{
short     rc;                                 // return code
cp->Write("\r");                            // send a <CR> to modem
cp->DTR();                                  // lower DTR
cp->Purge(1);                               // .. kill any receive messages
cp->Write(FAX_RSTMDM);                  // try to reset the 调制解调器
switch (rc = wait_for(cp,FAX_OK, FAX_ERR, 5))  // based on response
    {
    case -1:                                // user pressed escape
	   return;                             // .. return without message
    case 0:                                 // time out
    case 1:                                 // 已被确认 OK
    case 2:                                 // 已被确认 ERROR
	   Display_Msg(37+rc);                 // display appropriate message
    }
cp->SetBPS(oldspeed);                       // reset the comm speed
}



/* ******************************************************************** *
 *
 *  Send_Our_ID -- send our ID to the other station
 *
 *  returns: -2 = User pressed ESC
 *           -1 - Timeout
 *            0 = successful; CONNECT response from 调制解调器
 *            1 = NO CARRIER response from 调制解调器
 *
 * ******************************************************************** */

short Fax::Send_Our_ID(BYTE ctl_byte)        // FAX control field

{
short     i, j;                               // work variables
BYTE  *ch;                                 // work pointer

ch = hmsg.data;                             // get the data address

for (i = 0; i < 20; ch[i++] = 0x04);        // set to 'reversed' blanks

if (station != NULL)                        // q. station ID set?
    {                                       // a. yes ..
    i = strlen(station);                    // get station ID length

    i = i > 20 ? 20 : i;                    // max ID length = 20
    for (j = 0; i--;)                       // for each char in station ID
	   {
	   if(chk_char)
		  ch[j++] =Reverse_Byte(station[i]);
	   else
		   {
		   if (strchr("0123456789+ ",          // q. valid ID character?
				   station[i]) != 0)
			ch[j++] =Reverse_Byte(station[i]);
		   }

⌨️ 快捷键说明

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