Skip to content

Commit fe049c1

Browse files
committed
Add serial protocol and update demo project
1 parent a172a87 commit fe049c1

10 files changed

Lines changed: 390 additions & 19 deletions

File tree

src/Vip.Printer.Demo/Vip.Printer.Demo.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
<HintPath>..\..\packages\System.Drawing.Common.6.0.0\lib\net461\System.Drawing.Common.dll</HintPath>
4444
</Reference>
4545
<Reference Include="System.Drawing" />
46+
<Reference Include="System.IO.Ports, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
47+
<HintPath>..\..\packages\System.IO.Ports.6.0.0\lib\net461\System.IO.Ports.dll</HintPath>
48+
</Reference>
4649
<Reference Include="System.Windows.Forms" />
4750
</ItemGroup>
4851
<ItemGroup>

src/Vip.Printer.Demo/frmPrincipal.Designer.cs

Lines changed: 177 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Vip.Printer.Demo/frmPrincipal.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Diagnostics;
33
using System.IO;
4+
using System.IO.Ports;
45
using System.Windows.Forms;
56
using Vip.Printer.Enums;
67

@@ -23,6 +24,9 @@ private void frmPrincipal_Load(object sender, EventArgs e)
2324
{
2425
cboModelo.SelectedItem = cboModelo.Items[0];
2526
cboProtocolo.SelectedItem = cboProtocolo.Items[0];
27+
cboParity.SelectedItem = cboParity.Items[0];
28+
cboStopBits.SelectedItem = cboStopBits.Items[0];
29+
cboHandShake.SelectedItem = cboHandShake.Items[0];
2630
}
2731

2832
private void lblLinkGit_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
@@ -157,10 +161,14 @@ private Printer ObterPrinter()
157161
int.TryParse(txtCondensado.Text, out var condensado);
158162
int.TryParse(txtExpandido.Text, out var expandido);
159163

164+
int.TryParse(txtBaudeRate.Text, out var baudeRate);
165+
int.TryParse(txtDataBits.Text, out var dataBits);
166+
160167
var settings = PrinterSettings.Create()
161168
.WithPrinterName(txtImpressora.Text)
162169
.WithPrinterType(ObterTipo())
163170
.WithProtocol(ObterProtocolo())
171+
.WithSerialOptions(baudeRate, dataBits, ObterParity(), ObterStopBits(), ObterHandShake())
164172
.WithColumns(normal, condensado, expandido)
165173
.Build();
166174

@@ -175,10 +183,51 @@ private ProtocolType ObterProtocolo()
175183
{
176184
case "Raw": return ProtocolType.Raw;
177185
case "Network": return ProtocolType.Network;
186+
case "Serial": return ProtocolType.Serial;
178187
default: return ProtocolType.Raw;
179188
}
180189
}
181190

191+
private Parity ObterParity()
192+
{
193+
switch (cboParity.Text)
194+
{
195+
case "Odd": return Parity.Odd;
196+
case "Even": return Parity.Even;
197+
case "Mark": return Parity.Mark;
198+
case "Space": return Parity.Space;
199+
case "None":
200+
default:
201+
return Parity.None;
202+
}
203+
}
204+
205+
private StopBits ObterStopBits()
206+
{
207+
switch (cboStopBits.Text)
208+
{
209+
case "None": return StopBits.None;
210+
case "Two": return StopBits.Two;
211+
case "OnePointFive": return StopBits.OnePointFive;
212+
case "One":
213+
default:
214+
return StopBits.One;
215+
}
216+
}
217+
218+
private Handshake ObterHandShake()
219+
{
220+
switch (cboHandShake.Text)
221+
{
222+
case "XOnXOff": return Handshake.XOnXOff;
223+
case "RequestToSend": return Handshake.RequestToSend;
224+
case "RequestToSendXOnXOff": return Handshake.RequestToSendXOnXOff;
225+
case "None":
226+
default:
227+
return Handshake.None;
228+
}
229+
}
230+
182231
#endregion
183232
}
184233
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="System.Drawing.Common" version="6.0.0" targetFramework="net462" />
4+
<package id="System.IO.Ports" version="6.0.0" targetFramework="net48" />
45
</packages>
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using System;
2+
using System.IO.Ports;
3+
using System.Threading;
4+
using Vip.Printer.Interfaces.Engine;
5+
6+
namespace Vip.Printer.Engine
7+
{
8+
public sealed class SerialEngine : IEngine, IDisposable
9+
{
10+
#region Fields
11+
12+
private readonly PrinterSettings _settings;
13+
private SerialPort _port;
14+
15+
#endregion
16+
17+
#region Constructors
18+
19+
public SerialEngine(PrinterSettings settings)
20+
{
21+
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
22+
if (_settings.SerialOptions == null) throw new ArgumentNullException(nameof(settings.SerialOptions));
23+
if (!settings.SerialOptions.AutoOpenClose) _port = CreateAndOpenPort();
24+
}
25+
26+
#endregion
27+
28+
#region Methods
29+
30+
public bool Send(string printer, byte[] content)
31+
{
32+
try
33+
{
34+
if (_settings.SerialOptions.AutoOpenClose)
35+
{
36+
using (var port = CreateAndOpenPort())
37+
WriteInChunks(port, content);
38+
}
39+
else
40+
{
41+
if (_port == null || !_port.IsOpen)
42+
{
43+
_port?.Dispose();
44+
_port = CreateAndOpenPort();
45+
}
46+
47+
WriteInChunks(_port, content);
48+
}
49+
50+
return true;
51+
}
52+
catch
53+
{
54+
return false;
55+
}
56+
}
57+
58+
public void Dispose()
59+
{
60+
try { _port?.Dispose(); }
61+
catch { }
62+
}
63+
64+
private SerialPort CreateAndOpenPort()
65+
{
66+
var sp = new SerialPort(_settings.PrinterName, _settings.SerialOptions.BaudRate, _settings.SerialOptions.Parity, _settings.SerialOptions.DataBits, _settings.SerialOptions.StopBits)
67+
{
68+
Handshake = _settings.SerialOptions.Handshake,
69+
DtrEnable = false,
70+
RtsEnable = false,
71+
WriteTimeout = _settings.SerialOptions.WriteTimeoutMs
72+
};
73+
sp.Open();
74+
Thread.Sleep(10);
75+
return sp;
76+
}
77+
78+
private void WriteInChunks(SerialPort port, byte[] data)
79+
{
80+
if (data == null || data.Length == 0) return;
81+
82+
var offset = 0;
83+
while (offset < data.Length)
84+
{
85+
var count = Math.Min(_settings.SerialOptions.ChunkSize, data.Length - offset);
86+
port.Write(data, offset, count);
87+
offset += count;
88+
89+
if (_settings.SerialOptions.InterChunkDelayMs > 0)
90+
Thread.Sleep(_settings.SerialOptions.InterChunkDelayMs);
91+
}
92+
93+
if (_settings.SerialOptions.InterChunkDelayMs > 0)
94+
Thread.Sleep(_settings.SerialOptions.InterChunkDelayMs);
95+
}
96+
97+
#endregion
98+
}
99+
}

src/Vip.Printer/Enums/ProtocolType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ public enum ProtocolType
44
{
55
Raw = 0,
66
Network = 1,
7-
//Serial = 2 - Future
7+
Serial = 2
88
}
99
}

src/Vip.Printer/Printer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public class Printer : IPrinter
6767
#region Constructors
6868

6969
/// <summary>
70-
/// Initializes a new instance of the <see cref="Printer" /> class.
70+
/// Initializes a new instance of the <see cref="Printer" /> class.
7171
/// </summary>
7272
/// <param name="settings"></param>
7373
/// <exception cref="ArgumentNullException"></exception>
@@ -151,6 +151,7 @@ private IEngine EngineFactory(ProtocolType protocol)
151151
{
152152
case ProtocolType.Raw: return new RawEngine();
153153
case ProtocolType.Network: return new NetworkEngine();
154+
case ProtocolType.Serial: return new SerialEngine(_settings);
154155
default: return new RawEngine();
155156
}
156157
}

src/Vip.Printer/PrinterSettings.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text;
1+
using System.IO.Ports;
2+
using System.Text;
23
using Vip.Printer.Enums;
34
using Vip.Printer.Interfaces.Engine;
45

@@ -12,6 +13,7 @@ public sealed class PrinterSettings
1213
public PrinterType PrinterType { get; }
1314
public ProtocolType Protocol { get; }
1415
public Encoding Encoding { get; }
16+
public SerialOptions SerialOptions { get; }
1517
public IEngine Engine { get; }
1618
public int ColsNormal { get; }
1719
public int ColsCondensed { get; }
@@ -21,12 +23,13 @@ public sealed class PrinterSettings
2123

2224
#region Constructors
2325

24-
private PrinterSettings(string printerName, PrinterType printerType, ProtocolType protocol, Encoding encoding, IEngine engine, int colsNormal, int colsCondensed, int colsExpanded)
26+
private PrinterSettings(string printerName, PrinterType printerType, ProtocolType protocol, Encoding encoding, SerialOptions serialOptions, IEngine engine, int colsNormal, int colsCondensed, int colsExpanded)
2527
{
2628
PrinterName = string.IsNullOrWhiteSpace(printerName) ? "temp.prn" : printerName.Trim();
2729
PrinterType = printerType;
2830
Protocol = protocol;
2931
Encoding = encoding;
32+
SerialOptions = serialOptions;
3033
Engine = engine;
3134
ColsNormal = colsNormal;
3235
ColsCondensed = colsCondensed;
@@ -62,6 +65,7 @@ public sealed class PrinterSettingsBuilder
6265
private PrinterType _printerType = PrinterType.Epson;
6366
private ProtocolType _protocol = ProtocolType.Raw;
6467
private Encoding _encoding;
68+
private SerialOptions _serialOptions;
6569
private IEngine _engine;
6670
private int _colsNormal;
6771
private int _colsCondensed;
@@ -73,7 +77,7 @@ public sealed class PrinterSettingsBuilder
7377

7478
public PrinterSettings Build()
7579
{
76-
return new PrinterSettings(_printerName, _printerType, _protocol, _encoding, _engine, _colsNormal, _colsCondensed, _colsExpanded);
80+
return new PrinterSettings(_printerName, _printerType, _protocol, _encoding, _serialOptions, _engine, _colsNormal, _colsCondensed, _colsExpanded);
7781
}
7882

7983
#endregion
@@ -104,6 +108,12 @@ public PrinterSettingsBuilder WithEncoding(Encoding encoding)
104108
return this;
105109
}
106110

111+
public PrinterSettingsBuilder WithSerialOptions(int baudeRate, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One, Handshake handshake = Handshake.None)
112+
{
113+
_serialOptions = new SerialOptions(baudeRate, dataBits, parity, stopBits, handshake);
114+
return this;
115+
}
116+
107117
public PrinterSettingsBuilder WithEngine(IEngine engine)
108118
{
109119
_engine = engine;

src/Vip.Printer/SerialOptions.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.IO.Ports;
3+
4+
namespace Vip.Printer
5+
{
6+
public class SerialOptions
7+
{
8+
#region Constructors
9+
10+
public SerialOptions(int baudRate = 9600, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One, Handshake handshake = Handshake.None, int writeTimeoutMs = 5000, int chunkSize = 512, int interChunkDelayMs = 3, bool autoOpenClose = true)
11+
{
12+
if (baudRate <= 0) throw new ArgumentOutOfRangeException(nameof(baudRate));
13+
14+
BaudRate = baudRate;
15+
DataBits = dataBits;
16+
Parity = parity;
17+
StopBits = stopBits;
18+
Handshake = handshake;
19+
WriteTimeoutMs = writeTimeoutMs;
20+
ChunkSize = chunkSize;
21+
InterChunkDelayMs = interChunkDelayMs;
22+
AutoOpenClose = autoOpenClose;
23+
}
24+
25+
#endregion
26+
27+
#region Properties
28+
29+
public int BaudRate { get; }
30+
public int DataBits { get; }
31+
public Parity Parity { get; }
32+
public StopBits StopBits { get; }
33+
public Handshake Handshake { get; }
34+
35+
public int WriteTimeoutMs { get; }
36+
public int ChunkSize { get; }
37+
public int InterChunkDelayMs { get; }
38+
public bool AutoOpenClose { get; }
39+
40+
#endregion
41+
}
42+
}

src/Vip.Printer/Vip.Printer.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<PackageReference Include="System.Drawing.Common">
1414
<Version>6.0.0</Version>
1515
</PackageReference>
16+
<PackageReference Include="System.IO.Ports">
17+
<Version>6.0.0</Version>
18+
</PackageReference>
1619
</ItemGroup>
1720
<ItemGroup>
1821
<Folder Include="Interfaces\Engine\" />

0 commit comments

Comments
 (0)