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) { } } } }