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

📄 mymodemdlg.cpp

📁 wince modem 拨号程序源代码
💻 CPP
字号:
// MyModemDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MyModem.h"
#include "MyModemDlg.h"
#include "DBFEngine.h"

CDbFile g_dbf;
HANDLE hCommDev;
int g_nFirst=0;
BYTE g_sFirst=0;
char c[2];
TCHAR cc[120];

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyModemDlg dialog

CMyModemDlg::CMyModemDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyModemDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyModemDlg)
	m_sCommand = _T("");
	m_sText = _T("");
	m_sReceive = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyModemDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyModemDlg)
	DDX_Text(pDX, IDC_EDIT1, m_sCommand);
	DDX_Text(pDX, IDC_EDIT2, m_sText);
	DDX_Text(pDX, IDC_EDIT3, m_sReceive);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyModemDlg, CDialog)
	//{{AFX_MSG_MAP(CMyModemDlg)
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_BN_CLICKED(IDC_PICKUP, OnPickup)
	ON_BN_CLICKED(ID_SENDTEXT, OnSendtext)
	ON_BN_CLICKED(ID_SENDTEXT2, OnSendtext2)
	ON_EN_SETFOCUS(IDC_EDIT1, OnSetfocusEdit1)
	ON_EN_SETFOCUS(IDC_EDIT2, OnSetfocusEdit2)
	ON_WM_CHAR()
	ON_WM_TIMER()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyModemDlg message handlers

BOOL CMyModemDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

hCommDev=CreateFile(_T("COM1:"),GENERIC_READ | GENERIC_WRITE,
						0,
						NULL,
						OPEN_EXISTING,
						0,
						NULL);
	if(hCommDev==(HANDLE)-1)
		{
		::MessageBox(NULL,_T("??1????"),_T("????1"),MB_ICONEXCLAMATION);
		
		return 0;
		}
	DCB PortDCB;
	PortDCB.DCBlength = sizeof (DCB);     

  // Get the default port setting information.
  GetCommState (hCommDev, &PortDCB);

  // Change the DCB structure settings.
  PortDCB.BaudRate = 9600;              // Current baud 
  PortDCB.fBinary = TRUE;               // Binary mode; no EOF check 
  PortDCB.fParity = TRUE;               // Enable parity checking 
  PortDCB.fOutxCtsFlow = FALSE;         // No CTS output flow control 
  PortDCB.fOutxDsrFlow = FALSE;         // No DSR output flow control 
  PortDCB.fDtrControl = DTR_CONTROL_ENABLE; 
                                        // DTR flow control type 
  PortDCB.fDsrSensitivity = FALSE;      // DSR sensitivity 
  PortDCB.fTXContinueOnXoff = TRUE;     // XOFF continues Tx 
  PortDCB.fOutX = FALSE;                // No XON/XOFF out flow control 
  PortDCB.fInX = FALSE;                 // No XON/XOFF in flow control 
  PortDCB.fErrorChar = FALSE;           // Disable error replacement 
  PortDCB.fNull = FALSE;                // Disable null stripping 
  PortDCB.fRtsControl = RTS_CONTROL_ENABLE; 
                                        // RTS flow control 
  PortDCB.fAbortOnError = FALSE;        // Do not abort reads/writes on 
                                        // error
  PortDCB.ByteSize = 8;                 // Number of bits/byte, 4-8 
  PortDCB.Parity = NOPARITY;            // 0-4=no,odd,even,mark,space 
  PortDCB.StopBits = ONESTOPBIT;        // 0,1,2 = 1, 1.5, 2 

  // Configure the port according to the specifications of the DCB 
  // structure.
  if (!SetCommState (hCommDev, &PortDCB))
  {
    // Could not create the read thread.
	  ::MessageBox (NULL, TEXT("Unable to configure the serial port"), 
                TEXT("Error"), MB_OK);
    return FALSE;
  }

COMMTIMEOUTS CommTimeouts;
  CommTimeouts.ReadIntervalTimeout = MAXDWORD;  
  CommTimeouts.ReadTotalTimeoutMultiplier = 0;  
  CommTimeouts.ReadTotalTimeoutConstant = 0;    
  CommTimeouts.WriteTotalTimeoutMultiplier = 0;  
  CommTimeouts.WriteTotalTimeoutConstant = 1000;    

  if (!SetCommTimeouts (hCommDev, &CommTimeouts))
  {
    // Could not create the read thread.
	  ::MessageBox (NULL,TEXT("Unable to set the time-out parameters"), 
                TEXT("Error"), MB_OK);
    return 0;
  }
  PurgeComm(hCommDev,PURGE_TXCLEAR);
  PurgeComm(hCommDev,PURGE_RXCLEAR);
  DWORD dw;
  char sc[60];
  g_dbf.WideCharToChar(_T("ATZ\r"),sc);
  WriteFile(hCommDev,(BYTE*)sc,strlen(sc)*2,&dw,NULL);

  SetTimer(1,55,NULL);

	return TRUE;  // return TRUE  unless you set the focus to a control
	
}

void CMyModemDlg::OnSend() //拨号
{
UpdateData(1);
if(m_sCommand==_T(""))return;
DWORD dw;
m_sCommand=_T("ATDT")+m_sCommand+_T("\r");
char sc[60];
g_dbf.WideCharToChar(m_sCommand,sc);
WriteFile(hCommDev,(BYTE*)sc,strlen(sc)*2,&dw,NULL);
}

void CMyModemDlg::OnPickup() //响应拨号
{
	DWORD dw;
	char sc[60];
	g_dbf.WideCharToChar(_T("ATA\r"),sc);
	WriteFile(hCommDev,(BYTE*)sc,strlen(sc)*2,&dw,NULL);
}

void CMyModemDlg::OnSendtext() //实时发送
{
UpdateData(1);
if(m_sText==_T(""))return;
DWORD dw;
char sc[60];
g_dbf.WideCharToChar(m_sText,sc);
WriteFile(hCommDev,(BYTE*)sc,strlen(sc)*2,&dw,NULL);
}

void CMyModemDlg::OnSendtext2() //发送文件
{
	
}

void CMyModemDlg::OnCancel() //退出
{
EndDialog(0);	
//	CDialog::OnCancel();
}

void CMyModemDlg::OnSetfocusEdit1() 
{
m_sCommand=_T("");
UpdateData(0);
}

void CMyModemDlg::OnSetfocusEdit2() 
{
m_sText=_T("");
UpdateData(0);	
}

void CMyModemDlg::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnChar(nChar, nRepCnt, nFlags);
}

void CMyModemDlg::OnTimer(UINT nIDEvent) 
{
BYTE strCommRecvMsg[51];
memset(strCommRecvMsg,0,51);
BYTE bCommRecvMsg[51];
memset(bCommRecvMsg,0,51);

DWORD dw;
BOOL b=ReadFile(hCommDev,strCommRecvMsg,50,&dw,NULL);	
if(!b)return;
if(dw<1)return;
CString str,s;
UpdateData(1);

if(dw==1)
	{
	if(g_nFirst==1)
		{
		c[0]=(char)g_sFirst;
		c[1]=(char)strCommRecvMsg[0];
		wsprintf(cc,_T("%s"),g_dbf.CharToWideChar((unsigned char*)c,2));
		g_nFirst=0;
		g_sFirst=0;
		m_sReceive+=cc;
		UpdateData(0);
		}
	else//g_nFirst==0
		{
		if(IsChinesecharacter(strCommRecvMsg[0]))
			{
			g_sFirst=strCommRecvMsg[0];
			g_nFirst=1;
			}
		else
			{
			m_sReceive+=(TCHAR)strCommRecvMsg[0];
			UpdateData(0);
			g_nFirst=0;
			g_sFirst=0;
			}
		}
	}
else//dw>1
	{
	if(g_nFirst==1)
		{
		c[0]=(char)g_sFirst;
		c[1]=(char)strCommRecvMsg[0];
		memcpy(bCommRecvMsg,strCommRecvMsg+1,dw-1);
		ProcessChar(bCommRecvMsg,dw-1,str);
		wsprintf(cc,_T("%s"),g_dbf.CharToWideChar((unsigned char*)c,2));
		m_sReceive+=cc;
		m_sReceive+=str;
		UpdateData(0);
		g_nFirst=0;
		g_sFirst=0;
		}
	else
		{	
		ProcessChar(strCommRecvMsg,dw,str);
		m_sReceive+=str;
		UpdateData(0);
		g_nFirst=0;
		g_sFirst=0;
		}
	}

	CDialog::OnTimer(nIDEvent);
	
}

void CMyModemDlg::OnDestroy() 
{
KillTimer(1);	
CDialog::OnDestroy();
}

void CMyModemDlg ::ProcessChar(BYTE* bb,int len,CString& str)
{
str=_T("");
BYTE temp;
int i=0;
while(i<len)
	{
	temp=bb[i];
	if(!IsChinesecharacter(temp))
		{
		str+=(TCHAR)temp;
		i++;	
		}
	else
		{
		i++;
		if(i<len)
			{
			c[0]=(char)temp;
			c[1]=(char)bb[i];
			wsprintf(cc,_T("%s"),g_dbf.CharToWideChar((unsigned char*)c,2));
			str+=cc;
			i++;
			}
		}
	}
}

BOOL CMyModemDlg ::IsChinesecharacter(BYTE ch)
{
if((BYTE)ch>=0x80)
	return TRUE;
else
    return FALSE;
}

⌨️ 快捷键说明

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