Exemplo n.º 1
0
        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;

                //}
            }
        }
Exemplo n.º 2
0
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (dataGridView1.RowCount == 0)
            {
                return;
            }
            if (dataGridView1[0, 0].Value == null)
            {
                return;
            }
            if (dataGridView1[0, 0].Value.ToString() == "")
            {
                return;
            }

            if (send_is == true)
            {
                time_count++;

                if (time_count >= time_out)
                {
                    // 超时
                    send_is    = false;
                    time_count = 0;
                    connect_value_num++;


                    if (connect_value_num >= 9)
                    {
                        connect_value_num = 0;
                        device_num++;

                        if (device_num >= dataGridView1.RowCount)
                        {
                            device_num        = 0;
                            connect_value_num = 0;
                        }
                    }



                    return;
                }
            }



            if (isscaning == true && send_is == false)
            {
                send_is = true;


                string device_address = dataGridView1[3, device_num].Value.ToString();   // 设备地址
                string device_id      = dataGridView1[0, device_num].Value.ToString();   // 设备ID
                string device_type    = dataGridView1[2, device_num].Value.ToString();   // 设备类型


                ArrayList canshuzhongleilist = Mysql.Get_Sql_Select_Return("SELECT (select canshutype from saigedatabase.canshutable where canshutypeid=canshu1zhongleiID),(select canshutype from saigedatabase.canshutable where canshutypeid=canshu2zhongleiID),(select canshutype from saigedatabase.canshutable where canshutypeid=canshu3zhongleiID),(select canshutype from saigedatabase.canshutable where canshutypeid=canshu4zhongleiID),(select canshutype from saigedatabase.canshutable where canshutypeid=canshu5zhongleiID),(select canshutype from saigedatabase.canshutable where canshutypeid=canshu6zhongleiID) ,(select canshutype from saigedatabase.canshutable where canshutypeid=canshu7zhongleiID),(select canshutype from saigedatabase.canshutable where canshutypeid=canshu8zhongleiID),(select canshutype from saigedatabase.canshutable where canshutypeid=canshu9zhongleiID)  FROM saigedatabase.shebeizhongleitable where shebeizhongleitable.shebeizhongleiname=\"" + device_type + "\"");
                //ArrayList canshuzhongleilist = Mysql.Get_Sql_Select_Return("SELECT (select canshutype from saigedatabase.canshutable where canshutypeid=canshu1zhongleiID),(select canshutype from saigedatabase.canshutable where canshutypeid=canshu2zhongleiID),(select canshutype from saigedatabase.canshutable where canshutypeid=canshu3zhongleiID),(select canshutype from saigedatabase.canshutable where canshutypeid=canshu4zhongleiID),(select canshutype from saigedatabase.canshutable where canshutypeid=canshu5zhongleiID),(select canshutype from saigedatabase.canshutable where canshutypeid=canshu6zhongleiID) ,(select canshutype from saigedatabase.canshutable where canshutypeid=canshu7zhongleiID),(select canshutype from saigedatabase.canshutable where canshutypeid=canshu8zhongleiID),(select canshutype from saigedatabase.canshutable where canshutypeid=canshu9zhongleiID)  FROM saigedatabase.shebeizhongleitable");

                if (canshuzhongleilist.Count == 0)
                {
                    return;
                }

                ArrayList list = (ArrayList)canshuzhongleilist[0];



                time_count = 0;

                connect_device_address = device_address;
                connect_device_id      = device_id;
                connect_value_name     = list[connect_value_num].ToString();


                Send_Device_Message(device_address, device_id, device_type, connect_value_num, list[connect_value_num].ToString());
            }
        }