Beispiel #1
0
 public void UartRxProc(CP1616Packet rxPacket)
 {
     Debug.WriteLine(WFNetLib.StringFunc.StringsFunction.byteToHexStr(rxPacket.Data, " "));
     if (rxPacket.Header.Command != waitCommand)
     {
         return;
     }
     WaitSometingForm.bGenerateSometing = true;
     if (rxPacket.Data[0] == 1)//设定成功
     {
         this.Invoke((EventHandler)(delegate
         {
             MessageBox.Show(this, "设定成功:" + rxPacket.Data[1].ToString() + "->" + rxPacket.Data[2].ToString());
         }));
     }
     else if (rxPacket.Data[0] == 2)
     {
         this.Invoke((EventHandler)(delegate
         {
             MessageBox.Show(this, "设定失败,没有无线节点响应");
         }));
     }
     else if (rxPacket.Data[0] == 3)//设定成功
     {
         this.Invoke((EventHandler)(delegate
         {
             MessageBox.Show(this, "网络已经启动,请先利用“网络采集AD值”停止网络后再进行此操作");
         }));
     }
 }
Beispiel #2
0
        private void Uart_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int  irx;
            byte rx;

            while (true)
            {
                try
                {
                    irx = Uart.ReadByte();
                }
                catch
                {
                    return;
                }
                if (!Uart.IsOpen)
                {
                    return;
                }
                if (irx == -1)
                {
                    return;
                }
                rx = (byte)irx;
                if (RxPacket.DataPacketed(rx))
                {
                    if (uartRxProc != null)
                    {
                        uartRxProc(RxPacket);
                    }
                    RxPacket = new CP1616Packet();
                }
            }
        }
Beispiel #3
0
        bool OpenUart()
        {
            Uart.PortName = cbCom.Text;
            RxPacket      = new CP1616Packet();
            if (WFGlobal.OpenSerialPort(ref Uart) == false)
            {
                return(false);
            }
            Properties.Settings.Default.PortName = cbCom.Text;
            Properties.Settings.Default.Save();

            return(true);
        }
 private void button3_Click(object sender, EventArgs e)
 {
     byte[] data = new byte[1];
     data[0] = (byte)numericUpDown2.Value;
     WaitSometingForm.WaitSometing_Init();
     waitCommand = 7;
     CP1616Packet.CP1616ComSend(ref Uart, waitCommand, 0, data);
     if (!WaitSometingForm.WaitSometing(100, 150, "等待控制器读取节点标定系数,预计时长15s"))
     {
         waitCommand = 0;
         MessageBox.Show("读取失败!!!!!");
     }
     waitCommand = 0;
 }
Beispiel #5
0
 void SignInProc(CP1616Packet rxPacket)
 {
     this.Invoke((EventHandler)(delegate
     {
         sensorSingIn.clearSignIn();
         listView1.Items.Clear();
         textBox1.AppendText(DateTime.Now.ToString("HH:mm:ss:ffff") + ":收到节点名单\r\n");//WFNetLib.StringFunc.StringsFunction.byteToHexStr(RxPacket.Data, " ")\
         for (int k = 0; k < 4; k++)
         {
             if (rxPacket.Data[k] != 0)
             {
                 sensorSingIn.H[k] = true;
                 sensorBAT.H[k] = rxPacket.Data[104 + k];
                 ListViewItem li = listView1.Items.Add((k + 1).ToString());
                 li.SubItems.Add("0");
                 li.SubItems.Add("0");
                 li.SubItems.Add(Calc.GetBAT(sensorBAT.H[k]));
             }
         }
         for (int k = 0; k < 6; k++)
         {
             if (rxPacket.Data[4 + k] != 0)
             {
                 sensorSingIn.M[k] = true;
                 sensorBAT.M[k] = rxPacket.Data[104 + 4 + k];
                 ListViewItem li = listView1.Items.Add((k + 4 + 1).ToString());
                 li.SubItems.Add("0");
                 li.SubItems.Add("0");
                 li.SubItems.Add(Calc.GetBAT(sensorBAT.M[k]));
             }
         }
         for (int k = 0; k < 90; k++)
         {
             if (rxPacket.Data[12 + k] != 0)
             {
                 sensorSingIn.L[k] = true;
                 sensorBAT.L[k] = rxPacket.Data[104 + 12 + k];
                 ListViewItem li = listView1.Items.Add((k + 10 + 1).ToString());
                 li.SubItems.Add("0");
                 li.SubItems.Add("0");
                 li.SubItems.Add(Calc.GetBAT(sensorBAT.L[k]));
             }
         }
     }));
 }
Beispiel #6
0
        private void button1_Click(object sender, EventArgs e)
        {
            CP1616Packet Rx1616 = new CP1616Packet(100, 1);
            bool         bOK    = false;

            for (int i = 0; i < 3; i++)
            {
                try
                {
                    byte[] deviceID = new byte[2];
                    deviceID[0] = BytesOP.GetHighByte((ushort)numericUpDown1.Value);
                    deviceID[1] = BytesOP.GetLowByte((ushort)numericUpDown1.Value);
                    byte[] tx = CP1616Packet.MakeCP1616Packet(100, 1, deviceID);
                    try
                    {
                        COMPort1.Write(tx, 0, tx.Length);
                    }
                    catch { continue; }
                    while (true)
                    {
                        try
                        {
                            if (Rx1616.DataPacketed((byte)COMPort1.ReadByte()))
                            {
                                MessageBox.Show("设定成功");
                                bOK = true;
                                return;
                            }
                        }
                        catch
                        { break; }
                    }
                    if (bOK)
                    {
                        break;
                    }
                    Thread.Sleep(100);
                }
                catch
                {
                    break;
                }
            }
            MessageBox.Show("设定失败");
        }
Beispiel #7
0
 private void button1_Click(object sender, EventArgs e)
 {
     byte[] data = new byte[2];
     data[1] = (byte)numericUpDown1.Value;
     data[0] = (byte)numericUpDown2.Value;
     if (numericUpDown2.Value == 0)
     {
         data[0] = 0xfe;//广播地址
     }
     WaitSometingForm.WaitSometing_Init();
     waitCommand = 4;
     CP1616Packet.CP1616ComSend(ref Uart, waitCommand, 0, data);
     if (!WaitSometingForm.WaitSometing(100, 150, "等待控制器设定节点ID,预计时长15s"))
     {
         waitCommand = 0;
         MessageBox.Show("设定失败!!!!!");
     }
     waitCommand = 0;
 }
Beispiel #8
0
        private void button1_Click(object sender, EventArgs e)
        {
            CP1616Packet setID = CP1616Packet.CP1616ComProc(ref Uart, 4, 0, BytesOP.MakeShort((byte)numericUpDown1.Value, 0));

            if (setID != null)
            {
                if (setID.Data[0] == 1)//设定成功
                {
                    MessageBox.Show("设定成功:" + setID.Data[1].ToString() + "->" + setID.Data[2].ToString());
                }
                else if (setID.Data[0] == 2)
                {
                    MessageBox.Show("设定失败,没有无线节点响应");
                }
                else if (setID.Data[0] == 3)//设定成功
                {
                    MessageBox.Show("网络已经启动,请先利用“启动网络”功能接入网络,而后停止网络后再设定节点ID");
                }
            }
        }
Beispiel #9
0
        void HandWork()
        {
            CP1616Packet cp1616Packet = CP1616Packet.CP1616ComProc(ref serialPort1, 0x01, 0x01, null, 3);

            if (cp1616Packet != null)
            {
                RxCount++;
                TxCount++;
            }
            else
            {
                TxCount++;
                WFNetLib.Log.TextLog.AddTextLog(TxCount.ToString(), true);
            }
            this.Invoke((EventHandler)(delegate
            {
                label3.Text = TxCount.ToString();
                label4.Text = RxCount.ToString();
            }));
        }
        private void button1_Click(object sender, EventArgs e)
        {
            byte[] data = new byte[1 + 12];
            float  k, b, A;

            if (float.TryParse(textBox1.Text, out k) == false)
            {
                MessageBox.Show("节点斜率k输入有误");
                return;
            }
            if (float.TryParse(textBox2.Text, out b) == false)
            {
                MessageBox.Show("节点截距b输入有误");
                return;
            }
            if (float.TryParse(textBox3.Text, out A) == false)
            {
                MessageBox.Show("节点增益A输入有误");
                return;
            }
            data[0] = (byte)numericUpDown2.Value;
            byte[] kbyte = BitConverter.GetBytes(k);
            byte[] bbyte = BitConverter.GetBytes(b);
            byte[] Abyte = BitConverter.GetBytes(A);
            for (int i = 0; i < 4; i++)
            {
                data[1 + i] = kbyte[i];
                data[5 + i] = bbyte[i];
                data[9 + i] = Abyte[i];
            }
            WaitSometingForm.WaitSometing_Init();
            waitCommand = 6;
            CP1616Packet.CP1616ComSend(ref Uart, waitCommand, 0, data);
            if (!WaitSometingForm.WaitSometing(100, 150, "等待控制器设定节点标定系数,预计时长15s"))
            {
                waitCommand = 0;
                MessageBox.Show("设定失败!!!!!");
            }
            waitCommand = 0;
        }
Beispiel #11
0
 private void btDisconnect_Click(object sender, EventArgs e)
 {
     serialPort1.ReadTimeout = 500;
     Rx1616 = new CP1616Packet(2, 1);
     byte[] tx = CP1616Packet.MakeCP1616Packet(2, 1, 1);
     try
     {
         serialPort1.Write(tx, 0, tx.Length);
     }
     catch
     {
         PortName           = "";
         progressBar1.Value = progressBar1.Maximum;
         serialPort1.Close();
         bConnect.BackColor = Color.Red;
         lConnect.Text      = "已断开";
         MessageBox.Show("成功断开连接!!!");
     }
     while (true)
     {
         try
         {
             if (Rx1616.DataPacketed((byte)serialPort1.ReadByte()))
             {
                 PortName           = "";
                 progressBar1.Value = progressBar1.Maximum;
                 bConnect.BackColor = Color.Red;
                 lConnect.Text      = "已断开";
                 MessageBox.Show("成功与主机断开连接!!!");
                 break;
             }
         }
         catch
         { break; }
     }
     serialPort1.Close();
     btDisconnect.Enabled = false;
     btScan.Enabled       = false;
     btConnect.Enabled    = true;
 }
Beispiel #12
0
 private void btNetStart_Click(object sender, EventArgs e)
 {
     if (btNetStart.Text == "启动网络")
     {
         Uart.PortName = cbCom.Text;
         RxPacket      = new CP1616Packet();
         if (WFGlobal.OpenSerialPort(ref Uart) == false)
         {
             return;
         }
         Properties.Settings.Default.PortName = cbCom.Text;
         Properties.Settings.Default.Save();
         sensorSingIn.clearSignIn();
         listView1.Items.Clear();
         chart1.Series[0].Points.Clear();
         chart2.Series[0].Points.Clear();
         chart3.Series[0].Points.Clear();
         chart4.Series[0].Points.Clear();
         chart3.Series[1].Points.Clear();
         chart4.Series[1].Points.Clear();
         chart3.Series[2].Points.Clear();
         chart4.Series[2].Points.Clear();
         chart3.Series[3].Points.Clear();
         chart4.Series[3].Points.Clear();
         bStopNet = false;
         for (int i = 0; i < 3; i++)
         {
             startResult = 0;
             waitCommand = 1;
             CP1616Packet.CP1616ComSend(ref Uart, waitCommand, 0, (ushort)0);
             int x = 0;
             while (true)
             {
                 Thread.Sleep(1);
                 if (startResult != 0)
                 {
                     break;
                 }
                 x++;
                 if (x > 1000)
                 {
                     startResult = 0xff;
                     break;
                 }
             }
             if (startResult == 0xff)//超时了
             {
             }
             else
             {
                 break;
             }
         }
         if (startResult == 0xff)
         {
             MessageBox.Show("网络控制器未做应答!!!");
             Uart.Close();
             btNetStart.Text = "启动网络";
             return;
         }
         else if (startResult == 2)
         {
             bStopNet        = false;
             btNetStart.Text = "停止网络";
             MessageBox.Show("网络已经启动,等待接入");
         }
         else if (startResult == 3)
         {
             bStopNet        = false;
             btNetStart.Text = "停止网络";
             MessageBox.Show("标定网络已经启动,将自动转为测量网络,等待接入");
         }
         else
         {
             WaitSometingForm.WaitSometing_Init();
             waitCommand = 0x10;//点名完成
             if (!WaitSometingForm.WaitSometing(10, 3000, "等待无线节点组网,预计时长25s"))
             {
                 waitCommand = 0;
                 Uart.Close();
                 MessageBox.Show("组网失败");
                 return;
             }
             if (listView1.Items.Count != 0)
             {
                 WaitSometingForm.WaitSometing_Init();
                 waitCommand = 0x20;//等待测量数据
                 if (!WaitSometingForm.WaitSometing(10, 500, "等待网络数据,预计时长3s"))
                 {
                     waitCommand = 0;
                     MessageBox.Show("组网失败");
                     Uart.Close();
                     bStopNet = false;
                     return;
                 }
                 bStopNet        = false;
                 btNetStart.Text = "停止网络";
                 MessageBox.Show("网络启动成功");
             }
             else
             {
                 waitCommand = 0;
                 MessageBox.Show("当前网络没有发现任何无线节点,网络自动停止");
                 Uart.Close();
                 btNetStart.Text = "启动网络";
                 return;
             }
         }
     }
     else
     {
         bStopNet = true;
         WaitSometingForm.WaitSometing_Init();
         waitCommand = 0x02;//等待网络停止
         if (!WaitSometingForm.WaitSometing(10, 500, "等待网络停止,预计时长5s"))
         {
             waitCommand = 0;
             MessageBox.Show("网络停止失败");
             return;
         }
         MessageBox.Show("网络已停止");
         Uart.Close();
         btNetStart.Text = "启动网络";
     }
 }
Beispiel #13
0
        void Uart_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int  irx;
            byte rx;

            while (true)
            {
                try
                {
                    irx = Uart.ReadByte();
                }
                catch
                {
                    return;
                }
                if (!Uart.IsOpen)
                {
                    return;
                }
                if (irx == -1)
                {
                    return;
                }
                rx = (byte)irx;
                if (RxPacket.DataPacketed(rx))
                {
                    byte[] data = new byte[2];
                    data[0] = RxPacket.Header.Command;
                    if (waitCommand == RxPacket.Header.Command)
                    {
                        WaitSometingForm.bGenerateSometing = true;
                    }
                    if (bStopNet)
                    {
                        data[1] = 0x02;//要求关闭网络
                        CP1616Packet.CP1616ComSend(ref Uart, 3, 0, data);
                    }
                    else if (RxPacket.Header.Command == 1)//标定网络启动确认
                    {
                        startResult = RxPacket.Data[0];
                    }
                    else if (RxPacket.Header.Command == 0x22)//标定网络,请求转换为测量网络
                    {
                        this.Invoke((EventHandler)(delegate
                        {
                            textBox1.AppendText(DateTime.Now.ToString("HH:mm:ss:ffff") + ":标定网络转换为测量网络\r\n");
                        }));
                        startResult = 3;
                        data[1]     = 0x03;
                        CP1616Packet.CP1616ComSend(ref Uart, 3, 0, data);
                    }
                    else if (RxPacket.Header.Command == 0x10)
                    {
                        data[1] = 0x00;
                        CP1616Packet.CP1616ComSend(ref Uart, 3, 0, data);
                        SignInProc();
                    }
                    else if (RxPacket.Header.Command == 0x20)
                    {
                        startResult = 2;
                        if (listView1.Items.Count == 0)
                        {
                            data[1] = 0x01;//请求传感器名单
                            CP1616Packet.CP1616ComSend(ref Uart, 3, 0, data);
                        }
                        else
                        {
                            data[1] = 0x00;
                            CP1616Packet.CP1616ComSend(ref Uart, 3, 0, data);
                            ADCDataProc();
                        }
                        this.Invoke((EventHandler)(delegate
                        {
                            if (btNetStart.Text == "启动网络")
                            {
                                btNetStart.Text = "停止网络";
                            }
                        }));
                    }
                    RxPacket = new CP1616Packet();
                }
            }
        }
Beispiel #14
0
        private void button2_Click(object sender, EventArgs e)
        {
            string session = JsonWork.login();

            if (session == "")
            {
                return;
            }
            WFHttpWebResponse            web = new WFHttpWebResponse();
            IDictionary <string, string> p   = new Dictionary <string, string>();

            p.Add("session", session);
            p.Add("device_id", DeviceID.ToString());
            HttpWebResponse    hr       = web.CreatePostHttpResponse(JsonWork.strUrl + "get_device_list", p);
            JsonAddrListReturn AddrList = JsonWork.GetJsonObject <JsonAddrListReturn>(web.Content);

            if (AddrList.code != "200")
            {
                MessageBox.Show("获取数据失败:" + AddrList.message);
                return;
            }
            enBT(false);
//             ExcelImport el = new ExcelImport(true, "安装位置");
//             DataTable dt = el.ExcelImportProc_OleDB();
//             if (dt == null || dt.Rows.Count < 100 || dt.Columns.Count < 2)
//             {
//                 MessageBox.Show("地址数据文件格式有问题,请检查!");
//                 return;
//             }

            byte[] tx;
            byte[] temp;
            byte[] temp1;
            progressBar1.Maximum = AddrList.obj.list.Count;
            progressBar1.Value   = 0;
            byte id;

            for (int i = 0; i < AddrList.obj.list.Count; i++)
            {
                Debug.WriteLine(AddrList.obj.list[i].id + "," + AddrList.obj.list[i].name);
                temp = Encoding.Default.GetBytes(AddrList.obj.list[i].name);
                if (!byte.TryParse(AddrList.obj.list[i].id, out id))
                {
                    continue;
                }
                if (id == 0)
                {
                    continue;
                }
                id--;
                if (temp.Length > 20)
                {
                    temp1     = new byte[23];
                    temp1[0]  = id;
                    temp1[1]  = 21;
                    temp1[22] = 0;
                    for (int j = 0; j < 20; j++)
                    {
                        temp1[j + 2] = temp[j];
                    }
                    tx = CP1616Packet.MakeCP1616Packet(3, 1, temp1);
                }
                else
                {
                    temp1    = new byte[3 + temp.Length];
                    temp1[0] = id;
                    temp1[1] = (byte)(temp.Length + 1);
                    temp1[2 + temp.Length] = 0;
                    for (int j = 0; j < temp.Length; j++)
                    {
                        temp1[j + 2] = temp[j];
                    }
                    tx = CP1616Packet.MakeCP1616Packet(3, 1, temp1);
                }
                int retry = 20;
                Rx1616 = new CP1616Packet(3, 1);
                bool bOK = false;
                while (retry != 0)
                {
                    WaitMS(100);
                    try
                    {
                        COMPort1.Write(tx, 0, tx.Length);
                    }
                    catch { retry--; continue; }
                    while (true)
                    {
                        try
                        {
                            if (Rx1616.DataPacketed((byte)COMPort1.ReadByte()))
                            {
                                bOK = true;
                                break;
                            }
                        }
                        catch
                        { break; }
                    }
                    if (bOK)
                    {
                        progressBar1.Value++;
                        break;
                    }
                    retry--;
                }
                if (retry == 0)
                {
                    MessageBox.Show("第" + (i + 1).ToString() + "个传感器地址设置出错!");
                    break;
                }
            }
            MessageBox.Show("地址数据导入完成!");
            enBT(true);
        }
Beispiel #15
0
        private void btConnect_Click(object sender, EventArgs e)
        {
            serialPort1.ReadTimeout = 500;
            bool bOK = false;

            string[] ports = SerialPort.GetPortNames();
            Rx1616               = new CP1616Packet(1, 1);
            PortName             = "";
            progressBar1.Value   = 0;
            progressBar1.Maximum = ports.Length * 3;
            foreach (string com in ports)
            {
                serialPort1.PortName = com;
                for (int i = 0; i < 3; i++)
                {
                    progressBar1.PerformStep();
                    try
                    {
                        serialPort1.Open();
                        byte[] tx = CP1616Packet.MakeCP1616Packet(1, 1, null);
                        try
                        {
                            serialPort1.Write(tx, 0, tx.Length);
                        }
                        catch { continue; }
                        while (true)
                        {
                            try
                            {
                                if (Rx1616.DataPacketed((byte)serialPort1.ReadByte()))
                                {
                                    if (Rx1616.Data[0] == 0x01)//成功
                                    {
                                        PortName           = com;
                                        progressBar1.Value = progressBar1.Maximum;
                                        bOK = true;
                                        bConnect.BackColor = Color.Green;
                                        lConnect.Text      = "已连接";
                                        MessageBox.Show("成功连接到主机!!!");
                                        btDisconnect.Enabled = true;
                                        btScan.Enabled       = true;
                                        btConnect.Enabled    = false;
                                        break;
                                    }
                                    else
                                    {
                                        serialPort1.Close();
                                        if (Rx1616.Data[0] == 0x10)
                                        {
                                            MessageBox.Show("主机设备自动模式无法与计算机进行连接!");
                                        }
                                        else
                                        {
                                            MessageBox.Show("主机设备当前无法与计算机进行连接!" + Rx1616.Data[0].ToString("x02"));
                                        }
                                        return;
                                    }
                                }
                            }
                            catch
                            { break; }
                        }
                        if (PortName != "")
                        {
                            break;
                        }
                        serialPort1.Close();
                        Thread.Sleep(100);
                    }
                    catch
                    {
                        break;
                    }
                }
                if (PortName != "")
                {
                    break;
                }
            }
            if (PortName == "")
            {
                progressBar1.Value = progressBar1.Maximum;
                MessageBox.Show("未找到设备");
            }
        }
Beispiel #16
0
        void PickOne()
        {
            serialPort1.ReadTimeout = 2000;
            int retry = 3;

            Rx1616 = new CP1616Packet(3, 1);
            byte[] bv = new byte[2];
            bv[0] = BytesOP.GetHighByte((ushort)daV);
            bv[1] = BytesOP.GetLowByte((ushort)daV);
            byte[] tx = CP1616Packet.MakeCP1616Packet(3, 1, bv);
            while (retry != 0)
            {
                try
                {
                    serialPort1.Write(tx, 0, tx.Length);
                }
                catch
                {
                    PortName = "";
                    serialPort1.Close();
                    PickFinish();
                    MessageBox.Show("发送错误,与主机断开连接");
                    btDisconnect.Enabled = false;
                    btConnect.Enabled    = true;
                    return;
                }
                while (true)
                {
                    try
                    {
                        if (Rx1616.DataPacketed((byte)serialPort1.ReadByte()))
                        {
                            if (Rx1616.Data[0] == 0x01)
                            {
                                chart1.Series[0].Points.AddXY(daV * 0.08, BytesOP.MakeShort(Rx1616.Data[1], Rx1616.Data[2]));
                                lX.Text = (daV * 0.08).ToString("f01");
                                lY.Text = BytesOP.MakeShort(Rx1616.Data[1], Rx1616.Data[2]).ToString();
                                progressBar1.PerformStep();
                                if (daV >= 1023)
                                {
                                    tScan.Enabled = false;
                                    MessageBox.Show("采集完成");
                                    PickFinish();
                                }
                                else
                                {
                                    tScan.Enabled = true;
                                    daV          += stepV;
                                    if (daV >= 1023)
                                    {
                                        daV = 1023;
                                    }
                                }
                            }
                            else
                            {
                                PortName = "";
                                serialPort1.Close();
                                PickFinish();
                                btDisconnect.Enabled = false;
                                btConnect.Enabled    = true;
                                bConnect.BackColor   = Color.Red;
                                lConnect.Text        = "已断开";
                                MessageBox.Show("发送错误,与主机断开连接");
                            }
                            return;
                        }
                    }
                    catch
                    { break; }
                }
                retry--;
            }
            if (retry == 0)//重试失败
            {
                PortName = "";
                serialPort1.Close();
                PickFinish();
                btDisconnect.Enabled = false;
                btConnect.Enabled    = true;
                bConnect.BackColor   = Color.Red;
                lConnect.Text        = "已断开";
                MessageBox.Show("发送错误,与主机断开连接");
            }
        }
Beispiel #17
0
 private void btNetStart_Click(object sender, EventArgs e)
 {
     if (btNetStart.Text == "启动网络")
     {
         sensorSingIn.clearSignIn();
         listView1.Items.Clear();
         for (int i = 0; i < 3; i++)
         {
             startResult = 0;
             waitCommand = 5;
             //timer1.Enabled = true;
             CP1616Packet.CP1616ComSend(ref Uart, waitCommand, 0, (ushort)0);
             int x = 0;
             while (true)
             {
                 Thread.Sleep(1);
                 if (startResult != 0)
                 {
                     break;
                 }
                 x++;
                 if (x > 500)
                 {
                     startResult = 0xff;
                     break;
                 }
             }
             if (startResult == 0xff)//超时了
             {
             }
             else
             {
                 break;
             }
         }
         if (startResult == 0xff)
         {
             MessageBox.Show("网络控制器未做应答!!!");
             return;
         }
         else if (startResult == 2)
         {
             waitCommand = 0x10;
             MessageBox.Show("网络已经启动,等待接入");
         }
         else if (startResult == 3)
         {
             waitCommand = 0x10;
             MessageBox.Show("测量网络已经启动,将自动转为标定网络,等待接入");
         }
         else
         {
             WaitSometingForm.WaitSometing_Init();
             waitCommand = 0x10;//点名完成
             if (!WaitSometingForm.WaitSometing(10, 3000, "等待无线节点组网,预计时长25s"))
             {
                 waitCommand = 0;
                 MessageBox.Show("组网失败");
                 return;
             }
             if (listView1.Items.Count != 0)
             {
                 WaitSometingForm.WaitSometing_Init();
                 waitCommand = 0x22;//等待标定数据
                 if (!WaitSometingForm.WaitSometing(10, 500, "等待网络数据,预计时长3s"))
                 {
                     waitCommand = 0;
                     MessageBox.Show("组网失败");
                     bStopNet = false;
                     return;
                 }
                 bStopNet        = false;
                 btNetStart.Text = "停止网络";
                 MessageBox.Show("网络启动成功");
             }
             else
             {
                 waitCommand = 0;
                 MessageBox.Show("当前网络没有发现任何无线节点,网络自动停止");
                 return;
             }
         }
         bStopNet = false;
     }
     else
     {
         bStopNet = true;
         WaitSometingForm.WaitSometing_Init();
         waitCommand = 0x02;//等待网络停止
         if (!WaitSometingForm.WaitSometing(10, 500, "等待网络停止,预计时长5s"))
         {
             waitCommand = 0;
             MessageBox.Show("网络停止失败");
             return;
         }
         MessageBox.Show("网络已停止");
         btNetStart.Text = "启动网络";
     }
 }
Beispiel #18
0
        private void button2_Click(object sender, EventArgs e)
        {
            ExcelImport el = new ExcelImport(true, "安装位置");
            DataTable   dt = el.ExcelImportProc_OleDB();

            if (dt == null || dt.Rows.Count < 100 || dt.Columns.Count < 2)
            {
                MessageBox.Show("地址数据文件格式有问题,请检查!");
                return;
            }
            enBT(false);
            byte[] tx;
            byte[] temp;
            byte[] temp1;
            progressBar1.Value = 0;
            for (int i = 0; i < 100; i++)
            {
                temp = Encoding.Default.GetBytes(dt.Rows[i][1].ToString());
                Debug.WriteLine(dt.Rows[i][1].ToString());
                if (temp.Length > 20)
                {
                    temp1     = new byte[23];
                    temp1[0]  = (byte)(i);
                    temp1[1]  = 21;
                    temp1[22] = 0;
                    for (int j = 0; j < 20; j++)
                    {
                        temp1[j + 2] = temp[j];
                    }
                    tx = CP1616Packet.MakeCP1616Packet(3, 1, temp1);
                }
                else
                {
                    temp1    = new byte[3 + temp.Length];
                    temp1[0] = (byte)(i);
                    temp1[1] = (byte)(temp.Length + 1);
                    temp1[2 + temp.Length] = 0;
                    for (int j = 0; j < temp.Length; j++)
                    {
                        temp1[j + 2] = temp[j];
                    }
                    tx = CP1616Packet.MakeCP1616Packet(3, 1, temp1);
                }
                int retry = 20;
                Rx1616 = new CP1616Packet(3, 1);
                bool bOK = false;
                while (retry != 0)
                {
                    WaitMS(100);
                    try
                    {
                        COMPort1.Write(tx, 0, tx.Length);
                    }
                    catch { retry--; continue; }
                    while (true)
                    {
                        try
                        {
                            if (Rx1616.DataPacketed((byte)COMPort1.ReadByte()))
                            {
                                bOK = true;
                                break;
                            }
                        }
                        catch
                        { break; }
                    }
                    if (bOK)
                    {
                        progressBar1.Value++;
                        break;
                    }
                    retry--;
                }
                if (retry == 0)
                {
                    MessageBox.Show("第" + (i + 1).ToString() + "个传感器地址设置出错!");
                    break;
                }
            }
            MessageBox.Show("地址数据导入完成!");
            enBT(true);
        }
Beispiel #19
0
 //连接设备
 private void button1_Click(object sender, EventArgs e)
 {
     //bool bOK = false;
     if (button1.Text == "连接设备")//启动串口扫描,连接设备
     {
         Rx1616 = new CP1616Packet(1, 1);
         string[] ports = SerialPort.GetPortNames();
         PortName           = "";
         progressBar1.Value = 0;
         int perCount, per;
         perCount = ports.Length * 3;
         per      = 100 / perCount;
         foreach (string com in ports)
         {
             for (int i = 0; i < 3; i++)
             {
                 progressBar1.Value += per;
                 try
                 {
                     COMPort1.Close();
                     COMPort1.PortName = com;
                     COMPort1.Open();
                     byte[] tx = CP1616Packet.MakeCP1616Packet(1, 1, 1);
                     try
                     {
                         COMPort1.Write(tx, 0, tx.Length);
                     }
                     catch { continue; }
                     while (true)
                     {
                         try
                         {
                             if (Rx1616.DataPacketed((byte)COMPort1.ReadByte()))
                             {
                                 DeviceID           = BytesOP.MakeShort(Rx1616.Data[0], Rx1616.Data[1]);
                                 label3.Text        = "设备ID:" + DeviceID.ToString();
                                 label2.Text        = com;//.Show(com);
                                 PortName           = com;
                                 progressBar1.Value = 100;
                                 button1.Text       = "断开连接";
                                 enBT(true);
                                 //bOK = true;
                                 break;
                             }
                         }
                         catch
                         { break; }
                     }
                     if (PortName != "")
                     {
                         break;
                     }
                     COMPort1.Close();
                     Thread.Sleep(100);
                 }
                 catch
                 {
                     break;
                 }
             }
             if (PortName != "")
             {
                 break;
             }
         }
         if (PortName == "")
         {
             progressBar1.Value = 100;
             MessageBox.Show("未找到设备");
         }
     }
     else//已经连接,则断开
     {
         Rx1616 = new CP1616Packet(2, 1);
         byte[] tx = CP1616Packet.MakeCP1616Packet(2, 1, 1);
         try
         {
             COMPort1.Write(tx, 0, tx.Length);
         }
         catch
         {
             PortName           = "";
             progressBar1.Value = 100;
             button1.Text       = "连接设备";
             label3.Text        = "";
             enBT(false);
             button1.Enabled = true;
             COMPort1.Close();
         }
         while (true)
         {
             try
             {
                 if (Rx1616.DataPacketed((byte)COMPort1.ReadByte()))
                 {
                     PortName           = "";
                     progressBar1.Value = 100;
                     button1.Text       = "连接设备";
                     enBT(false);
                     button1.Enabled = true;
                     label3.Text     = "";
                     break;
                 }
             }
             catch
             { break; }
         }
         COMPort1.Close();
     }
 }
Beispiel #20
0
        private void button1_Click(object sender, EventArgs e)
        {
            Rx1616 = new CP1616Packet(100, 1);
            string[] ports = SerialPort.GetPortNames();
            PortName           = "";
            progressBar1.Value = 0;
            int perCount, per;

            perCount = ports.Length * 3;
            per      = 100 / perCount;
            bool bOK = false;

            foreach (string com in ports)
            {
                COMPort1.PortName = com;
                for (int i = 0; i < 3; i++)
                {
                    progressBar1.Value += per;
                    try
                    {
                        COMPort1.Open();
                        byte[] deviceID = new byte[2];
                        deviceID[0] = BytesOP.GetHighByte((ushort)numericUpDown1.Value);
                        deviceID[1] = BytesOP.GetLowByte((ushort)numericUpDown1.Value);
                        byte[] tx = CP1616Packet.MakeCP1616Packet(100, 1, deviceID);
                        try
                        {
                            COMPort1.Write(tx, 0, tx.Length);
                        }
                        catch { continue; }
                        while (true)
                        {
                            try
                            {
                                if (Rx1616.DataPacketed((byte)COMPort1.ReadByte()))
                                {
                                    MessageBox.Show("设定成功");
                                    bOK = true;
                                    break;
                                }
                            }
                            catch
                            { break; }
                        }
                        if (bOK)
                        {
                            break;
                        }
                        COMPort1.Close();
                        Thread.Sleep(100);
                    }
                    catch
                    {
                        break;
                    }
                }
                if (bOK)
                {
                    break;
                }
            }
            if (!bOK)
            {
                progressBar1.Value = 100;
                MessageBox.Show("未找到设备");
            }
            COMPort1.Close();
        }
Beispiel #21
0
        void ADCDataProc(CP1616Packet rxPacket)
        {
            int liIndex   = 0;
            int dataIndex = 0;

            this.Invoke((EventHandler)(delegate
            {
                textBox1.AppendText(DateTime.Now.ToString("HH:mm:ss:ffff") + ":收到传感器数据\r\n");//WFNetLib.StringFunc.StringsFunction.byteToHexStr(RxPacket.Data, " ")\
                Debug.WriteLine(WFNetLib.StringFunc.StringsFunction.byteToHexStr(rxPacket.Data, " "));
                for (int k = 0; k < 4; k++)
                {
                    if (sensorSingIn.H[k])
                    {
                        sensorBAT.H[k] = rxPacket.Data[dataIndex + 3];
                        ListViewItem li = listView1.Items[liIndex];
                        li.SubItems[3].Text = Calc.GetBAT(sensorBAT.H[k]);
                        ushort ad1, ad2 = 0;
                        byte[] adad = new byte[3];
                        adad[0] = rxPacket.Data[dataIndex + 0];
                        adad[1] = rxPacket.Data[dataIndex + 1];
                        adad[2] = rxPacket.Data[dataIndex + 2];
                        ad1 = BytesOP.MakeShort(BytesOP.GetLowNibble(adad[1]), adad[0]);
                        ad2 = BytesOP.MakeShort(BytesOP.GetHighNibble(adad[1]), adad[2]);

                        li.SubItems[1].Text = ad1.ToString("");
                        li.SubItems[2].Text = ad2.ToString("");
                        liIndex++;
                        dataIndex = dataIndex + 4;
                        //Debug.WriteLine(debug.ToString());
                    }
                }
                for (int k = 0; k < 6; k++)
                {
                    if (sensorSingIn.M[k])
                    {
                        sensorBAT.M[k] = rxPacket.Data[dataIndex + 3];
                        ListViewItem li = listView1.Items[liIndex];
                        li.SubItems[3].Text = Calc.GetBAT(sensorBAT.M[k]);
                        ushort ad1, ad2 = 0;
                        byte[] adad = new byte[3];
                        adad[0] = rxPacket.Data[dataIndex + 0];
                        adad[1] = rxPacket.Data[dataIndex + 1];
                        adad[2] = rxPacket.Data[dataIndex + 2];
                        ad1 = BytesOP.MakeShort(BytesOP.GetLowNibble(adad[1]), adad[0]);
                        ad2 = BytesOP.MakeShort(BytesOP.GetHighNibble(adad[1]), adad[2]);

                        li.SubItems[1].Text = ad1.ToString("");
                        li.SubItems[2].Text = ad2.ToString("");
                        liIndex++;
                        dataIndex = dataIndex + 4;
                    }
                }
                for (int k = 0; k < 90; k++)
                {
                    if (sensorSingIn.L[k])
                    {
                        sensorBAT.L[k] = rxPacket.Data[dataIndex + 3];
                        ListViewItem li = listView1.Items[liIndex];
                        li.SubItems[3].Text = Calc.GetBAT(sensorBAT.L[k]);
                        ushort ad1, ad2 = 0;
                        byte[] adad = new byte[3];
                        adad[0] = rxPacket.Data[dataIndex + 0];
                        adad[1] = rxPacket.Data[dataIndex + 1];
                        adad[2] = rxPacket.Data[dataIndex + 2];
                        ad1 = BytesOP.MakeShort(BytesOP.GetLowNibble(adad[1]), adad[0]);
                        ad2 = BytesOP.MakeShort(BytesOP.GetHighNibble(adad[1]), adad[2]);

                        li.SubItems[1].Text = ad1.ToString("");
                        li.SubItems[2].Text = ad2.ToString("");
                        liIndex++;
                        dataIndex = dataIndex + 4;
                    }
                }
            }));
        }
Beispiel #22
0
        private void button1_Click_1(object sender, EventArgs e)
        {
            if (radioButton1.Checked)//当前数据
            {
                Rx1616 = new CP1616Packet(4, 1);
                byte[] tx = CP1616Packet.MakeCP1616Packet(4, 1, 1);
                COMPort1.Write(tx, 0, tx.Length);
                while (true)
                {
                    try
                    {
                        if (Rx1616.DataPacketed((byte)COMPort1.ReadByte()))
                        {
                            string session = JsonWork.login();
                            if (session == "")
                            {
                                return;
                            }
                            JsonTempDataList jsonTempDataList = new JsonTempDataList();
                            jsonTempDataList.list = new List <JsonTempData>();
                            for (int i = 0; i < 100; i++)
                            {
                                JsonTempData jtd = new JsonTempData();
                                jtd.cid = i.ToString();
                                if (Rx1616.Data[i * 27 + 21] == 0xff)
                                {
                                    continue;
                                }
                                else
                                {
                                    jtd.date        = Rx1616.Data[i * 27 + 21].ToString("D02") + "-" + Rx1616.Data[i * 27 + 22].ToString("D02") + " " + Rx1616.Data[i * 27 + 23].ToString("D02") + ":" + Rx1616.Data[i * 27 + 24].ToString("D02");
                                    jtd.temperature = ((BytesOP.MakeShort(Rx1616.Data[i * 27 + 26], Rx1616.Data[i * 27 + 25])) * 0.0625 + 0.5).ToString("f1");
                                }
                                jsonTempDataList.list.Add(jtd);
                            }
                            WFHttpWebResponse            web = new WFHttpWebResponse();
                            IDictionary <string, string> p   = new Dictionary <string, string>();
                            p.Add("temperature_list", JsonWork.GetJsonString <JsonTempDataList>(jsonTempDataList));
                            p.Add("session", session);
                            p.Add("device_id", Form1.DeviceID.ToString());

                            HttpWebResponse    hr       = web.CreatePostHttpResponse(JsonWork.strUrl + "post_temperature_list", p);
                            JsonAddrListReturn AddrList = JsonWork.GetJsonObject <JsonAddrListReturn>(web.Content);
                            if (AddrList.code != "200")
                            {
                                MessageBox.Show("数据导出失败:" + AddrList.message);
                                return;
                            }
                            else
                            {
                                MessageBox.Show("数据导出成功");
                                return;
                            }
                            break;
                        }
                    }
                    catch
                    {
                        MessageBox.Show("数据导出失败");
                        break;
                    }
                }
            }
            else if (radioButton2.Checked)
            {
                hl = new List <byte[]>();
                se = new List <byte>();
                er = new List <byte>();
                progressBar1.Value = 0;
                int  retry;
                bool bOK;
                for (int i = 0; i < 100; i++)
                {
                    retry = 20;
                    bOK   = false;
                    while (retry != 0)
                    {
                        Rx1616 = new CP1616Packet(5, 1);
                        byte[] tx = CP1616Packet.MakeCP1616Packet(5, 1, (byte)i);
                        try
                        {
                            COMPort1.Write(tx, 0, tx.Length);
                        }
                        catch { retry--; continue; }
                        while (true)
                        {
                            try
                            {
                                if (Rx1616.DataPacketed((byte)COMPort1.ReadByte()))
                                {
                                    //                                 byte[] temp = new byte[Rx1616.Data.Length - 2];
                                    //                                 for (int j = 0; j < Rx1616.Data.Length - 2; j++)
                                    //                                 {
                                    //                                     temp[j]
                                    //                                 }
                                    hl.Add(Rx1616.Data);
                                    se.Add((byte)(i + 1));
                                    progressBar1.Value++;
                                    Thread.Sleep(100);
                                    bOK = true;
                                    break;
                                }
                            }
                            catch
                            {
                                break;
                            }
                        }
                        if (bOK)
                        {
                            break;
                        }
                        retry--;
                        Thread.Sleep(1000);
                    }
                    if (retry == 0)
                    {
                        er.Add((byte)(i + 1));
                    }
                }
                if (se.Count != 0)
                {
                    if (er.Count != 0)
                    {
                        string str = "如下传感器数据读取失败:\r\n";
                        for (int i = 0; i < er.Count; i++)
                        {
                            str += "第" + er[i].ToString() + "号传感器" + "\r\n";
                        }
                        str += "是否继续导出";
                        if (MessageBox.Show(str, "读取错误", MessageBoxButtons.YesNo) == DialogResult.No)
                        {
                            return;
                        }
                    }
                    string session = JsonWork.login();
                    if (session == "")
                    {
                        return;
                    }
                    JsonTempDataList jsonTempDataList = new JsonTempDataList();
                    jsonTempDataList.list = new List <JsonTempData>();
                    for (int i = 0; i < se.Count; i++)
                    {
                        int ix1 = 21;
                        while ((ix1 + 6) < hl[i].Length)
                        {
                            JsonTempData jtd = new JsonTempData();
                            jtd.cid  = se[i].ToString();
                            jtd.date = hl[i][ix1].ToString("D02") + "-" + hl[i][ix1 + 1].ToString("D02") + " " + hl[i][ix1 + 2].ToString("D02") + ":" + hl[i][ix1 + 3].ToString("D02");

                            ushort t = BytesOP.MakeShort(hl[i][ix1 + 5], hl[i][ix1 + 4]);
                            if (t == 0xffff)
                            {
                                jtd.temperature = "-100";
                            }
                            else
                            {
                                jtd.temperature = ((t) * 0.0625 + 0.5).ToString("f1");
                            }
                            jsonTempDataList.list.Add(jtd);
                            ix1 += 6;
                        }
                    }
                    WFHttpWebResponse            web = new WFHttpWebResponse();
                    IDictionary <string, string> p   = new Dictionary <string, string>();
                    p.Add("temperature_list", JsonWork.GetJsonString <JsonTempDataList>(jsonTempDataList));
                    p.Add("session", session);
                    p.Add("device_id", Form1.DeviceID.ToString());

                    HttpWebResponse    hr       = web.CreatePostHttpResponse(JsonWork.strUrl + "post_temperature_list", p);
                    JsonAddrListReturn AddrList = JsonWork.GetJsonObject <JsonAddrListReturn>(web.Content);
                    if (AddrList.code != "200")
                    {
                        MessageBox.Show("数据导出失败:" + AddrList.message);
                        return;
                    }
                    else
                    {
                        MessageBox.Show("数据导出成功");
                        return;
                    }
//                     ee = new ExcelExport();
//                     ee.SheetCount = se.Count;
//                     ee.ExcelWorkbookCallbackProc = new ExcelExport.ExcelWorkbookCallback(ExcelWorkbookCallbackProc1);
//                     ee.ExcelExportProc1();
                }
                else
                {
                    MessageBox.Show("读取历史数据失败");
                }
            }
            else if (radioButton3.Checked)
            {
                if (numericUpDown2.Value < numericUpDown1.Value)
                {
                    MessageBox.Show("范围有误");
                    return;
                }
                hl = new List <byte[]>();
                se = new List <byte>();
                er = new List <byte>();
                progressBar1.Value = 0;
                int per = (int)(numericUpDown2.Value - numericUpDown1.Value + 1);
                per = 100 / per;
                int  retry;
                bool bOK;
                for (int i = (int)(numericUpDown1.Value - 1); i < (int)numericUpDown2.Value; i++)
                {
                    retry = 20;
                    bOK   = false;
                    while (retry != 0)
                    {
                        Rx1616 = new CP1616Packet(5, 1);
                        byte[] tx = CP1616Packet.MakeCP1616Packet(5, 1, (byte)i);
                        try
                        {
                            COMPort1.Write(tx, 0, tx.Length);
                        }
                        catch { retry--; continue; }
                        while (true)
                        {
                            try
                            {
                                if (Rx1616.DataPacketed((byte)COMPort1.ReadByte()))
                                {
                                    //                                 byte[] temp = new byte[Rx1616.Data.Length - 2];
                                    //                                 for (int j = 0; j < Rx1616.Data.Length - 2; j++)
                                    //                                 {
                                    //                                     temp[j]
                                    //                                 }
                                    hl.Add(Rx1616.Data);
                                    se.Add((byte)(i + 1));
                                    progressBar1.Value += per;
                                    Thread.Sleep(100);
                                    bOK = true;
                                    break;
                                }
                            }
                            catch
                            {
                                break;
                            }
                        }
                        if (bOK)
                        {
                            break;
                        }
                        retry--;
                        Thread.Sleep(1000);
                    }
                    if (retry == 0)
                    {
                        er.Add((byte)(i + 1));
                    }
                }
                progressBar1.Value = 100;
                if (se.Count != 0)
                {
                    if (er.Count != 0)
                    {
                        string str = "如下传感器数据读取失败:\r\n";
                        for (int i = 0; i < er.Count; i++)
                        {
                            str += "第" + er[i].ToString() + "号传感器" + "\r\n";
                        }
                        str += "是否继续导出";
                        if (MessageBox.Show(str, "读取错误", MessageBoxButtons.YesNo) == DialogResult.No)
                        {
                            return;
                        }
                    }
                    string session = JsonWork.login();
                    if (session == "")
                    {
                        return;
                    }
                    JsonTempDataList jsonTempDataList = new JsonTempDataList();
                    jsonTempDataList.list = new List <JsonTempData>();
                    for (int i = 0; i < se.Count; i++)
                    {
                        int ix1 = 21;
                        while ((ix1 + 6) < hl[i].Length)
                        {
                            JsonTempData jtd = new JsonTempData();
                            jtd.cid  = se[i].ToString();
                            jtd.date = hl[i][ix1].ToString("D02") + "-" + hl[i][ix1 + 1].ToString("D02") + " " + hl[i][ix1 + 2].ToString("D02") + ":" + hl[i][ix1 + 3].ToString("D02");

                            ushort t = BytesOP.MakeShort(hl[i][ix1 + 5], hl[i][ix1 + 4]);
                            if (t == 0xffff)
                            {
                                jtd.temperature = "-100";
                            }
                            else
                            {
                                jtd.temperature = ((t) * 0.0625 + 0.5).ToString("f1");
                            }
                            jsonTempDataList.list.Add(jtd);
                            ix1 += 6;
                        }
                    }
                    WFHttpWebResponse            web = new WFHttpWebResponse();
                    IDictionary <string, string> p   = new Dictionary <string, string>();
                    p.Add("temperature_list", JsonWork.GetJsonString <JsonTempDataList>(jsonTempDataList));
                    p.Add("session", session);
                    p.Add("device_id", Form1.DeviceID.ToString());

                    HttpWebResponse    hr       = web.CreatePostHttpResponse(JsonWork.strUrl + "post_temperature_list", p);
                    JsonAddrListReturn AddrList = JsonWork.GetJsonObject <JsonAddrListReturn>(web.Content);
                    if (AddrList.code != "200")
                    {
                        MessageBox.Show("数据导出失败:" + AddrList.message);
                        return;
                    }
                    else
                    {
                        MessageBox.Show("数据导出成功");
                        return;
                    }
//                     ee = new ExcelExport();
//                     ee.SheetCount = se.Count;
//                     ee.ExcelWorkbookCallbackProc = new ExcelExport.ExcelWorkbookCallback(ExcelWorkbookCallbackProc1);
//                     ee.ExcelExportProc1();
                }
                else
                {
                    MessageBox.Show("读取历史数据失败");
                }
            }
        }
Beispiel #23
0
 private void button1_Click(object sender, EventArgs e)
 {
     if (radioButton1.Checked)
     {
         int  retry;
         bool bOK;
         retry = 20;
         bOK   = false;
         while (retry != 0)
         {
             Rx1616 = new CP1616Packet(6, 1);
             byte[] tx = CP1616Packet.MakeCP1616Packet(6, 1, new byte[2] {
                 0, 100
             });
             try
             {
                 COMPort1.Write(tx, 0, tx.Length);
             }
             catch { retry--; continue; }
             while (true)
             {
                 try
                 {
                     if (Rx1616.DataPacketed((byte)COMPort1.ReadByte()))
                     {
                         bOK = true;
                         MessageBox.Show("清空数据成功");
                         break;
                     }
                 }
                 catch
                 {
                     break;
                 }
             }
             if (bOK)
             {
                 break;
             }
             retry--;
         }
         if (retry == 0)
         {
             MessageBox.Show("清空数据失败");
         }
     }
     else if (radioButton2.Checked)
     {
         int  retry;
         bool bOK;
         retry = 20;
         bOK   = false;
         while (retry != 0)
         {
             Rx1616 = new CP1616Packet(6, 1);
             byte[] tx = CP1616Packet.MakeCP1616Packet(6, 1, new byte[2] {
                 (byte)(numericUpDown1.Value - 1), (byte)(numericUpDown2.Value)
             });
             try
             {
                 COMPort1.Write(tx, 0, tx.Length);
             }
             catch { retry--; continue; }
             while (true)
             {
                 try
                 {
                     if (Rx1616.DataPacketed((byte)COMPort1.ReadByte()))
                     {
                         bOK = true;
                         MessageBox.Show("清空数据成功");
                         break;
                     }
                 }
                 catch
                 {
                     break;
                 }
             }
             if (bOK)
             {
                 break;
             }
             retry--;
         }
         if (retry == 0)
         {
             MessageBox.Show("清空数据失败");
         }
     }
 }
Beispiel #24
0
        //同步地址信息
        private void ImportAddrInfo_Click(object sender, EventArgs e)
        {
            ExcelImport el = new ExcelImport(true, "安装位置");
            DataTable   dt = el.ExcelImportProc_OleDB();

            if (dt == null || dt.Rows.Count != 104)
            {
                MessageBox.Show("地址数据文件格式有问题,请检查!");
                return;
            }
            enBT(false);

            int areaLen     = 64;
            int addrInfoLen = areaLen + 100 * 2;

            byte[] addrInfo = new byte[addrInfoLen];
            for (int i = 0; i < addrInfoLen; i++)
            {
                addrInfo[i] = 0;
            }
            //设备安装小区
            string strInstallArea = dt.Rows[2][1].ToString();

            Debug.WriteLine("设备安装小区:" + strInstallArea);
            byte[] byteInstallArea = Encoding.Default.GetBytes(strInstallArea);
            if (byteInstallArea.Length >= areaLen)
            {
                for (int i = 0; i < areaLen; i++)
                {
                    addrInfo[i] = byteInstallArea[i];
                }
            }
            else
            {
                for (int i = 0; i < byteInstallArea.Length; i++)
                {
                    addrInfo[i] = byteInstallArea[i];
                }
            }
            //栋单元信息处理
            byte dong, danyuan;

            for (int i = 0; i < 100; i++)
            {
                Debug.WriteLine("传感器" + (i + 1).ToString() + ":" + dt.Rows[4 + i][1].ToString() + "-" + dt.Rows[4 + i][2].ToString());
                if (!byte.TryParse(dt.Rows[4 + i][1].ToString(), out dong))
                {
                    MessageBox.Show("单元格B" + (i + 5).ToString() + "内容输入有问题,只能输入0-200的数字");
                    return;
                }
                if (!byte.TryParse(dt.Rows[4 + i][2].ToString(), out danyuan))
                {
                    MessageBox.Show("单元格C" + (i + 5).ToString() + "内容输入有问题,只能输入0-200的数字");
                    return;
                }
                addrInfo[areaLen + i * 2]     = dong;
                addrInfo[areaLen + i * 2 + 1] = danyuan;
            }
            byte[] tx    = CP1616Packet.MakeCP1616Packet(3, 1, addrInfo);
            int    retry = 20;

            Rx1616 = new CP1616Packet(3, 1);
            bool bOK = false;

            while (retry != 0)
            {
                WaitMS(100);
                try
                {
                    COMPort1.Write(tx, 0, tx.Length);
                }
                catch { retry--; continue; }
                while (true)
                {
                    try
                    {
                        if (Rx1616.DataPacketed((byte)COMPort1.ReadByte()))
                        {
                            bOK = true;
                            break;
                        }
                    }
                    catch
                    { break; }
                }
                if (bOK)
                {
                    break;
                }
                retry--;
            }
            if (retry == 0)
            {
                MessageBox.Show("地址数据导入失败,请重试!");
                enBT(true);
            }
            else
            {
                MessageBox.Show("地址数据导入完成!");
                enBT(true);
            }
        }
        public void UartRxProc(CP1616Packet rxPacket)
        {
            Debug.WriteLine(WFNetLib.StringFunc.StringsFunction.byteToHexStr(rxPacket.Data, " "));
            if (rxPacket.Header.Command != waitCommand)
            {
                return;
            }
            WaitSometingForm.bGenerateSometing = true;
            if (rxPacket.Header.Command == 6)
            {
                if (rxPacket.Data[0] == 1)//设定成功
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.Append("设定成功:\r\n");
                        sb.Append("          节点ID:" + rxPacket.Data[1].ToString() + "\r\n");
                        float k, b, A;
                        k = BitConverter.ToSingle(rxPacket.Data, 2);
                        b = BitConverter.ToSingle(rxPacket.Data, 6);
                        A = BitConverter.ToSingle(rxPacket.Data, 10);
                        sb.Append("          原系数为:\r\n");
                        sb.Append("          k=" + k.ToString() + "\r\n");
                        sb.Append("          b=" + b.ToString() + "\r\n");
                        sb.Append("          A=" + A.ToString() + "\r\n");
                        sb.Append("          新系数为:\r\n");
                        k = BitConverter.ToSingle(rxPacket.Data, 14);
                        b = BitConverter.ToSingle(rxPacket.Data, 18);
                        A = BitConverter.ToSingle(rxPacket.Data, 22);
                        sb.Append("          k=" + k.ToString() + "\r\n");
                        sb.Append("          b=" + b.ToString() + "\r\n");
                        sb.Append("          A=" + A.ToString() + "\r\n");
                        MessageBox.Show(this, sb.ToString());
                    }));
                }
                else if (rxPacket.Data[0] == 2)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        MessageBox.Show(this, "设定失败,没有无线节点响应");
                    }));
                }
                else if (rxPacket.Data[0] == 3)//设定成功
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        MessageBox.Show(this, "网络已经启动,请先利用“网络采集AD值”停止网络后再进行此操作");
                    }));
                }
            }
            else if (rxPacket.Header.Command == 7)
            {
                if (rxPacket.Data[0] == 1)//设定成功
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        float k, b, A;
                        k = BitConverter.ToSingle(rxPacket.Data, 2);
                        b = BitConverter.ToSingle(rxPacket.Data, 6);
                        A = BitConverter.ToSingle(rxPacket.Data, 10);

                        textBox1.Text = k.ToString();
                        textBox2.Text = b.ToString();
                        textBox3.Text = A.ToString();
                        MessageBox.Show(this, "读取成功!!!");
                    }));
                }
                else if (rxPacket.Data[0] == 2)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        MessageBox.Show(this, "读取失败,没有无线节点响应");
                    }));
                }
                else if (rxPacket.Data[0] == 3)//设定成功
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        MessageBox.Show(this, "网络已经启动,请先利用“网络采集AD值”停止网络后再进行此操作");
                    }));
                }
            }
        }
Beispiel #26
0
 private void button1_Click(object sender, EventArgs e)
 {
     if (radioButton1.Checked)
     {
         Rx1616 = new CP1616Packet(4, 1);
         byte[] tx = CP1616Packet.MakeCP1616Packet(4, 1, 1);
         COMPort1.Write(tx, 0, tx.Length);
         while (true)
         {
             try
             {
                 if (Rx1616.DataPacketed((byte)COMPort1.ReadByte()))
                 {
                     ee = new ExcelExport(100);
                     ee.ExcelWorkbookCallbackProc = new ExcelExport.ExcelWorkbookCallback(ExcelWorkbookCallbackProc);
                     ee.ExcelExportProc();
                     break;
                 }
             }
             catch
             {
                 MessageBox.Show("读取数据失败");
                 break;
             }
         }
     }
     else if (radioButton2.Checked)
     {
         hl = new List <byte[]>();
         se = new List <byte>();
         er = new List <byte>();
         progressBar1.Value = 0;
         int  retry;
         bool bOK;
         for (int i = 0; i < 100; i++)
         {
             retry = 20;
             bOK   = false;
             while (retry != 0)
             {
                 Rx1616 = new CP1616Packet(5, 1);
                 byte[] tx = CP1616Packet.MakeCP1616Packet(5, 1, (byte)i);
                 try
                 {
                     COMPort1.Write(tx, 0, tx.Length);
                 }
                 catch { retry--; continue; }
                 while (true)
                 {
                     try
                     {
                         if (Rx1616.DataPacketed((byte)COMPort1.ReadByte()))
                         {
                             //                                 byte[] temp = new byte[Rx1616.Data.Length - 2];
                             //                                 for (int j = 0; j < Rx1616.Data.Length - 2; j++)
                             //                                 {
                             //                                     temp[j]
                             //                                 }
                             hl.Add(Rx1616.Data);
                             se.Add((byte)(i + 1));
                             progressBar1.Value++;
                             Thread.Sleep(100);
                             bOK = true;
                             break;
                         }
                     }
                     catch
                     {
                         break;
                     }
                 }
                 if (bOK)
                 {
                     break;
                 }
                 retry--;
                 Thread.Sleep(1000);
             }
             if (retry == 0)
             {
                 er.Add((byte)(i + 1));
             }
         }
         if (se.Count != 0)
         {
             if (er.Count != 0)
             {
                 string str = "如下传感器数据读取失败:\r\n";
                 for (int i = 0; i < er.Count; i++)
                 {
                     str += "第" + er[i].ToString() + "号传感器" + "\r\n";
                 }
                 str += "是否继续保存";
                 if (MessageBox.Show(str, "读取错误", MessageBoxButtons.YesNo) == DialogResult.No)
                 {
                     return;
                 }
             }
             ee            = new ExcelExport();
             ee.SheetCount = se.Count;
             ee.ExcelWorkbookCallbackProc = new ExcelExport.ExcelWorkbookCallback(ExcelWorkbookCallbackProc1);
             ee.ExcelExportProc1();
         }
         else
         {
             MessageBox.Show("读取历史数据失败");
         }
     }
     else if (radioButton3.Checked)
     {
         if (numericUpDown2.Value < numericUpDown1.Value)
         {
             MessageBox.Show("范围有误");
             return;
         }
         hl = new List <byte[]>();
         se = new List <byte>();
         er = new List <byte>();
         progressBar1.Value = 0;
         int per = (int)(numericUpDown2.Value - numericUpDown1.Value + 1);
         per = 100 / per;
         int  retry;
         bool bOK;
         for (int i = (int)(numericUpDown1.Value - 1); i < (int)numericUpDown2.Value; i++)
         {
             retry = 20;
             bOK   = false;
             while (retry != 0)
             {
                 Rx1616 = new CP1616Packet(5, 1);
                 byte[] tx = CP1616Packet.MakeCP1616Packet(5, 1, (byte)i);
                 try
                 {
                     COMPort1.Write(tx, 0, tx.Length);
                 }
                 catch { retry--; continue; }
                 while (true)
                 {
                     try
                     {
                         if (Rx1616.DataPacketed((byte)COMPort1.ReadByte()))
                         {
                             //                                 byte[] temp = new byte[Rx1616.Data.Length - 2];
                             //                                 for (int j = 0; j < Rx1616.Data.Length - 2; j++)
                             //                                 {
                             //                                     temp[j]
                             //                                 }
                             hl.Add(Rx1616.Data);
                             se.Add((byte)(i + 1));
                             progressBar1.Value += per;
                             Thread.Sleep(100);
                             bOK = true;
                             break;
                         }
                     }
                     catch
                     {
                         break;
                     }
                 }
                 if (bOK)
                 {
                     break;
                 }
                 retry--;
                 Thread.Sleep(1000);
             }
             if (retry == 0)
             {
                 er.Add((byte)(i + 1));
             }
         }
         progressBar1.Value = 100;
         if (se.Count != 0)
         {
             if (er.Count != 0)
             {
                 string str = "如下传感器数据读取失败:\r\n";
                 for (int i = 0; i < er.Count; i++)
                 {
                     str += "第" + er[i].ToString() + "号传感器" + "\r\n";
                 }
                 str += "是否继续保存";
                 if (MessageBox.Show(str, "读取错误", MessageBoxButtons.YesNo) == DialogResult.No)
                 {
                     return;
                 }
             }
             ee            = new ExcelExport();
             ee.SheetCount = se.Count;
             ee.ExcelWorkbookCallbackProc = new ExcelExport.ExcelWorkbookCallback(ExcelWorkbookCallbackProc1);
             ee.ExcelExportProc1();
         }
         else
         {
             MessageBox.Show("读取历史数据失败");
         }
     }
 }
Beispiel #27
0
        public void UartRxProc(CP1616Packet rxPacket)
        {
            if (bClosing)
            {
                return;
            }
            //Debug.WriteLine(WFNetLib.StringFunc.StringsFunction.byteToHexStr(rxPacket.Data, " "));
            byte[] data = new byte[2];
            data[0] = rxPacket.Header.Command;
            if (bStopNet)
            {
                data[1] = 0x02;                                   //
                CP1616Packet.CP1616ComSend(ref Uart, 3, 0, data); //接收应答,并要求关闭网络
            }
            else if (rxPacket.Header.Command == 5)                //标定网络启动确认
            {
                startResult = rxPacket.Data[0];
            }
            else if (rxPacket.Header.Command == 0x10)
            {
                data[1] = 0x00;
                CP1616Packet.CP1616ComSend(ref Uart, 3, 0, data);//接收应答
                SignInProc(rxPacket);
            }
            else if (rxPacket.Header.Command == 0x22)//标定软件只处理标定数据
            {
                startResult = 2;
                if (listView1.Items.Count == 0)
                {
                    data[1] = 0x01;                                   //请求传感器名单
                    CP1616Packet.CP1616ComSend(ref Uart, 3, 0, data); //接收应答,并请求传感器名单
                }
                else
                {
                    data[1] = 0x00;
                    CP1616Packet.CP1616ComSend(ref Uart, 3, 0, data);//接收应答
                    ADCDataProc(rxPacket);
                }
                this.Invoke((EventHandler)(delegate
                {
                    if (btNetStart.Text == "启动网络")
                    {
                        btNetStart.Text = "停止网络";
                    }
                }));
            }
            else if (rxPacket.Header.Command == 0x20)//测量网络,请求转换为标定网络
            {
                this.Invoke((EventHandler)(delegate
                {
                    textBox1.AppendText(DateTime.Now.ToString("HH:mm:ss:ffff") + ":测量网络转换为标定网络\r\n");
                }));

                startResult = 3;
                data[1]     = 0x03;
                CP1616Packet.CP1616ComSend(ref Uart, 3, 0, data);
            }
            if (waitCommand == rxPacket.Header.Command)
            {
                WaitSometingForm.bGenerateSometing = true;
            }
        }