Skip to content

Commit 8ede501

Browse files
committed
feat: connection through USB port
1 parent ac185fc commit 8ede501

File tree

6 files changed

+391
-1
lines changed

6 files changed

+391
-1
lines changed

ESCPOS_NET.ConsoleTest/ESCPOS_NET.ConsoleTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net6.0</TargetFramework>
6+
<ApplicationManifest>app.manifest</ApplicationManifest>
67
</PropertyGroup>
78

89
<ItemGroup>

ESCPOS_NET.ConsoleTest/Program.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using System.Runtime.InteropServices;
66
using Microsoft.Extensions.Logging;
77
using System.Threading;
8+
using ESCPOS_NET.Printers;
9+
using ESCPOS_NET.Utils;
810

911
namespace ESCPOS_NET.ConsoleTest
1012
{
@@ -32,13 +34,15 @@ static void Main(string[] args)
3234
Console.WriteLine("1 ) Test Serial Port");
3335
Console.WriteLine("2 ) Test Network Printer");
3436
Console.WriteLine("3 ) Test Samba-Shared Printer");
37+
Console.WriteLine("4 ) Test USB Printer");
3538
Console.Write("Choice: ");
3639
string comPort = "";
3740
string ip;
3841
string networkPort;
3942
string smbPath;
43+
string usbPort = string.Empty;
4044
response = Console.ReadLine();
41-
var valid = new List<string> { "1", "2", "3" };
45+
var valid = new List<string> { "1", "2", "3", "4" };
4246
if (!valid.Contains(response))
4347
{
4448
response = "1";
@@ -100,6 +104,43 @@ static void Main(string[] args)
100104

101105
printer = new SambaPrinter(tempFileBasePath: @"C:\Temp", filePath: smbPath);
102106
}
107+
else if (choice == 4)
108+
{
109+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
110+
{
111+
var usboptions = DeviceFinder.GetDevices();//gets the usb devices connected to the pc
112+
if (usboptions.Count > 0)
113+
{
114+
int i = 0;
115+
int num = 1;
116+
while (i < usboptions.Count)
117+
{
118+
119+
Console.WriteLine($"{i + num}. Name: {usboptions[i].BusName} S/N: {usboptions[i].SerialNum}");
120+
i++;
121+
//serial number and name for printer. Name reported might just be USB Printing Support or something generic
122+
//the property necessary for printing is Device Path this is just for UI
123+
}
124+
Console.Write("Choose Printer (eg. 1): ");
125+
string c = Console.ReadLine();
126+
if (int.TryParse(c, out int chosen) && chosen > 0)
127+
{
128+
usbPort = usboptions[chosen - 1].DevicePath;
129+
}
130+
}
131+
printer = new USBPrinter(usbPort);
132+
}
133+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
134+
{
135+
Console.Write("File / usb path (eg. /dev/usb/lp0): ");
136+
usbPort = Console.ReadLine();
137+
if (string.IsNullOrWhiteSpace(usbPort))
138+
{
139+
comPort = "/dev/usb/lp0";
140+
}
141+
printer = new FilePrinter(filePath: usbPort, false);
142+
}
143+
}
103144

104145
bool monitor = false;
105146
Thread.Sleep(500);
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
3+
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
4+
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
5+
<security>
6+
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
7+
<!-- UAC Manifest Options
8+
If you want to change the Windows User Account Control level replace the
9+
requestedExecutionLevel node with one of the following.
10+
11+
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
12+
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
13+
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
14+
15+
Specifying requestedExecutionLevel element will disable file and registry virtualization.
16+
Remove this element if your application requires this virtualization for backwards
17+
compatibility.
18+
-->
19+
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
20+
</requestedPrivileges>
21+
</security>
22+
</trustInfo>
23+
24+
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
25+
<application>
26+
<!-- A list of the Windows versions that this application has been tested on
27+
and is designed to work with. Uncomment the appropriate elements
28+
and Windows will automatically select the most compatible environment. -->
29+
30+
<!-- Windows Vista -->
31+
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->
32+
33+
<!-- Windows 7 -->
34+
<!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->
35+
36+
<!-- Windows 8 -->
37+
<!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->
38+
39+
<!-- Windows 8.1 -->
40+
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->
41+
42+
<!-- Windows 10 -->
43+
<!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />-->
44+
45+
</application>
46+
</compatibility>
47+
48+
<!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher
49+
DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need
50+
to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should
51+
also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config.
52+
53+
Makes the application long-path aware. See https://docs.microsoft.com/windows/win32/fileio/maximum-file-path-limitation -->
54+
<!--
55+
<application xmlns="urn:schemas-microsoft-com:asm.v3">
56+
<windowsSettings>
57+
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
58+
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
59+
</windowsSettings>
60+
</application>
61+
-->
62+
63+
<!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
64+
<!--
65+
<dependency>
66+
<dependentAssembly>
67+
<assemblyIdentity
68+
type="win32"
69+
name="Microsoft.Windows.Common-Controls"
70+
version="6.0.0.0"
71+
processorArchitecture="*"
72+
publicKeyToken="6595b64144ccf1df"
73+
language="*"
74+
/>
75+
</dependentAssembly>
76+
</dependency>
77+
-->
78+
79+
</assembly>

ESCPOS_NET/Printers/USBPrinter.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.IO;
2+
3+
namespace ESCPOS_NET.Printers
4+
{
5+
public class USBPrinter : BasePrinter
6+
{
7+
private readonly FileStream _rfile;
8+
private readonly FileStream _wfile;
9+
public USBPrinter(string usbPath)
10+
: base()
11+
{
12+
//keeping separate file streams performs better
13+
//while using 1 file stream printers were having intermittent issues while printing
14+
//your milege may vary
15+
_rfile = File.Open(usbPath, FileMode.Open, FileAccess.Read);
16+
_wfile = File.Open(usbPath, FileMode.Open, FileAccess.Write);
17+
Writer = new BinaryWriter(_wfile);
18+
Reader = new BinaryReader(_rfile);
19+
}
20+
21+
~USBPrinter()
22+
{
23+
Dispose(false);
24+
}
25+
26+
protected override void OverridableDispose()
27+
{
28+
_rfile?.Close();
29+
_rfile?.Dispose();
30+
_wfile?.Close();
31+
_wfile?.Dispose();
32+
}
33+
}
34+
}

ESCPOS_NET/Utils/DeviceDetails.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace ESCPOS_NET.Utils
6+
{
7+
public class DeviceDetails
8+
{
9+
public string DisplayName { get; set; }
10+
/// <summary>
11+
/// DEVPKEY_Device_BusReportedDeviceDesc <see href="https://github.com/tpn/winsdk-10/blob/master/Include/10.0.16299.0/shared/devpkey.h">see reference</see>
12+
/// </summary>
13+
public string BusName { get; set; }
14+
public string SerialNum { get; set; }
15+
public string DeviceDescription { get; set; }
16+
public string DevicePath { get; set; }
17+
public string Manufacturer { get; set; }
18+
public ushort VID { get; set; }
19+
public ushort PID { get; set; }
20+
}
21+
}

0 commit comments

Comments
 (0)