Esempio n. 1
0
        public virtual bool Start()
        {
            if (this.ScaleWight != null)
            {
                var e = new WeightEventArgs(0.0d);


                e.RealWeight = true;
                this.ScaleWight(this, e);
            }
            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// 实时获取当前揽件重量
        /// </summary>
        private void GetWight()
        {
            // 串口触发事件和线程退出事件
            WaitHandle[] handles = new WaitHandle[] { _WightWaitHandle, _ExitHandle };
            while (_bLooping)
            {
                int bWait = WaitHandle.WaitAny(handles);
                // 退出线程
                if (bWait == 1)
                {
                    break;
                }

                // 实时称重数据回调
                if (Interlocked.Read(ref _RealWightFlag) == 1)
                {
                    if (ScaleWight != null)
                    {
                        double tmp = Interlocked.CompareExchange(ref _RealWight, 0, 0);

                        WeightEventArgs e = new WeightEventArgs(tmp);
                        e.RealWeight = false;
                        ScaleWight(this, e);
                    }
                }

                if (Interlocked.Read(ref _SampleWeightFlag) == 1)
                {
                    // 计算样本平均值
                    Double avg = _SampleArray.Average();

                    // 排除最小电子称精度误差一下的重量
                    if (avg < 0.02 || Array.IndexOf(_SampleArray, 0.0) != -1)
                    {
                        // 重新取样运算
                        lock (_LockFlagObj)
                        {
                            Interlocked.CompareExchange(ref _SampleWeightFlag, 0, 1);
                            Array.Clear(_SampleArray, 0, _SampleArray.Length);
                        }
                        continue;
                    }

                    // 计算样本方差大小
                    Double sum = 0.0;
                    foreach (var x in _SampleArray)
                    {
                        sum += (x - avg) * (x - avg);
                    }

                    // 计算当前样本集合的方差值
                    Double variance = sum / _nSampleCountPerGroup;

                    // 校验样本的方差值和最大误差之间的大小关系
                    if (avg >= 0.03 /*kg*/ && variance <= _MaxDeviation)
                    {
                        lock (_LockFlagObj)
                        {
                            Interlocked.CompareExchange(ref _SampleFlag, 0, 1);
                            Interlocked.CompareExchange(ref _RealWightFlag, 0, 1);
                            Interlocked.CompareExchange(ref _SampleWeightFlag, 0, 1);
                            Array.Clear(_SampleArray, 0, _SampleArray.Length);
                        }

                        // 最终数据回调
                        if (ScaleWight != null)
                        {
                            WeightEventArgs e = new WeightEventArgs(avg);
                            e.RealWeight = true;
                            ScaleWight(this, e);
                        }
                    }
                    // 获取采样样本
                    else
                    {
                        lock (_LockFlagObj)
                        {
                            Array.Clear(_SampleArray, 0, _SampleArray.Length);
                            Interlocked.CompareExchange(ref _SampleWeightFlag, 0, 1);
                        }
                    }
                }

                Thread.Sleep(1);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 实时获取当前揽件重量
        /// </summary>
        private bool GetWight()
        {
            StreamWriter sw4 = File.AppendText("D:\\HaiMing.txt");
            string       w4  = "  已经进入AsyncPost \n";

            sw4.Write(w4);
            sw4.Close();

            try
            {
                int cnt = 10;
                while (cnt-- > 0)
                {
                    //重量发送指令
                    _Serial.DiscardInBuffer();

                    byte[] buffer = new byte[8] {
                        0X1, 0X3, 0X0, 0X1, 0X0, 0X2, 0X95, 0XCB
                    };
                    _Serial.Write(buffer, 0, 8);

                    byte[] recvBuffer = new byte[16];

                    while (_Serial.BytesToRead < 9)
                    {
                        Thread.Sleep(10);
                    }
                    int i = _Serial.Read(recvBuffer, 0, 9);

                    if (recvBuffer[0] != 1 || recvBuffer[1] != 3)
                    {
                        StreamWriter sw8 = File.AppendText("D:\\HaiMing.txt");
                        string       w8  = string.Format("  读取串口重量错误返回  {0} {1} {2}  {3} {4} {5} {6} {7} {8}", recvBuffer[0], recvBuffer[1], recvBuffer[2], recvBuffer[3], recvBuffer[4], recvBuffer[5], recvBuffer[6], recvBuffer[7], recvBuffer[8]);
                        sw8.Write(w8);
                        sw8.Close();
                        continue;
                    }

                    StreamWriter sw3 = File.AppendText("D:\\HaiMing.txt");
                    string       w3  = string.Format("  读取串口重量返回  {0} {1} {2}  {3} {4} {5} {6} {7} {8} {9}", recvBuffer[0], recvBuffer[1], recvBuffer[2], recvBuffer[3], recvBuffer[4], recvBuffer[5], recvBuffer[6], recvBuffer[7], recvBuffer[8], recvBuffer[9]);
                    sw3.Write(w3);
                    sw3.Close();

                    weight = ((recvBuffer[3] << 8) | recvBuffer[4]);

                    StreamWriter sw9 = File.AppendText("D:\\HaiMing.txt");
                    string       w9  = "转化完成重量:::" + weight.ToString();
                    sw9.Write(w9);
                    sw9.Close();

                    // 计算重量
                    weight = weight * (float)0.01;
                    count  = (UInt32)((recvBuffer[5] << 8) | recvBuffer[6]);

                    StreamWriter swa = File.AppendText("D:\\HaiMing.txt");
                    string       wa  = "当前count : " + count.ToString() + "前一次count(last count)" + _lastCount.ToString();
                    swa.Write(wa);
                    swa.Close();

                    if (flag == false)
                    {
                        _lastCount = count;
                        flag       = true;
                        continue;
                    }

                    if (count == _lastCount)
                    {
                        Thread.Sleep(50);
                        continue;
                    }
                    else
                    {
                        _lastCount  = count;
                        _lastWeight = weight;
                        break;
                    }
                }

                // 最终数据回调
                if (ScaleWight != null)
                {
                    StreamWriter sw9 = File.AppendText("D:\\HaiMing.txt");
                    string       w9  = "   \n 最终数据回调 \n ";
                    sw9.Write(w9);
                    sw9.Close();

                    //不能置ling
                    //if (cnt <= 0)
                    //{
                    //    weight = 0;
                    //}

                    WeightEventArgs e = new WeightEventArgs(weight);
                    e.RealWeight   = true;
                    e._bNeedOutPut = true;
                    ScaleWight(this, e);
                    return(true);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("GetWight serial exception, " + e.Message);
            }
            return(false);
        }
Esempio n. 4
0
        /// <summary>
        /// TCP数据流回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OmStreamData(object sender, StreamEventArgs e)
        {
            byte[] data = e.Data;
            if (!Protocol.CheckResponse(data))
            {
                return;
            }

            /*
             * StringBuilder sb = new StringBuilder();
             * foreach (var b in data)
             * {
             *  sb.AppendFormat("{0:x2} ", b);
             * }
             * Console.WriteLine(sb.ToString());
             */

            byte[] weight       = null;
            byte[] code         = null;
            int    weightLenIdx = 0x05;

            if (weightLenIdx + 2 < data.Length)
            {
                // 解析重量
                int nSymbol   = (data[weightLenIdx + 1] == 0x2d) ? 1 : 0;
                int weightLen = Convert.ToInt32(data[weightLenIdx]) + nSymbol;
                if (weightLenIdx + weightLen + 1 < data.Length)
                {
                    weight = new byte[weightLen];
                    Array.Copy(data, weightLenIdx + 1, weight, 0, weightLen);

                    // 解析条码
                    int codeLenIndex = weightLenIdx + weightLen + 2;
                    if (codeLenIndex < data.Length)
                    {
                        int codeLen = Convert.ToInt32(data[codeLenIndex]);
                        if (codeLenIndex + codeLen + 1 < data.Length)
                        {
                            code = new byte[codeLen];
                            Array.Copy(data, codeLenIndex + 1, code, 0, codeLen);
                        }
                    }
                }
            }

            if (ScaleWight != null)
            {
                double val = 0.0d;
                if (weight != null)
                {
                    val = Convert.ToDouble(
                        System.Text.Encoding.Default.GetString(weight));
                }
                WeightEventArgs we = new WeightEventArgs(val);
                if (code != null)
                {
                    we.BarCode = System.Text.Encoding.Default.GetString(code);
                }
                we.RealWeight = true;

                ScaleWight(this, we);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// 实时获取当前揽件重量
        /// </summary>
        private void GetWight()
        {
            // 串口触发事件和线程退出事件
            WaitHandle[] handles = new WaitHandle[] { _WightWaitHandle, _ExitHandle };
            while (_bLooping)
            {
                int bWait = WaitHandle.WaitAny(handles);
                // 退出线程
                if (bWait == 1)
                {
                    break;
                }

                // 实时称重数据回调
                if (Interlocked.Read(ref _RealWightFlag) == 1)
                {
                    if (ScaleWight != null)
                    {
                        double tmp = Interlocked.CompareExchange(ref _RealWight, 0, 0);

                        WeightEventArgs e = new WeightEventArgs(tmp);
                        e.RealWeight = false;
                        ScaleWight(this, e);
                    }
                }


                if (Interlocked.Read(ref _SampleWeightFlag) == 1)
                {
                    StringBuilder _weightList = new StringBuilder();
                    for (int i = 0; i < _nSampleCountPerGroup; i++)
                    {
                        _weightList.Append("  ");
                        _weightList.Append(_SampleArray[i].ToString());
                    }
                    string weightss = _weightList.ToString();

                    // 计算样本平均值
                    Double avg = _SampleArray.Average();

                    // 排除最小电子称精度误差一下的重量
                    if (avg < 0.02 || Array.IndexOf(_SampleArray, 0.0) != -1)
                    {
                        // 重新取样运算
                        lock (_LockFlagObj)
                        {
                            Interlocked.CompareExchange(ref _SampleWeightFlag, 0, 1);
                            Array.Clear(_SampleArray, 0, _SampleArray.Length);
                        }

                        MvBarCode.MvBarCodeGlobalVar.Log.DebugFormat("实时重量:数值太小 return:{0}", weightss);
                        continue;
                    }

                    // 计算样本方差大小
                    Double sum = 0.0;
                    foreach (var x in _SampleArray)
                    {
                        sum += (x - avg) * (x - avg);
                    }

                    // 计算当前样本集合的方差值
                    Double variance = sum / _nSampleCountPerGroup;

                    // 校验样本的方差值和最大误差之间的大小关系
                    if (avg >= 0.03 /*kg*/ && variance <= _MaxDeviation)
                    {
                        lock (_LockFlagObj)
                        {
                            Interlocked.CompareExchange(ref _SampleFlag, 0, 1);
                            Interlocked.CompareExchange(ref _RealWightFlag, 0, 1);
                            Interlocked.CompareExchange(ref _SampleWeightFlag, 0, 1);
                            Array.Clear(_SampleArray, 0, _SampleArray.Length);
                        }

                        // 最终数据回调
                        if (ScaleWight != null)
                        {
                            WeightEventArgs e = new WeightEventArgs(avg);
                            e.RealWeight = true;
                            ScaleWight(this, e);
                        }
                    }
                    // 获取采样样本
                    else
                    {
                        lock (_LockFlagObj)
                        {
                            MvBarCode.MvBarCodeGlobalVar.Log.DebugFormat("实时重量:不符合精度要求数据:{0}", weightss);
                            Array.Clear(_SampleArray, 0, _SampleArray.Length);
                            Interlocked.CompareExchange(ref _SampleWeightFlag, 0, 1);
                        }
                    }
                }

                Thread.Sleep(1);
            }
        }