From 2fd4b21b555e6281af81710113e9d2f7d4a5bd1d Mon Sep 17 00:00:00 2001 From: wangyueyang <645583145@qq.com> Date: Tue, 9 Jul 2024 16:50:05 +0800 Subject: [PATCH] =?UTF-8?q?led=E8=B0=83=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConsoleStarter/Program.cs | 9 +++++--- Service/Led/LedClient.cs | 41 +++++++++++++++++++++------------ WebStarter/appsettings.dev.json | 10 ++++---- 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/ConsoleStarter/Program.cs b/ConsoleStarter/Program.cs index fa08bc8..85965fd 100644 --- a/ConsoleStarter/Program.cs +++ b/ConsoleStarter/Program.cs @@ -13,8 +13,11 @@ internal class Program { XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + @"\log4net.xml")); - - ExportDb exportDb = new ExportDb(); - exportDb.Export(); + string s = "55 AA 00 00 01 01 00 D9 00 00 00 00 00 00 0E 00 00 00 0E 00 25 64 69 73 70 30 3B 31 CD A3 D7 BC C1 CB 00 00 0D 0A".Replace(" ", ""); + for (int i = 0; i < s.Length-1; i+=2) + { + Console.Write($"0x{s[i]}{s[i+1]},"); + } + } } diff --git a/Service/Led/LedClient.cs b/Service/Led/LedClient.cs index 1f7de86..3108b1c 100644 --- a/Service/Led/LedClient.cs +++ b/Service/Led/LedClient.cs @@ -1,4 +1,5 @@ using System.Net.Sockets; +using HybirdFrameworkCore.Utils; using log4net; namespace Service.Led; @@ -13,18 +14,14 @@ public class LedClient "houtui", new byte[] { - 0x55, 0xAA, 0x00, 0x00, 0x01, 0x01, 0x00, 0xD9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, - 0x00, 0x0C, 0x00, 0x25, 0x64, 0x69, 0x73, 0x70, 0x30, 0x3A, 0x34, 0xBA, 0xF3, 0xCD, 0xCB, 0x00, 0x00, - 0x0D, 0x0A + 0x55,0xAA,0x00,0x00,0x01,0x01,0x00,0xD9,0x00,0x00,0x00,0x00,0x00,0x00,0x0C,0x00,0x00,0x00,0x0C,0x00,0x25,0x64,0x69,0x73,0x70,0x30,0x3A,0x36,0xCE,0xDE,0xB3,0xB5,0x00,0x00,0x0D,0x0A } }, { "qianjin", new byte[] { - 0x55, 0xAA, 0x00, 0x00, 0x01, 0x01, 0x00, 0xD9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, - 0x00, 0x0C, 0x00, 0x25, 0x64, 0x69, 0x73, 0x70, 0x30, 0x3A, 0x37, 0xC7, 0xB0, 0xBD, 0xF8, 0x00, 0x00, - 0x0D, 0x0A + 0x55,0xAA,0x00,0x00,0x01,0x01,0x00,0xD9,0x00,0x00,0x00,0x00,0x00,0x00,0x0E,0x00,0x00,0x00,0x0E,0x00,0x25,0x64,0x69,0x73,0x70,0x30,0x3B,0x31,0xCD,0xA3,0xD7,0xBC,0xC1,0xCB,0x00,0x00,0x0D,0x0A } }, { @@ -95,22 +92,36 @@ public class LedClient #endregion private static readonly ILog Log = LogManager.GetLogger(typeof(LedClient)); - private static TcpClient? _tcpClient; + private static Socket? _socket; + private static string _ip; + private static int _port; public static bool Init(string ip, int port) { - Log.Info($"begin connect {ip}:{port}"); - _tcpClient = new TcpClient(); - _tcpClient.Connect(ip, port); - Log.Info($"end connect {ip}:{port} {_tcpClient.Connected}"); - return _tcpClient.Connected; + _ip = ip; + _port = port; + Connect(); + return _socket.Connected; + } + + private static void Connect() + { + Log.Info($"begin connect {_ip}:{_port}"); + _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + _socket.NoDelay = true; + _socket.Connect(_ip, _port); + Log.Info($"end connect {_ip}:{_port} {_socket.Connected}"); } public static bool InnerSend(byte[] bytes) { - if (_tcpClient?.Connected ?? true) + Connect(); + if (_socket?.Connected ?? true) { - _tcpClient.GetStream().Write(bytes, 0, bytes.Length); + _socket?.Send(bytes); + Log.Info($"send {BitUtls.BytesToHexStr(bytes)}"); + _socket?.Close(); + return true; } return false; @@ -126,4 +137,4 @@ public class LedClient return false; } -} +} \ No newline at end of file diff --git a/WebStarter/appsettings.dev.json b/WebStarter/appsettings.dev.json index 599d1ad..43b3716 100644 --- a/WebStarter/appsettings.dev.json +++ b/WebStarter/appsettings.dev.json @@ -114,17 +114,17 @@ "enable": false }, "plc": { - "enable": true + "enable": false }, "swap": { - "enable": true + "enable": false }, "task": { "enable": false }, "led": { - "enable": false, - "ip": "127.0.0.1", - "port": 10021 + "enable": true, + "ip": "192.168.3.79", + "port": 10000 } }