schangxiang@126.com
2025-11-04 f5ed29dc26c7cd952d56ec5721a2efc43cd25992
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using XImageSystem.Service.Exceptions;
using XImaging.Automation.Library.HxDriverLib;
 
namespace HxDriverService.Runtime.Peripheral
{
    public class ConveyerControl
    {
        /// <summary>
        /// 网络是否连接
        /// </summary>
        private bool m_bIsNetConnect = false;
        /// <summary>
        /// 接收线程
        /// </summary>
        private Thread tRevTcp;
        /// <summary>
        /// 读写输入输出的流
        /// </summary>
        private NetworkStream m_nsClient;
        /// <summary>
        /// 客户端
        /// </summary>
        private TcpClient m_tcpClient;
        /// <summary>
        /// ip地址
        /// </summary>
        private string m_strIPAddress = "192.168.0.7";
        /// <summary>
        /// 端口号
        /// </summary>
        private int m_nPortNumber = 2022;
        /// <summary>
        /// 当前命令
        /// </summary>
        public string m_strCommand = "";
        /// <summary>
        /// 是否做完(error或正常做完皆认为做完)
        /// </summary>
        private bool m_bDone;
 
        public ConveyerControl(string strIP, int nPort)
        {
            m_strIPAddress = strIP;
            m_nPortNumber = nPort;
        }
 
        public bool Start()
        {
            try
            {
                if (!m_bIsNetConnect)
                {
                    m_tcpClient = new TcpClient();
                    m_tcpClient.Connect(m_strIPAddress, m_nPortNumber);
                    m_bIsNetConnect = true;
                    m_nsClient = m_tcpClient.GetStream();
 
                    LogConstant.logger.Print("Connect to conveyer");
 
                    tRevTcp = new Thread(RevTcp);
                    tRevTcp.IsBackground = true;
                    tRevTcp.Start();
                }
            }
            catch (Exception ex)
            {
                throw new ConveyerException(ex.Message);
            }
            return m_bIsNetConnect && (m_tcpClient != null);
        }
 
        public void Close()
        {
            if (m_tcpClient != null)
            {
                m_tcpClient.Close();
                m_tcpClient = null;
            }
            m_bIsNetConnect = false;
            LogConstant.logger.Print("Disconnected to the conveyer");
        }
 
        public bool SendCommand(string strMsg)
        {
            if (m_nsClient == null)
            {
                return false;
            }
            try
            {
                LogConstant.logger.Print("SendCommand: " + strMsg);
                byte[] dataSend = Encoding.ASCII.GetBytes(strMsg);
                m_nsClient.Write(dataSend, 0, dataSend.Length);
                return true;
            }
            catch (Exception ex)
            {
                Close();
                LogConstant.logger.Print("Send:" + strMsg + " failed");
                return false;
            }
        }
 
        public void RevTcp()
        {
            while (true)
            {
                if (!m_bIsNetConnect) break;
                if (m_tcpClient == null || m_nsClient == null) break;
 
                byte[] byteRev = new byte[1024];
                int byteRead = m_nsClient.Read(byteRev, 0, 1024);
                byte[] byText = new byte[byteRead];
                Array.Copy(byteRev, 0, byText, 0, byteRead);
 
                string strMsg = Encoding.ASCII.GetString(byText);
                LogConstant.logger.Print("Received resp:" + strMsg);
                if (strMsg.Contains("ERROR"))
                {
                    m_bDone = true;
                    throw new ConveyerException("Command: " + m_strCommand + " failed");
                }
                else if (strMsg == m_strCommand)
                {
                    m_bDone = true;
                }
            }
 
            //避免接收不全
            Thread.Sleep(200);
        }
 
        public void STOP()
        {
            m_strCommand = "STOP\r\n";
            try
            {
                SendCommand(m_strCommand);
                //等待做完
                while (!m_bDone)
                {
                    Thread.Sleep(500);
                }
                //置回false
                m_bDone = false;
            }
            catch (Exception ex)
            {
                throw new ConveyerException(ex.Message);
            }
        }
        public void HOME()
        {
            m_strCommand = "HOME\r\n";
            try
            {
                SendCommand(m_strCommand);
                //等待做完
                while (!m_bDone)
                {
                    Thread.Sleep(500);
                }
                //置回false
                m_bDone = false;
            }
            catch (Exception ex)
            {
                //置回false
                m_bDone = false;
                throw new ConveyerException(ex.Message);
            }
        }
 
        public void STEPS(int x = 329500)
        {
            m_strCommand = "STEPS" + x.ToString() + "\r\n";
            try
            {
                SendCommand(m_strCommand);
                //等待做完
                while (!m_bDone)
                {
                    Thread.Sleep(500);
                }
                //置回false
                m_bDone = false;
            }
            catch (Exception ex)
            {
                //置回false
                m_bDone = false;
                throw new ConveyerException(ex.Message);
            }
        }
    }
}