文档章节

这脚本写得真帅,记录一下;很少这么佩服自己

o
 osc_z1hvg4cu
发布于 2018/04/24 14:48
字数 1510
阅读 10
收藏 0

精选30+云产品,助力企业轻松上云!>>>

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using UNI.MES.Core;
using UNI.MES.Services;

namespace UNI_IPC_MES.modules
{
    /// <summary>
    /// 气密检查流程
    /// </summary>
    public class AirtightnessWorkFlow : ModuleBaseFlow
    {
        //线程池,负责存取已扫条码到出库记录
        private CoreThreadPool pool = new CoreThreadPool();
        //检测通过的数据
        private Queue<AirtghtnessResult> scanQueue = new Queue<AirtghtnessResult>();

        private Regex codeRegex = new Regex(@"^C\d{8}$");
        protected IModuleFlowFormService FlowFormService { get; private set; }
        /// <summary>
        /// Com端口的集合
        /// </summary>
        protected Dictionary<string, object> ComPorts = new Dictionary<string, object>();
        /// <summary>
        /// 缓存区集合,每个串口使用自己的缓冲区
        /// </summary>
        protected Dictionary<string, List<byte>> revBuffer = new Dictionary<string, List<byte>>();
        public AirtightnessWorkFlow(IModuleFlowFormService fromService)
        {
            FlowFormService = fromService;
        }

        /// <summary>
        /// 扫码条码处理
        /// </summary>
        /// <param name="data"></param>
        public override void OnExecScanReceiving(string data)
        {
            var temp = data.Split("|".ToCharArray());
            string flag = temp.LastOrDefault();
            data = temp.FirstOrDefault();
            if (data.IndexOf("NR") != -1)
            {
                string msg = string.Format("{0}道读取条码错误,NOREAD!", flag);
                RaiseException(this, msg, new object[] { data });
            }
            if (codeRegex.IsMatch(data))
            {
                RaiseInfo(this, string.Format("{0}道读取到条码[{1}]。", flag, data));
                using (var db = new MESDb())
                {
                    string cmd = string.Format("select line from hkwsAll with(nolock) where code3='{0}'", data);
                    string lineNo = db.Database.SqlQuery<string>(cmd).FirstOrDefault();
                    if (!lineNo.Equals(flowContext.LineNo))
                    {
                        string msg = string.Format("{0}道校验条码错误,条码{1}上一站测试未通过,或者非本线产品!", flag, data);
                        RaiseException(this, msg, new object[] { data });
                    }
                }
                if (flag.Equals("A"))
                {
                    flowContext.L1Code = data;
                }
                if (flag.Equals("B"))
                {
                    flowContext.L2Code = data;
                }
                RaiseCustomEvent("StartNewTest", "", flag, data);
            }
            else
            {
                string msg = string.Format("{0}道读取条码错误,编码规则错误!条码:{1}", flag, data);
                RaiseException(this, msg, new object[] { data });
            }
        }
        /// <summary>
        /// 脚本初始化
        /// </summary>
        protected override void BeforeWorkInit()
        {
            ComPorts.Clear();
            revBuffer.Clear();
            string path = AppDomain.CurrentDomain.BaseDirectory + "portname.txt";
            using (var sr = new StreamReader(path, false))
            {
                string str = "";
                while ((str = sr.ReadLine()) != null)
                {
                    #region 读取每一行
                    if (str.IndexOf("线体=") != -1)    //找到txt中通道1的各行
                    {
                        string[] mm = Regex.Split(str, "=", RegexOptions.IgnoreCase);   //通过txt中_分隔符取出文件名信息,和超时信息 
                        flowContext.LineNo = mm[1];
                    }
                    else if (str.IndexOf("上层条码a=") != -1)    //找到txt中通道1的各行
                    {
                        string[] mm = Regex.Split(str, "=", RegexOptions.IgnoreCase);   //通过txt中_分隔符取出文件名信息,和超时信息 
                        ComPorts.Add("COM_Up_A", new SerialPort()
                        {
                            PortName = mm[1],
                            BaudRate = 9600,
                            DataBits = 8,
                            StopBits = StopBits.One,
                            //ParityReplace = 63,
                            ReadBufferSize = 4096,
                            WriteBufferSize = 2048
                        });
                    }
                    else if (str.IndexOf("上层条码b=") != -1)    //找到txt中通道1的各行
                    {
                        string[] mm = Regex.Split(str, "=", RegexOptions.IgnoreCase);   //通过txt中_分隔符取出文件名信息,和超时信息 
                        ComPorts.Add("COM_Up_B", new SerialPort()
                        {
                            PortName = mm[1],
                            BaudRate = 9600,
                            DataBits = 8,
                            StopBits = StopBits.One,
                            //ParityReplace = 63,
                            ReadBufferSize = 4096,
                            WriteBufferSize = 2048
                        });
                    }
                    else if (str.IndexOf("下层条码a=") != -1)    //找到txt中通道1的各行
                    {
                        string[] mm = Regex.Split(str, "=", RegexOptions.IgnoreCase);   //通过txt中_分隔符取出文件名信息,和超时信息 
                        ComPorts.Add("COM_Down_A", new SerialPort()
                        {
                            PortName = mm[1],
                            BaudRate = 9600,
                            DataBits = 8,
                            StopBits = StopBits.One,
                            //ParityReplace = 63,
                            ReadBufferSize = 4096,
                            WriteBufferSize = 2048
                        });
                    }
                    else if (str.IndexOf("下层条码b=") != -1)    //找到txt中通道1的各行
                    {
                        string[] mm = Regex.Split(str, "=", RegexOptions.IgnoreCase);   //通过txt中_分隔符取出文件名信息,和超时信息 
                        ComPorts.Add("COM_Down_B", new SerialPort()
                        {
                            PortName = mm[1],
                            BaudRate = 9600,
                            DataBits = 8,
                            StopBits = StopBits.One,
                            //ParityReplace = 63,
                            ReadBufferSize = 4096,
                            WriteBufferSize = 2048
                        });
                    }
                    #endregion
                }
                sr.Close();
            }
            #region 启动参数检测
            if (string.IsNullOrEmpty(flowContext.WorkStation))
            {
                RaiseException(this, "请联系管理员设置工位或者线号!");
            }
            if (string.IsNullOrEmpty(flowContext.Operator))
            {
                RaiseException(this, "请点击'切换员工'进行身份验证!");
            }
            #endregion
            #region 打开全部COM口
            foreach (var com in ComPorts)
            {
                var sp = com.Value as SerialPort;
                try
                {
                    sp.Open();
                    sp.DataReceived += Sp_DataReceived;
                    revBuffer.Add(com.Key, new List<byte>(4096));
                }
                catch (Exception ex)
                {
                    RaiseException(this, ex.Message);
                }
            }
            #endregion
            pool.Exceute += Pool_Exceute;
            pool.Start();
            flowContext.L1CountEx = 0;
            RaiseInfo(this, "程序初始化完成,准备就绪请开始作业!");
        }
        /// <summary>
        /// 接收数据逻辑,是扫描枪的调用OnExecScanReceiving,是PLC的消息调用OnExecDataReceived
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            var sp = sender as SerialPort;
            var dict = ComPorts.SingleOrDefault(port => port.Value == sp);
            if (sp.BytesToRead == 0)
            {
                revBuffer[dict.Key].Clear();
                return;
            }
            byte[] buffer = new byte[sp.BytesToRead];
            int numofbyte = sp.Read(buffer, 0, buffer.Length);
            if (numofbyte > 0)
            {
                revBuffer[dict.Key].AddRange(buffer);
            }
            string revMessage = Encoding.UTF8.GetString(revBuffer[dict.Key].ToArray()).Trim();
            if (dict.Key.IndexOf("Up") != -1 && revMessage.Length > 8)
            {
                string[] temp = revMessage.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                string message = temp.FirstOrDefault().Length > 9 ? temp.FirstOrDefault().Substring(0, 9) : temp.FirstOrDefault();
                FrameAppContext.GetInstance().AppLogger.Info(string.Format("收到扫描枪数据:[{0}]", message));
                OnExecScanReceiving(message + "|" + dict.Key.Split("_".ToCharArray()).LastOrDefault());
                revBuffer[dict.Key].Clear();
            }
            if (dict.Key.IndexOf("Down") != -1 && revMessage.Contains("OK"))
            {
                FrameAppContext.GetInstance().AppLogger.Info(string.Format("收到PLC数据:[{0}]", revMessage));
                OnExecDataReceived(revMessage + "|" + dict.Key.Split("_".ToCharArray()).LastOrDefault());
                revBuffer[dict.Key].Clear();
            }
            //try
            //{
            //    if (sp.BytesToRead == 0)
            //    {
            //        revBuffer.Clear();
            //        return;
            //    }
            //    byte[] buffer = new byte[sp.BytesToRead];
            //    int numofbyte = sp.Read(buffer, 0, buffer.Length);
            //    if (numofbyte > 0)
            //    {
            //        revBuffer.AddRange(buffer);
            //    }
            //    string line = Encoding.UTF8.GetString(revBuffer.ToArray()).Trim();
            //    if (line.Length > 8)
            //    {
            //        FrameAppContext.GetInstance().AppLogger.Info(string.Format("收到数据:[{0}]", line));
            //    }
            //    回车换行符是结束标志
            //    if (revBuffer)
            //    {
            //        var dict = ComPorts.SingleOrDefault(port => port.Value == sp);
            //        if (dict.Key.IndexOf("Up") != -1)
            //        {
            //            if (line.Length < 11)
            //                return;
            //            string message = line.Substring(0, line.IndexOf(Environment.NewLine));
            //            FrameAppContext.GetInstance().AppLogger.Info(string.Format("收到扫描枪数据:[{0}]", message));
            //            OnExecScanReceiving(message + "|" + dict.Key.Split("_".ToCharArray()).LastOrDefault());
            //        }
            //        if (dict.Key.IndexOf("Down") != -1)
            //        {
            //            string message = line.Substring(0, line.IndexOf(Environment.NewLine));
            //            FrameAppContext.GetInstance().AppLogger.Info(string.Format("收到PLC数据:[{0}]", message));
            //            OnExecDataReceived(message + "|" + dict.Key.Split("_".ToCharArray()).LastOrDefault());
            //        }
            //    }
            //}
            //catch (Exception ex)
            //{
            //    FrameAppContext.GetInstance().AppLogger.Error(ex);
            //}
        }
        /// <summary>
        /// 执行数据库操作
        /// </summary>
        /// <param name="obj"></param>
        private void Pool_Exceute(object obj)
        {
            if (obj is AirtghtnessResult && scanQueue.Contains(obj))
            {
                AirtghtnessResult result = scanQueue.Peek();
                using (var db = new MESDb())
                {
                    try
                    {
                        string cmd = string.Format(@"update hkwsAll set code4='{0}',name4='{1}',time4='{2}',fth=1,Res='成功' where code3='{3}'",
                                                                      result.Barcode, result.Oper, result.ResultTime, result.Barcode);
                        db.Database.ExecuteSqlCommand(cmd);
                    }
                    finally
                    {
                        scanQueue.Dequeue();
                    }
                }
            }
            System.Threading.Thread.Sleep(100);
        }
        /// <summary>
        /// 接收到PLC返回的数据
        /// </summary>
        /// <param name="data"></param>
        protected void OnExecDataReceived(string data)
        {
            var temp = data.Split("|".ToCharArray());
            string flag = temp.LastOrDefault();
            data = temp.FirstOrDefault();
            string barcode = string.Empty;
            if (flag.Equals("A"))
                barcode = flowContext.L1Code;
            if (flag.Equals("B"))
                barcode = flowContext.L2Code;
            data.Replace("|" + flag, "");
            #region 获取检测结果
            byte[] buffer = Encoding.UTF8.GetBytes(data);
            int mol = 0;
            for (int i = 0; i < buffer.Length; i++)
            {
                if (buffer[i] == 0x6c)
                {
                    mol = i;
                }
            }
            byte[] xlv = new byte[9];
            xlv[0] = buffer[mol - 10];
            xlv[1] = buffer[mol - 9];
            xlv[2] = buffer[mol - 8];
            xlv[3] = buffer[mol - 7];
            xlv[4] = buffer[mol - 6];
            xlv[5] = buffer[mol - 5];
            xlv[6] = buffer[mol - 4];
            xlv[7] = buffer[mol - 3];
            xlv[8] = buffer[mol - 2];
            if (flag.Equals("A"))
                flowContext.Parameter1 = Encoding.UTF8.GetString(xlv);
            if (flag.Equals("B"))
                flowContext.Parameter2 = Encoding.UTF8.GetString(xlv);
            #endregion
            AirtghtnessResult result = new AirtghtnessResult()
            {
                Barcode = barcode,
                LineNo = flowContext.LineNo,
                Oper = flowContext.Operator,
                ResultTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
            };
            data.Replace("NOK","NG");
            //测试不通过
            if (data.Contains("NG"))
            {
                if (flag.Equals("A"))
                    flowContext.L1Code = string.Empty;
                if (flag.Equals("B"))
                    flowContext.L2Code = string.Empty;
                RaiseCustomEvent("DataReceived", flag, "NG", string.Join("|", new string[] { barcode, data }));
            }
            else if (data.Contains("OK"))
            {
                scanQueue.Enqueue(result);
                pool.Post(result);
                RaiseCustomEvent("DataReceived", flag, "OK", string.Join("|", new string[] { barcode, data }));
                //计数加1
                flowContext.L1CountEx++;
            }
            else
            {
                if (flag.Equals("A"))
                    flowContext.L1Code = string.Empty;
                if (flag.Equals("B"))
                    flowContext.L2Code = string.Empty;
                RaiseCustomEvent("DataReceived", flag, "NG", string.Join("|", new string[] { barcode, data }));
            }
        }
        /// <summary>
        /// 停止作业时的检查
        /// </summary>
        protected override void StopWorkCheck()
        {
            if (scanQueue.Count > 0)
            {
                var list = scanQueue.ToArray();
                if (list != null)
                {
                    foreach (var dict in list)
                    {
                        pool.Post(dict);
                    }
                }
                RaiseException(this, string.Format("还有已经检测的条码没有保存到数据库,请重试!"));
            }
        }
        /// <summary>
        /// 结束作业
        /// </summary>
        public override void ExecEndWork()
        {
            pool.Stop();
            foreach (var sp in ComPorts)
            {
                var com = sp.Value as SerialPort;
                com.Close();
            }
            ComPorts.Clear();
            base.ExecEndWork();
        }
        /// <summary>
        /// 异常提醒方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="message"></param>
        /// <param name="args"></param>
        protected override void RaiseException(object sender, string message, params object[] args)
        {
            OnFeedInfo(sender, new Exception(message), message, MessageTipsLevel.Error);
            FlowFormService.ShowErrorAlertNotityForm(1000, 1888, message);
        }
    }
}

虽然解决了别人老程序崩溃引起产线停线的事情,但是领导并没有高看我。哎!老程序是个菜鸟写的,他反反复复改了好几版还是像个DEMO。以前正常用了好久,刚开始产线工人还说我这个麻烦些。

o
粉丝 0
博文 500
码字总数 0
作品 0
私信 提问
加载中
请先登录后再评论。
这脚本写得真帅,记录一下;很少这么佩服自己

using System;using System.Collections.Generic;using System.IO;using System.IO.Ports;using System.Linq;using System.Text;using System.Text.RegularExpressions;using UNI.MES.Core;u......

数据酷软件
2018/04/24
0
0
你以为你很优秀,但却面试屡屡失败?

二哥,最近我在参加面试,但遇到了一个很尴尬的现实:我以为自己很优秀,但面试官好像对我有偏见,面试屡屡失败,现在有点心慌了,我该怎么办呢? 以上是读者小利问我的一个问题,我觉得挺值...

osc_o60il3e6
04/16
3
0
你以为你很优秀,但却面试屡屡失败?

二哥,最近我在参加面试,但遇到了一个很尴尬的现实:我以为自己很优秀,但面试官好像对我有偏见,面试屡屡失败,现在有点心慌了,我该怎么办呢? 以上是读者小利问我的一个问题,我觉得挺值...

沉默王二
03/27
0
0
年度总结 是不公平和智障的产物

双蛋快乐! 「 又到年底了? 」 上一篇文章「 对不起,我不加班 」得到了很多留言和认可,甚至好多人留言说:看了你的文章让我彻底彻悟了,原来我一直在浪费时间和生命。 我觉得这个就有点夸...

xllily_11
2017/12/29
0
0
年度总结 是不公平和智障的产物

双蛋快乐! 「 又到年底了? 」 上一篇文章「 对不起,我不加班 」得到了很多留言和认可,甚至好多人留言说:看了你的文章让我彻底彻悟了,原来我一直在浪费时间和生命。 我觉得这个就有点夸...

小北哥哥和北妈
2017/12/29
0
0

没有更多内容

加载失败,请刷新页面

加载更多

为什么从HBase的0.96版本开始,舍弃了-ROOT-文件?

HBase结构的读写流程 (1). HBase0.96版本之前: (2). HBase0.96开始: a. 当客户端获取到.meta文件的位置之后,会缓存.meta.文件的位置 b. 客户端还会缓存HRegion的位置 -ROOT-存在的意义: ...

其乐m
49分钟前
18
0
volatile关键字对 - What is the volatile keyword useful for

问题: At work today, I came across the volatile keyword in Java. 今天的工作中,我遇到了Java中的volatile关键字。 Not being very familiar with it, I found this explanation: 不太熟......

技术盛宴
54分钟前
25
0
golang 封装 mysql 和 redis 连接

Mysql封装 package dbimport ("fmt"_ "github.com/go-sql-driver/mysql""github.com/jmoiron/sqlx")var DB *sqlx.DBfunc init(){database, err := sqlx.Op......

开源中国最牛的人
55分钟前
21
0
pdfbox 读取文件报错 java.io.IOException: Page tree root must be a dictionary

pdfbox java.io.IOException: Page tree root must be a dictionary 示例代码 public static void main(String[] args) { try (InputStream sampleInputs = new ClassPathResource("s......

lemos
今天
28
0
整理 Linux下列出目录内容的命令

在 Linux 中,有非常多的命令可以让我们用来执行各种各样的任务。当我们想要像使用文件浏览器一样列出一个目录下的内容时,大家第一时间想到的是 ls 命令。但只有 ls 命令能实现这个目的吗?...

良许Linux
今天
17
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部