예제 #1
0
        /// <summary>
        /// 取消零点偏移
        /// </summary>
        /// <param name="timeout">超时</param>
        /// <returns></returns>
        public bool CancelZeroOffset(TimeSpan timeout)
        {
            if (this.EasySerialPort == null)
            {
                return(false);
            }
            try
            {
                // SW,00,002\CR\LF : 11个字节
                this.EasySerialPort.SerialPort.ReceivedBytesThreshold = 11;
                ByteData reply = this.EasySerialPort.WriteAndGetReply(this.CmdCancelZeroOffset + ",0" + Environment.NewLine, timeout);
                if (reply == null)
                {
                    return(false);
                }
                if (string.Equals(reply.ToString().Substring(0, 9), CmdCancelZeroOffset))
                {
                    reply = this.EasySerialPort.WriteAndGetReply(this.CmdCancelZeroOffset + ",1" + Environment.NewLine, timeout);
                    if (reply == null)
                    {
                        return(false);
                    }
                    if (string.Equals(reply.ToString().Substring(0, 9), CmdCancelZeroOffset))
                    {
                        return(true);
                    }
                }

                return(false);
            }
            catch (Exception)
            {
                return(false);
            }
        }
예제 #2
0
 /// <summary>
 /// 读取通讯模块与探测器的连接状态
 /// </summary>
 /// <param name="timeout">超时</param>
 /// <param name="isConnected">是否连接</param>
 /// <returns></returns>
 public bool ReadConnectState(TimeSpan timeout)
 {
     if (this.EasySerialPort == null)
     {
         return(false);
     }
     try
     {
         //例如 返回数据:SR,00,195,****\r\n  触发接收事件输入缓冲区为16个字节
         this.EasySerialPort.SerialPort.ReceivedBytesThreshold = 16;
         //读取状态正确返回格式:****,將“*”表示為“0~9”的任意一個數字,
         ByteData reply = this.EasySerialPort.WriteAndGetReply(this.CmdConnectState + Environment.NewLine, timeout);
         if (reply == null || reply.ToString().Count() != 16)
         {
             return(false);
         }
         string[] splitArray = reply.ToString().Split(',');
         if (splitArray[0] == "ER")
         {
             return(false);
         }
         if (splitArray[3].Substring(0, 4) == "0001")//0001代表IL-030
         {
             return(true);
         }
         return(false);
     }
     catch (Exception)
     {
         return(false);
     }
 }
예제 #3
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);
 }
예제 #4
0
        private async void btnSend_Click(object sender, EventArgs e)
        {
            ByteData data = null;
            string   cmd  = this.txtSend.Text;
            await Task.Factory.StartNew(() =>
            {
                data = this.currEasySerialPort?.WriteAndGetReply(cmd, TimeSpan.FromSeconds(1));
            });

            this.txtReceive.Text = data?.ToString();
        }
예제 #5
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);
            }
        }
예제 #6
0
        public bool Zero()
        {
            if (this.EasySerialPort == null)
            {
                return(false);
            }
            ByteData reply = this.EasySerialPort.WriteAndGetReply(this.ZeroCmd, TimeSpan.FromSeconds(1));

            if (reply == null)
            {
                return(false);
            }

            try
            {
                return(reply.ToString().Substring(0, 3) == "Z A");
            }
            catch
            {
                return(false);
            }
        }
예제 #7
0
 public int Read(out double value)
 {
     value = 0.0;
     if (this.EasySerialPort == null || !this.EasySerialPort.Connected)
     {
         AlarmServer.Instance.Fire(this, AlarmInfoSensors.SerialPortOpenAlarm);
         return(-1);
     }
     try
     {
         ByteData reply    = this.EasySerialPort.Reply;
         string   replyStr = reply.ToString();
         //this.resArray = reply.Bytes.Take(9).ToArray();
         //数据解析  共
         value = double.Parse(replyStr);
         return(0);
     }
     catch
     {
         return(2);
     }
 }
예제 #8
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);
        }
예제 #9
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);
        }
예제 #10
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);
            }
        }