void WeeklyRout_InsertDia::write_grid()
{
	UpdateData(true);
	unsigned char p[72];
	CString str;
	unsigned char left_value=0,right_value=0;
	int i=0,j=0,k=0;
	
	for(i=1;i<GRID_COL_NUMBER;i++)
		for(j=1;j<GRID_ROW_NUMBER;j++)
		{//write On:
			if(j%2==1)
			{
				str=m_FlexGrid.get_TextMatrix(j,i);
				get_two_value_from_one_grid(str,left_value,right_value);
				p[k]=left_value;
				p[k+1]=right_value;
				k+=2;
			}
		}

		if (m_strtype.CompareNoCase(_T("Lightingcontroller")) == 0)
			Write_Multi(254,p,6488 + WR_TIME_SIZE*(m_addr-1),0x48);
		else
			Write_Multi(g_tstat_id,p,MODBUS_WR_ONTIME_FIRST + WR_TIME_SIZE*(m_addr-1),0x48);
	k=0;//////////////////////////////////clear
	for(int i=0;i<72;i++)
	{
		p[i]=0;
	}
	
	for(i=1;i<GRID_COL_NUMBER;i++)
		for(j=1;j<GRID_ROW_NUMBER;j++)
		{
			if(j%2==0)//write Off
			{
				str=m_FlexGrid.get_TextMatrix(j,i);
				get_two_value_from_one_grid(str,left_value,right_value);
				p[k]=left_value;
				p[k+1]=right_value;
				k+=2;
			}
		}
//	for(int i=0;i<72;i++)
//		p[i]=0;
	Sleep(10);
	if (m_strtype.CompareNoCase(_T("Lightingcontroller")) == 0)
		Write_Multi(254,p,7928 + WR_TIME_SIZE*(m_addr-1),0x48);
	else
		Write_Multi(g_tstat_id,p,MODBUS_WR_OFFTIME_FIRST + WR_TIME_SIZE*(m_addr-1),0x48);
	
}
void Annual_Routines::clear_addr(unsigned char addr2)
{
	CString str_temp;
	description temp_descri;
	memset(&temp_descri,0,sizeof(description));
	unsigned char *p;
	p=(unsigned char *) &temp_descri;
	for(int i=0;i<AR_DESCRIPTION_SIZE;i++)
		*(p++)=0xFF;
	p=(unsigned char *) &temp_descri;
	if(m_strtype.CompareNoCase(_T("Lightingcontroller")) == 0)
		Write_Multi(254,p,5228+(addr2-1)*AR_DESCRIPTION_SIZE,AR_DESCRIPTION_SIZE);
	else
		Write_Multi(g_tstat_id,p,MODBUS_AR_DESCRIP_FIRST+(addr2-1)*AR_DESCRIPTION_SIZE,AR_DESCRIPTION_SIZE);
}
void Annual_Routines::write_addr(unsigned char addr2)
{
	CString str_temp;
	description2 temp_descri;
	memset(&temp_descri,0,sizeof(description2));
	str_temp=m_FlexGrid.get_TextMatrix(addr2,1);//1
	int i=0;
	for(i=1;i<=FULL_LABEL_NUMBER ;i++)
	{
		if(i<str_temp.GetLength()+1)
            temp_descri.full_label[i-1]=str_temp.GetAt(i-1);
		else
			temp_descri.full_label[i-1]=0;
	}
	temp_descri.full_label[FULL_LABEL_NUMBER-1]=0;

//	str_temp=m_FlexGrid.get_TextMatrix(addr2,4);//4
//	for(i=1;i<=LABEL_NUMBER;i++)
//	{
//		if(i<str_temp.GetLength()+1)
  //          temp_descri.label[i-1]=str_temp.GetAt(i-1);
	//	else
	//		temp_descri.label[i-1]=0;
//	}
	temp_descri.flag=0;
	str_temp=m_FlexGrid.get_TextMatrix(addr2,3);//3
	if(str_temp==_T("ON"))
		temp_descri.flag=temp_descri.flag | 64;
	str_temp=m_FlexGrid.get_TextMatrix(addr2,2);//2
	if(str_temp==_T("MAN"))
		temp_descri.flag=temp_descri.flag | 128;
	unsigned char *p;
	p=(unsigned char *) &temp_descri;
	if(m_strtype.CompareNoCase(_T("Lightingcontroller")) == 0)
		Write_Multi(254,p,5228+(addr2-1)*AR_DESCRIPTION_SIZE,AR_DESCRIPTION_SIZE);
	else
		Write_Multi(g_tstat_id,p,MODBUS_AR_DESCRIP_FIRST+(addr2-1)*AR_DESCRIPTION_SIZE,AR_DESCRIPTION_SIZE);
	NET_WORK_SLEEP_BETWEEN_WRITE_READ
	temp_descri=read_addr(addr2-1);
	put_row_col(temp_descri,addr2);
}
void Cconfigure::OnBnClickedButtonSend()
{
    CString str,str1,str3;
    BYTE Sendswitch[24];
    BYTE SendTimebye[48];
    WORD SendTimeword[24];
    WORD Wtemp = 0;

    BYTE bytetmep = 0;

    memset(Sendswitch,0,sizeof(Sendswitch));
    memset(SendTimebye,0,sizeof(SendTimebye));
    memset(SendTimeword,0,sizeof(SendTimeword));

    for (int i = 0; i<24; i++)
    {
        str = m_msflexgrid.get_TextMatrix(i+1,1);
        if(str.CompareNoCase(_T("ON/OFF")) == 0)
        {
            Sendswitch[i] = 0;

        } else if (str.CompareNoCase(_T("OFF/ON") )== 0)
        {
            Sendswitch[i] = 1;

        } else if (str.CompareNoCase(_T("SW OPEN PULSE")) == 0)
        {
            Sendswitch[i] = 2;

        } else if (str.CompareNoCase(_T("SW CLOSE PULSE")) == 0)
        {
            Sendswitch[i] = 3;

        }

        str1 = m_msflexgrid.get_TextMatrix(i+1,2);
        if (str1.CompareNoCase(_T("ON&OFF")) == 0)
        {
            bytetmep = 1;
            bytetmep = bytetmep<<4;
            Sendswitch[i] = Sendswitch[i]|bytetmep;
        }

        //time
        str3 = m_msflexgrid.get_TextMatrix(i+1,3);
        SendTimeword[i] = _ttoi(str3);
// 		 Wtemp = SendTimeword[i];
// 		 Wtemp = Wtemp&0x0F;
// 		 SendTimebye[i*2+1] = (BYTE)Wtemp;
// 		 Wtemp = SendTimeword[i];
// 		 Wtemp = Wtemp>>8;
// 		 SendTimebye[i*2] = (BYTE)Wtemp;

    }


    //201…224	1 * 24	switch (1..24) types: 0 --- low active,  1--- high active,   2 --- falling edge active,    3 --- rising edge active
    //252…275	2 * 24	override time for each switch. Uint is second. 2bytes = 65536s =~18hours max.
    int ret1=0,ret=0;
    ret = Write_Multi(g_tstat_id,Sendswitch,201,24);
    //int ret1 = Write_Multi(g_tstat_id,SendTimebye,252,48);
    for (int i = 0; i<24; i++)
    {
        ret1= write_one(g_tstat_id,252+i,SendTimeword[i]);

    }


    if ((ret>0)&&(ret1>0))
        AfxMessageBox(_T("Send successful!"));
    else
        AfxMessageBox(_T("Send unsuccessful!"));

}
void Timer_Calibrate::OnBnClickedOk()
{
    // TODO: 在此添加控件通知处理程序代码
    //OnOK();
    KILLTIMER;
    had_select=false;
    UpdateData(true);
    unsigned char t_time[8]= {0};
    unsigned short temp_us=0;
    temp_us=m_date_time.GetYear();
    t_time[0]=temp_us/100;
    t_time[1]=temp_us%100;
    t_time[2]=m_date_time.GetMonth();
    t_time[3]=m_date_time.GetDayOfWeek();
// 	if(t_time[3]==1)
// 		t_time[3]=6;
// 	else
// 		t_time[3]-=2;
    if(t_time[3]==1)
        t_time[3]=7;
    else
        t_time[3]-=1;

    t_time[4]=m_date_time.GetDay();
    t_time[5]=m_time_time.GetHour();
    t_time[6]=m_time_time.GetMinute();
    t_time[7]=m_time_time.GetSecond();
    Write_Multi(g_tstat_id,t_time,200,8);
    int t=_wtoi(m_time_zone.Right(m_time_zone.GetLength()-1));
    if(m_time_zone.Left(1)=="+")
        t+=12;
    else
        t=12-t;
    write_one(g_tstat_id,11,t);

    Read_Multi(g_tstat_id,machine_time,200,8,3);
    if(machine_time[1]<10)
        m_machine_time.Format(_T("   %d/%d/%d0%d %d:%d:%d"),machine_time[2],machine_time[4],machine_time[0],machine_time[1],machine_time[5],machine_time[6],machine_time[7]);
    else
        m_machine_time.Format(_T("   %d/%d/%d%d %d:%d:%d"),machine_time[2],machine_time[4],machine_time[0],machine_time[1],machine_time[5],machine_time[6],machine_time[7]);
    if(machine_time[5]>12)
        m_machine_time+=_T(" PM");
    else
        m_machine_time+=_T(" AM");

    m_NCDateCtrl.SetDate(machine_time[0]*100+machine_time[1],machine_time[2],machine_time[4]);
    m_NCTimeCtrl.SetTime(machine_time[5],machine_time[6],machine_time[7]);

    if(had_select==false)
        m_time_time=m_time_time.GetCurrentTime();
    TIME_ZONE_INFORMATION temp;
    GetTimeZoneInformation(&temp);
    m_time_zone_str=temp.StandardName;
    int i_temp=read_one(g_tstat_id,11);
    if(temp.Bias>0)
    {
        m_time_zone=_T("-");
        CString t;
        t.Format(_T("%d"),temp.Bias/60);
        m_time_zone=m_time_zone+t;
        if(i_temp==255)
        {
            m_building_time_zone.SetCurSel(12-temp.Bias/60);
            write_one(g_tstat_id,11,(short)(12-temp.Bias/60));
        }
        else if(i_temp>=0)
            m_building_time_zone.SetCurSel(i_temp);
    }
    else
    {
        m_time_zone=_T("+");
        CString t;
        t.Format(_T("%d"),temp.Bias/-60);
        m_time_zone=m_time_zone+t;
        if(i_temp==255)
        {
            m_building_time_zone.SetCurSel(12-temp.Bias/60);
            write_one(g_tstat_id,11,(short)(12-temp.Bias/60));
        }
        else if(i_temp>=0)
            m_building_time_zone.SetCurSel(i_temp);
    }

    UpdateData(FALSE);
    SETTIMER;
}