Пример #1
0
 public void Update()
 {
     if (!this.EasySerialPort.Connected)
     {
         this.CommunicationOK = ComCommunicationSts.ERROR;
     }
 }
Пример #2
0
 public int ReadValue(TimeSpan timeout, out string str)
 {
     str = "";
     if (this.EasySerialPort == null)
     {
         //AlarmServer.Instance.Fire(this, AlarmInfoSensors.ErrorBarcodeScannerState);
         this.CommunicationOK = ComCommunicationSts.ERROR;
         return(-1);
     }
     this.EasySerialPort.ReadDelimiter = '\n';
     this.EasySerialPort.isDelimiter   = true;
     for (int i = 0; i < 1; i++)
     {
         //this.EasySerialPort.SerialPort.ReceivedBytesThreshold = 29;
         ByteData reply = this.EasySerialPort.WriteAndGetReply(CmdReadValue, timeout);
         if (reply != null)
         {
             string temp = reply.ToString();
             temp = temp.Replace('\r', ' ');
             str  = temp.Trim(new char[] { ' ' });
             return(0);
         }
         Logger.DEFAULT.Info(" ReadValue reply is null");
     }
     str = "";
     //连续三次读取失败关闭读取,返回异常值
     this.EasySerialPort.WriteAndGetReply("LOFF\r\n", timeout);
     //AlarmServer.Instance.Fire(this, AlarmInfoSensors.ErrorBarcodeScannerRead);
     return(-1);
 }
Пример #3
0
 public bool GetTemp(out double result, int chanelNo)
 {
     try
     {
         if (this.ModbusMaster == null)
         {
             result = 0;
             return(false);
         }
         byte[] bytes = this.ModbusMaster.BuildReadMessage(this.SlaveAddr, (ushort)chanelNo, 2);
         this.EasySerialPort.SerialPort.DiscardInBuffer();
         ByteData data = this.EasySerialPort.WriteAndGetReply(bytes, TimeSpan.FromSeconds(1));
         if (data == null)
         {
             result = 0;
             return(false);
         }
         this.CommunicationOK = ComCommunicationSts.OK;
         string s1 = data.Bytes[3].ToString("X2");
         string s2 = data.Bytes[4].ToString("X2");
         result = Convert.ToInt32((s1 + s2), 16) * 0.1;
         return(true);
     }
     catch (Exception)
     {
         result = 0;
         return(false);
     }
 }
Пример #4
0
        public bool Connect(TimeSpan timeout)
        {
            if (this.EasySerialPort == null)
            {
                return(false);
            }
            if (!this.EasySerialPort.Open())
            {
                return(false);
            }
            CommunicationOK = ComCommunicationSts.OK;
            SerialPortAdapter adapter = new SerialPortAdapter(this.EasySerialPort.SerialPort);

            this.ModbusMaster = ModbusSerialMaster.CreateRtu(adapter);
            return(true);
        }
Пример #5
0
        public int  Print(TimeSpan timeout, out string value)
        {
            value = string.Empty;

            if (this.EasySerialPort == null && !this.EasySerialPort.Connected)
            {
                AlarmServer.Instance.Fire(this, AlarmInfoSensors.ErrorScaleState);
                //this.CommunicationOK = false;
                this.CommunicationOK = ComCommunicationSts.ERROR;
                return(-1);//串口为空或未打开
            }
            //返回22个字节   N         0.0000 g  \CR\LF
            //this.EasySerialPort.SerialPort.ReceivedBytesThreshold = 22;
            ByteData reply = this.EasySerialPort.WriteAndGetReply(this.PrintCmd, timeout);

            if (reply == null)
            {
                AlarmServer.Instance.Fire(this, AlarmInfoSensors.ErrorScaleReadNull);
                //this.CommunicationOK = false;
                this.CommunicationOK = ComCommunicationSts.ERROR;
                return(-1);//空值 表示天平不稳定
            }

            //this.CommunicationOK = true;
            this.CommunicationOK = ComCommunicationSts.OK;
            string data = reply.ToString();

            Debug.WriteLine(data);
            try
            {
                if (!data.Contains('\r') && data.Length != 21)
                {
                    AlarmServer.Instance.Fire(this, AlarmInfoSensors.ErrorScaleReadIncomplete);
                    return(1);                                          //不完整的值 表示天平不稳定
                }
                string id     = data.Substring(0, 5).Replace(" ", "");  //ID码
                string result = data.Substring(6, 10).Replace(" ", ""); //结果数据
                string unit   = data.Substring(17, 2).Replace(" ", ""); //单位
                value = result.Trim();
                return(0);                                              //执行成功,获取正常值
            }
            catch
            {
                AlarmServer.Instance.Fire(this, AlarmInfoSensors.ErrorScaleReadIncomplete);
                return(3);
            }
        }
Пример #6
0
        /// <summary>
        /// 设置气压值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool SetValue(ushort value)
        {
            if (this.EasySerialPort == null)
            {
                return(false);
            }
            string cmd = string.Format("L{0} {1}F", this.Channel, value);
            //string cmd = "L" + this.Channel.ToString() + value.ToString().PadLeft(4, '0') + "F";
            bool rtn = this.EasySerialPort.Write(cmd);

            if (rtn)
            {
                this.CurrentValue = value;
            }
            this.CommunicationOK = rtn ? ComCommunicationSts.OK : ComCommunicationSts.ERROR;
            return(rtn);
        }
Пример #7
0
        /// <summary>
        /// 读取测高值
        /// </summary>
        /// <param name="timeout"></param>
        /// <param name="value"></param>
        /// <returns>0:成功|-1:通信错误|1:转换错误|2:超量程</returns>
        public int ReadValue(TimeSpan timeout, out double value)
        {
            value = 0;
            if (this.EasySerialPort == null)
            {
                AlarmServer.Instance.Fire(this, AlarmInfoSensors.ErrorLaserState);
                this.CommunicationOK = ComCommunicationSts.ERROR;
                Log.Dprint("laser return -1");
                return(-1);
            }
            try
            {
                this.EasySerialPort.SerialPort.ReceivedBytesThreshold = 18;//例如:SR,00,038,±**.***\r\n
                //读取测量值正确的返回格式:SR,ID编号,数据编号,±**.***CR LF
                ByteData reply = this.EasySerialPort.WriteAndGetReply(CmdReadValue + Environment.NewLine, timeout);
                if (reply == null)
                {
                    AlarmServer.Instance.Fire(this, AlarmInfoSensors.ErrorLaserState);
                    this.CommunicationOK = ComCommunicationSts.ERROR;
                    Log.Dprint("laser return -1");
                    return(-1);
                }

                this.CommunicationOK = ComCommunicationSts.OK;
                if (!DealWithReadValue(reply.ToString(), out value))//解析数据
                {
                    AlarmServer.Instance.Fire(this, AlarmInfoSensors.ErrorLaserRead);
                    Log.Dprint("laser return 1");
                    return(1);
                }

                if (value <= this.ErrorValue)
                {
                    Log.Dprint("laser return 2");
                    return(2);
                }
                Log.Dprint("laser height :" + value.ToString());
                return(0);
            }
            catch (Exception ex)
            {
                AlarmServer.Instance.Fire(this, AlarmInfoSensors.ErrorLaserRead.AppendMsg(ex.Message));
                Log.Dprint("laser return 2");
                return(1);
            }
        }
Пример #8
0
        public bool SetValue(ushort value)
        {
            if (this.EasySerialPort == null)
            {
                return(false);
            }

            string valueTo16 = GetValueTo16String(value).ToUpper();

            string baseCmd  = "%01#WDD1223012230";
            string valueCmd = string.Format("{0}{1}", baseCmd, valueTo16);
            string cmd      = string.Format("{0}{1}\r\n", valueCmd, GetBCCCode(System.Text.Encoding.Default.GetBytes(valueCmd)).ToString("X"));
            bool   rtn      = this.EasySerialPort.Write(cmd.ToUpper());

            if (rtn)
            {
                this.CurrentValue = value;
            }
            this.CommunicationOK = rtn ? ComCommunicationSts.OK : ComCommunicationSts.ERROR;
            return(rtn);
        }
Пример #9
0
        /// <summary>
        /// 打印结果
        /// </summary>
        /// <param name="timeout"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public int Print(TimeSpan timeout, int readTimes, out double value)
        {
            value = 0.0;
            if (this.EasySerialPort == null && !this.EasySerialPort.Connected)
            {
                AlarmServer.Instance.Fire(this, AlarmInfoSensors.ErrorScaleState);
                //this.CommunicationOK = false;
                this.CommunicationOK = ComCommunicationSts.ERROR;
                return(-1);//串口为空或未打开
            }
            double weightMin  = 0;
            double weightMax  = 0;
            double perValue   = 0;
            double weightSum  = 0;
            int    nullCount  = 0;
            int    sCount     = 0;
            int    inComplete = 0;
            int    readErr    = 0;
            int    ReadCnt    = 0;

            if (readTimes < 3)
            {
                readTimes = 5;
            }
            List <double> results        = new List <double>();
            double        mean           = 0;
            double        variance       = 0;
            double        specifiedValue = 0;
            Stopwatch     startTime      = new Stopwatch();

            startTime.Start();
            //返回22个字节   N         0.0000 g  \CR\LF
            //this.EasySerialPort.SerialPort.ReceivedBytesThreshold = 22;
            ByteData reply = null;

            while (true)
            {
                if (this.isStop)
                {
                    Logger.DEFAULT.Debug(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    this.isStop = false;
                    return(4);
                }
                perValue = 0;
                if (startTime.ElapsedMilliseconds > (timeout.TotalMilliseconds * (readTimes + 2)))
                {
                    inComplete = 3;
                    break;
                }
                //每次读取串口前延时
                Thread.Sleep(this.Prm.SingleReadDelay);
                reply = this.EasySerialPort.WriteAndGetReply(this.PrintCmd, timeout);
                if (reply == null)
                {
                    nullCount++;
                    perValue = 0;
                    continue;
                }
                else//reply not null
                {
                    try
                    {
                        string str = reply.ToString();
                        int    rtn = GetResult(str, out perValue);
                        Logger.DEFAULT.Debug("rtn:" + rtn + "  current Weight:" + perValue);
                        if (rtn == 2)//超重
                        {
                            sCount++;
                            break;
                        }
                        else if (rtn == 1)//不完整
                        {
                            inComplete++;
                            continue;
                        }
                        results.Add(perValue);
                        if (results.Count >= 2)
                        {
                            mean           = MathUtils.CalMean(results.ToArray());
                            variance       = MathUtils.CalVariance(results.ToArray());
                            specifiedValue = mean * 0.1;
                            Logger.DEFAULT.Debug("variance:" + variance + "  specifiedValue:" + specifiedValue);
                            if (variance > specifiedValue)
                            {
                                results.Remove(perValue);
                                continue;
                            }
                        }
                        if (results.Count == readTimes)
                        {
                            value   = MathUtils.CalMean(results.ToArray());
                            ReadCnt = results.Count;
                            break;
                        }
                        //if (ReadCnt == 0)
                        //{
                        //    weightMax = perValue;
                        //    weightMin = perValue;
                        //}
                        //if (weightMax < perValue)
                        //{
                        //    weightMax = perValue;
                        //}
                        //if (weightMin > perValue)
                        //{
                        //    weightMin = perValue;
                        //}

                        //weightSum += perValue;
                        //ReadCnt++;
                        //if (ReadCnt == readTimes)
                        //{
                        //    value = (weightSum - weightMax - weightMin) / (ReadCnt - 2);
                        //    break;
                        //}
                    }
                    catch
                    {
                        readErr++;
                    }
                }
            }
            startTime.Stop();
            if (ReadCnt < readTimes) // 达到指定读取次数不报错
            {
                if (sCount > 0)
                {
                    AlarmServer.Instance.Fire(this, AlarmInfoSensors.ErrorScaleReadOverrun);
                    return(2); //超重或者失重
                }
                else if (inComplete > 2)
                {
                    AlarmServer.Instance.Fire(this, AlarmInfoSensors.ErrorScaleReadIncomplete);
                    return(1);//不完整的值 表示天平不稳定
                }
                else if (readErr > 2)
                {
                    AlarmServer.Instance.Fire(this, AlarmInfoSensors.ErrorScaleReadIncomplete);
                    return(3);
                }
            }

            //this.CommunicationOK = true;
            this.CommunicationOK = ComCommunicationSts.OK;
            Logger.DEFAULT.Debug("Read Weight:" + value);
            return(0);
        }
Пример #10
0
        public int Print(TimeSpan timeout, int readTimes, out double value)
        {
            value = 0;
            if (this.EasySerialPort == null)
            {
                AlarmServer.Instance.Fire(this, AlarmInfoSensors.ErrorScaleState);
                this.CommunicationOK = ComCommunicationSts.ERROR;
                return(-1);
            }
            double    weightMin  = 0;
            double    weightMax  = 0;
            double    perValue   = 0;
            double    weightSum  = 0;
            int       nullCount  = 0;
            int       sCount     = 0;
            int       inComplete = 0;
            int       readErr    = 0;
            int       ReadCnt    = 0;
            ByteData  reply      = null;
            Stopwatch startTime  = new Stopwatch();

            startTime.Start();
            if (readTimes < 3)
            {
                readTimes = 5;
            }
            while (true)
            {
                if (startTime.ElapsedMilliseconds > (timeout.TotalMilliseconds * (readTimes + 2)))
                {
                    inComplete = 3;
                    break;
                }
                reply = this.EasySerialPort.WriteAndGetReply(this.PrintCmd, TimeSpan.FromSeconds(1));
                if (reply == null)
                {
                    nullCount++;
                    perValue = 0;
                }
                else
                {
                    try
                    {
                        //"S S   200.0000 g"
                        string str = reply.ToString();
                        string s   = str.Substring(0, 3);
                        if (s != "S S")
                        {
                            sCount++;
                            break;
                        }
                        //减去前面的S S的长度和后面g\r的长度
                        s        = str.Substring(s.Length, str.Length - s.Length - 2).Trim();
                        perValue = double.Parse(s) * 1000;
                    }
                    catch
                    {
                        readErr++;
                    }
                }
                if (ReadCnt == 0)
                {
                    weightMax = perValue;
                    weightMin = perValue;
                }
                if (weightMax < perValue)
                {
                    weightMax = perValue;
                }
                if (weightMin > perValue)
                {
                    weightMin = perValue;
                }
                weightSum += perValue;
                ReadCnt++;
                if (ReadCnt == readTimes)
                {
                    value = (weightSum - weightMax - weightMin) / (ReadCnt - 2);
                    break;
                }
            }
            startTime.Stop();
            if (ReadCnt < readTimes) // 达到指定读取次数不报错
            {
                if (sCount > 0)
                {
                    AlarmServer.Instance.Fire(this, AlarmInfoSensors.ErrorScaleReadOverrun);
                    return(2); //超重或者失重
                }
                else if (nullCount > 2)
                {
                    AlarmServer.Instance.Fire(this, AlarmInfoSensors.ErrorScaleReadNull);
                    //this.CommunicationOK = false;
                    this.CommunicationOK = ComCommunicationSts.ERROR;
                    return(-1);//空值 表示天平不稳定
                }
                else if (inComplete > 2)
                {
                    AlarmServer.Instance.Fire(this, AlarmInfoSensors.ErrorScaleReadIncomplete);
                    return(1);//不完整的值 表示天平不稳定
                }
                else if (readErr > 2)
                {
                    AlarmServer.Instance.Fire(this, AlarmInfoSensors.ErrorScaleReadIncomplete);
                    return(3);
                }
            }
            //this.CommunicationOK = true;
            this.CommunicationOK = ComCommunicationSts.OK;
            Log.Dprint("Read Weight:" + value);
            return(0);
        }
Пример #11
0
        /// <summary>
        /// 读取测高值
        /// </summary>
        /// <param name="timeout"></param>
        /// <param name="value"></param>
        /// <returns>0:成功|-1:通信错误|1:转换错误|2:超量程</returns>
        public int ReadValue(TimeSpan timeout, out double value)
        {
            value = 0;
            try
            {
                this.EasySerialPort.SerialPort.ReceivedBytesThreshold = 9;//"28.0000"
                ByteData reply = this.EasySerialPort.WriteAndGetReply(CmdReadValue, timeout);
                if (reply == null)
                {
                    AlarmServer.Instance.Fire(this, AlarmInfoSensors.ErrorLaserState);
                    this.CommunicationOK = ComCommunicationSts.ERROR;
                    Log.Dprint("laser return -1");
                    return(-1);
                }

                this.CommunicationOK = ComCommunicationSts.OK;
                int len = reply.Bytes.Length;
                if (len < 2)
                {
                    AlarmServer.Instance.Fire(this, AlarmInfoSensors.ErrorLaserRead);
                    Log.Dprint("laser return 1,error data:" + reply.ToString());
                    return(1);
                }
                //判断起始标志STX
                if (reply.Bytes[0] != STX)
                {
                    AlarmServer.Instance.Fire(this, AlarmInfoSensors.ErrorLaserRead);
                    Log.Dprint("laser return 1,error data:" + reply.ToString());
                    return(1);
                }
                //结束标志ETX有时存在有时不存在
                byte[] data = null;
                if (reply.Bytes[len - 1] == ETX)
                {
                    data = new byte[len - 2];
                }
                else
                {
                    data = new byte[len - 1];
                }
                //转换结果数据
                for (int i = 0; i < data.Length; i++)
                {
                    data[i] = reply.Bytes[i + 1];
                }
                string dataString = Encoding.UTF8.GetString(data);
                Log.Print("laser height :" + dataString);
                double result = double.Parse(dataString);

                if (result >= this.ErrorValue)
                {
                    Log.Dprint("laser return 1,error data:" + reply.ToString());
                    return(2);
                }

                //取反是为了和基恩士激光尺统一
                value = -result;

                return(0);
            }
            catch (Exception ex)
            {
                AlarmServer.Instance.Fire(this, AlarmInfoSensors.ErrorLaserRead.AppendMsg(ex.Message));
                Log.Dprint("laser return 1");
                return(1);
            }
        }
Пример #12
0
 public void Update()
 {
     this.CommunicationOK = ComCommunicationSts.DISABLE;
 }
Пример #13
0
 public int ReadValue(TimeSpan timeout, out double value)
 {
     value = 0;
     this.CommunicationOK = ComCommunicationSts.DISABLE;
     return(0);
 }
Пример #14
0
 public void Update()
 {
     this.CommunicationOK = this.EasySerialPort.Connected ? ComCommunicationSts.OK : ComCommunicationSts.ERROR;
 }