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

📄 communite.cpp

📁 一个基于PXA255的水情遥测、遥控系统现场服务器软件
💻 CPP
字号:
// Communite.cpp : implementation file
//

#include "stdafx.h"
#include "Interface.h"
#include "Communite.h"

#include <atlconv.h>
#include <stdio.h>
#define WM_RORDER  WM_USER+100


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

/////////////////////////////////////////////////////////////////////////////
// CCommunite dialog

UINT CommProc(LPVOID lParam)//实时显示消息,
{

	CCommunite	* ad=(CCommunite *)lParam;
	DWORD dwBytesTransferred;
	char buff[100];
	CString str;
	Sleep(500);
		while(ad->Serial.m_hIDComDev!= INVALID_HANDLE_VALUE)
		{
				ReadFile (ad->Serial.m_hIDComDev,
					buff, 
					50, 
					&dwBytesTransferred,
					0
					);
     
		if(dwBytesTransferred>0)
		{
			buff[dwBytesTransferred]='\0';
			USES_CONVERSION; 


//////////////////////////////////////////////
		//	if(str.Compare(_T("AAA4CWJ1BB"))!=0) //传文件命令
			if(!strncmp(buff,"AA",2))//AAA表示命令;
			{
			str.Format(_T("ORDER!\r\n"));
			ad->CenterMsg+=str;
			ad->GetDlgItem(IDC_EDIT1)->SetWindowText(ad->CenterMsg);
			strncpy(ad->Ordersbj,buff,10);
			PostMessage(ad->m_hWnd,WM_RORDER,0,0);
		//	AfxGetMainWnd()->PostMessage(WM_RORDER,0,0);
			}
			else if(!strncmp(buff,"OK",2))
			{
			   	buff[3]='\0';
               	ad->Serial.SendData(buff,strlen(buff));
			}
			else//通知命令处理//AfxMessageBox(_T("dfg"));
			{
			str.Format(_T("Center Msg:%s\r\n"),A2W(buff));
			ad->CenterMsg+=str;
			 ad->GetDlgItem(IDC_EDIT1)->SetWindowText(ad->CenterMsg);
			 }
		}
	 Sleep(500);
	}
  return 0;
}


CCommunite::CCommunite(CWnd* pParent /*=NULL*/)
	: CDialog(CCommunite::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCommunite)
	m_sdata = _T("");
	//}}AFX_DATA_INIT
}


void CCommunite::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCommunite)
	DDX_Text(pDX, IDC_EDIT2, m_sdata);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCommunite, CDialog)
	//{{AFX_MSG_MAP(CCommunite)
	ON_BN_CLICKED(IDC_BUTTON1, OnSend)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_RORDER,DilMsg)//添加消息处理
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCommunite message handlers
void CCommunite::DilMsg(UINT lParam, LONG wParam)
{
   CString str;
//	 USES_CONVERSION; 
//	 str.Format(_T("%s"),A2W(Databuf));
	char n[2];//判断命令类型
	char n1;
	char Gdata[3]="02";//取地址
	int rs;
	char szMessage[11];//发送的消息
    strncpy(n,Ordersbj+2,1);
    n1=Ordersbj[5];
	Ordersbj[10]='\0';
	switch(*n)
	{
	case '1'://采集命令
           	//strncpy(Gdata,Ordersbj+3,2);//取得传送来的4位16进制;
            strncpy(n,Ordersbj+4,1);
			rs=int(n1)-48;
	        sprintf(szMessage, "%d",SC51.GetData(Gdata,rs));//对C51串口
			Serial.SendData(szMessage,strlen(szMessage));//发回主空中心
			
			str.Format(_T("Collectd OK!\r\n"));
			CenterMsg+=str;
			GetDlgItem(IDC_EDIT1)->SetWindowText(CenterMsg);
			break;
    case '2'://控制命令
		    
		    SC51.SendData(Ordersbj,strlen(Ordersbj));//51串口
			//strncpy(n,Ordersbj,1);
			str.Format(_T("Control Complete!\r\n"));
			CenterMsg+=str;
			GetDlgItem(IDC_EDIT1)->SetWindowText(CenterMsg);
		    break;
    case '3'://
           	strncpy(Gdata,Ordersbj+3,2);//取得传送来的4位16进制;
		    Gdata[3]='\0';
			rs=SC51.CheckhOP(Gdata);
			sprintf(szMessage, "%d",SC51.CheckhOP(Gdata)); 
			Serial.SendData(szMessage,strlen(szMessage));

			str.Format(_T("Check OP Complete!\r\n"));
			CenterMsg+=str;
            GetDlgItem(IDC_EDIT1)->SetWindowText(CenterMsg);
			break;

    case 'A'://文件传送
			str.Format(_T("Tramslating File!\r\n"));
			CenterMsg+=str;
            GetDlgItem(IDC_EDIT1)->SetWindowText(CenterMsg);

			SuspendThread(m_pThread);
			Serial.TramFile();    
			ResumeThread(m_pThread);
            GetDlgItem(IDC_EDIT1)->SetWindowText(_T("Tramslate File Complete!"));
			break;
	default:
		break;
	}


	/*
//   CString str;
//	 USES_CONVERSION; 
//	 str.Format(_T("%s"),A2W(Databuf));
	 SuspendThread(m_pThread);
	 Serial.TramFile(); 
	 //WaitForSingleObject(m_pThread->m_hThread,INFINITE);
	 ResumeThread(m_pThread);
	 //if(str.Compare(_T("AAA4CWJ1BB")==0)  
//	 m_srdata=str;
	 UpdateData(0);
*/
}

BOOL CCommunite::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
  	if(!Serial.Open(1,38400))
         AfxMessageBox(_T("init error!")); 

	if(!SC51.Open(5,2400))
         AfxMessageBox(_T("init error!")); 

    m_pThread=AfxBeginThread(CommProc,this,THREAD_PRIORITY_BELOW_NORMAL,0,0);
    GetDlgItem(IDC_COMBO1)->SetWindowText(_T("COM1"));
    GetDlgItem(IDC_COMBO2)->SetWindowText(_T("38400"));
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CCommunite::OnSend() 
{
     UpdateData(1); 
	 TCHAR *msg=new TCHAR[100]; 
	 
	 msg=m_sdata.GetBuffer(m_sdata.GetLength());
     USES_CONVERSION; 

	 Serial.SendData(W2A(msg),strlen(W2A(msg)));

	 m_sdata.Empty();
	 UpdateData(0);  	
}

void CCommunite::OnOK() 
{
	// TODO: Add extra validation here

	

	CDialog::OnOK();
}

⌨️ 快捷键说明

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