#
mrzhssss
2022-09-22 110f3977b399dd117ce1af49ca792d602e2ba9a9
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using DevComponents.DotNetBar;
 
namespace WCS
{
    /// <summary>
    /// 条码线程
    /// </summary>
    class BarcodeThread
    {
        #region 画面上同步显示控件
        public ListBox barcodedisplay;
        public LabelX lb_barcode;
        delegate void displayresponse(string text);
        delegate void SetTextCallback(string barcode);
        #endregion
 
        Thread trd = null;
        string barcodeip = "";
        int barcodeno = 0;
        int portno = 0;
        
        //int i = 1;
        //int scaleno = 0;
        main form1;
 
        private IPEndPoint ServerInfo;
        private Socket socket;
        private Byte[] MsgBuffer = new Byte[65535];
        private Byte[] MsgSend = new Byte[65535];
 
        public BarcodeThread(int barcode_no, string barcode_ip, int port, main f)
        {
            try
            {
                barcodeno = barcode_no;
                barcodeip = barcode_ip;
                this.portno = port;
                form1 = f;
                //MsgBuffer = new Byte[65535];
                //MsgSend = new Byte[65535];
 
                trd = new Thread(new ThreadStart(this.ThreadTask));
                trd.IsBackground = true;
                trd.Start();
            }
            catch (Exception em)
            {
                Common.WriteLogFile("WcsError", "BarcodeThread/BarcodeThread--条码线程启动出错:" + em.Message);
            }
        }
 
        #region socket连接
        /// <summary>
        /// socket连接WMS服务器
        /// </summary>
        private void SocketConnect()
        {
            ServerInfo = new IPEndPoint(IPAddress.Parse(barcodeip), portno);
            try
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Connect(ServerInfo);
                //Int32 bytes = socket.Receive(MsgBuffer, MsgBuffer.Length, SocketFlags.None);
                //StringBuilder buff = new StringBuilder();
                //String receiveData = Encoding.UTF8.GetString(MsgBuffer, 0, bytes);
                //socket.Send(Encoding.Unicode.GetBytes("socket connect!"));
                //RecevieData = "s";
                //socket.BeginReceive(MsgBuffer, 0, MsgBuffer.Length, 0, new AsyncCallback(ReceiveCallBack), null);
 
                DisplayInfo("【" + DateTime.Now.ToString() + "】读取条码数据...");
 
            }
            catch (Exception em)
            {
                DisplayInfo("【" + DateTime.Now.ToString() + "】条码连接失败!" + em.Message);
                Common.WriteLogFile("WcsError", "BarcodeThread/SocketConnect--条码连接失败:" + em.Message);
            }
        }
 
        /// <summary>
        /// socket重新连接WMS服务器
        /// </summary>
        private void SocketReConnect()
        {
            if (socket.Connected)
            {
                socket.Close();
            }
            Thread.Sleep(100);
            SocketConnect();
        }
 
        /// <summary>
        /// 判断socket是否已连接
        /// </summary>
        /// <returns></returns>
        private bool checkSocket()
        {
            bool ret = true;
            bool isConnected = false;
            bool isRead = false;
            bool isWrite = false;
            bool isError = false;
            int avalSize = 0;
            try
            {
                isConnected = socket.Connected;
                isRead = socket.Poll(50, SelectMode.SelectRead);
                avalSize = socket.Available;
                isWrite = socket.Poll(50, SelectMode.SelectWrite);
                isError = socket.Poll(50, SelectMode.SelectError);
                if (isConnected == false || isError == true || (isRead == true && avalSize == 0))
                {
                    ret = false;
                }
            }
            catch (Exception em)
            {
                Common.WriteLogFile("WcsError", "BarcodeThread/checkSocket--判断条码是否连接失败:" + em.Message);
                ret = false;
            }
            return ret;
        }
        #endregion
 
        /// <summary>
        /// 获取条码扫描器数据并解析
        /// </summary>
        private void GetBarcodeData()
        {
            string dataValue = "P", s_RecStr = "";
            double d_RecStr = 0;
            try
            {
                if (checkSocket() == false)
                {
                    //调用重连
                    //Common.gs_barcode_data[barcodeno - 1] = "";
                    //DisplayInfo("【" + DateTime.Now.ToString() + "】条码连接已断开,重连...");
                    SocketReConnect();
                    return;
                }
 
                MsgSend = Encoding.UTF8.GetBytes(dataValue);
                if (socket.Connected)
                {
                    // 向主机发送数据.
                    socket.Send(MsgSend, MsgSend.Length, SocketFlags.None);
                    //socket.Send(MsgSend);
                    //DisplayInfo("发送信息:" + dataValue);
                    //RecevieData = "";
                    // 接收主机应答         
                    socket.ReceiveTimeout = 60000;
                    Int32 bytes = socket.Receive(MsgBuffer, MsgBuffer.Length, SocketFlags.None);
                    StringBuilder buff = new StringBuilder();
                    if (bytes > 0)
                    {
                        // 将缓冲的字节数组,装换为字符串.
                        // String receiveData = Encoding.ASCII.GetString(MsgBuffer, 0, bytes);                        
                        s_RecStr = Encoding.ASCII.GetString(MsgBuffer, 0, bytes);
                        s_RecStr = s_RecStr.Trim('');
                        if (s_RecStr.IndexOf("NOREAD") >= 0)// || (s_RecStr.Length < 7 && s_RecStr.Length > 0))
                        {
                            //Common.gs_barcode_data[barcodeno - 1] = "";
                            DisplayInfo("【" + DateTime.Now.ToString() + "】条码返回信息:" + s_RecStr);
                            return;
                        }
                        if (s_RecStr.Length < 7)
                        {
                            return;
                        }
                        s_RecStr = s_RecStr.Substring(0, 7);
                        Common.gs_barcode_data[barcodeno - 1] = s_RecStr;
                        //DisplayBarcode(Common.gs_barcode_data[barcodeno - 1]);
                        DisplayInfo("【" + DateTime.Now.ToString() + "】" + Common.gs_barcode_data[barcodeno - 1]);
                        //DisplayInfo("收到消息:" + receiveData);
                        // AnalysisReceive(receiveData);   //解析收到的数据
                        // 加入字符串缓存
                        //buff.Append(str);
                        // 再次接受,看看后面还有没有数据.                        
                    }
                }
                else
                {
                    //Common.gs_barcode_data[barcodeno - 1] = "";
                    DisplayInfo("【" + DateTime.Now.ToString() + "】条码连接已断开,无法读取数据!");
                }
                return;
            }
            catch(Exception em)
            {
                Common.WriteLogFile("WcsError", "BarcodeThread/GetBarcodeData--读取条码数据失败:" + em.Message);
                //Common.gs_barcode_data[barcodeno - 1] = "";
                //DisplayInfo("【" + DateTime.Now.ToString() + "】扫描超时:" + t.Message);
                //Thread.Sleep(400);
                return;
            }
        }
 
        #region 通讯反馈同步显示
        /// <summary>
        /// 显示条码通讯信息
        /// </summary>
        /// <param name="text"></param>
        private void DisplayInfo(string text)
        {
            if (barcodedisplay == null)
            {
                return;
            }
            if (barcodedisplay.InvokeRequired)
            {
                try
                {
                    displayresponse d = new displayresponse(DisplayInfo);
                    barcodedisplay.Invoke(d, new object[] { text });
                }
                catch (Exception em)
                {
                    Common.WriteLogFile("WcsError", "BarcodeThread/DisplayInfo--同步显示条码通讯信息失败:" + em.Message);
                }
            }
            else
            {
                if (barcodedisplay.Items.Count > 12)
                {
                    barcodedisplay.Items.Clear();
                }
                barcodedisplay.Items.Add(text);
            }
 
        }
 
        /// <summary>
        /// 显示条码号
        /// </summary>
        /// <param name="text"></param>
        private void DisplayBarcode(string barcode)
        {
            if (lb_barcode == null)
            {
                return;
            }
            if (lb_barcode.InvokeRequired)
            {
                try
                {
                    SetTextCallback d = new SetTextCallback(DisplayBarcode);
                    lb_barcode.Invoke(d, new object[] { barcode });
                }
                catch (Exception em)
                {
                    Common.WriteLogFile("WcsError", "BarcodeThread/DisplayBarcode--同步显示条码号失败:" + em.Message);
                }
            }
            else
            {
                lb_barcode.Text = barcode;
            }
 
        }
        #endregion
 
        /// <summary>
        /// 条码主线程
        /// </summary>
        private void ThreadTask()
        {
            SocketConnect();
            while (true)
            {
                GetBarcodeData();
                Thread.Sleep(Common.ci_BarcodetimeInterval);
            }
        }
 
    }
}