add IUsb interface as connection interface for printer library
This commit is contained in:
parent
3820cbc13e
commit
b76f3ab65f
|
@ -1,6 +1,8 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using Vadavo.NEscPos;
|
||||||
|
using Vadavo.NEscPos.Printable;
|
||||||
|
|
||||||
namespace libsharperang {
|
namespace libsharperang {
|
||||||
public abstract class Base {
|
public abstract class Base {
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using LibUsbDotNet;
|
||||||
|
using Vadavo.NEscPos.Connectors;
|
||||||
|
|
||||||
|
namespace libsharperang
|
||||||
|
{
|
||||||
|
public class IUsb : IPrinterConnector
|
||||||
|
{
|
||||||
|
internal UsbDevice iPrinter;
|
||||||
|
private UsbEndpointReader pReader;
|
||||||
|
private UsbEndpointWriter pWriter;
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
pReader?.Dispose();
|
||||||
|
pWriter?.Dispose();
|
||||||
|
IUsbDevice p = iPrinter as IUsbDevice;
|
||||||
|
_ = p?.ReleaseInterface(0);
|
||||||
|
_ = iPrinter?.Close();
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] Read()
|
||||||
|
{
|
||||||
|
if (pReader == null) pReader = iPrinter.OpenEndpointReader(LibUsbDotNet.Main.ReadEndpointID.Ep01);
|
||||||
|
byte[] bRead = new byte[1024];
|
||||||
|
_ = pReader.Read(bRead, 100, out int _);
|
||||||
|
return bRead;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Write(byte[] data)
|
||||||
|
{
|
||||||
|
if (pWriter == null) pWriter = iPrinter.OpenEndpointWriter(LibUsbDotNet.Main.WriteEndpointID.Ep01);
|
||||||
|
pWriter?.Write(data, 100, out int _);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,6 +8,7 @@
|
||||||
<PackageReference Include="Device.Net" Version="3.0.0" />
|
<PackageReference Include="Device.Net" Version="3.0.0" />
|
||||||
<PackageReference Include="LibUsbDotNet" Version="2.2.29" />
|
<PackageReference Include="LibUsbDotNet" Version="2.2.29" />
|
||||||
<PackageReference Include="NativeUsbLib" Version="1.4.6" />
|
<PackageReference Include="NativeUsbLib" Version="1.4.6" />
|
||||||
|
<PackageReference Include="Vadavo.NEscPos" Version="0.0.4" />
|
||||||
<PackageReference Include="WinUSBNet" Version="1.0.3" />
|
<PackageReference Include="WinUSBNet" Version="1.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
|
@ -3,14 +3,23 @@ using System.Linq;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using LibUsbDotNet;
|
using LibUsbDotNet;
|
||||||
using LibUsbDotNet.Main;
|
using LibUsbDotNet.Main;
|
||||||
|
using Vadavo.NEscPos.Connectors;
|
||||||
|
|
||||||
namespace libsharperang {
|
namespace libsharperang {
|
||||||
public class USB : Base {
|
public class USB : Base {
|
||||||
|
//both P1 and P2 models share the same idV and idP over USB
|
||||||
public readonly ushort idVendor=0x4348;
|
public readonly ushort idVendor=0x4348;
|
||||||
public readonly ushort idProduct=0x5584;
|
public readonly ushort idProduct=0x5584;
|
||||||
|
//differentiating between the two is a case of checking the product name
|
||||||
|
//P1 is identified as "MiaoMiaoji" or "MTTII Printer"
|
||||||
|
//P2 is identified as "Paperang_P2"
|
||||||
|
//Notable is that the idV and idP appear to belong not to Paperang, but to a generic CH34x printer adaptor device
|
||||||
|
// so it should be a constraint to check that the device descriptors match known details for supported devices,
|
||||||
|
// not just idV and idP
|
||||||
public UsbDevice printer;
|
public UsbDevice printer;
|
||||||
public List<UsbRegistry> pInstances;
|
public List<UsbRegistry> pInstances;
|
||||||
public List<Guid> pIds;
|
public List<Guid> pIds;
|
||||||
|
public IUsb pUsb;
|
||||||
|
|
||||||
public USB() {
|
public USB() {
|
||||||
ActiveConnectionType=ConnectionType.USB;
|
ActiveConnectionType=ConnectionType.USB;
|
||||||
|
@ -19,12 +28,13 @@ namespace libsharperang {
|
||||||
public bool InitUSB() {
|
public bool InitUSB() {
|
||||||
//NOTE - in order for this to work, you must use Zadig to change the driver for
|
//NOTE - in order for this to work, you must use Zadig to change the driver for
|
||||||
// the printer from usbprint to WinUSB
|
// the printer from usbprint to WinUSB
|
||||||
if (UsbDevice.AllWinUsbDevices.Count == 0) return false;
|
//On MacOS and Linux libusb should "just work" here - testing is pending however.
|
||||||
|
if (UsbDevice.AllDevices.Count == 0) return false;
|
||||||
//genuinely just having some fun with Linq, don't judge me.
|
//genuinely just having some fun with Linq, don't judge me.
|
||||||
//TODO: work out a way of distinguishing multiple devices with same GUID
|
//TODO: work out a way of distinguishing multiple devices with same GUID
|
||||||
// which happens when two of the same device are connected
|
// which happens when two of the same device are connected
|
||||||
// the only thing that differs seemingly is the Address
|
// the only thing that differs seemingly is the Address
|
||||||
pInstances = (from d in UsbDevice.AllWinUsbDevices
|
pInstances = (from d in UsbDevice.AllDevices
|
||||||
where d.Vid == idVendor && d.Pid == idProduct
|
where d.Vid == idVendor && d.Pid == idProduct
|
||||||
select d)
|
select d)
|
||||||
.ToList<UsbRegistry>();
|
.ToList<UsbRegistry>();
|
||||||
|
@ -35,12 +45,12 @@ namespace libsharperang {
|
||||||
return (pIds.Count > 0);
|
return (pIds.Count > 0);
|
||||||
}
|
}
|
||||||
public List<int> GetAddressesFromGuid(Guid deviceId) {
|
public List<int> GetAddressesFromGuid(Guid deviceId) {
|
||||||
return (from d in UsbDevice.AllWinUsbDevices
|
return (from d in UsbDevice.AllDevices
|
||||||
where d.DeviceInterfaceGuids.Contains(deviceId)
|
where d.DeviceInterfaceGuids.Contains(deviceId)
|
||||||
select d.DeviceProperties.Where(k => k.Key=="Address").FirstOrDefault().Value).ToList().ConvertAll(v => (int)v);
|
select d.DeviceProperties.Where(k => k.Key=="Address").FirstOrDefault().Value).ToList().ConvertAll(v => (int)v);
|
||||||
}
|
}
|
||||||
public bool OpenUSB() {
|
public bool OpenUSB() {
|
||||||
if (UsbDevice.AllWinUsbDevices.Count == 0) return false;
|
if (UsbDevice.AllDevices.Count == 0) return false;
|
||||||
if (pIds.Count == 0) return false;
|
if (pIds.Count == 0) return false;
|
||||||
return OpenUSB(pIds.FirstOrDefault());
|
return OpenUSB(pIds.FirstOrDefault());
|
||||||
}
|
}
|
||||||
|
@ -49,7 +59,7 @@ namespace libsharperang {
|
||||||
}
|
}
|
||||||
public bool OpenUSB(Guid deviceId, int deviceIndex) {
|
public bool OpenUSB(Guid deviceId, int deviceIndex) {
|
||||||
//first thought is to say "forgive me lord for i have sinned" but i am absolutely not repentant for this
|
//first thought is to say "forgive me lord for i have sinned" but i am absolutely not repentant for this
|
||||||
bool OpenResult = (from d in UsbDevice.AllWinUsbDevices
|
bool OpenResult = (from d in UsbDevice.AllDevices
|
||||||
where d.DeviceInterfaceGuids.Contains(deviceId) &&
|
where d.DeviceInterfaceGuids.Contains(deviceId) &&
|
||||||
d.DeviceProperties.Where(k=>k.Key=="Address" && (int)k.Value==deviceIndex).Count() > 0
|
d.DeviceProperties.Where(k=>k.Key=="Address" && (int)k.Value==deviceIndex).Count() > 0
|
||||||
select d)
|
select d)
|
||||||
|
@ -59,6 +69,34 @@ namespace libsharperang {
|
||||||
return OpenResult;
|
return OpenResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool ClaimUSB()
|
||||||
|
{
|
||||||
|
if (printer is null || printer.IsOpen) return false;
|
||||||
|
IUsbDevice p = printer as IUsbDevice;
|
||||||
|
p?.SetConfiguration(1);
|
||||||
|
p?.ClaimInterface(0);
|
||||||
|
pUsb.iPrinter = printer;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TODO PollPrinter
|
||||||
|
* disassembly of Paperang for Mac comes up with the following
|
||||||
|
* method.Printer.checkConnect() does:
|
||||||
|
* * method.Printer.checkConnectFlag() && return something
|
||||||
|
* 0x01a022300
|
||||||
|
* 0xffffffff
|
||||||
|
* 0x00000000963007772c610eeeba51099919c46d078ff4
|
||||||
|
*
|
||||||
|
* need to USBPcap the mac software's comms to confirm above
|
||||||
|
*
|
||||||
|
* Paperang for Windows does completely different.
|
||||||
|
* ...apparently when the USB URB_CONTROL response says "COMMAND SET:ESC/POS" that means you literally just communicate with it using the ESC/POS protocol
|
||||||
|
* ...so i've wasted a lot of time trying to reverse-engineer an open spec..
|
||||||
|
*/
|
||||||
|
|
||||||
public string FoundPrinterGuids() => pIds
|
public string FoundPrinterGuids() => pIds
|
||||||
.ConvertAll(p => p.ToString())
|
.ConvertAll(p => p.ToString())
|
||||||
.Aggregate((a, b) => a+","+b);
|
.Aggregate((a, b) => a+","+b);
|
||||||
|
|
Loading…
Reference in New Issue