Ejemplo n.º 1
0
 /// <summary>
 /// 求数据平均数, 根据时间存入数据库
 /// </summary>
 /// <param name="divice"></param>
 private void SumData(Divice divice)
 {
     tmpDivice.Temperature = (tmpDivice.Temperature + divice.Temperature) / 2;
     tmpDivice.Hum = (tmpDivice.Hum + divice.Hum) / 2;
     tmpDivice.ErrorNum = divice.ErrorNum;
     tickTime++;
     //半小时存一次
     if (tickTime >= 1800)
     {
         tickTime = 0;
         SaveData();
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        ///  进程方法, 开始接收串口数据
        /// </summary>
        private void run()
        {
            w: while (true)
            {
                count = 0;
                c = new char[DataLen];
                try
                {
                    for (int i = 0; i < DataLen; i++)
                    {
                        //度一个字节
                        int Data = sp.ReadByte();
                        //存入缓存
                        c[i] = (char)Data;
                        //如果有头
                        if (i < Start.Length)
                        {
                            if (Data != Start[i])
                            {
                                sp.DiscardInBuffer();
                                sp.DiscardOutBuffer();
                                goto w;
                            }
                        }

                        //如果有尾
                        if (i >= DataLen - End.Length)
                        {
                            if (Data != End[i - DataLen + End.Length])
                            {
                                sp.DiscardInBuffer();
                                sp.DiscardOutBuffer();
                                goto w;
                            }
                        }
                        count++;
                    }
                    isExist = false;
                    foreach (var d in ld)
                    {
                        if (d.DiviceNum == (int)c[2])
                        {
                            isExist = true;
                            d.Temperature = c[3];
                            d.Hum = c[4];
                            d.Wo = c[5];
                            d.ErrorNum = c[6];
                            d.MotorState = c[7];
                            break;
                        }
                    }

                    if (!isExist)
                    {
                        Divice d = new Divice();
                        d.DiviceNum = c[2];
                        d.Temperature = c[3];
                        d.Hum = c[4];
                        d.Wo = c[5];
                        d.ErrorNum = c[6];
                        d.MotorState = c[7];
                        ld.Add(d);
                    }

                }
                catch
                {

                }
                Thread.Sleep(10);
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// 向数据库存数据
 /// </summary>
 private void SaveData()
 {
     SQLiteHelper.SaveData(tmpDivice.DiviceNum, tmpDivice.Temperature, tmpDivice.Hum, tmpDivice.Wo, tmpDivice.MotorState, tmpDivice.ErrorNum);
     tmpDivice = new Divice();
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 初始化
        /// </summary>
        private void init()
        {
            //初始化串口
            try
            {
                tmpDivice = new Divice();
                //Control.CheckForIllegalCrossThreadCalls = false;
                this.MaximizeBox = false;
                b = new byte[RebackDataLen];
                for (int i = 0; i < b.Length; i++)
                {
                    if (i < Start.Length)
                    {
                        b[i] = Convert.ToByte(Start[i]);
                    }

                    if (i > RebackDataLen - End.Length - 1)
                    {
                        b[i] = Convert.ToByte(End[i - RebackDataLen + End.Length]);
                    }
                }
                try
                {
                    Ports = SerialPort.GetPortNames();
                }
                catch
                {
                    Ports = new String[] { "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", };
                }
                c = new char[DataLen];
                sp = new SerialPort();
                sp.BaudRate = BaudTate;
                sp.StopBits = stopBit;
                sp.DataBits = DataBits;
                sp.ReadTimeout = ReadTimeout;
                sp.PortName = ComPortName;
                sp.Open();
                labStatus.Text = ComPortName + STATUS.已连接.ToString();
                lbRoll.Visible = true;
                //sp.Write(new Byte[] { 0xaa }, 0, 1);

            }
            catch
            {
                MessageBox.Show("串口打开失败, 请检查是否选择正确或已被占用.");
                labStatus.Text = ComPortName + STATUS.未连接.ToString();
                lbRoll.Visible = false;
            }
        }