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

📄 mymodemdlg.cpp

📁 eVC串口通讯+GPS定位信息显示. 不错的原码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MyModemDlg.cpp : implementation file
//

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

CDbFile g_dbf;

int g_nFirst=0;
BYTE g_sFirst=0;
char c[2];
TCHAR cc[120];
TCHAR  cctv[100];
HANDLE hCommDev;
char ReceiveBuf[1000];
int   actNumber;
CString Recs;
HWND hWnd;

int  JD[1000],WD[1000];

//设置保存数据的全局数组
CString HReceive[1000];
int RCounter;

#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("");
	m_baud = 0;
	m_NS = _T("");
	m_WE = _T("");
	m_showj = _T("");
	m_showjf = _T("");
	m_showw = _T("");
	m_wx = _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_Control(pDX, IDC_STATIC1, m_pic);
	DDX_Text(pDX, IDC_edtj, m_NS);
	DDX_Text(pDX, IDC_edtw, m_WE);
	DDX_Text(pDX, IDC_EDIT_showj, m_showj);
	DDX_Text(pDX, IDC_EDIT_showjx, m_showjf);
	DDX_Text(pDX, IDC_EDIT_showw, m_showw);
	DDX_Text(pDX, IDC_EDIT5, m_wx);
	//}}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_EN_SETFOCUS(IDC_EDIT1, OnSetfocusEdit1)
//	ON_EN_SETFOCUS(IDC_EDIT2, OnSetfocusEdit2)
	ON_WM_CHAR()
	ON_WM_TIMER()
	ON_WM_DESTROY()
//	ON_EN_CHANGE(IDC_EDIT3, OnChangeEdit3)
//	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
//	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
//	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_MESSAGE(WM_RECEIVE,OnReceive)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

UINT ReadThread(LPVOID pParam)
{
	CString cc,mm;
   cc="failed";
   mm="read !";
    HANDLE hcommport=pParam;
  if(hcommport==INVALID_HANDLE_VALUE)
  {
   AfxMessageBox(cc);
   return -1;
  }
	//AfxMessageBox(cc);
  
   BYTE ReadBuf[10];
   DWORD actRead;
//   DWORD willread;
   DWORD dwReadErrors;
   COMSTAT cmState;
   
 
  BYTE Byte;
  int iCounter=0;
  DWORD dwBytes;
  SetCommMask(hcommport,EV_RXCHAR);
 
  while(hcommport!=INVALID_HANDLE_VALUE)
  {
   DWORD dwCommStatus;
   WaitCommEvent(hcommport,&dwCommStatus,0);
   SetCommMask(hcommport,EV_RXCHAR);
   int ff=0;
   int tt;
   //for(int i=0;i<1000;i++)
    //ReceiveBuf[i]=0;
    //actNumber=0;
   do{
     ReadFile(hcommport,&ReadBuf,10,&dwBytes,0);
	 ff=dwBytes;
	 if(dwBytes<1)continue;
	 /*while(ff>0)
	 {
	  ReceiveBuf[iCounter++]=Byte[];
	  if(iCounter==1000)
	  {
	     AfxMessageBox(mm);
   	     return -1;
	  }
      ff=ff-1;
	 }*/
	 
	 wsprintf(cctv,_T("%s"),g_dbf.CharToWideChar((unsigned char*)ReadBuf,dwBytes));
	 Recs+=cctv;
	 
	 if(Recs.Find('F')>-1)
	   PostMessage(hWnd,WM_RECEIVE,0,0);
	 
	/* if(dwBytes==1)
	 {
	  if(ReadBuf[0]=='F')
		  PostMessage(hWnd,WM_RECEIVE,0,0);
	 }
	 else{
	   if(ReadBuf[dwBytes-2]=='F')
	 PostMessage(hWnd,WM_RECEIVE,0,0);
	}*/

	 //UpdateData(0);
	/*
	 tt=iCounter;
	 for(int i=0;i<ff;i++)
	 {
	  ReceiveBuf[i+tt]=ReadBuf[i];
      iCounter++;
	 }*/
   
   }while(dwBytes>0);
  // if(iCounter==0) continue;
   //actNumber=iCounter;
   //iCounter=0;
   
  
   
	
  }
 
  return 0;
  /*
  SetCommMask(hcommport,EV_RXCHAR);
   for(int i=0;i<1000;i++)
    ReceiveBuf[i]=0;
    actNumber=0;
   do{
     ReadFile(hcommport,&Byte,1,&dwBytes,0);
	 if(dwBytes==1)
	 {
	  ReceiveBuf[iCounter++]=Byte;
	  if(iCounter==1000)
	  {
	     AfxMessageBox(mm);
   	  
		  return -1;
	  }
	 }
   
   }while(dwBytes==1);
   if(iCounter==0) continue;*/
   

}

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
	Recs="";

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 = 115200;              // 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;
  }
 // SetupComm(hCommDev,512,512);
  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,100,NULL);
  
//fuzhi
  for(int i=0;i<1000;i++)
    ReceiveBuf[i]=0;
  actNumber=0;
  hWnd=this->m_hWnd;
  RCounter=0;
  AfxBeginThread(ReadThread,hCommDev);	
  

	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];

⌨️ 快捷键说明

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