lukevp / ESC-POS-.NET

Efficient, Easy to Use Thermal Printing & POS (Windows/Linux/OSX, WiFi/BT/USB/Ethernet)
MIT License
523 stars 171 forks source link

Bluetooth Printer Selection Exception #226

Open Cruik opened 1 year ago

Cruik commented 1 year ago

I try to print something on a thermal printer with my .NET MAUI App. How can i specify my Bluetooth-Printer which is already connected to my device?

// USB, Bluetooth, or Serial var printer = new SerialPrinter(portName: "COM5", baudRate: 115200);

The documentation is not that helpful, when i am using this code snippet, i 'm getting the following Exception thrown

{System.DllNotFoundException: libSystem.IO.Ports.Native at System.IO.Ports.SafeSerialDeviceHandle.Open(String portName) at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace) at System.IO.Ports.SerialPort.Open() at ESCPOS_NET.SerialPrinter..ctor(String portName, Int32 baudRate) at Lobi.Pos.Printer.PrintWorker.GetBTPrinter(LobiPrinterSettings printerSetting) in D:\work\awagd\LOBI\New\src\Lobi.Pos.Printer\PrintWorker.cs:line 187 at Lobi.Pos.Printer.PrintWorker.InitiatePrinterConnection(LobiPrinterSettings lobiPrinterSettings, String deviceName) in D:\work\awagd\LOBI\New\src\Lobi.Pos.Printer\PrintWorker.cs:line 134}

lukevp commented 1 year ago

@Cruik this is an os-specific implementation detail that you'll have to figure out and adapt to the library, unfortunately. This library just uses System.IO.Ports for the Serial implementation, so you will need to figure out how to get the correct path and interface technology that your printer needs. For example, on Linux, you would only use the Serial implementation if your printer exposed itself as a Bluetooth Serial device, but some devices expose themselves as /dev/ filesystem items on Linux, and in that case you'd use FilePrinter. It depends on the drivers for your printer and how they pair.

Are you trying to do this from Windows? or from a phone OS?

Cruik commented 1 year ago

I am trying to use it from android

lukevp commented 1 year ago

I've successfully done this in a Xamarin Forms application, so I believe you should be able to do the same in MAUI. Maybe there's something wrong with your build or with the DLL loading, because I see it says that the DLL for System.IO.Ports.Native wasn't found (which is probably the Android-specific implementation).

Regarding connecting to the printer, I would suggest using BLE - then you need to use a library that supports BLE to find devices, connect to them (you'd make a UI to display a list of printers when scanned from bluetooth, for example), and then you can send bytes to them.