Beispiel #1
0
        private void button3_Click(object sender, EventArgs e)
        {
            //挂起循环发送线程
            //Pause();
            //停止工作时间定时器
            //timer1.Stop();
            //timerSend.Stop();
            if (!checkOk())
            {
                //MessageBox.Show("没有选中开关设备或没有选中端口");
                Notify notify = new Notify("没有选中开关设备或没有选中端口");
                notify.ShowDialog();
                return;
            }
            if (cycleFlag == false)
            {
                if (comboBox10.Text.Trim().Length == 0)
                {
                    saveTime = 5  * 60;
                    Notify notify = new Notify("数据保存时间输入错误,自动默认为5分钟");
                    notify.ShowDialog();
                   //MessageBox.Show("数据保存时间输入错误,自动默认为5分钟");
                }
                else
                    saveTime = Int32.Parse(comboBox10.Text)  * 60;

                Int32 timeRound = Int32.Parse(comboBox6.Text) * 1000 + Int32.Parse(comboBox5.Text) * 1000 * 60 + Int32.Parse(comboBox4.Text) * 3600 * 1000;
                try
                {

                    timerSend.Interval = timeRound;
                }
                catch (Exception)
                {
                    Notify notify = new Notify("时间设置有误,请重新输入");
                    notify.ShowDialog();
                    //MessageBox.Show("时间设置有误,请重新输入");
                    return;
                }
                timeRound /= 1000;

                timeHours = timeRound / 3600; timeHoursInit = timeHours;
                timeMinutes = (timeRound % 3600) / 60; timeMinutesInit = timeMinutes;
                timeSeconds = timeRound % 60; timeSecondsInit = timeSeconds;
                toolStripStatusLabel1.Text = "启动周期发送命令,正在准备中";
                cycleFlag = true;
                isFirstCycleFlag = true;
                groupBox5.Enabled = false;
                groupBox13.Enabled = false;
                groupBox9.Enabled = false;
                groupBox10.Enabled = false;
                oneFlag = false;
                label3.Text = "剩余时间:";
                cycleSend();
                timerSend.Start();
                //button3.Text = "终止周期";

            }
            else
            {
                toolStripStatusLabel1.Text = "结束周期发送命令";
                clearTimeCount();
                groupBox5.Enabled = true;
                groupBox13.Enabled = true;
                groupBox9.Enabled = true;
                groupBox10.Enabled = true;
                timerSend.Stop();
                sendClose();
                cycleFlag = false;
                isFirstCycleFlag = true;
               // button3.Text = "周期发送";
                this.pictureBox1.Image = global::AccleZigBee.Properties.Resources.off2;
            }
        }
Beispiel #2
0
        private void ok_Click(object sender, EventArgs e)
        {
            if (!isP)
            {
                Notify notify = new Notify("资源正在准备,请稍后...");
                notify.Show();
                return;
            }
            this.timer1.Stop();
            if (!isOpen)
            {
               // if (!comm.IsOpen)
                if(true)
                {
                    //关闭时点击,则设置好端口,波特率后打开
                    comm.PortName = comboPortName.Text;
                    comm.BaudRate = int.Parse(comboBaudrate.Text);
                    comm.DataBits = int.Parse(comboBoxDataBit.Text);
                    switch (comboBoxStopBit.SelectedIndex)
                    {

                        case -1:
                            comm.StopBits = StopBits.One;
                            break;
                        case 0:
                            comm.StopBits = StopBits.One;
                            break;
                        case 1:
                            comm.StopBits = StopBits.Two;
                            break;
                        // comm.StopBits = StopBits.None;
                    }

                    //把字符串转换为eunm枚举类型。
                    comm.Parity = (Parity)Enum.Parse(typeof(Parity), comboBoxParity.SelectedItem.ToString());
                    try
                    {
                        comm.Open(); //打开串口
                        mainHome.startCount();
                    }
                    catch (Exception ex)
                    {
                        //捕获到异常信息,创建一个新的comm对象,之前的不能用了。
                        //comm = new SerialPort();
                        //现实异常信息给客户。
                        MessageBox.Show(ex.Message);
                        return;
                    }
                    isOpen = true;
                }
            }
            //mainHome = new MainHome(comm);
            //Origination or = new Origination(mainHome);
            Thread.Sleep(1);
            if(pOk)
            {
                or.Show();
                this.Hide();
            }
            else
            {
                Notify notify = new Notify("资源正在准备,请稍后...");
                notify.Show();
                return;
            }
            this.timer1.Dispose();
            //串口线程启动
            mainHome.StartSerialPort();

            this.comboBaudrate.Dispose();
            this.comboBoxDataBit.Dispose();
            this.comboBoxStopBit.Dispose();
            this.comboPortName.Dispose();
            this.label1.Dispose();
            this.label2.Dispose();
            this.label3.Dispose();
            this.label4.Dispose();
            this.label5.Dispose();
            //this.label6.Dispose();
            this.label8.Dispose();
            //this.Dispose();
        }
Beispiel #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            //让循环控制停下来
            Pause();
            //让自动发送停止
            timerSend.Stop();
            if (!checkOk())
            {
                Notify notify = new Notify("没有选中开关设备或没有选中端口");
                notify.ShowDialog();
                //MessageBox.Show("没有选中开关设备或没有选中端口");
                return;
            }
            int n = nodeInfoArray.Count;
            UInt16 ctlWord = 0x0000;
            string cmd="";
            //首选判断命令
            if (comboBox10.Text.Trim().Length == 0)
            {
                saveTime = 5 * 60;
                //MessageBox.Show("数据保存时间输入错误,自动默认为5分钟");
                Notify notify = new Notify("数据保存时间输入错误,自动默认为5分钟");
                notify.ShowDialog();
            }
            else
                saveTime = Int32.Parse(comboBox10.Text) * 60;
            for (int i = 0; i < n; ++i)
            {
                if (nodeInfoArray[i].NodeName == 0x02) continue;
                //如果该设备被选中
                if (nodeInfoArray[i].checkBox.Checked == false) continue;
                //nodeInfoArray[i].nodeBtn.PerformClick();
                if (checkBox18.Checked)
                {
                    this.pictureBox1.Image = global::AccleZigBee.Properties.Resources.on1;
                    ctlWord = getOnFlag(i, true);
                    nodeInfoArray[i].status = 0x01;
                    cmd = "打开";
                    oneFlag = true;
                    label3.Text = "运行时间:";
                    timeHours = 0;
                    timeMinutes = 0;
                    timeSeconds = 0;
                }
                if (checkBox16.Checked)
                {
                     this.pictureBox1.Image = global::AccleZigBee.Properties.Resources.off2;
                     ctlWord = getOnFlag(i, false);
                    nodeInfoArray[i].status = 0x00;
                    cmd = "关闭";
                    oneFlag = false;
                    label3.Text = "剩余时间:";
                    hourLabel.Text = "0";
                    minuteLabel.Text = "0";
                    secondsLabel.Text = "0";

                }
                toolStripStatusLabel1.Text = "当前正在" + cmd + "节点" + nodeInfoArray[i].name + "的所有已经选中端口";
                sendMsg(i,ctlWord);
                Thread.Sleep(100);
            }//for 节点循环
            toolStripStatusLabel1.Text = "单次控制命令发送完毕!";
        }
Beispiel #4
0
        private void button2_Click(object sender, EventArgs e)
        {
            //timerSend.Stop();
            if (!checkOk())
            {
                //MessageBox.Show("没有选中开关设备或没有选中端口");
                Notify notify = new Notify("没有选中开关设备或没有选中端口");
                notify.ShowDialog();
                return;
            }
            //if (paused == true)
            if(ringFlag == false)
            {
                if (comboBox10.Text.Trim().Length == 0)
                {
                    saveTime = 5  * 60;
                    //MessageBox.Show("数据保存时间输入错误,自动默认为5分钟");
                    Notify notify = new Notify("数据保存时间输入错误,自动默认为5分钟");
                    notify.ShowDialog();
                }
                else
                    saveTime = Int32.Parse(comboBox10.Text)  * 60;

                Int32 timCycle = 0,timWork = 0;
                try
                {
                    timCycle = Int32.Parse(comboBox3.Text) * 1000 + Int32.Parse(comboBox2.Text) * 1000 * 60 + Int32.Parse(comboBox1.Text) * 3600 * 1000;
                    timWork = Int32.Parse(comboBox9.Text) * 1000 + Int32.Parse(comboBox8.Text) * 1000 * 60 + Int32.Parse(comboBox7.Text) * 3600 * 1000;
                }
                catch (Exception)
                {
                    Notify notify = new Notify("没有输入正确的时间");
                    notify.ShowDialog();
                    //MessageBox.Show("没有输入正确的时间");
                    return;
                }
                if(timCycle < 21000)
                {
                    Notify notify = new Notify("端口循环时间不得低于20秒");
                    notify.ShowDialog();
                 //   MessageBox.Show("端口循环时间不得低于20秒");
                    return;
                }
                if (timWork != 0)
                {
                    if (timWork < 41000)
                    {
                        Notify notify = new Notify("工作时间不得低于40秒");
                        notify.ShowDialog();
                        //MessageBox.Show("工作时间不得低于40秒");
                        return;
                    }
                    timer1.Interval = timWork;
                    timer1.Start();
                    workFlag = true;
                }
                timCycle /= 1000;

                timeHours = timCycle / 3600; timeHoursInit = timeHours;
                timeMinutes = (timCycle % 3600) / 60; timeMinutesInit = timeMinutes;
                timeSeconds = timCycle % 60; timeSecondsInit = timeSeconds;

                //首先关闭其他节点
                sendClose();
                toolStripStatusLabel1.Text = "当前正在准备循环发送";
                                //启动
                Resume();
                ringFlag = true;
               // button2.Text = "中断循环";
                groupBox6.Enabled = false;
                groupBox9.Enabled = false;
                groupBox12.Enabled = false;
                groupBox8.Enabled = false;
                groupBox10.Enabled = false;
                oneFlag = false;
                label3.Text = "剩余时间:";
            }
            else
            {
                toolStripStatusLabel1.Text = "循环发送停止";
                timer1.Stop();
                Pause();
                sendClose();

                clearTimeCount();

                //button2.Text = "启动循环";
                workFlag = false;
                ringFlag = false;
                groupBox6.Enabled = true;
                groupBox9.Enabled = true;
                groupBox12.Enabled = true;
                groupBox8.Enabled = true;
                groupBox10.Enabled = true;
            }
        }
Beispiel #5
0
        public void updateAngle(string name)
        {
            string tpath = System.AppDomain.CurrentDomain.BaseDirectory + @"\Config\" + name + @".txt";
            if (!File.Exists(tpath))
            {
                Notify notify = new Notify("未找到该开关节点的端口映射表,不能配置角度文件");
                notify.ShowDialog();
              //  MessageBox.Show("未找到该开关节点的端口映射表,不能配置角度文件");

                return;
            }
            using (StreamReader sr = new StreamReader(tpath))
            {
                string line;
                string angleString;
                if ((line = sr.ReadLine()) != null)
                {
                    string[] split = line.Split(new Char[] { ';' });
                    int num = split.Length;
                    //newNode.portNum = split.Length;
                    for (int j = 0; j < num; ++j)
                    {
                        line = split[j].Trim();
                        if (line.Length != 0)
                        {
                            //line为名字

                           angleString = System.AppDomain.CurrentDomain.BaseDirectory + @"\Angle\" + line;
                           if (!File.Exists(angleString))
                           {
                               Notify notify = new Notify("未找到某角度文件");
                               notify.ShowDialog();
                                // MessageBox.Show("未找到某角度文件");
                                return;
                            }
                            using (StreamReader sr1 = new StreamReader(angleString))
                            {
                                angleString = sr1.ReadLine().Trim();
                               // Console.WriteLine("mainhome---1612-----{0}",line);
                                if(nodeNameIndexHash.ContainsKey(line))
                                {
                                    if (angleString.Length == 0)
                                        nodeInfoArray[nodeNameIndexHash[line]].angle = "NULL";
                                    else
                                    {
                                        nodeInfoArray[nodeNameIndexHash[line]].angle = angleString;
                                    }
                                }
                                sr1.Close();
                            }//using

                           // Console.WriteLine("正在更新{0}的角度数据文件,角度为{1}", line, angleString);
                        }
                    }
                    sr.Close();
                }
                else
                {
                    Notify notify = new Notify("文件存在但是其中并没有数据,请检查配置文件" + name);
                    notify.ShowDialog();
                   // MessageBox.Show("文件存在但是其中并没有数据,请检查配置文件" + name);
                }
            }//using
        }
Beispiel #6
0
        //用于处理串口收到的所有数据
        public void DataProcess()
        {
            if (0x00 == tmpBuf[2])
            {
                #region 描述符

                byte nodeType = tmpBuf[3];
                ushort net = Convert.ToUInt16(tmpBuf[4] * 256 + tmpBuf[5]);
                string mac = Convert.ToString(tmpBuf[6], 16).PadLeft(2, '0') + Convert.ToString(tmpBuf[7], 16).PadLeft(2, '0')
                    + Convert.ToString(tmpBuf[8], 16).PadLeft(2, '0') + Convert.ToString(tmpBuf[9], 16).PadLeft(2, '0') +
                    Convert.ToString(tmpBuf[10], 16).PadLeft(2, '0') + Convert.ToString(tmpBuf[11], 16).PadLeft(2, '0') +
                    Convert.ToString(tmpBuf[12], 16).PadLeft(2, '0') + Convert.ToString(tmpBuf[13], 16).PadLeft(2, '0');
                mac = mac.ToUpper();
                ushort parentAddr = Convert.ToUInt16(tmpBuf[14] * 256 + tmpBuf[15]);
                //获取节点名字
                string name = readNameByMac(mac);
                if (name == null)
                {
                    //MessageBox.Show("该节点没有入库,请检查Config目录下的IEEE.txt。添加信息后复位该节点");
                  //  Console.WriteLine("name:{0},mac:{1}",name,mac);
                    Notify notify = new Notify("该节点没有入库,请检查Config目录下的IEEE.txt。添加信息后复位该节点");
                    notify.ShowDialog();
                    return;
                }
                //因为协调器在软件启动时已经虚拟出来,所以真正节点上线后需要特殊处理

                if (0x00 == nodeType)
                {
                    if (coorIsOnline)
                        return;
                    # region 协调器
                    if (nodeMacIndexHash.ContainsKey("0000000000000000"))
                    {
                        nodeMacIndexHash.Remove("0000000000000000");
                    }

                    if (!nodeMacIndexHash.ContainsKey(mac))
                        nodeMacIndexHash.Add(mac, 0);
                    else
                        nodeMacIndexHash[mac] = 0;

                    if (name != "V000")
                    {
                        nodeNameIndexHash.Remove("V000");
                        if (!nodeNameIndexHash.ContainsKey(name))
                            nodeNameIndexHash.Add(name, 0);
                        else
                            nodeNameIndexHash[name] = 0;
                    }
                    if (net != 0x0000)
                    {
                        nodeAddrIndexHash.Remove(0);
                        if (!nodeAddrIndexHash.ContainsKey(net))
                            nodeAddrIndexHash.Add(net, 0);
                        else
                            nodeAddrIndexHash[net] = 0;
                    }
                    nodeInfoArray[0].NodeName = nodeType;
                    nodeInfoArray[0].Mac = mac;
                    nodeInfoArray[0].NetAddr = net;
                    nodeInfoArray[0].name = name;
                    nodeInfoArray[0].ParentAddr = 0;
                    this.Invoke((EventHandler)(delegate
                    {
                        //按钮的名字就是MAC地址
                        nodeInfoArray[0].nodeBtn.Name = mac;
                        //按钮的文本就是通用名字
                        nodeInfoArray[0].nodeBtn.Text = name;
                        //修改协调器的节点图标属性
                        nodeInfoArray[0].nodeBtn.Image = global::AccleZigBee.Properties.Resources.Coor;
                        nodeInfoArray[0].nodeBtn.ToolTipText = mac;
                        nodeInfoArray[0].nodeBtn.Size = new System.Drawing.Size(56, 56);
                        // newNode.nodeBtn.Click += new System.EventHandler(this.toolStripSplitRouterClick);

                    }));
                    coorIsOnline = true;
                    addStripBtn(mac);
                    #endregion
                }
                else
                {
                    if (!coorIsOnline)
                    {
                        this.Invoke((EventHandler)(delegate
                        {
                            //按钮的名字就是MAC地址
                            nodeInfoArray[0].nodeBtn.Name = "0000000000000000";
                            //按钮的文本就是通用名字
                            nodeInfoArray[0].nodeBtn.Text = "V000";
                            //修改协调器的节点图标属性
                            nodeInfoArray[0].nodeBtn.Image = global::AccleZigBee.Properties.Resources.Coor;
                            nodeInfoArray[0].nodeBtn.ToolTipText = "0000000000000000";
                            nodeInfoArray[0].nodeBtn.Size = new System.Drawing.Size(56, 56);
                            // newNode.nodeBtn.Click += new System.EventHandler(this.toolStripSplitRouterClick);
                        }));
                        coorIsOnline = true;

                    }
                    //节点第一次上线
                    if (!nodeMacIndexHash.ContainsKey(mac))
                    {
                        NodeDescribePacket newNode = new NodeDescribePacket();
                        newNode.NodeName = nodeType;
                        newNode.Mac = mac;
                        newNode.NetAddr = net;
                        newNode.name = name;
                        newNode.ParentAddr = parentAddr;
                        //按钮的名字就是MAC地址
                        newNode.nodeBtn.Name = mac;
                        //按钮的文本就是通用名字
                        newNode.nodeBtn.Text = name;

                        //终端节点处理
                        if (0x02 == nodeType)
                        {
                            #region 终端节点
                            //[2]读取标定系数
                            if (!ReadCalibrationCoefficient(newNode))
                            {
                               // MessageBox.Show("没有找到config.txt标定文件,默认为各个系数为1");
                                Notify notify = new Notify("没有找到config.txt标定文件,默认为各个系数为1");
                                notify.ShowDialog();
                            }
                            //设置终端节点的图标与大小
                            this.Invoke((EventHandler)(delegate
                            {
                                newNode.nodeBtn.Image = global::AccleZigBee.Properties.Resources.End;
                                newNode.nodeBtn.ToolTipText = newNode.Mac;
                                newNode.nodeBtn.Size = new System.Drawing.Size(32, 32);
                                newNode.nodeBtn.Click += new System.EventHandler(this.toolStripSplitWaveClick);
                                newNode.linkLabel.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.LinkClicked);
                                //设置linkLabel,为了防止与toolstripbtn同名,在后边加了空格
                                newNode.linkLabel.Name = newNode.Mac + " ";
                             }));
                            newNode.count = count;
                            #endregion

                        }//终端处理完毕
                        else if (0x01 == nodeType)
                        {
                            #region 路由器
                            //设置路由器节点的图标与大小
                            this.Invoke((EventHandler)(delegate
                            {
                            newNode.nodeBtn.Image = global::AccleZigBee.Properties.Resources.midRouter;
                            newNode.nodeBtn.ToolTipText = newNode.Mac;
                            newNode.nodeBtn.Size = new System.Drawing.Size(56, 56);
                            newNode.nodeBtn.Click += new System.EventHandler(this.toolStripSplitRouterClick);
                            }));
                            if (!ReadPortWithName(newNode))
                            {
                               // MessageBox.Show("未找到该开关节点的端口映射表,请重新配置");
                                Notify notify = new Notify("未找到该开关节点的端口映射表,请重新配置");
                                notify.ShowDialog();
                                return;
                            }
                            #endregion
                        }

                        nodeInfoArray.Add(newNode);
                        if(!nodeMacIndexHash.ContainsKey(mac))
                            nodeMacIndexHash.Add(mac, nodeInfoArray.Count - 1);
                        if (!nodeNameIndexHash.ContainsKey(name))
                            nodeNameIndexHash.Add(name, nodeInfoArray.Count - 1);
                        else
                            nodeNameIndexHash[name] = nodeInfoArray.Count - 1;

                        //更新短地址映射的数组索引
                        if (nodeAddrIndexHash.ContainsKey(net))
                            nodeAddrIndexHash[net] = nodeInfoArray.Count - 1;
                        else
                            nodeAddrIndexHash.Add(net, nodeInfoArray.Count - 1);
                        if (newNode.NodeName != 0x02)
                            addStripBtn(newNode.Mac);
                        this.toolStripStatusLabel3.Text = "节点数:" + nodeInfoArray.Count.ToString();
                      //  Console.WriteLine("节点{0},的索引是{1}", mac, nodeInfoArray.Count - 1);
                    }//如果是节点第一次上线
                    else
                    {
                        int index = nodeInfoArray.Count;
                        try
                        {
                            index = nodeMacIndexHash[mac];
                        }
                        catch (Exception)
                        {
                            return;
                        }
                        if (index >= nodeInfoArray.Count)
                        {
                            return;
                        }
                        try
                        {
                            if (net != nodeInfoArray[index].NetAddr)
                            {
                                int outIndex;
                                if (nodeAddrIndexHash.TryGetValue(nodeInfoArray[index].NetAddr, out outIndex))
                                {
                                    nodeAddrIndexHash.Remove(nodeInfoArray[index].NetAddr);
                                    nodeAddrIndexHash.Add(net, index);
                                }
                            }
                        }catch(Exception)
                        {
                            if (nodeAddrIndexHash.ContainsKey(net))
                            {
                                nodeAddrIndexHash[net] = index;
                            }
                            else
                                nodeAddrIndexHash.Add(net, index);
                        }

                       // Console.WriteLine("再次上线节点的MAC地址为{0},IP地址为{1}",mac, net);
                        nodeInfoArray[index].NetAddr = net;
                        nodeInfoArray[index].NodeName = nodeType;
                        nodeInfoArray[index].Mac = mac;
                        nodeInfoArray[index].name = name;
                        nodeInfoArray[index].ParentAddr = parentAddr;
                        //foreach(KeyValuePair<ushort,int>kvp in nodeAddrIndexHash)
                        //{
                        //}

                        this.Invoke((EventHandler)(delegate
                        {
                                //按钮的名字就是MAC地址
                                nodeInfoArray[index].nodeBtn.Name = mac;
                                //按钮的文本就是通用名字
                                nodeInfoArray[index].nodeBtn.Text = name;
                                nodeInfoArray[index].nodeBtn.ToolTipText = mac;
                        }));
                        if (0x01 == nodeType)
                        {

                            if (!ReadPortWithName(nodeInfoArray[index]))
                            {
                                //MessageBox.Show("未找到该开关节点的端口映射表,请重新配置");
                                Notify notify = new Notify("未找到该开关节点的端口映射表,请重新配置");
                                notify.ShowDialog();
                                return;
                            }
                        }
                        else
                        {
                            nodeInfoArray[index].avg = 0;
                            nodeInfoArray[index].count = count;
                            nodeInfoArray[index].sum = 0;
                            nodeInfoArray[index].dataSaveCount = 1;
                            nodeInfoArray[index].head = 0;
                            nodeInfoArray[index].tail = 0;
                            nodeInfoArray[index].flag = false;
                            nodeInfoArray[index].packetCount = 0;
                            //nodeInfoArray[index].packetCount = 0;
                            if (!ReadCalibrationCoefficient(nodeInfoArray[index]))
                            {
                                //MessageBox.Show("没有找到config.txt标定文件,默认为各个系数为1");
                                Notify notify = new Notify("没有找到config.txt标定文件,默认为各个系数为1");
                                notify.ShowDialog();
                            }
                        }
                    }
                }
                #endregion
            }//if (0x00 == tmpBuf[2])
            else if (0x01 == tmpBuf[2])
            {
                #region 数据处理
                int k = 6;
                double data3;
                int index = -1;
            # if false
                try
                {

                    index = nodeAddrIndexHash[Convert.ToUInt16(tmpBuf[3] * 256 + tmpBuf[4])];
                    //增加该节点收到包的计数
                    ++nodeInfoArray[index].packetCount;
                    nodeDataUsed = nodeInfoArray[index];

                //    Console.WriteLine("收到MAC地址{0}的数据,他的IP地址是{1},索引是{2}",nodeDataUsed.Mac,tmp,index);
                }
                catch (Exception)
                {
                    int tmp = Convert.ToUInt16(tmpBuf[3] * 256 + tmpBuf[4]);
                    Console.WriteLine("错误的匹配,当前IP为{0}",tmp);
                    if (!error)
                    {
                        //MessageBox.Show("收到未知节点的数据,请检查端口映射是否添加正确,设备是否入库");
                        Notify notify = new Notify("没收到未知节点的数据,请检查端口映射是否添加正确,设备是否入库");
                        notify.ShowDialog();
                        LogError(tmpBuf.ToString()+" "+index.ToString()+" ");
                        error = true;
                    }
                    return;
                }
            #else
                if (nodeAddrIndexHash.TryGetValue(Convert.ToUInt16(tmpBuf[3] * 256 + tmpBuf[4]), out index))
                {
                    //增加该节点收到包的计数
                    ++nodeInfoArray[index].packetCount;
                    nodeDataUsed = nodeInfoArray[index];
                }
                else
                {
                    //int tmp = Convert.ToUInt16(tmpBuf[3] * 256 + tmpBuf[4]);///////////////////////
                    if (!error)//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                    {
                        //MessageBox.Show("收到未知节点的数据,请检查端口映射是否添加正确,设备是否入库");
                        Notify notify = new Notify("没收到未知节点的数据,请检查端口映射是否添加正确,设备是否入库");
                        notify.ShowDialog();
                      //  Console.WriteLine("数据异常:地址:{0},{1},{2}",tmp,tmpBuf[3],tmpBuf[4]);
                       // LogError(tmpBuf.ToString() + " " + index.ToString() + " ");
                        error = true;
                    }
                    return;
                }

            #endif
                //开始处理数据
                if (0x20 == tmpBuf[k])
                {
                    Int32 tmpData;
                    tmpBuf[k + 1] ^= 0x80;
                    if ((tmpBuf[k + 1] & 0x80) != 0) //负数
                        tmpData = (65536 * tmpBuf[k + 1] + 256 * tmpBuf[k + 2] + tmpBuf[k + 3]) - 16777216;
                    else
                        tmpData = 65536 * tmpBuf[k + 1] + 256 * tmpBuf[k + 2] + tmpBuf[k + 3];
                    data3 = Convert.ToDouble(tmpData) / accleAccuracy;
                    //将数据放到循环队列
                    nodeDataUsed.inptData(data3); //元素数据

                    if (nodeDataUsed.packetCount == UInt32.MaxValue)
                        nodeDataUsed.packetCount = 2;

                    //节点收到第一次数据,才建立文件.系统将excel行的创建与文件的创建延迟到了这里
                    if (nodeDataUsed.packetCount == 1)
                    {
                        //当前需要写的行号
                        nodeInfoArray[index].line = nodeDataUsed.line = ++ecxelLine;
                        /***这里一定要先创建myDataPath,因为creatMyDataText中会对角度文件进行处理**/
                        //创建原始数据文件
                        nodeDataUsed.myDataPath = nodeInfoArray[index].myDataPath = creatMyDataText(nodeDataUsed.name);
                       // Console.WriteLine("名字是446----{0}",nodeDataUsed.name);
                        Thread.Sleep(500);
                        //创建excel行+++++++++++++++++++++++++++++++++++++++++++++
                        nodeDataUsed.path = nodeInfoArray[index].path = creatDataTxt(nodeDataUsed.name);
                        Thread.Sleep(500);
                        using (StreamWriter sw = File.AppendText(nodeDataUsed.myDataPath))
                        {
                            sw.WriteLine(data3.ToString());
                            sw.Close();
                        }

                    }

                    //为了减少数据量,每4个数据才记录一次
                    if ((nodeDataUsed.packetCount & 1) == 0)
                    {
                        //偶数折半后还是偶数,则表示为4的倍数
                        if (((nodeDataUsed.packetCount >> 4) & 1) == 0)
                        {
                            using (StreamWriter sw = File.AppendText(nodeDataUsed.myDataPath))
                            {
                                sw.WriteLine(data3.ToString());
                                sw.Close();
                            }
                        }
                    }

                    if (filter)
                    {
                        //如果未满30组数据
                        if (nodeDataUsed.flag == false)
                        {
                            //小于26个数的均值
                            data3 = nodeDataUsed.sum / (nodeDataUsed.tail - nodeDataUsed.head);
                        }
                        else
                        {
                           // data3 = nodeDataUsed.sum / 26;
                            //等于26个值得均值
                            data3 = nodeDataUsed.sum * 0.03846;
                        }
                    }
                    //如果选择了标定
                    if (modify)
                    {
                        double tmpdata3 = data3;
                        double retdata3 = nodeDataUsed.config[4];
                        retdata3 += nodeDataUsed.config[3] * tmpdata3; tmpdata3 *= data3;
                        retdata3 += nodeDataUsed.config[2] * tmpdata3; tmpdata3 *= data3;
                        retdata3 += nodeDataUsed.config[1] * tmpdata3; tmpdata3 *= data3;
                        retdata3 += nodeDataUsed.config[0] * tmpdata3;
                        data3 = retdata3;
                        //标定后计算完成
                    }

                    //是否更新label实时显示数据
                    if (nodeDataUsed.dataLabel.Visible)
                    {
                        this.Invoke((EventHandler)(delegate
                        {
                            nodeDataUsed.dataLabel.Text = Math.Round(data3, 4).ToString();
                        }));
                    }

                    //更新波形显示
                    //如果当前收到的数据的索引是打开的索引
                    if ((index == openingEndIndnex) && this.userControl11.Visible)
                    {
                        this.Invoke((EventHandler)(delegate { update(index); }));
                    }

                    //检查数据超时保存时间到
                    if (nodeDataUsed.count > count)
                    {
                        if (4294967290 - nodeDataUsed.count + count < saveTime)
                        {
                            nodeDataUsed.avg = data3;
                            return;
                        }
                    }
                    else
                    {
                        if (count - nodeDataUsed.count < saveTime)
                        {
                            nodeDataUsed.avg = data3;
                            return;
                        }
                    }

                    //求均值
                    data3 = (data3 + nodeDataUsed.avg) * 0.5;
                    //保存该节点的均值
                    nodeDataUsed.avg = data3;

                    if (nodeDataUsed.dataSaveCount == 1)
                    {
                        //注释原因:以前为增加数据到记事本,现在导入到excel
            #if false
                        using (StreamReader sr = new StreamReader(nodeDataUsed.path))
                        {
                            tmpAngleString = sr.ReadLine();
                            sr.Close();
                        }
                        using (StreamWriter sw = new StreamWriter(nodeDataUsed.path, false))
                        {

                            sw.WriteLine(tmpAngleString);
                            sw.WriteLine(data3.ToString());
                            sw.Close();
                        }
            #endif
                        try
                        {
                            //防止excel在换文件时,导致数据错乱
                            if (nodeDataUsed.line <= ecxelLine)
                            {
                                excel.AddData<string>(nodeDataUsed.line, 4, nodeDataUsed.avg.ToString());
                                excel.SaveData();
                            }
                        }
                        catch (Exception)
                        {
                            Notify notify = new Notify("请关闭data.xls,在软件中使用转存后打开data_copy.xls,软件稍后需要使用data.xls");
                            notify.ShowDialog();
                            try
                            {
                                excel.RealeseResource();
                                initExcel(excelName+".xls");
                                excel.AddData<string>(nodeDataUsed.line, 4, nodeDataUsed.avg.ToString());
                                excel.SaveData();
                            }
                            catch (Exception)
                            {
                            }
                        }
                        ++nodeDataUsed.dataSaveCount;
                    }
                    else if (nodeDataUsed.dataSaveCount >= 50)
                        nodeDataUsed.dataSaveCount = 1;
                    else
                        ++nodeDataUsed.dataSaveCount;
            #if false
                    Console.WriteLine("{0}",nodeDataUsed.dataSaveCount);
                    using (FileStream stream2 = File.Open(nodeDataUsed.path, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        stream2.Seek(0, SeekOrigin.Begin);
                        stream2.SetLength(0);
                        stream2.Close();
                        using (StreamWriter sw = File.AppendText(nodeDataUsed.path))
                        {
                            sw.WriteLine(data3.ToString());
                            sw.Close();
                        }

                    }
            #endif

                }//if (0x20 == tmpBuf[k])加速度数据处理完毕
                #endregion
            }//数据处理完毕
            else if (0x03 == tmpBuf[2])
            {
                #region 控制状态返回
                int index = nodeAddrIndexHash[Convert.ToUInt16(tmpBuf[3] * 256 + tmpBuf[4])];
                nodeAck = nodeInfoArray[index];
                //更新开关状态
                nodeInfoArray[index].onFlag = Convert.ToUInt16(tmpBuf[5] * 256 + tmpBuf[6]);
                if(index == openingRouterIndex)
                {
                    uptateCheckBox(index);
                }
                #endregion
            }
            else if (0x04 == tmpBuf[2])
            {
                #region  有空隙时间
                Monitor.Enter(this.ls);
                if (SendPacket.Count != 0)
                {
                    //发送数据,并返回ACK值
                    ACK = SendPacket.First.Value.doTask();
                    this.toolStripStatusLabel2.Text = "当前正在尝试发送编号为"+ACK.ToString()+"的控制命令";
                }
                else
                {
                    //this.toolStripStatusLabel2.Text = "正确收到ACK编号" + ACK.ToString() + "/当前共" + SendPacket.Count.ToString() + "个命令在排队"; ;
                    serialPort.Write(noDataAck, 0, 9);
                }
                Monitor.Exit(this.ls);
                #endregion
            }
            else if (0x07 == tmpBuf[2])
            {
                #region ACK返回
                if (tmpBuf[3] == ACK)
                {
                    // Console.WriteLine("队首元素发出后ACK收到,当前正在排队的命令共{0}", SendPacket.Count);
                    if (SendPacket.Count != 0)
                    {
                        if (SendPacket.Count != 0)
                        SendPacket.RemoveFirst();
                        this.toolStripStatusLabel2.Text = "正确收到ACK编号" + ACK.ToString() + "/当前共" + SendPacket.Count.ToString() + "个命令在排队";
                    }
                }
                else
                {
                    if(SendPacket.Count!=0)
                        SendPacket.RemoveLast();
                    Notify notify = new Notify("ACK返回错误,设备间同步出错,正在重新协调同步");
                    notify.ShowDialog();
                    //MessageBox.Show("ACK返回错误,设备间同步出错");
                }
                #endregion
            }
        }
Beispiel #7
0
        private void ok_Click(object sender, EventArgs e)
        {
            if (!isP)
            {
                Notify notify = new Notify("资源正在准备,请稍后...");
                notify.Show();
                return;
            }
            this.timer1.Stop();
            if (!isOpen)
            {
                // if (!comm.IsOpen)
                if (true)
                {
                    //关闭时点击,则设置好端口,波特率后打开
                    comm.PortName = comboPortName.Text;
                    comm.BaudRate = int.Parse(comboBaudrate.Text);
                    comm.DataBits = int.Parse(comboBoxDataBit.Text);
                    switch (comboBoxStopBit.SelectedIndex)
                    {
                    case -1:
                        comm.StopBits = StopBits.One;
                        break;

                    case 0:
                        comm.StopBits = StopBits.One;
                        break;

                    case 1:
                        comm.StopBits = StopBits.Two;
                        break;
                        // comm.StopBits = StopBits.None;
                    }

                    //把字符串转换为eunm枚举类型。
                    comm.Parity = (Parity)Enum.Parse(typeof(Parity), comboBoxParity.SelectedItem.ToString());
                    try
                    {
                        comm.Open(); //打开串口
                        mainHome.startCount();
                    }
                    catch (Exception ex)
                    {
                        //捕获到异常信息,创建一个新的comm对象,之前的不能用了。
                        //comm = new SerialPort();
                        //现实异常信息给客户。
                        MessageBox.Show(ex.Message);
                        return;
                    }
                    isOpen = true;
                }
            }
            //mainHome = new MainHome(comm);
            //Origination or = new Origination(mainHome);
            Thread.Sleep(1);
            if (pOk)
            {
                or.Show();
                this.Hide();
            }
            else
            {
                Notify notify = new Notify("资源正在准备,请稍后...");
                notify.Show();
                return;
            }
            this.timer1.Dispose();
            //串口线程启动
            mainHome.StartSerialPort();

            this.comboBaudrate.Dispose();
            this.comboBoxDataBit.Dispose();
            this.comboBoxStopBit.Dispose();
            this.comboPortName.Dispose();
            this.label1.Dispose();
            this.label2.Dispose();
            this.label3.Dispose();
            this.label4.Dispose();
            this.label5.Dispose();
            //this.label6.Dispose();
            this.label8.Dispose();
            //this.Dispose();
        }
Beispiel #8
0
        public string creatDataTxt(string name)
        {
            string angle = "NULL";

            string anglePath = System.AppDomain.CurrentDomain.BaseDirectory + @"\Angle\" + name;

            if (File.Exists(anglePath))
            {
                using (StreamReader sr = new StreamReader(anglePath))
                {
                    angle = sr.ReadLine();
                    if (angle == null)
                    {
                        angle = "NULL";
                    }
                    sr.Close();
                }//using
            }
            else
            {
                angle = "NULL";
            }

            /*
             * if (!File.Exists(tpath))
             *  File.CreateText(tpath).Close();
             * using (StreamWriter sw = File.AppendText(tpath))
             * {
             *  sw.WriteLine(angle);
             *  sw.Close();
             * }
             */
            string nowTime = DateTime.Now.Year.ToString() + "年" + DateTime.Now.Month.ToString().PadLeft(2, '0') + "月" +
                             DateTime.Now.Day.ToString().PadLeft(2, '0') + "日" + DateTime.Now.Hour.ToString().PadLeft(2, '0') + "时" +
                             DateTime.Now.Minute.ToString().PadLeft(2, '0') + "分" + DateTime.Now.Second.ToString().PadLeft(2, '0') + "秒";

            try
            {
                //行数太多,准备换文件存放
                if (ecxelLine >= 2000)
                {
                    excel.RealeseResource();
                    excelName = excelName.Replace("data", "");
                    excelName = "data" + (UInt16.Parse(excelName) + 1).ToString();
                    //关闭节点
                    sendClose();
                    Thread.Sleep(4000);
                    //再重启
                    initExcel(excelName + ".xls");
                    ecxelLine = 2;
                    return(null);

#if false
                    if (ringFlag)
                    {
                        button2.PerformClick();
                    }
                    else if (cycleFlag)
                    {
                        button3.PerformClick();
                    }
#endif
                }
                // Console.WriteLine("在{0}新建一条记录,行数为{1}", excelName, ecxelLine);

                excel.AddData <string>(ecxelLine, 1, name);
                excel.AddData <string>(ecxelLine, 2, nowTime /*DateTime.Now.ToLocalTime().ToString()*/);
                excel.AddData <string>(ecxelLine, 3, angle);
                excel.AddData <string>(ecxelLine, 4, "NULL");
                excel.SaveData();
            }
            catch (Exception)
            {
                // MessageBox.Show("请关闭data.xls,在软件中使用转存后打开data_copy.xls,软件稍后需要使用data.xls");
                Notify notify = new Notify("请关闭data.xls,在软件中使用转存后打开data_copy.xls,软件稍后需要使用data.xls");
                notify.ShowDialog();

                try
                {
                    excel.RealeseResource();
                    initExcel(excelName + ".xls");

                    excel.AddData <string>(ecxelLine, 1, name);
                    //excel.AddData<string>(ecxelLine, 2, DateTime.Now.ToLocalTime().ToString());
                    excel.AddData <string>(ecxelLine, 2, nowTime /*DateTime.Now.ToLocalTime().ToString()*/);
                    excel.AddData <string>(ecxelLine, 3, angle);
                    excel.AddData <string>(ecxelLine, 4, "NULL");
                    excel.SaveData();
                }
                catch (Exception)
                { }
            }
            return(null);
        }
Beispiel #9
0
        public string creatDataTxt(string name)
        {
            string angle = "NULL";

            string anglePath = System.AppDomain.CurrentDomain.BaseDirectory + @"\Angle\" + name;
            if (File.Exists(anglePath))
            {
                using (StreamReader sr = new StreamReader(anglePath))
                {
                    angle = sr.ReadLine();
                    if (angle == null)
                        angle = "NULL";
                    sr.Close();
                }//using
            }
            else
            {
                angle = "NULL";
            }
            /*
            if (!File.Exists(tpath))
                File.CreateText(tpath).Close();
            using (StreamWriter sw = File.AppendText(tpath))
            {
                sw.WriteLine(angle);
                sw.Close();
            }
            */
            string nowTime = DateTime.Now.Year.ToString() + "年" + DateTime.Now.Month.ToString().PadLeft(2, '0') + "月" +
                    DateTime.Now.Day.ToString().PadLeft(2, '0') + "日" + DateTime.Now.Hour.ToString().PadLeft(2, '0') + "时" +
                    DateTime.Now.Minute.ToString().PadLeft(2, '0') + "分" + DateTime.Now.Second.ToString().PadLeft(2, '0')+"秒";
            try
            {
                //行数太多,准备换文件存放
                if (ecxelLine >= 2000)
                {
                    excel.RealeseResource();
                    excelName = excelName.Replace("data", "");
                    excelName = "data" + (UInt16.Parse(excelName) + 1).ToString();
                    //关闭节点
                    sendClose();
                    Thread.Sleep(4000);
                    //再重启
                    initExcel(excelName+".xls");
                    ecxelLine = 2;
                    return null;
            #if false
                    if (ringFlag)
                    {
                        button2.PerformClick();
                    }
                    else if(cycleFlag)
                    {
                        button3.PerformClick();
                    }
            #endif
                }
               // Console.WriteLine("在{0}新建一条记录,行数为{1}", excelName, ecxelLine);

                excel.AddData<string>(ecxelLine, 1, name);
                excel.AddData<string>(ecxelLine, 2, nowTime/*DateTime.Now.ToLocalTime().ToString()*/);
                excel.AddData<string>(ecxelLine, 3, angle);
                excel.AddData<string>(ecxelLine, 4, "NULL");
                excel.SaveData();
            }
            catch(Exception)
            {
               // MessageBox.Show("请关闭data.xls,在软件中使用转存后打开data_copy.xls,软件稍后需要使用data.xls");
                Notify notify = new Notify("请关闭data.xls,在软件中使用转存后打开data_copy.xls,软件稍后需要使用data.xls");
                notify.ShowDialog();

                try
                {
                    excel.RealeseResource();
                    initExcel(excelName+".xls");

                    excel.AddData<string>(ecxelLine, 1, name);
                    //excel.AddData<string>(ecxelLine, 2, DateTime.Now.ToLocalTime().ToString());
                    excel.AddData<string>(ecxelLine, 2, nowTime/*DateTime.Now.ToLocalTime().ToString()*/);
                    excel.AddData<string>(ecxelLine, 3, angle);
                    excel.AddData<string>(ecxelLine, 4, "NULL");
                    excel.SaveData();
                }
                catch (Exception)
                { }
            }
            return null;
        }