diff --git a/Seyounth.Hyosung.Core/Plc/HyosungPlcService.cs b/Seyounth.Hyosung.Core/Plc/HyosungPlcService.cs index 923d337..8f5b57a 100644 --- a/Seyounth.Hyosung.Core/Plc/HyosungPlcService.cs +++ b/Seyounth.Hyosung.Core/Plc/HyosungPlcService.cs @@ -106,29 +106,49 @@ public class HyosungPlcService(ILogger logger) : IHyosungPlcS await _writer.WriteShortsAsync(12120, ls.ToArray()); } - public async Task WritePrintLableOptionsAsync(int side) + public async Task WritePrintLableOptionsAsync(int side, int slave) { - short data = 0; + short masterCount = 0; switch (side) { case 0: - data = 1; + masterCount = 1; break; case 1: - data = 2; + masterCount = 2; break; case 2: - data = 6; + masterCount = 6; break; case 3: - data = 14; + masterCount = 14; break; case 4: - data = 30; + masterCount = 30; break; } - await _writer.WriteShortsAsync(13050, [1, 1, 1, 2]); + short slaveCount = 0; + switch (slave) + { + case 0: + masterCount = 1; + break; + case 1: + masterCount = 2; + break; + case 2: + masterCount = 6; + break; + case 3: + masterCount = 14; + break; + case 4: + masterCount = 30; + break; + } + + await _writer.WriteShortsAsync(13050, [1, 1, slaveCount, masterCount]); } public async Task WritePrintLabelResultAsync(int index, bool result) diff --git a/Seyounth.Hyosung.Core/Plc/IHyosungPlcService.cs b/Seyounth.Hyosung.Core/Plc/IHyosungPlcService.cs index e16d3e3..b15f47b 100644 --- a/Seyounth.Hyosung.Core/Plc/IHyosungPlcService.cs +++ b/Seyounth.Hyosung.Core/Plc/IHyosungPlcService.cs @@ -58,7 +58,7 @@ public interface IHyosungPlcService Task WritePackLineOptionAsync(PackLineOption option); - Task WritePrintLableOptionsAsync(int side); + Task WritePrintLableOptionsAsync(int side,int slaveCount); Task WritePrintLabelResultAsync(int index, bool result); diff --git a/Seyounth.Hyosung.Core/Printer/AveryPrinter.cs b/Seyounth.Hyosung.Core/Printer/AveryPrinter.cs index b3a0ac8..130d6cd 100644 --- a/Seyounth.Hyosung.Core/Printer/AveryPrinter.cs +++ b/Seyounth.Hyosung.Core/Printer/AveryPrinter.cs @@ -7,18 +7,32 @@ public class AveryPrinter(string host, int port) { public string Host => host; public int Port => port; - private readonly Socket _socket = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + private Socket _socket = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); public async Task ConnectAsync(CancellationToken token) { try { await _socket.ConnectAsync(Host, Convert.ToInt32(port), token); - }catch(Exception e) + } + catch (Exception e) { Console.WriteLine($"Host[{Host}:{port}] error:{e.Message}"); } - + } + + private async Task CheckConnectAsync() + { + byte[] arrMsg = [0xff]; + try + { + await _socket.SendAsync(arrMsg); //发送字节数组 + return true; + } + catch + { + return false; + } } public async Task CloseAsync(CancellationToken token) @@ -29,6 +43,12 @@ public class AveryPrinter(string host, int port) public async Task SendContentAsync(string context) { + if (!await CheckConnectAsync()) + { + _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + await ConnectAsync(CancellationToken.None); + } + await _socket.SendAsync(Encoding.UTF8.GetBytes(context)); } @@ -37,11 +57,23 @@ public class AveryPrinter(string host, int port) public async Task CleanContextAsync() { + if (!await CheckConnectAsync()) + { + _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + await ConnectAsync(CancellationToken.None); + } + await _socket.SendAsync(Encoding.UTF8.GetBytes(CleanCmd)); } public async Task PrintAsync() { + if (!await CheckConnectAsync()) + { + _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + await ConnectAsync(CancellationToken.None); + } + await _socket.SendAsync(Encoding.UTF8.GetBytes(PrintCmd)); } } \ No newline at end of file diff --git a/Seyounth.Hyosung.Runtime/HyosungRuntime.cs b/Seyounth.Hyosung.Runtime/HyosungRuntime.cs index 7978416..5e2628e 100644 --- a/Seyounth.Hyosung.Runtime/HyosungRuntime.cs +++ b/Seyounth.Hyosung.Runtime/HyosungRuntime.cs @@ -383,7 +383,7 @@ public class HyosungRuntime( // } tray = await trayService.PrintTrayAsync(arg, mod,variety); await dictService.SetValue("System", "CurrentPackingTrayCode", arg); - await hyosungPlcService.WritePrintLableOptionsAsync(variety.MasterLabelCount); + await hyosungPlcService.WritePrintLableOptionsAsync(variety.MasterLabelCount,variety.SubLabelCount); } ///