CRC32 function finally works; validated that code now generates fully compliant frames for printer. tomorrow's task: make the printer actually print something.

This commit is contained in:
Maff 2019-08-28 01:06:10 +01:00
parent 1687d16832
commit a69c8dcebb
4 changed files with 16 additions and 4 deletions

View File

@ -4,7 +4,8 @@ using System.Linq;
namespace libsharperang {
public class CRC32 {
public uint Initial { get; private set; } = 0xFFFFFFFF;
private uint _Initial = 0xFFFFFFFF;
public uint Initial { get { return ~_Initial; } private set { _Initial=~value; } }
private uint Polynomial = 0xedb88320;
//private uint Polynomial = 0x04c11db7;
private uint[] Table;
@ -34,7 +35,7 @@ namespace libsharperang {
public uint GetChecksum<T>(IEnumerable<T> bytes) {
if (!Initialised) Initialise();
try {
return ~bytes.Aggregate(Initial,
return ~bytes.Aggregate(_Initial,
(csR, cB) => Table[(csR & 0xFF) ^ Convert.ToByte(cB)] ^ (csR>>8));
} catch (FormatException e) {
throw new Exception("Could not read stream as bytes", e);

View File

@ -17,6 +17,7 @@ namespace libsharperang {
//public void InitialiseCrc(uint Key) => CRC=new CrcSum(Bludgeon(Key), Key);
public void InitialiseCrc(uint Key=0x35769521) {
if (hasher==null || (hasher.Initialised && hasher.Initial != Key)) hasher=new CRC32(Key);
Console.WriteLine("{0:X}", hasher.Initial);
hasher.Initialise();
}
public byte[] GetHashSum(byte[] data) {
@ -27,7 +28,7 @@ namespace libsharperang {
public uint GetCrcKey() {
if (!IsCrcInitialised()) InitialiseCrc();
//return CRC.CrcKey;
return (hasher.Initial == MagicNumber? hasher.Initial : hasher.Initial ^ MagicNumber);
return hasher.Initial == MagicNumber? hasher.Initial : hasher.Initial ^ MagicNumber;
}
public byte[] GetCrcKeyBytes() {
if (!IsCrcInitialised()) InitialiseCrc();

View File

@ -186,6 +186,11 @@ namespace libsharperang {
_=p?.Close();
_uDv?.Close();
}
public void TransmitCrcKey() => WriteBytes(builder.BuildTransmitCrc());
public void StartSession() => StartSession(new byte[2] { 0, 0 });
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 bool IsPrinterPresent() => (Devices != null && Devices.Count > 0);
bool IPrinter.Initialised() => Initialised();
byte[] IPrinter.ReadBytes() => ReadBytes();

View File

@ -29,7 +29,12 @@ namespace sharperang {
}
private void BtTestLine_Click(object sender, RoutedEventArgs e) {
logger.Debug("printer::TransmitCrc() => "+BitConverter.ToString(printer.builder.BuildTransmitCrc()).Replace('-', ' '));
logger.Debug("printer::TestCRC(0x0000) => "+BitConverter.ToString(printer.builder.Build(Frame.Opcode.SessionBegin, new byte[] { 0x00, 0x00 })).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();
}
}
}