xcd
2020-10-26 b424db02d11d2234bfea2eac3d589b4dae6866c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Collections.Generic;
using System.Drawing.Text;
using System.Linq;
using System.Net;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms.VisualStyles;
using HalconDotNet;
 
namespace M423project
{
    //上料检测类
    public class LoadDetection
    {
        #region 上料检测相关私有成员
        private ConfigStruct opcConfig;
        private TcpListener robotServer;
        private ImageProcess ipImage;
        private Thread loadProcesser;
        #endregion
        /// <summary>
        /// 上料检类构造函数
        /// </summary>
        /// <param name="_opcConfig">OPC配置对象引用</param>
        /// <param name="_ipImage">图象处理对象引用</param>
        public LoadDetection(ConfigStruct _opcConfig, ImageProcess _ipImage)
        {
            opcConfig = _opcConfig;
            ipImage = _ipImage;
            robotServer = new TcpListener(IPAddress.Any, opcConfig.loadRobotConfig.port);
        }
 
        /// <summary>
        /// 启动检测线程
        /// </summary>
        public void Start()
        {
            loadProcesser = new Thread(Execute);
            loadProcesser.Start();
        }
 
        public void Stop()
        {
            if (loadProcesser != null && loadProcesser.IsAlive)
            {
                loadProcesser.Abort();
                loadProcesser.Join();
            }
            loadProcesser = null;
        }
 
        /// <summary>
        /// 启动对机械手臂监听
        /// </summary>
        /// <returns>是否正常启动</returns>
        public bool OpenListener()
        {
            try
            {
                robotServer.Start();
                CommonUtil.WriteLog(LogType.Inf, string.Format("机械手臂监听服务已启动,绑定端口:{0}", opcConfig.loadRobotConfig.port));
                return true;
            }
            catch (Exception e)
            {
                CommonUtil.WriteLog(LogType.Exc, string.Format("监听服务启动失败:{0}", e.Message));
                return false;
            }
        }
 
        public void CloseListener()
        {
            robotServer.Stop();
            CommonUtil.WriteLog(LogType.Inf, string.Format("机械手臂监听服务已关闭"));
        }
 
        /// <summary>
        /// 执行和机械手臂会话,处理上料检测结果。
        /// 连结是否需要多次建立还 是只建立一次?
        /// </summary>
        public void Execute()
        {
            TcpClient robortClient = null;
            NetworkStream netStream = null;
            byte[] recvBuf = new byte[1024];
            byte[] sendBuf;
            
            while (true)
            {
                try
                {
                    robortClient = robotServer.AcceptTcpClient();
                    CommonUtil.WriteLog(LogType.Inf, string.Format("机械手臂已连接:{0}", robortClient.Client.RemoteEndPoint));
                    netStream = robortClient.GetStream();
                    while (robortClient.Connected)
                    {
                        if (netStream.DataAvailable)
                        {
                            int n = netStream.Read(recvBuf, 0, 1024);
                            string receivedMsg = System.Text.Encoding.ASCII.GetString(recvBuf, 0, n);
                            CommonUtil.WriteLog(LogType.Inf, string.Format("接收到机械手臂上传数据:{0}", receivedMsg));
                            if (receivedMsg.Contains("slgrab"))
                            {
                                HObject ihImage = null;
                                HObject ihInterestImage = null;
                                VisionDetect vd = new VisionDetect();
                                ipImage.GrapLoad(ref ihImage);
                                ipImage.GetImageIntrest(ihImage, out ihInterestImage);
                                //
                                string resp = ipImage.JudgeLoad(ihInterestImage) ? "ok" : "ng";
                                CommonUtil.WriteLog(LogType.Inf, string.Format("上料判断结果:{0}", resp));
                                sendBuf = System.Text.Encoding.ASCII.GetBytes(resp);
                                netStream.Write(sendBuf, 0, sendBuf.Length);
                            }
                        }
                        Thread.Sleep(20);
                    }
                }
                catch (Exception e)
                {
                    if (e is ThreadAbortException)  //手动终止则退出,否则继续等待连接
                    {
                        CommonUtil.WriteLog(LogType.Inf, "用户手动终止程序, 连接关闭"); 
                        break;
                    }
                    else
                        CommonUtil.WriteLog(LogType.Exc, string.Format("处理上料检测过程中出现异常:{0}", e.Message));
                }
                finally
                {
                    if (robortClient != null)
                        robortClient.Close();
                    if (netStream != null)
                        netStream.Close();
                }
 
                Thread.Sleep(10);
            }
            CommonUtil.WriteLog(LogType.Exc, string.Format("上料检测过程退出"));
        }
    }
}