Reputation: 89
I'm trying to print a simple line of text using a POS printer. Currently i do not have an actual pos printer hooked up but i do have the microsoft pos printer simulator.
Currently i have a combobox control that will list all found posprinters. The idea is that in this combobox i select the printer that i want and then it will use that printer to print the text.
The code to get all the printers and add them to the combobox is the following code:
// Get available printers and add them to the combobox in the settings menu
PosExplorer posExplorer = new PosExplorer();
DeviceCollection printers = posExplorer.GetDevices("PosPrinter");
for (int i = 0; i < printers.Count; i++)
{
settings_ComboBox_Printer.Items.Add(printers[i].ServiceObjectName);
}
Then i have a button control to print a sample line of text, the code for the button press event is the following:
private void ButCart_PrintOrder_Click(object sender, RoutedEventArgs e)
{
PosExplorer posExplorer = new PosExplorer();
// Get connected printer devices
DeviceCollection printers = posExplorer.GetDevices("PosPrinter");
for (int i = 0; i < printers.Count; i++)
{
if (printers[i].ServiceObjectName == settings_ComboBox_Printer.Text)
{
selectedPrinter = (PosPrinter)printers[i];
}
}
try
{
string text = "test text";
selectedPrinter.PrintNormal(PrinterStation.Receipt, text);
} catch (Exception ae)
{
MessageBox.Show("An error occured: " + ae.ToString());
}
}
And lastely the selectedPrinter object is defined as this:
using Microsoft.PointOfService;
PosPrinter selectedPrinter;
The error that i get in the for loop in the button click event is that the printer[i] returns a DeviceInfo object rather than the device itself. So i would have to retreive the device and afterwards claim it and open it i assume. However i have no clue how to get the actual device and open it.
Thanks in advance!
Upvotes: 0
Views: 897
Reputation: 89
I figured it out, u can initialize the printer using the posexplorer class by feeding it the device info. If anyone else ran into this issue, i changed the code and it works for me now :) I also added some checks to make sure that the printer is actually ready to print. Thanks for the replies and happy holidays!
private void ButCart_PrintOrder_Click(object sender, RoutedEventArgs e)
{
PosExplorer posExplorer = new PosExplorer();
// Get connected printer devices
DeviceCollection printers = posExplorer.GetDevices(DeviceType.PosPrinter);
for (int i = 0; i < printers.Count; i++)
{
if (printers[i].ServiceObjectName == settings_ComboBox_Printer.Text)
{
selectedPrinter = posExplorer.CreateInstance(printers[i]) as PosPrinter;
}
}
try
{
selectedPrinter.Open();
if (!selectedPrinter.Claimed)
{
selectedPrinter.Claim(0);
selectedPrinter.DeviceEnabled = true;
}
bool printerReady = true;
if (selectedPrinter.CoverOpen)
{
MessageBox.Show("Printer cover is open.");
printerReady = false;
}
if (selectedPrinter.PowerState == PowerState.OffOffline)
{
MessageBox.Show("Printer is has no power or is offline");
printerReady = false;
}
if (selectedPrinter.State != ControlState.Idle)
{
MessageBox.Show("Printer is busy");
printerReady = false;
}
if (printerReady)
{
string text = "test text";
selectedPrinter.PrintNormal(PrinterStation.Receipt, text);
}
}
catch (Exception ae)
{
MessageBox.Show("An error occured: " + ae.ToString());
}
}
Upvotes: 0