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

📄 gtcfgdlg.cpp

📁 uart configuration source based on PC com
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// GTcfgDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GTcfg.h"
#include "GTcfgDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
 //CGTcfgDlg* gpView=NULL;
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGTcfgDlg dialog

CGTcfgDlg::CGTcfgDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGTcfgDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGTcfgDlg)
	m_strRxdata = _T("");
	m_strTxdata = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	gpPort= new Serial();
}

void CGTcfgDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGTcfgDlg)
	DDX_Control(pDX, IDC_IPBOX, m_strIP);
	DDX_Text(pDX, IDC_EDIT_RXDATA, m_strRxdata);
	DDX_Text(pDX, IDC_EDIT_TXDATA, m_strTxdata);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGTcfgDlg, CDialog)
	//{{AFX_MSG_MAP(CGTcfgDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_SRV1IP, OnButtonSrv1ip)
	ON_BN_CLICKED(IDC_BUTTON_SRV1PT, OnButtonSrv1pt)
	ON_BN_CLICKED(IDC_BUTTON_SRV2IP, OnButtonSrv2ip)
	ON_BN_CLICKED(IDC_BUTTON_SRV2PT, OnButtonSrv2pt)
	ON_BN_CLICKED(IDC_BUTTON_IPGPRS, OnButtonIpgprs)
	ON_BN_CLICKED(IDC_BUTTON_TUM, OnButtonTum)
	ON_BN_CLICKED(IDC_BUTTON_IMEI, OnButtonImei)
	ON_BN_CLICKED(IDC_BUTTON_HBS, OnButtonHbs)
	ON_BN_CLICKED(IDC_BUTTON_IPR, OnButtonIpr)
	ON_BN_CLICKED(IDC_BUTTON_HBT, OnButtonHbt)
	ON_BN_CLICKED(IDC_BUTTON_COPEN, OnButtonCopen)
	ON_BN_CLICKED(IDC_BUTTON_MANUALSEND, OnButtonManualsend)
	ON_BN_CLICKED(IDC_BUTTON_OPENC, OnButtonOpenc)
	ON_BN_CLICKED(IDC_BUTTON_VER, OnButtonVer)
	ON_BN_CLICKED(IDC_BUTTON_SELALL, OnButtonSelall)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGTcfgDlg message handlers

BOOL CGTcfgDlg::OnInitDialog()
{

CDialog::OnInitDialog();
 
	// Add "About..." menu item to system menu.
   CString strMsg;
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
	
	// TODO: Add extra initialization here

	m_pPortNum=1;
	nBaudRate=9600;
	ByteSize=8;
	StopBits=1;
	strcpy(Parity,"N");
	  CComboBox*pComb=NULL; 
	  CString c;
	  int nIndex;
		//串口号

	HKEY KeyHandle; 
	LONG ErrCode;
	int Index;
	char ValueName[256];
	BYTE Data[256];
	DWORD ValueLen, DataLen, ValueType;
	//gpPort= new Serial();
    ErrCode = RegOpenKeyEx(
							HKEY_LOCAL_MACHINE,
							"HARDWARE\\DEVICEMAP\\SERIALCOMM",
							0,
							KEY_READ,
							&KeyHandle);
	pComb=(CComboBox*)this->GetDlgItem(IDC_COMBO1);
	try
	{
		Index = 0;
		do {
			ValueLen = 256;
			DataLen = 256;
			ErrCode = RegEnumValue(
				KeyHandle,
				Index,
				(LPSTR)(LPCTSTR)ValueName,
				&ValueLen,
				NULL,
				&ValueType,
				Data,
				&DataLen);
			if(ErrCode == ERROR_SUCCESS)
			{
				pComb->AddString((char *)Data);
				Index++;
			}
		} while(ErrCode == ERROR_SUCCESS);
	}
	catch (...) 
	{
		RegCloseKey(KeyHandle);
	}
	if(Index>0)
	{
		//pComb->SetCurSel(0);
		c.Format("COM%d",this->m_pPortNum); 
     nIndex=pComb->FindString(0,c);
     pComb->SetCurSel(nIndex);

	}
  
/*	pComb=(CComboBox*)this->GetDlgItem(IDC_COMBO1);
	pComb->AddString("COM1");
	pComb->AddString("COM2");
	pComb->AddString("COM3");
	pComb->AddString("COM4");
	pComb->AddString("COM5");
	CString c;
	c.Format("Com%d",this->m_pPortNum); 
    int nIndex=pComb->FindString(0,c);
    pComb->SetCurSel(nIndex);*/
    //波特率
	pComb=(CComboBox*)this->GetDlgItem(IDC_COMBO2);
    pComb->AddString("1200");
	pComb->AddString("2400");
	pComb->AddString("4800");
	pComb->AddString("9600");
	pComb->AddString("19200");
	pComb->AddString("38400");
	pComb->AddString("43000");
	pComb->AddString("56000");
	pComb->AddString("57600");
	pComb->AddString("115200");
    c.Format("%d",this->nBaudRate);
    nIndex=pComb->FindString(0,c);
    pComb->SetCurSel(nIndex);
   /* //数据位
    pComb=(CComboBox*)this->GetDlgItem(IDC_COMBO4);
    pComb->AddString("4");
    pComb->AddString("5");
	pComb->AddString("6");
	pComb->AddString("7");
	pComb->AddString("8");
    c.Format("%d",this->ByteSize);
    nIndex=pComb->FindString(0,c);
    pComb->SetCurSel(nIndex);

	//停止位
    pComb=(CComboBox*)this->GetDlgItem(IDC_COMBO5);
    pComb->AddString("1");
	//pComb->AddString("1.5");
	pComb->AddString("2");
    
	 c.Format("%d",this->StopBits);
    nIndex=pComb->FindString(0,c);
    pComb->SetCurSel(nIndex);

    //校验
    pComb=(CComboBox*)this->GetDlgItem(IDC_COMBO3);
    pComb->AddString("无校验");
	pComb->AddString("奇校验");
    pComb->AddString("偶校验");

	// c.Format("%s",this->Parity);
	if (strcmp(Parity,"O")==0)
		c="偶校验";
	else if (strcmp(Parity,"E")==0)
		c="奇校验";
	else
        c="无校验";
    nIndex=pComb->FindString(0,c);
    pComb->SetCurSel(nIndex);
	*/
	if(!gpPort->Open(m_pPortNum,nBaudRate))
	{
		AfxMessageBox("不能打开窗口");
        isOpen=FALSE;
	}

	 CButton* pBtn=(CButton*)this->GetDlgItem(IDC_BUTTON_COPEN);
	 CStatic   *pStatic=(CStatic*)GetDlgItem(IDC_STATIC_PIC);  
	 //CEdit*   pEditRx=(CEdit*)GetDlgItem(IDC_EDIT_RXDATA);
	 HICON    hIcon;
	 if (isOpen)
	 {
	  pBtn->SetWindowText("关闭串口");
 
	  SendUserAt(0,"ATE0");
	  SendUserAt(0,"at+ipc=0");
	  SendEnable(0);
	  //gpView=(CGTcfgDlg*)this;
	  // hReadThread=CreateThread(NULL,0,ReadPortThread,(LPVOID)this,0,NULL);
	  hReadThread=AfxBeginThread(ReadPortThread,(LPVOID)this);  
	  hCheckhread=CreateThread(NULL,0,GetCheckThread,(LPVOID)this,0,NULL);
			hIcon=AfxGetApp()->LoadIcon(IDI_ICON2);
		  pStatic->SetIcon(hIcon);   
		  InvalidateRect(NULL,TRUE); 
	 }
	 else
	 {
	 		hIcon=AfxGetApp()->LoadIcon(IDI_ICON1);
		  pStatic->SetIcon(hIcon);   
		  InvalidateRect(NULL,TRUE);   
	 }
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CGTcfgDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CGTcfgDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CGTcfgDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CGTcfgDlg::OnButtonSrv1ip() 
{
	// TODO: Add your control notification handler code here

	gstrTxat1="at+srv1ip?";
	SendUserAt(1,"at+srv1ip?");
}

void CGTcfgDlg::OnButtonSrv1pt() 
{
	// TODO: Add your control notification handler code here
	
	gstrTxat1="at+srv1pt?";
	SendUserAt(2,"at+srv1pt?");
}

void CGTcfgDlg::OnButtonSrv2ip() 
{
	// TODO: Add your control notification handler code here
	
		gstrTxat1="at+srv2ip?";
	    SendUserAt(3,"at+srv2ip?");
}


void CGTcfgDlg::OnButtonSrv2pt() 
{
	// TODO: Add your control notification handler code here
	
		gstrTxat1="at+srv2pt?";
	    SendUserAt(4,"at+srv2pt?");
}

void CGTcfgDlg::OnButtonIpgprs() 
{
	// TODO: Add your control notification handler code here
		
		gstrTxat1="at+ipgprs?";
	    SendUserAt(5,"at+ipgprs?");
}

void CGTcfgDlg::OnButtonIpc() 
{
	// TODO: Add your control notification handler code here
	
		gstrTxat1="at+ipc?";
	    SendUserAt(6,"at+ipc?");
}

void CGTcfgDlg::OnButtonTum() 
{
	// TODO: Add your control notification handler code here
	
		gstrTxat1="at+tum?";
	    SendUserAt(12,"at+tum?");
}

void CGTcfgDlg::OnButtonImei() 
{
	// TODO: Add your control notification handler code here
		if (gstrTxat1=="at+ipc=1")
	{
//		m_ctrlComm.SetOutput(COleVariant("+"));
//		m_ctrlComm.SetOutput(COleVariant("+"));
//		m_ctrlComm.SetOutput(COleVariant("+"));
	}
//	SendUserAt(8,"at+imei=07079988");
		gstrTxat1="at+imei?";
	SendUserAt(8,"at+imei?");
}

void CGTcfgDlg::OnButtonHbs() 
{
	// TODO: Add your control notification handler code here
	
		gstrTxat1="at+hbs?";
	SendUserAt(9,"at+hbs?");
}

void CGTcfgDlg::OnButtonIpr() 
{
	// TODO: Add your control notification handler code here
	
		gstrTxat1="at+ipr?";
	SendUserAt(10,"at+ipr?");
}

void CGTcfgDlg::OnButtonHbt() 
{
	// TODO: Add your control notification handler code here

		gstrTxat1="at+hbt?";
	SendUserAt(11,"at+hbt?");
}

void CGTcfgDlg::OnButtonCopen() 
{
	// TODO: Add your control notification handler code here
	 CButton* pBtn=(CButton*)this->GetDlgItem(IDC_BUTTON_COPEN);
	   pBtn->SetWindowText("打开串口");
     CStatic   *pStatic=(CStatic*)GetDlgItem(IDC_STATIC_PIC);  
	   HICON    hIcon;
		hIcon=AfxGetApp()->LoadIcon(IDI_ICON1);
 
	  pStatic->SetIcon(hIcon);   
	  InvalidateRect(NULL,TRUE);   
   if (isOpen)
   { 
	  //pRecvThread->
	   isOpen=FALSE;
       gstrChck="";
	  if(! gpPort->ClosePort(m_hComDev))
         AfxMessageBox("不能成功关闭串口!");
	   return;

   }
	CComboBox* pCombo = (CComboBox*)GetDlgItem(IDC_COMBO2);
	int index = pCombo->GetCurSel();
	//m_ioctl.pos.baud_pos = index;
     isOpen=TRUE;
	  pBtn->SetWindowText("关闭串口");
	switch(index) 
	{
	case 0:
		nBaudRate=1200;
		break;
	case 1:
        nBaudRate=2400;
		break;
	case 2:
        nBaudRate=4800;
		break;
	case 3:
        nBaudRate=9600;
		break;
	case 4:
        nBaudRate=2400;
		break;
	case 5:
        nBaudRate=19200;
		break;
	case 6:
        nBaudRate=38400;
		break;
	case 7:
        nBaudRate=43000;
		break;
	case 8:
        nBaudRate=56000;
		break;
	case 9:
        nBaudRate=57600;
		break;
	case 10:
       nBaudRate=115200;
		break;

	}


CString c;
	if(!gpPort->Open(m_pPortNum,nBaudRate))
	{
		AfxMessageBox("不能打开窗口");
        isOpen=FALSE;
		 pBtn->SetWindowText("打开串口");
		 return;
	}
	if(isOpen)
	{
	 hIcon=AfxGetApp()->LoadIcon(IDI_ICON2);
  pStatic->SetIcon(hIcon);   
  InvalidateRect(NULL,TRUE);   
	SendUserAt(0,"ate0");
	// hHand=CreateThread(NULL,0,Get_Thread,0,0,NULL);
	//SendUserAt(0,"at+ipc=0");
   hReadThread=AfxBeginThread(ReadPortThread,(LPVOID)this); 
   hCheckhread=CreateThread(NULL,0,GetCheckThread,(LPVOID)this,0,NULL);
	}
}

void CGTcfgDlg::OnButtonManualsend() 
{int iAt;
	char sTemp[100];
	CString csTemp;
    iAt=gAt_id;
	CComboBox* pComb=(CComboBox*)this->GetDlgItem(IDC_COMBO_BT);
	//CEdit* pEdit=(CEdit*)this->GetDlgItem(IDC_EDIT_TXDATA);
	switch (iAt)
	{
	case 1:
        gstrTxat="at+srv1ip=";
		break;
	case 2:
        gstrTxat="at+srv1pt=";
		break;
	case 3:
        gstrTxat="at+srv2ip=";
		break;
	case 4:
        gstrTxat="at+srv2pt=";
		break;
	case 5:
        gstrTxat="at+ipgprs=";
		break;
	case 6:
        gstrTxat="at+ipc=";
		break;
//	case 7:
  //      gstrTxat="at+rs485=";
	//	break;
	case 8:
        gstrTxat="at+imei=";
		break;
	case 9:
        gstrTxat="at+hbs=";
		break;
	case 10:
        gstrTxat="at+ipr=";
		break;

⌨️ 快捷键说明

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