📄 momentvaluedlg.cpp
字号:
// MomentValueDlg.cpp : implementation file
//
#include "stdafx.h"
#include "EcoPowerMeter.h"
#include "MomentValueDlg.h"
#include "SerialCommunication.h"
#import <msxml4.dll>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMomentValueDlg dialog
CMomentValueDlg::CMomentValueDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMomentValueDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMomentValueDlg)
//}}AFX_DATA_INIT
//m_ecoPowerMeter = (CEcoPowerMeterApp *)AfxGetApp();
m_serial = new CSerialCommunication(this);
memset(chCommand,0,sizeof(chCommand));
memcpy(&chCommand[0][0],"%01#RDD001760017754\r",20);
memcpy(&chCommand[1][0],"%02#RDD001760017757\r",20);
memcpy(&chCommand[2][0],"%03#RDD001760017756\r",20);
memcpy(&chCommand[3][0],"%04#RDD001760017751\r",20);
memcpy(&chCommand[4][0],"%05#RDD001760017750\r",20);
memcpy(&chCommand[5][0],"%06#RDD001760017753\r",20);
memcpy(&chCommand[6][0],"%07#RDD001760017752\r",20);
memcpy(&chCommand[7][0],"%08#RDD00176001775D\r",20);
//memset(chBranch,0,sizeof(chBranch));
/*memset(m_editAverageValue,0,sizeof(m_editAverageValue));
memcpy(&m_editAverageValue[0],m_editAverageValue1,19);
memcpy(&m_editAverageValue[1],m_editAverageValue2,19);
memcpy(&m_editAverageValue[2],m_editAverageValue3,19);
memcpy(&m_editAverageValue[3],m_editAverageValue4,19);
memcpy(&m_editAverageValue[4],m_editAverageValue5,19);
memcpy(&m_editAverageValue[5],m_editAverageValue6,19);
memcpy(&m_editAverageValue[6],m_editAverageValue7,19);
memcpy(&m_editAverageValue[7],m_editAverageValue8,19);
memset(m_editMomentValue,0,sizeof(m_editMomentValue));
memcpy(&m_editMomentValue[0],m_editMomentValue1,18);
memcpy(&m_editMomentValue[1],m_editMomentValue2,18);
memcpy(&m_editMomentValue[2],m_editMomentValue3,18);
memcpy(&m_editMomentValue[3],m_editMomentValue4,18);
memcpy(&m_editMomentValue[4],m_editMomentValue5,18);
memcpy(&m_editMomentValue[5],m_editMomentValue6,18);
memcpy(&m_editMomentValue[6],m_editMomentValue7,18);
memcpy(&m_editMomentValue[7],m_editMomentValue8,18);*/
}
CMomentValueDlg::~CMomentValueDlg(void)
{
delete m_serial;
}
void CMomentValueDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMomentValueDlg)
DDX_Control(pDX, IDC_EDIT16, m_editAverageValue8);
DDX_Control(pDX, IDC_EDIT15, m_editAverageValue7);
DDX_Control(pDX, IDC_EDIT14, m_editAverageValue6);
DDX_Control(pDX, IDC_EDIT13, m_editAverageValue5);
DDX_Control(pDX, IDC_EDIT12, m_editAverageValue4);
DDX_Control(pDX, IDC_EDIT11, m_editAverageValue3);
DDX_Control(pDX, IDC_EDIT10, m_editAverageValue2);
DDX_Control(pDX, IDC_EDIT9, m_editAverageValue1);
DDX_Control(pDX, IDC_EDIT8, m_editMomentValue8);
DDX_Control(pDX, IDC_EDIT7, m_editMomentValue7);
DDX_Control(pDX, IDC_EDIT6, m_editMomentValue6);
DDX_Control(pDX, IDC_EDIT5, m_editMomentValue5);
DDX_Control(pDX, IDC_EDIT4, m_editMomentValue4);
DDX_Control(pDX, IDC_EDIT3, m_editMomentValue3);
DDX_Control(pDX, IDC_EDIT2, m_editMomentValue2);
DDX_Control(pDX, IDC_EDIT1, m_editMomentValue1);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMomentValueDlg, CDialog)
//{{AFX_MSG_MAP(CMomentValueDlg)
ON_BN_CLICKED(IDC_BUTTON1, OnButtonSerialTest)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON2, OnButtonTcpIpSerialStart)
ON_BN_CLICKED(IDC_BUTTON3, OnButtonClose)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMomentValueDlg message handlers
// the test of the communication of the serial
void CMomentValueDlg::OnButtonSerialTest()
{
// TODO: Add your control notification handler code here
CString strPort,strCustomerID,strBranch,strFileBranch;
//------------grobol変数がほしい-------削除--------------
//CString branch1,branch2,branch3,branch4,branch5,branch6,branch7,branch8;
//CString strBranchUsed[D_KYOKUNUMS];// = {branch1,branch2,branch3,branch4,branch5,branch6,branch7,branch8};
//CString branch1,branch2,branch3,branch4,branch5,branch6,branch7,branch8;
//CString strBranchUsed[D_KYOKUNUMS];// = {branch1,branch2,branch3,branch4,branch5,branch6,branch7,branch8};
//--------------------------------------------------
GetPrivateProfileString("ConfigSet","Port","",strPort.GetBuffer(MAX_PATH),MAX_PATH,".\\ConfigSet.ini");
GetPrivateProfileString("ConfigSet","CustomerID","",strCustomerID.GetBuffer(MAX_PATH),MAX_PATH,".\\ConfigSet.ini");
// acquire the checked branch
bool blnBranch = true;
for(int i=1; i<D_KYOKUNUMS+1; i++)
{
strBranch.Format("strBranch%d",i);
strFileBranch.Format("Branch%d",i);
GetPrivateProfileString("ConfigSet",strFileBranch,"",strBranch.GetBuffer(MAX_PATH),MAX_PATH,".\\ConfigSet.ini");
strBranchUsed[i-1] = strBranch;//acquire the branch
TRACE("strBranchUsed[%d] = %s\n",i-1,strBranch);
if(blnBranch)
{
if(!strBranchUsed[i-1].IsEmpty()&&strBranchUsed[i-1]!="")
{
m_iSndKyoku = i;
blnBranch = false;
}
}
}
TRACE("m_iSndKyoku = %d\n",m_iSndKyoku);
// initialize the struct
for(int j=0; j<D_KYOKUNUMS; j++)
{
m_dKyoku[j].dElectricityAve=0.0;
m_dKyoku[j].iCnt=0;
}
this->m_serial->Open(strPort);
SetTimer(1,10000,NULL);//10s serial communication
}
void CMomentValueDlg::OnReceive(LPCSTR str, DWORD strLength)
{
CEdit *m_editAverageValue[D_KYOKUNUMS] = {&m_editAverageValue1,&m_editAverageValue2,&m_editAverageValue3,&m_editAverageValue4,
&m_editAverageValue5,&m_editAverageValue6,&m_editAverageValue7,&m_editAverageValue8};
CEdit *m_editMomentValue[D_KYOKUNUMS] = {&m_editMomentValue1,&m_editMomentValue2,&m_editMomentValue3,&m_editMomentValue4,
&m_editMomentValue5,&m_editMomentValue6,&m_editMomentValue7,&m_editMomentValue8};
CString received = str;
CString receivedTemp ;
double dPow,dTmp;
int i,j;
char rcv[21];
if(received[3] != '$')
return;
i=received.GetLength();
char int_tmp = 0;
//エラーチャック
for(int k=0;k<received.GetLength()-3;k++)
{
int_tmp ^= received[k] ;
}
if(((int_tmp>>4)+0x30) != received[i-3] )
return;
if(((int_tmp&0x0F)+0x30) != received[i-2] )
return;
memset(rcv, 0, sizeof(rcv));
rcv[6] = received[6];
rcv[7] = received[7];
rcv[4] = received[8];
rcv[5] = received[9];
rcv[2] = received[10];
rcv[3] = received[11];
rcv[0] = received[12];
rcv[1] = received[13];
for(dPow=268435456.0,dTmp=0.0,j=7,i=0; i<8; j--,i++)
{
if('A'<=rcv[i] && rcv[i]<='F')
rcv[i] = rcv[i]-'A'+10;
else if('a'<=rcv[i] && rcv[i]<='f')
rcv[i] = rcv[i]-'a'+10;
else
rcv[i]-='0';
dTmp+=((double)rcv[i]*dPow);
dPow/=16.0;
}
printf("rcv = %lf\r", dTmp);
iKyoku = received[2]-0x31;
if(!m_dKyoku[iKyoku].iCnt)
{
m_dKyoku[iKyoku].dElectricityAve=dTmp;
m_dKyoku[iKyoku].iCnt++;
}
else
{
m_dKyoku[iKyoku].dElectricityAve+=dTmp;
m_dKyoku[iKyoku].dElectricityAve/=2.0;
m_dKyoku[iKyoku].iCnt++;
}
CString strTmp;
strTmp.Format("%.0lf", dTmp);
//m_editReceived.SetWindowText(strTmp);
TRACE("strTemp = %s\n",strTmp);
m_editMomentValue[iKyoku]->SetWindowText(strTmp);
CString stringAve ;
stringAve.Format("%.0lf",m_dKyoku[iKyoku].dElectricityAve);
TRACE("stringAve = %s\n",stringAve);
//m_editAver.SetWindowText(stringAve);
m_editAverageValue[iKyoku]->SetWindowText(stringAve);
}
void CMomentValueDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
static BOOL bln1stml(true);
CString strCommand;
char chTime[200];
CTime cTime;
int year, month, day, hour, min, sec;
cTime = CTime::GetCurrentTime();
year = cTime.GetYear();
month = cTime.GetMonth();
day = cTime.GetDay();
hour = cTime.GetHour();
min = cTime.GetMinute();
sec = cTime.GetSecond();
switch(nIDEvent)
{
case 1:
strCommand = (LPTSTR)(&chCommand[m_iSndKyoku-1][0]);// get command for example %01#RDD001760017754
TRACE("strCommand = %s\n",strCommand);
this->m_serial->Send(strCommand,strCommand.GetLength());//送信
do
{
m_iSndKyoku++;
if(m_iSndKyoku == D_KYOKUNUMS + 1)
{
m_iSndKyoku = 1 ;
}
}while(strBranchUsed[m_iSndKyoku-1].IsEmpty()||(strBranchUsed[m_iSndKyoku-1]==""));
break;
case 2:
if(min%10)
{
bln1stml = true;
TRACE("Case 2-----------------------\n");
}
else
{
if(!bln1stml)
break;
KillTimer(1);
CString strPastTime;
D_TIMEVALUE_DATA TimeValePast;
strPastTime.Format("%04d-%02d-%02d %02d:%02d:00",year,month,day,hour,min);
memset(chTime,0,sizeof(chTime));
strncpy(chTime,strPastTime,strPastTime.GetLength()+1);//get pasttime
//the time of transmission
TimeValePast.dTime = strPastTime;
//put data in m_pastTimeValueList
for(int j=0; j<D_KYOKUNUMS; j++)
{
if(!m_dKyoku[j].iCnt)
//if(m_dKyoku[j].iCnt)
continue;
TimeValePast.dValue=(long)(m_dKyoku[j].dElectricityAve+0.5);
m_pastTimeValueList[j].AddTail(TimeValePast);
}
this->SendTcpIpContent(chTime);//send data to server
bln1stml = false;
this->XMLDOMParserToData();
//TCHAR *pFileName = _T("sample.xml");
//CFile::Remove(pFileName);
this->ToGetLostData();//获得ringbuffer中的数据欠損数据
if(!(this->m_tempTimeValueList[0].IsEmpty()))//判断必要ですか?
{
this->SendTcpIpLostData();
}
for(int i=0; i<D_KYOKUNUMS; i++)
{
m_dKyoku[i].dElectricityAve=0.0;
m_dKyoku[i].iCnt=0;
}
SetTimer(1,10000,NULL);
}
break;
}
CDialog::OnTimer(nIDEvent);
}
void CMomentValueDlg::OnButtonTcpIpSerialStart()
{
// TODO: Add your control notification handler code here
KillTimer(1);
this->m_serial->Close();
this->OnButtonSerialTest();
SetTimer(1,10000,NULL);//10s serial communication
SetTimer(2,10000,NULL);//10s Tcp/Ip communication
}
int CMomentValueDlg::SendTcpIpContent(char chTime[])
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -