Skip to content

Commit cd0f8bc

Browse files
committed
[+] Adicionado comando para enviar a pagina de codigo para impressora.
[-] Corrigido erro ao imprimir condensado no protocolo EscPos.
1 parent e3b5727 commit cd0f8bc

19 files changed

Lines changed: 301 additions & 44 deletions

src/OpenAC.Net.EscPos.Demo/Commom/PaginaCodigo.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/OpenAC.Net.EscPos.Demo/Form1.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,6 @@ private void Form1_Shown(object sender, EventArgs e)
6161
private EscPosPrinter GetPosPrinter()
6262
{
6363
var tipo = cbbConexao.GetSelectedValue<TipoConexao>();
64-
var protocolo = cbbProtocolo.GetSelectedValue<ProtocoloEscPos>();
65-
var paginaCodigo = cbbEnconding.GetSelectedValue<PaginaCodigo>();
66-
67-
var encoding = paginaCodigo switch
68-
{
69-
PaginaCodigo.pc437 => Encoding.GetEncoding(437),
70-
PaginaCodigo.pc850 => OpenEncoding.IBM850,
71-
PaginaCodigo.pc852 => Encoding.GetEncoding("IBM852"),
72-
PaginaCodigo.pc860 => OpenEncoding.IBM860,
73-
PaginaCodigo.pcUTF8 => Encoding.UTF8,
74-
PaginaCodigo.pc1252 => OpenEncoding.CP1252,
75-
_ => throw new ArgumentOutOfRangeException()
76-
};
7764

7865
EscPosPrinter ret = tipo switch
7966
{
@@ -115,8 +102,8 @@ private EscPosPrinter GetPosPrinter()
115102
_ => throw new ArgumentOutOfRangeException()
116103
};
117104

118-
ret.Protocolo = protocolo;
119-
ret.Encoder = encoding;
105+
ret.Protocolo = cbbProtocolo.GetSelectedValue<ProtocoloEscPos>();
106+
ret.PaginaCodigo = cbbEnconding.GetSelectedValue<PaginaCodigo>();
120107
ret.Device.ControlePorta = chkControlePortas.Checked;
121108
ret.EspacoEntreLinhas = (byte)nudEspacos.Value;
122109
ret.LinhasEntreCupons = (byte)nudLinhas.Value;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// ***********************************************************************
2+
// Assembly : OpenAC.Net.EscPos
3+
// Author : Rafael Dias
4+
// Created : 17-03-2022
5+
//
6+
// Last Modified By : Rafael Dias
7+
// Last Modified On : 17-03-2022
8+
// ***********************************************************************
9+
// <copyright file="CodePageCommand.cs" company="OpenAC .Net">
10+
// The MIT License (MIT)
11+
// Copyright (c) 2014 - 2021 Projeto OpenAC .Net
12+
//
13+
// Permission is hereby granted, free of charge, to any person obtaining
14+
// a copy of this software and associated documentation files (the "Software"),
15+
// to deal in the Software without restriction, including without limitation
16+
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17+
// and/or sell copies of the Software, and to permit persons to whom the
18+
// Software is furnished to do so, subject to the following conditions:
19+
// The above copyright notice and this permission notice shall be
20+
// included in all copies or substantial portions of the Software.
21+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24+
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27+
// DEALINGS IN THE SOFTWARE.
28+
// </copyright>
29+
// <summary></summary>
30+
// ***********************************************************************
31+
32+
using OpenAC.Net.EscPos.Commom;
33+
using OpenAC.Net.EscPos.Interpreter;
34+
35+
namespace OpenAC.Net.EscPos.Command
36+
{
37+
public sealed class CodePageCommand : PrintCommand<CodePageCommand>
38+
{
39+
#region Constructors
40+
41+
public CodePageCommand(EscPosInterpreter interpreter) : base(interpreter)
42+
{
43+
}
44+
45+
#endregion Constructors
46+
47+
#region Properties
48+
49+
public PaginaCodigo PaginaCodigo { get; set; } = PaginaCodigo.pc850;
50+
51+
#endregion Properties
52+
}
53+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// ***********************************************************************
2+
// Assembly : OpenAC.Net.EscPos
3+
// Author : Rafael Dias
4+
// Created : 17-03-2022
5+
//
6+
// Last Modified By : Rafael Dias
7+
// Last Modified On : 17-03-2022
8+
// ***********************************************************************
9+
// <copyright file="PaginaCodigo.cs" company="OpenAC .Net">
10+
// The MIT License (MIT)
11+
// Copyright (c) 2014 - 2021 Projeto OpenAC .Net
12+
//
13+
// Permission is hereby granted, free of charge, to any person obtaining
14+
// a copy of this software and associated documentation files (the "Software"),
15+
// to deal in the Software without restriction, including without limitation
16+
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17+
// and/or sell copies of the Software, and to permit persons to whom the
18+
// Software is furnished to do so, subject to the following conditions:
19+
// The above copyright notice and this permission notice shall be
20+
// included in all copies or substantial portions of the Software.
21+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24+
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27+
// DEALINGS IN THE SOFTWARE.
28+
// </copyright>
29+
// <summary></summary>
30+
// ***********************************************************************
31+
32+
namespace OpenAC.Net.EscPos.Commom
33+
{
34+
public enum PaginaCodigo
35+
{
36+
pc437,
37+
pc850,
38+
pc852,
39+
pc860,
40+
pcUTF8,
41+
pc1252
42+
}
43+
}

src/OpenAC.Net.EscPos/EscPosInterpreterFactory.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
using System;
3333
using System.Text;
34+
using OpenAC.Net.Core;
3435
using OpenAC.Net.EscPos.Commom;
3536
using OpenAC.Net.EscPos.Interpreter;
3637
using OpenAC.Net.EscPos.Interpreter.Bematech;
@@ -54,11 +55,22 @@ public static class EscPosInterpreterFactory
5455
///
5556
/// </summary>
5657
/// <param name="protocolo"></param>
57-
/// <param name="enconder"></param>
58+
/// <param name="paginaCodigo"></param>
5859
/// <returns></returns>
5960
/// <exception cref="ArgumentOutOfRangeException"></exception>
60-
public static EscPosInterpreter Create(ProtocoloEscPos protocolo, Encoding enconder)
61+
public static EscPosInterpreter Create(ProtocoloEscPos protocolo, PaginaCodigo paginaCodigo)
6162
{
63+
var enconder = paginaCodigo switch
64+
{
65+
PaginaCodigo.pc437 => Encoding.GetEncoding(437),
66+
PaginaCodigo.pc850 => OpenEncoding.IBM850,
67+
PaginaCodigo.pc852 => Encoding.GetEncoding("IBM852"),
68+
PaginaCodigo.pc860 => OpenEncoding.IBM860,
69+
PaginaCodigo.pcUTF8 => Encoding.UTF8,
70+
PaginaCodigo.pc1252 => OpenEncoding.CP1252,
71+
_ => throw new ArgumentOutOfRangeException()
72+
};
73+
6274
switch (protocolo)
6375
{
6476
case ProtocoloEscPos.EscPos: return new EpsonInterpreter(enconder);

src/OpenAC.Net.EscPos/EscPosPrinter.cs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
using System.Drawing;
3535
using System.Linq;
3636
using System.Reflection;
37-
using System.Text;
38-
using System.Threading;
3937
using OpenAC.Net.Core;
4038
using OpenAC.Net.Core.Extensions;
4139
using OpenAC.Net.Core.Logging;
@@ -56,7 +54,7 @@ public abstract class EscPosPrinter : OpenDisposable, IOpenLog
5654
private EscPosInterpreter interpreter;
5755
private ProtocoloEscPos protocolo;
5856
private bool inicializada;
59-
private Encoding encoder;
57+
private PaginaCodigo paginaCodigo;
6058

6159
#endregion Fields
6260

@@ -70,9 +68,9 @@ protected EscPosPrinter(IDeviceConfig device)
7068

7169
commands = new List<IPrintCommand>();
7270

73-
encoder = OpenEncoding.IBM850;
71+
paginaCodigo = PaginaCodigo.pc850;
7472
protocolo = ProtocoloEscPos.EscPos;
75-
interpreter = EscPosInterpreterFactory.Create(protocolo, encoder);
73+
interpreter = EscPosInterpreterFactory.Create(protocolo, paginaCodigo);
7674
}
7775

7876
#endregion Constructors
@@ -98,22 +96,22 @@ public ProtocoloEscPos Protocolo
9896
protocolo = value;
9997

10098
interpreter = null;
101-
interpreter = EscPosInterpreterFactory.Create(value, Encoder);
99+
interpreter = EscPosInterpreterFactory.Create(value, PaginaCodigo);
102100
}
103101
}
104102

105103
/// <summary>
106104
/// Define/Obtém o enconding para comunicação com a impressora.
107105
/// </summary>
108-
public Encoding Encoder
106+
public PaginaCodigo PaginaCodigo
109107
{
110-
get => encoder;
108+
get => paginaCodigo;
111109
set
112110
{
113111
Guard.Against<OpenException>(Conectado, "Não pode mudar o protocolo quando esta conectado.");
114-
if (encoder == value) return;
112+
if (paginaCodigo == value) return;
115113

116-
encoder = value;
114+
paginaCodigo = value;
117115

118116
interpreter = null;
119117
interpreter = EscPosInterpreterFactory.Create(protocolo, value);
@@ -230,10 +228,24 @@ private void Inicializar()
230228

231229
if (inicializada) return;
232230

233-
var cmd = new EspacoEntreLinhasCommand(interpreter) { Espaco = EspacoEntreLinhas };
234-
var dados = cmd.Content;
231+
using var builder = new ByteArrayBuilder();
232+
233+
IPrintCommand cmd;
234+
if (interpreter.CommandResolver.HasResolver<EspacoEntreLinhasCommand>())
235+
{
236+
cmd = new EspacoEntreLinhasCommand(interpreter) { Espaco = EspacoEntreLinhas };
237+
builder.Append(cmd.Content);
238+
}
239+
240+
if (interpreter.CommandResolver.HasResolver<CodePageCommand>())
241+
{
242+
cmd = new CodePageCommand(interpreter) { PaginaCodigo = paginaCodigo };
243+
builder.Append(cmd.Content);
244+
}
245+
246+
if (builder.Length > 0)
247+
device.Write(builder.ToArray());
235248

236-
device.Write(dados);
237249
inicializada = true;
238250
}
239251

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// ***********************************************************************
2+
// Assembly : OpenAC.Net.EscPos
3+
// Author : Rafael Dias
4+
// Created : 17-03-2022
5+
//
6+
// Last Modified By : Rafael Dias
7+
// Last Modified On : 17-03-2022
8+
// ***********************************************************************
9+
// <copyright file="BemaCodePageResolver.cs" company="OpenAC .Net">
10+
// The MIT License (MIT)
11+
// Copyright (c) 2014 - 2021 Projeto OpenAC .Net
12+
//
13+
// Permission is hereby granted, free of charge, to any person obtaining
14+
// a copy of this software and associated documentation files (the "Software"),
15+
// to deal in the Software without restriction, including without limitation
16+
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17+
// and/or sell copies of the Software, and to permit persons to whom the
18+
// Software is furnished to do so, subject to the following conditions:
19+
// The above copyright notice and this permission notice shall be
20+
// included in all copies or substantial portions of the Software.
21+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24+
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27+
// DEALINGS IN THE SOFTWARE.
28+
// </copyright>
29+
// <summary></summary>
30+
// ***********************************************************************
31+
32+
using System;
33+
using System.Collections.Generic;
34+
using OpenAC.Net.Core.Extensions;
35+
using OpenAC.Net.Devices.Commom;
36+
using OpenAC.Net.EscPos.Command;
37+
using OpenAC.Net.EscPos.Commom;
38+
using OpenAC.Net.EscPos.Interpreter.Resolver;
39+
40+
namespace OpenAC.Net.EscPos.Interpreter.Bematech
41+
{
42+
public sealed class BemaCodePageResolver : CommandResolver<CodePageCommand>
43+
{
44+
#region Constructors
45+
46+
public BemaCodePageResolver(IReadOnlyDictionary<CmdEscPos, byte[]> dict) : base(dict)
47+
{
48+
}
49+
50+
#endregion Constructors
51+
52+
#region Methods
53+
54+
public override byte[] Resolve(CodePageCommand command)
55+
{
56+
if (command.PaginaCodigo.IsIn(PaginaCodigo.pc852, PaginaCodigo.pc1252)) return new byte[0];
57+
58+
using var builder = new ByteArrayBuilder();
59+
60+
var codePage = command.PaginaCodigo switch
61+
{
62+
PaginaCodigo.pc437 => new byte[] { 3 },
63+
PaginaCodigo.pc850 => new byte[] { 2 },
64+
PaginaCodigo.pc860 => new byte[] { 4 },
65+
PaginaCodigo.pcUTF8 => new byte[] { 8 },
66+
_ => throw new ArgumentOutOfRangeException()
67+
};
68+
69+
builder.Append(new byte[] { CmdConst.ESC, 116 });
70+
builder.Append(codePage);
71+
72+
return builder.ToArray();
73+
}
74+
75+
#endregion Methods
76+
}
77+
}

src/OpenAC.Net.EscPos/Interpreter/Bematech/BematechInterpreter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ protected override void IniciarInterpreter()
5555
{
5656
Status = new BematechStatusResolver();
5757

58+
CommandResolver.AddResolver<CodePageCommand, BemaCodePageResolver>(new BemaCodePageResolver(DefaultCommands.EscBema));
5859
CommandResolver.AddResolver<TextCommand, DefaultTextResolver>(new DefaultTextResolver(Enconder, DefaultCommands.EscBema));
5960
CommandResolver.AddResolver<ZeraCommand, DefaultZeraResolver>(new DefaultZeraResolver(DefaultCommands.EscBema));
6061
CommandResolver.AddResolver<EspacoEntreLinhasCommand, DefaultEspacoEntreLinhasResolver>(new DefaultEspacoEntreLinhasResolver(DefaultCommands.EscBema));

src/OpenAC.Net.EscPos/Interpreter/Datecs/DatecsInterpreter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ protected override void IniciarInterpreter()
6161
var commandos = DefaultCommands.EscPos.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
6262
commandos[CmdEscPos.Beep] = new[] { CmdConst.BELL };
6363

64+
CommandResolver.AddResolver<CodePageCommand, DefaultCodePageResolver>(new DefaultCodePageResolver(commandos));
6465
CommandResolver.AddResolver<TextCommand, DefaultTextResolver>(new DefaultTextResolver(Enconder, commandos));
6566
CommandResolver.AddResolver<ZeraCommand, DefaultZeraResolver>(new DefaultZeraResolver(commandos));
6667
CommandResolver.AddResolver<EspacoEntreLinhasCommand, DefaultEspacoEntreLinhasResolver>(new DefaultEspacoEntreLinhasResolver(commandos));

src/OpenAC.Net.EscPos/Interpreter/DefaultCommands.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ static DefaultCommands()
6161
{CmdEscPos.FonteB, new byte[] {CmdConst.ESC, (byte) 'M', 1}},
6262
{CmdEscPos.LigaExpandido, new byte[] {CmdConst.GS, (byte) '!', 16}},
6363
{CmdEscPos.DesligaExpandido, new byte[] {CmdConst.GS, (byte) '!', 0}},
64-
{CmdEscPos.LigaCondensado, new[] {CmdConst.SI}},
65-
{CmdEscPos.DesligaCondensado, new[] {CmdConst.DC2}},
64+
{CmdEscPos.LigaCondensado, new byte[] {CmdConst.ESC, (byte) '!', 1}},
65+
{CmdEscPos.DesligaCondensado, new byte[] {CmdConst.ESC, (byte) '!', 0}},
6666
{CmdEscPos.LigaNegrito, new byte[] {CmdConst.ESC, (byte) 'E', 1}},
6767
{CmdEscPos.DesligaNegrito, new byte[] {CmdConst.ESC, (byte) 'E', 0}},
6868
{CmdEscPos.LigaSublinhado, new byte[] {CmdConst.ESC, (byte) '-', 1}},

0 commit comments

Comments
 (0)