From 1b8ae60f2bcfdcd43392fa94bb4993d93065e288 Mon Sep 17 00:00:00 2001 From: liuwf Date: Wed, 31 Jul 2024 18:58:55 +0800 Subject: [PATCH] =?UTF-8?q?add-=E6=B7=BB=E5=8A=A0=E5=85=89=E7=94=B5?= =?UTF-8?q?=E4=BF=A1=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SlnMesnac.Business/LogoBusiness.cs | 40 ++++++++++ SlnMesnac.Config/DebugConfig.cs | 9 +++ SlnMesnac.TouchSocket/TcpServer.cs | 119 ++++++++++++++++++++++------- SlnMesnac.WPF/SlnMesnac.WPF.csproj | 2 +- SlnMesnac.WPF/Views/IndexPage.xaml | 20 ++--- 5 files changed, 151 insertions(+), 39 deletions(-) diff --git a/SlnMesnac.Business/LogoBusiness.cs b/SlnMesnac.Business/LogoBusiness.cs index 2901906..a9e5191 100644 --- a/SlnMesnac.Business/LogoBusiness.cs +++ b/SlnMesnac.Business/LogoBusiness.cs @@ -13,10 +13,12 @@ using System.Diagnostics; using System.IO; using System.Security.Cryptography.Xml; using System.ServiceModel.Channels; +using System.Text; using System.Threading; using System.Threading.Tasks; using System.Timers; using TouchSocket.Core; +using TouchSocket.Sockets; namespace SlnMesnac.Business { @@ -69,6 +71,7 @@ namespace SlnMesnac.Business tcpServer = _tcpServer; TcpServer.RefreshMaterialCodeStrEvent += BarCodeHandler; TcpServer.CameraResultEvent += ReceiveCameraResult; + TcpServer.SerialSignalPushEvent += SerialSignalPush; this.baseMaterialService = baseMaterialService; this.logoIdentifyService = logoIdentifyService; this.logoConfigService = logoConfigService; @@ -80,6 +83,43 @@ namespace SlnMesnac.Business CleanOldLogs(PicturePath); CleanOldLogs(LogPath); InitClearTimer(); + test(); + } + public void test() + { + Task.Run(async() => + { + Thread.Sleep(5000); + TcpClient _tcpClient = new TcpClient(); + + _tcpClient.Setup(new TouchSocketConfig().SetRemoteIPHost($"127.0.0.1:7024")); + _tcpClient.Connect(); + + var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions() + { + FilterFunc = response => + { + return true; + } + }); + byte[] release = new byte[] { (byte)0x01, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0xFF, (byte)0xF9, (byte)0x94 }; + var reciveBuffer =await waitClient.SendThenResponseAsync(release); + }); + + } + + + private void SerialSignalPush(string ip, int flag) + { + // 光电触发拍照 + if (flag == 1) + { + + } + else //光电释放 + { + + } } private void ReceiveCameraResult(string result) diff --git a/SlnMesnac.Config/DebugConfig.cs b/SlnMesnac.Config/DebugConfig.cs index 3cf5c99..a9ce019 100644 --- a/SlnMesnac.Config/DebugConfig.cs +++ b/SlnMesnac.Config/DebugConfig.cs @@ -53,6 +53,15 @@ namespace SlnMesnac.Config set { iniHelper.IniWriteValue("system", "CameraIP", value); } } + /// + /// 串口模块--IP + /// + public string SerialIP + { + get { return iniHelper.IniReadValue("system", "SerialIP"); } + set { iniHelper.IniWriteValue("system", "SerialIP", value); } + } + /// /// 海康相机拍照存放路径 /// diff --git a/SlnMesnac.TouchSocket/TcpServer.cs b/SlnMesnac.TouchSocket/TcpServer.cs index b31ceee..6dd47e2 100644 --- a/SlnMesnac.TouchSocket/TcpServer.cs +++ b/SlnMesnac.TouchSocket/TcpServer.cs @@ -32,10 +32,15 @@ namespace SlnMesnac.TouchSocket public delegate void RefreshMaterialCodeStr(string materialCodeStr, string ip); public static event RefreshMaterialCodeStr RefreshMaterialCodeStrEvent; - //相机拍照识别结果事件 + //相机拍照识别结果 public delegate void CameraResult(string result); public static event CameraResult CameraResultEvent; + + //光电信号推送 + public delegate void SerialSignalPush(string ip, int flag); + public static event SerialSignalPush SerialSignalPushEvent; + #endregion public TcpServer(ILogger logger,TcpService tcpService) @@ -67,42 +72,66 @@ namespace SlnMesnac.TouchSocket { try { - //从客户端收到信息 - var mes = Encoding.UTF8.GetString(e.ByteBlock.Buffer, 0, e.ByteBlock.Len);//注意:数据长度是byteBlock.Len - - //心跳包 - if (mes == "heartbeat") - { - //扫码器心跳连接 - client.Logger.Info($"心跳{client.IP}:{client.Port}》接收到心跳信息:{mes}"); - RefreshStateEvent?.Invoke(client.IP, true); - } - else if (mes == "NoRead") + + // 串口模块光电信号 + if (client.IP == config.SerialIP) { - client.Logger.Info($"客户端{client.IP}:{client.Port}》NoRead事件{mes}"); - // TODO + int result = ChangeSerialSignal(e.ByteBlock.Buffer,e.ByteBlock.Len); + if(result == -1) + { + client.Logger.Info($"{client.IP}:{client.Port}》接收到光电信息异常"); + } + else + { + string message = result == 1 ? "触发" : "释放"; + client.Logger.Info($"{client.IP}:{client.Port}》接收到光电信息:{message}"); + SerialSignalPushEvent?.Invoke(client.IP, result); + + } + + } - else + else //扫码器及相机 { - string code = mes.Trim().TrimEnd('\0'); - client.Logger.Info($"客户端{client.IP}:{client.Port}》接收到信息:{mes}"); - // 区分相机扫码器数据 - if (client.IP == config.ScannerIP) + //从客户端收到信息 + var mes = Encoding.UTF8.GetString(e.ByteBlock.Buffer, 0, e.ByteBlock.Len);//注意:数据长度是byteBlock.Len + + //心跳包 + if (mes == "heartbeat") { - if (code.Contains("heart")) - { - return Task.CompletedTask; - } - RefreshMaterialCodeStrEvent?.Invoke(code, client.IP); + //扫码器心跳连接 + client.Logger.Info($"心跳{client.IP}:{client.Port}》接收到心跳信息:{mes}"); + RefreshStateEvent?.Invoke(client.IP, true); } - else if (client.IP == config.CameraIP) + else if (mes == "NoRead") { - // 相机返回数据 - CameraResultEvent?.Invoke(code); + client.Logger.Info($"客户端{client.IP}:{client.Port}》NoRead事件{mes}"); + // TODO扫码器扫码失败报警 } - + else + { + string code = mes.Trim().TrimEnd('\0'); + client.Logger.Info($"客户端{client.IP}:{client.Port}》接收到信息:{mes}"); + // 区分相机扫码器数据 + if (client.IP == config.ScannerIP) + { + if (code.Contains("heart")) + { + return Task.CompletedTask; + } + RefreshMaterialCodeStrEvent?.Invoke(code, client.IP); + } + else if (client.IP == config.CameraIP) + { + // 相机返回数据 + CameraResultEvent?.Invoke(code); + } + + } } + + } catch (Exception ex) { @@ -158,5 +187,39 @@ namespace SlnMesnac.TouchSocket } + /// + /// 转换光电信号 + /// 释放 01 03 02 01 00 B9 D4 + /// 触发 01 03 02 01 FF F9 94 + /// + /// + public int ChangeSerialSignal(byte[] data,int len) + { + if (data == null || len < 0) + { + return -1; + } + len = Math.Min(len, data.Length); + byte[] CheckByte = new byte[len]; + Array.Copy(data, 0, CheckByte, 0, len); + + byte[] release = new byte[] { (byte)0x01, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0x00, (byte)0xB9, (byte)0xD4 }; + byte[] trigger = new byte[] { (byte)0x01, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0xFF, (byte)0xF9, (byte)0x94 }; + + if (CheckByte.Length == release.Length && CheckByte.SequenceEqual(release)) + { + + return 0; + } + else if (CheckByte.Length == trigger.Length && CheckByte.SequenceEqual(trigger)) + { + + return 1; + } + + // 异常 + return -1; + } + } } diff --git a/SlnMesnac.WPF/SlnMesnac.WPF.csproj b/SlnMesnac.WPF/SlnMesnac.WPF.csproj index 27de711..f3dfef2 100644 --- a/SlnMesnac.WPF/SlnMesnac.WPF.csproj +++ b/SlnMesnac.WPF/SlnMesnac.WPF.csproj @@ -1,7 +1,7 @@  - WinExe + Exe net6.0-windows enable true diff --git a/SlnMesnac.WPF/Views/IndexPage.xaml b/SlnMesnac.WPF/Views/IndexPage.xaml index 8400376..9eb2422 100644 --- a/SlnMesnac.WPF/Views/IndexPage.xaml +++ b/SlnMesnac.WPF/Views/IndexPage.xaml @@ -111,36 +111,36 @@ - - --> +