private void timer1_Tick_1(object sender, EventArgs e) { // 更新能源消耗情况 正泰电表能源消耗 value5 string now_day = DateTime.Now.ToString("yyyy-MM-dd"); string now_day_start = now_day + " 00:00:00"; string now_day_end = now_day + " 23:59:59"; try { if (isscaning == true) { ArrayList all_elect_devices_id = Mysql.Get_Sql_Select_Return("select shebeiID from shebeitable where shebeizhongleiID=(select shebeizhongleiID from shebeizhongleitable where shebeizhongleiname=\"正泰电表\")"); foreach (ArrayList device_id_object in all_elect_devices_id) { string device_id = device_id_object[0].ToString(); Mysql.Ex_Sql("insert into elect_device_energy_cost values(\"" + device_id + now_day + "\",\"" + device_id + "\",\"" + now_day + "\",(select max(value)-min(value) from history_save where value_id=(select canshutypeid from canshutable where canshutype=\"正向有功总电能\") and device_id=\"" + device_id + "\" and savetime>=\"" + now_day_start + "\" and savetime<=\"" + now_day_end + "\" ))"); Mysql.Ex_Sql("update elect_device_energy_cost set cost=(select max(value)-min(value) from history_save where value_id=(select canshutypeid from canshutable where canshutype=\"正向有功总电能\") and device_id=\"" + device_id + "\" and savetime>=\"" + now_day_start + "\" and savetime<=\"" + now_day_end + "\" ) where device_id=\"" + device_id + "\" and datetime=\"" + now_day + "\""); } } } catch { } }
private void timer_project_online_Tick(object sender, EventArgs e) { if (comboBox_project.Text == "") { return; } if (isscaning == false) { return; } string project_name = comboBox_project.Text; Mysql.Ex_Sql("insert into project_online values(( select xiangmuID from xiangmuguanlitable where xiangmuname=\"" + project_name + "\"),\"" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\")"); Mysql.Ex_Sql("update project_online set onlinetime=\"" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\" where ProjectID= ( select xiangmuID from xiangmuguanlitable where xiangmuname=\"" + project_name + "\")"); }
void Receive_Data(object sender, EventArgs e) { string str = ""; if (client1.RECEIVE_NUM <= 30) { return; } for (int i = 0; i < 300; i++) { str = str + client1.receive_byte[i].ToString("X").PadLeft(2, '0'); } // 检查数据的地址是否正确 if (str.Substring(4, 16) == connect_device_address) { string reslut_value = ""; // 写入某个数值 // 三相电压采集 #region if (connect_value_name == "正泰电表电压") { byte[] Ua_bytes = new byte[4]; Ua_bytes[0] = client1.receive_byte[33]; Ua_bytes[1] = client1.receive_byte[32]; Ua_bytes[2] = client1.receive_byte[31]; Ua_bytes[3] = client1.receive_byte[30]; byte[] Ub_bytes = new byte[4]; Ub_bytes[0] = client1.receive_byte[37]; Ub_bytes[1] = client1.receive_byte[36]; Ub_bytes[2] = client1.receive_byte[35]; Ub_bytes[3] = client1.receive_byte[34]; byte[] Uc_bytes = new byte[4]; Uc_bytes[0] = client1.receive_byte[41]; Uc_bytes[1] = client1.receive_byte[40]; Uc_bytes[2] = client1.receive_byte[39]; Uc_bytes[3] = client1.receive_byte[38]; float Ua = BitConverter.ToSingle(Ua_bytes, 0) / 10; float Ub = BitConverter.ToSingle(Ub_bytes, 0) / 10; float Uc = BitConverter.ToSingle(Uc_bytes, 0) / 10; reslut_value = Ua.ToString() + " " + Ub.ToString() + " " + Uc.ToString(); show_text = "电压a:" + Ua.ToString() + " 电压b:" + Ub.ToString() + " 电压c:" + Uc.ToString(); } #endregion // 三相电流采集 #region if (connect_value_name == "正泰电表电流") { byte[] Ia_bytes = new byte[4]; Ia_bytes[0] = client1.receive_byte[33]; Ia_bytes[1] = client1.receive_byte[32]; Ia_bytes[2] = client1.receive_byte[31]; Ia_bytes[3] = client1.receive_byte[30]; byte[] Ib_bytes = new byte[4]; Ib_bytes[0] = client1.receive_byte[37]; Ib_bytes[1] = client1.receive_byte[36]; Ib_bytes[2] = client1.receive_byte[35]; Ib_bytes[3] = client1.receive_byte[34]; byte[] Ic_bytes = new byte[4]; Ic_bytes[0] = client1.receive_byte[41]; Ic_bytes[1] = client1.receive_byte[40]; Ic_bytes[2] = client1.receive_byte[39]; Ic_bytes[3] = client1.receive_byte[38]; float Ia = BitConverter.ToSingle(Ia_bytes, 0) * 50f * 0.001f; float Ib = BitConverter.ToSingle(Ib_bytes, 0) * 50f * 0.001f; float Ic = BitConverter.ToSingle(Ic_bytes, 0) * 50f * 0.001f; reslut_value = Ia.ToString() + " " + Ib.ToString() + " " + Ic.ToString(); show_text = "电流a:" + Ia.ToString() + " 电流b:" + Ib.ToString() + " 电流c:" + Ic.ToString(); } #endregion // 合相有功功率 #region if (connect_value_name == "合相有功功率") { byte[] power_bytes = new byte[4]; power_bytes[0] = client1.receive_byte[33]; power_bytes[1] = client1.receive_byte[32]; power_bytes[2] = client1.receive_byte[31]; power_bytes[3] = client1.receive_byte[30]; float power = BitConverter.ToSingle(power_bytes, 0); reslut_value = power.ToString(); show_text = "合相有功功率:" + reslut_value; } #endregion // 合相功率因数 #region if (connect_value_name == "合相功率因数") { byte[] power_factor_bytes = new byte[4]; power_factor_bytes[0] = client1.receive_byte[33]; power_factor_bytes[1] = client1.receive_byte[32]; power_factor_bytes[2] = client1.receive_byte[31]; power_factor_bytes[3] = client1.receive_byte[30]; float power_factor = BitConverter.ToSingle(power_factor_bytes, 0); reslut_value = power_factor.ToString(); show_text = "合相功率因数:" + reslut_value; } #endregion if (connect_value_name == "正向有功总电能") { byte[] Total_positive_active_energy_bytes = new byte[4]; Total_positive_active_energy_bytes[0] = client1.receive_byte[33]; Total_positive_active_energy_bytes[1] = client1.receive_byte[32]; Total_positive_active_energy_bytes[2] = client1.receive_byte[31]; Total_positive_active_energy_bytes[3] = client1.receive_byte[30]; float Total_positive_active_energy = BitConverter.ToSingle(Total_positive_active_energy_bytes, 0) * 50f; reslut_value = Total_positive_active_energy.ToString(); show_text = "正向有功总电能:" + reslut_value; } if (connect_value_name == "电流互感器倍率") { float Current_ratio = client1.receive_byte[30] * 256 + client1.receive_byte[31]; reslut_value = Current_ratio.ToString(); show_text = "电流互感器倍数:" + reslut_value; } if (connect_value_name == "电压互感器倍率") { float voltage_ratio = client1.receive_byte[30] * 256 + client1.receive_byte[31]; reslut_value = voltage_ratio.ToString(); show_text = "电压互感器倍数:" + reslut_value; } if (connect_value_name == "正泰三相电力温度") { float temp1 = client1.receive_byte[30] * 256 + client1.receive_byte[31]; float temp2 = client1.receive_byte[32] * 256 + client1.receive_byte[33]; float temp3 = client1.receive_byte[34] * 256 + client1.receive_byte[35]; reslut_value = (temp1 / 10).ToString() + " " + (temp2 / 10).ToString() + " " + (temp3 / 10).ToString(); show_text = "正泰三相电力温度:" + reslut_value; } if (connect_value_name == "8位开关量") { byte temp = client1.receive_byte[30]; // 第一位 //int bit1 = temp % 2; //int bit2 = (temp - bit1) % 4; ArrayList bit_list = new ArrayList(); ArrayList bit_result = new ArrayList(); for (int i = 1; i <= 8; i++) { int bit = (int)(temp % Math.Pow(2, i)); temp = (byte)(temp - (int)(temp % Math.Pow(2, i))); bit_list.Add(bit); } for (int i = 0; i <= 7; i++) { if ((int)bit_list[i] != 0) { bit_result.Add(1); } else { bit_result.Add(0); } } //reslut_value = bit1.ToString() + " " + bit2.ToString() + " " + bit3.ToString() + " " + bit4.ToString() + " " + bit5.ToString() + " " + bit6.ToString() + " " + bit7.ToString() + " " + bit8.ToString(); for (int i = 0; i <= 7; i++) { if (i != 7) { reslut_value = reslut_value + bit_result[i].ToString() + " "; } if (i == 7) { reslut_value = reslut_value + bit_result[i].ToString(); } } show_text = "开关量为:" + reslut_value; } // 4路模拟量采集 if (connect_value_name == "4路模拟量采集") { int a = 0; int value1 = client1.receive_byte[36] * 256 + client1.receive_byte[37]; int value2 = client1.receive_byte[34] * 256 + client1.receive_byte[35]; int value3 = client1.receive_byte[32] * 256 + client1.receive_byte[33]; int value4 = client1.receive_byte[30] * 256 + client1.receive_byte[31]; reslut_value = value1.ToString() + " " + value2.ToString() + " " + value3.ToString() + " " + value4.ToString(); show_text = "模拟量为:" + reslut_value; } // 写入数据库并更新datagridview // 写入数据库 try { Mysql.Ex_Sql("update shebeitable set value" + (connect_value_num + 1).ToString() + "=\"" + reslut_value + "\" where shebeiID=\"" + connect_device_id + "\""); // 在value10里写入消息来到的时间 Mysql.Ex_Sql("update shebeitable set value10=\"" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\" where shebeiID=\"" + connect_device_id + "\""); // 写入历史库 Mysql.Ex_Sql("insert into history_save values(\"" + connect_device_id + "\",(select canshutypeid from canshutable where canshutype=\"" + connect_value_name + "\"),\"" + reslut_value + "\",\"" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\")"); } catch { } // 写入datagridview try { //for(int i=0;i<dataGridView1.RowCount;i++) //{ // string id = dataGridView1[0, i].Value.ToString(); // if(id==connect_device_id) // { // dataGridView1[connect_value_num + 4, i].Value = reslut_value; // break; // } //} } catch { } //connect_value_num++; //if (connect_value_num >= 9) //{ // connect_value_num = 0; // device_num++; // send_is = false; // time_count = 0; //} } }