modem.cpp

来自「使用zmodem协议传输文件的程序,适当修改可以在嵌入式系统中用来传输文件」· C++ 代码 · 共 571 行 · 第 1/2 页

CPP
571
字号
	if(event & EV_RXCHAR)
		{
		wsprintf(Buffer,"EV_RXCHAR");
		SendMessage(HWindow,UM_DATA,lstrlen(Buffer),(long)Buffer);
		}  
	if(event & EV_BREAK)
		{
		wsprintf(Buffer,"EV_BREAK");
		SendMessage(HWindow,UM_DATA,lstrlen(Buffer),(long)Buffer);
        }
	if(event & EV_TXEMPTY)
		{
		wsprintf(Buffer,"EV_TXEMPTY");
        SendMessage(HWindow,UM_DATA,lstrlen(Buffer),(long)Buffer);
		}     
	}

int TSerialWindow::CommNotification(RTMessage Msg)
// This procedure is invoked when Windows sends a CommNotification
// message.  There are 3 types of messages, this program only really
// repsonds to CN_RECEIVE messages by reading the input buffer, and to
// CN_EVENT messages.  CN_EVENT messages are further analyzed by
// TSerialWindow::CommEvent.

	{
	char Buffer[80];
    int message;

	message=Msg.LP.Lo;
	if(message & CN_EVENT)
		{
		CommEvent();
		}
	if(message & CN_RECEIVE)
		{
	 /*	wsprintf(Buffer,"CN RECEIVE");
		SendMessage(HWindow,UM_DATA,lstrlen(Buffer),(long)Buffer); */
		CheckReceiveBuffer();
		FlushComm(CPort,1);
		}
	if(message & CN_TRANSMIT)
		{
		wsprintf(Buffer,"CN TRANSMIT");
		SendMessage(HWindow,UM_DATA,lstrlen(Buffer),(long)Buffer);
		}
    return 0;
	}

void TSerialWindow::DialPhone(RTMessage Msg)
	{
	//Simple procedure to dial the Phone.  Note that a '\r' (carrage return)
	//must be sent to the port for the modem to dial properly. 
	wsprintf(PhoneNumber,"");
	if(GetModule()->ExecDialog(new TPhoneInputDialog(this, "DIALOG_1"))!=IDCANCEL)
    	{
		if(lstrlen(PhoneNumber)>0)
			{
			WriteComm(CPort,"ATDT ",5);
            WriteComm(CPort,PhoneNumber,lstrlen(PhoneNumber));
			WriteComm(CPort,"\r",1);
			}
		}

	}

void TSerialWindow::SendText(RTMessage Msg) 
	{
//This is really an arcane way to communicate, but this procedure
//pops up a dialog in response to a menu command and inputs text to send.
//It then sends the text, and Posts a message to echo it to the window.
//You probably will want to overhaul it.
    wsprintf(Command,"");
	if(GetModule()->ExecDialog(new TMyInputDialog(this, "DIALOG_2"))!=IDCANCEL)
		{
		if(lstrlen(Command)>0)
			{
			WriteComm(CPort,Command,lstrlen(Command));
			PostMessage(HWindow,UM_DATA,lstrlen(Command),(long)Command);
			}
		}
	}


void TSerialWindow::SendCommand(RTMessage Msg)
	{
//This silly procedure doesn't even work(That is, it wont send the
//modem into command mode) but I'm to lazy to fix it.  What is needed is
//to send +++ to the serial port, and then WAIT for the modem to respond
//with OK.  THEN send the command.  To hang up from the program as it
//is written now, use the SEND dialog to send +++(no carriage return!)
//and wait for OK.  Then use SEND again to send AT H to hang up, or
//whatever it is you want to do.

	wsprintf(Command,"AT H");
	WriteComm(CPort,"+++ ",4);
	MessageBeep(0);
	if(GetModule()->ExecDialog(new TMyInputDialog(this, "DIALOG_2"))!=IDCANCEL)
    	{
		if(lstrlen(Command)>0)
			{
			WriteComm(CPort,Command,lstrlen(Command));
			wsprintf(Command,"\r");
			WriteComm(CPort,Command,1);
			}
		}
	}


void TSerialWindow::GetStatus(RTMessage)

//This procedure gets the status of the port into a COMSTAT
//structure, and alerts you for any set flags.  The flags are
//all explained in help.
	{
	int Err;
	char Buffer[80];
	COMSTAT Comstat;

    wsprintf(Buffer,"\0");
	Err=GetCommError(CPort,&Comstat);
    if(Err>0) SendMessage(HWindow,UM_ERROR,Err,1);
	if(Comstat.status)
		{
		if(Comstat.status & CSTF_CTSHOLD)
			{
			wsprintf(Buffer,"CSTF_CTSHOLD");
		    SendMessage(HWindow, UM_DATA,lstrlen(Buffer),(long)Buffer);
			}
		if(Comstat.status & CSTF_DSRHOLD)
			{
			wsprintf(Buffer,"CSTF_DSRHOLD");
			SendMessage(HWindow, UM_DATA,lstrlen(Buffer),(long)Buffer);
			}
		if(Comstat.status & CSTF_RLSDHOLD)
			{
			wsprintf(Buffer,"CSTF_RLSHOLD");
			SendMessage(HWindow, UM_DATA,lstrlen(Buffer),(long)Buffer);
			}
		if(Comstat.status & CSTF_XOFFHOLD)
			{
			wsprintf(Buffer,"CSTF_XOFFHOLD");
			SendMessage(HWindow, UM_DATA,lstrlen(Buffer),(long)Buffer);
			}
		if(Comstat.status & CSTF_XOFFSENT)
			{
			wsprintf(Buffer,"CSTF_XOFFSENT\0");
			SendMessage(HWindow, UM_DATA,lstrlen(Buffer),(long)Buffer);
			}
		if(Comstat.status & CSTF_EOF)
			{
			wsprintf(Buffer,"CSTF_EOF\0");
			SendMessage(HWindow, UM_DATA,lstrlen(Buffer),(long)Buffer);
			}
		if(Comstat.status & CSTF_TXIM)
			{
			wsprintf(Buffer,"CSTF_TXIM\0");
		    SendMessage(HWindow, UM_DATA,lstrlen(Buffer),(long)Buffer);
			}
        }
	else //No flags set in COMSTAT
   		{
		wsprintf(Buffer,"Ok");
   		SendMessage(HWindow, UM_DATA,lstrlen(Buffer),(long)Buffer);
		}
	 wsprintf(Buffer,"In queue contains %i characters.",Comstat.cbInQue);
     SendMessage(HWindow, UM_DATA,lstrlen(Buffer),(long)Buffer);
	 wsprintf(Buffer,"Out queue contains %i characters.",Comstat.cbOutQue);
     SendMessage(HWindow, UM_DATA,lstrlen(Buffer),(long)Buffer);
    }


void TSerialWindow::SerError(RTMessage Msg)
//This is a simple procedure to write error messages on the window.
//Curiously, sometimes ReadComm() will return an error state -1, but
//GetCommErr() will return 0, stating no error occured.  Ignoring these
//seems to work fine.
	{
	HDC hDC;
	char astr[5];
    int Err;

	hDC=GetDC(HWindow);
	Err=Msg.LParam;
	wsprintf(astr,"%i  %i",Msg.LParam,Msg.WParam);
	TextOut(hDC,10,y,"Error:",6);
	TextOut(hDC,60,y,astr,lstrlen(astr));
    //primitive scroll...
	y+=15;
    if(y>300) y=10;
	ReleaseDC(HWindow,hDC);
	}


void TSerialWindow::SerData(RTMessage Msg)
	//This procedure Outputs all data received from the COM port.
    //The bulk of it simply edits out '\r' and '\n' characters.
	{
	HDC hDC;
    char Instring[80],Outstring[80];
	int i,j;

	for(i=0;i<80;i++)
		{
		Instring[i]='\0';
		Outstring[i]='\0';
        }
    wsprintf(Outstring,"");
	wsprintf(Instring,(LPSTR)Msg.LParam);
	if(lstrlen(Instring)>0)
    	{
		for(i=0,j=0;Instring[i]!='\0';i++)
			{
			if((Instring[i]!='\r')&&(Instring[i]!='\n'))
				{
				Outstring[j]=Instring[i];
				j++;
				}
			else
				{
				if(Instring[i]!='\r')y+=15;
				}
			}
	 	hDC=GetDC(HWindow);   
		TextOut(HWindow,(LPSTR)Outstring,j);
		y+=15;
		if(y>Attr.H) y=10;
	 	ReleaseDC(HWindow,hDC);  
		}
	}
/*******************************
 **    TMyInputDialog Functions*
 *******************************/


TMyInputDialog::TMyInputDialog(PTWindowsObject AParent, LPSTR AResource)
				 :TDialog(AParent,AResource)
		{
		new TEdit(this,101,MAXINPUTLENGTH);
		TransferBuffer = (void far *) &((TSerialWindow*)Parent)->Command;
        }



TPhoneInputDialog::TPhoneInputDialog(PTWindowsObject AParent, LPSTR AResource)
				 :TDialog(AParent,AResource)
		{
		new TEdit(this,101,PHONEINPUTLENGTH);
		TransferBuffer = (void far *) &((TSerialWindow*)Parent)->PhoneNumber;
		}

TCommPortDialog::TCommPortDialog(PTWindowsObject AParent, LPSTR AResource)
				 :TDialog(AParent,AResource)
		{
		}

void TCommPortDialog::PickOne(RTMessage Msg)
	{
	ShutDownWindow(1);
	}

void TCommPortDialog::PickTwo(RTMessage Msg)
	{
	ShutDownWindow(2);
	}

void TCommPortDialog::PickThree(RTMessage Msg)
	{
	ShutDownWindow(3);
	}

void TCommPortDialog::PickFour(RTMessage Msg)
	{
	ShutDownWindow(4);
    }

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	 LPSTR lpCmdLine, int nCmdShow)
       {
	   TSerialApp App("Modem",hInstance,hPrevInstance,lpCmdLine,nCmdShow);
	   App.Run();
       return App.Status;
       }


⌨️ 快捷键说明

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