added functions to print arbitrary arrays of bytes

This commit is contained in:
Maff 2019-08-29 00:47:44 +01:00
parent a69c8dcebb
commit 901e384fe4
3 changed files with 43 additions and 15 deletions

View File

@ -34,6 +34,10 @@ namespace libsharperang {
if (!IsCrcInitialised()) InitialiseCrc();
return BitConverter.GetBytes(GetCrcKey());
}
public byte[] GeneratePrintOpcode(byte[] data) => BitConverter.GetBytes(SwapEndianness(
0x00010000 | (((((
(uint)data.Length & 0xFFU) << 16) |
(uint)data.Length) & 0xFFFF00U) >> 8)));
public uint SwapEndianness(uint value) => (
(value & 0x000000FFU) << 24) |
((value & 0x0000FF00U) << 8) |

View File

@ -76,18 +76,18 @@ namespace libsharperang {
switch (opcode) {
case Opcode.SessionBegin: return new byte[] { 0x06, 0x00, 0x02, 0x00 };
case Opcode.SessionEnd: return new byte[] { 0x1a, 0x00, 0x02, 0x00 };
case Opcode.PrintBegin: return new byte[] { 0x00, 0x01, 0xf0, 0x03 };
case Opcode.PrintContinue: return new byte[] { 0x00, 0x01, 0xf0, 0x02 };
case Opcode.CrcTransmit: return new byte[] { 0x18, 0x01, 0x04, 0x00 };
default: throw new NullReferenceException();
}
}
public byte[] Build(Opcode opcode, byte[] data) => Build(opcode, data, transformer);
public byte[] Build(Opcode opcode, byte[] data, DataTransforms transformer) {
public byte[] Build(Opcode opcode, byte[] data) => Build(ResolveOpcode(opcode), data, transformer);
public byte[] Build(byte[] opcode, byte[] data) => Build(opcode, data, transformer);
public byte[] Build(Opcode opcode, byte[] data, DataTransforms transformer) => Build(ResolveOpcode(opcode), data, transformer);
public byte[] Build(byte[] opcode, byte[] data, DataTransforms transformer) {
byte[] result=new byte[data.Length+10];
result[0]=FrameStart;
result[result.Length-1]=FrameEnd;
Buffer.BlockCopy(ResolveOpcode(opcode), 0, result, 1, 4);
Buffer.BlockCopy(opcode, 0, result, 1, 4);
Buffer.BlockCopy(data, 0, result, 5, data.Length);
Buffer.BlockCopy(transformer.GetHashSum(data), 0, result, result.Length-5, 4);
return result;
@ -191,6 +191,31 @@ namespace libsharperang {
public void StartSession(byte[] data) => WriteBytes(builder.Build(Frame.Opcode.SessionBegin, data));
public void EndSession() => EndSession(new byte[2] { 0, 0 });
public void EndSession(byte[] data) => WriteBytes(builder.Build(Frame.Opcode.SessionEnd, data));
public void InitPrinter() {
TransmitCrcKey();
EndSession();
PollPrinter();
}
public void PollPrinter() {
StartSession();
EndSession();
}
public void Feed() => EndSession(new byte[2] { 0x64, 0x00 });
public void Feed(int milliseconds) => EndSession(BitConverter.GetBytes(transform.SwapEndianness(
0x00000000 | (((((
(uint)milliseconds & 0xFFU) << 16) |
(uint)milliseconds) & 0xFFFF00U) >> 8))).Skip(2).ToArray());
public void PrintBytes(byte[] data, bool autofeed=true) {
List<byte[]> datas = data
.Select((b, i) => new {Index = i, Value = b })
.GroupBy(b => b.Index/1008)
.Select(b => b.Select(bb => bb.Value).ToArray())
.ToList();
datas.ForEach(b => WriteBytes(builder.Build(transform.GeneratePrintOpcode(b), b)));
if (autofeed) Feed(60);
}
public void PrintTestA(byte[] data) => WriteBytes(builder.Build(Frame.Opcode.PrintBegin, data));
public void PrintTestB(byte[] data) => WriteBytes(builder.Build(Frame.Opcode.PrintContinue, data));
public bool IsPrinterPresent() => (Devices != null && Devices.Count > 0);
bool IPrinter.Initialised() => Initialised();
byte[] IPrinter.ReadBytes() => ReadBytes();

View File

@ -23,18 +23,17 @@ namespace sharperang {
logger.Debug("IsPrinterPresent => "+printer.IsPrinterPresent());
logger.Debug("FoundPrinterGuids => "+printer.FoundPrinterGuids());
printer?.IDs?.ForEach(p => logger.Debug("FoundPrinterGuidAddrs "+p.ToString()+" => "+printer?.FoundPrinterGuidAddrs(p)));
logger.Debug("OpenUSB => "+printer?.Open());
logger.Debug("ClaimUSB => " + printer?.Claim());
//logger.Debug("IUsb::Initialised => " + printer?.pUsb?.Initialised());
logger.Debug("Open => "+printer?.Open());
logger.Debug("Claim => " + printer?.Claim());
logger.Debug("Init => "+BitConverter.ToString(printer.builder.BuildTransmitCrc()).Replace('-', ' '));
printer.InitPrinter();
logger.Debug("Printer initialised and ready");
}
private void BtTestLine_Click(object sender, RoutedEventArgs e) {
logger.Debug("printer::TransmitCrc() => "+BitConverter.ToString(printer.builder.BuildTransmitCrc()).Replace('-', ' '));
logger.Debug("printer::TestCRC(SessionBegin, 0x0000) => "+BitConverter.ToString(printer.builder.Build(Frame.Opcode.SessionBegin, new byte[] { 0x00, 0x00 })).Replace('-',' '));
logger.Debug("Transmit CRC and session preamble to printer, if validation needed please follow in wireshark");
printer.TransmitCrcKey();
printer.EndSession();
printer.StartSession();
printer.EndSession();
byte[] data = new byte[384];
data[5] = 0x10; data[4]=0x10; data[3]=0x20; data[2]=0x20; data[1]=0x30; data[0]=0x30;
data[192] = 0x10; data[193]=0x10;data[194]=0x20;data[195]=0x20;data[196]=0x30;data[197]=0x30;
printer.PrintBytes(data);
}
}
}