wangxinhui
2025-06-05 17bca2d690f283ec110c0c327a8508ed8fba9d21
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
using HslCommunication;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Numeric;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
 
namespace WIDESEAWCS_Tasks
{
    public class BarcodeScanner
    {
        static Socket client ;
        public static string barcode = "";
        static byte[] buffers = new byte[1024];
        public static void StartServer()
        {
            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress iPAddress = IPAddress.Parse("10.30.102.56");
            IPEndPoint endPoint = new IPEndPoint(iPAddress, 2002);
            client.SendTimeout = 2000;
            client.ReceiveTimeout = 2000;
            client.Connect(endPoint);
            client.BeginReceive(buffers, 0, buffers.Length, SocketFlags.None, new AsyncCallback(AsyncCallback), client);
        }
 
        public static void AsyncCallback(IAsyncResult ar)
        {
            try
            {
               
                int length = client.EndReceive(ar);
 
                byte[] result = buffers.SelectMiddle(0,length);
 
                string resultStr = Encoding.Default.GetString(result);
 
                Console.Out.WriteLine(resultStr);
 
                if (resultStr.StartsWith(".BC"))
                {
                    barcode = resultStr.Replace(".", "").Replace("/", "");
                    Console.Out.WriteLine($"托盘号:{barcode}");
                }
 
                client.BeginReceive(buffers, 0, buffers.Length, SocketFlags.None, new AsyncCallback(AsyncCallback), client);
            }
            catch (Exception ex)
            {
 
            }
        }
    }
}