How do I send ESC/POS to a receipt printer using Objective-C - objective-c

I would like to print a receipt on an Epson thermal receipt printer using ESC/POS.
The printer is connected by USB and there is a printer driver installed. But I do not want to print a generated raster image. Instead I want to send the raw ESC/POS commands to the printer.
ESC/POS is basically plain text with some escaped commands that allow some additional formatting, cut the paper and even open a cash drawer that is connected to the printer. I've already created a Windows version of my application in C# and already know how to format the data that has to be send to the printer. For all intents and purposes of this question, assume that I have a buffer with valid ESC/POS commands that need to be send to the printer.
C# does not allow me to open a printer directly and write raw data to it, so I basically P/Invoke some functions from the Win32 API.
The code looks like this:
public Boolean Print(string printerName, byte[] document)
{
try
{
NativeMethods.DOC_INFO_1 documentInfo;
IntPtr printerHandle;
documentInfo = new NativeMethods.DOC_INFO_1();
documentInfo.pDataType = "RAW";
documentInfo.pDocName = "POS receipt";
printerHandle = new IntPtr(0);
if (NativeMethods.OpenPrinter(printerName.Normalize(), out printerHandle, IntPtr.Zero))
{
if (NativeMethods.StartDocPrinter(printerHandle, 1, documentInfo))
{
int bytesWritten;
byte[] managedData;
IntPtr unmanagedData;
managedData = document;
unmanagedData = Marshal.AllocCoTaskMem(managedData.Length);
Marshal.Copy(managedData, 0, unmanagedData, managedData.Length);
if (NativeMethods.StartPagePrinter(printerHandle))
{
NativeMethods.WritePrinter(
printerHandle,
unmanagedData,
managedData.Length,
out bytesWritten);
NativeMethods.EndPagePrinter(printerHandle);
}
else
{
throw new Win32Exception();
}
Marshal.FreeCoTaskMem(unmanagedData);
NativeMethods.EndDocPrinter(printerHandle);
}
else
{
throw new Win32Exception();
}
NativeMethods.ClosePrinter(printerHandle);
}
else
{
throw new Win32Exception();
}
}
catch (Exception e)
{
}
}
The Print() command takes the name of the printer and an array of bytes as parameters. The purpose is to open the printer that is named and send the array of bytes as raw data.
I hope there is a solution that uses the printer driver in one way or another, because that abstracts the way the printer is connected. The printer I currently have for testing is connected using USB, but there are also models that have a serial port and can be connected using a Serial to USB cable.
My question:
How would I do the equivalent of the code above in Objective-C?

Related

How do i send an intent via react native to Print Connect zebra app

I am currently trying to communicate with a Zebra printer via a react-native application, on mobile I am trying to send my ZPL code (instructions for the printer to print the content i want) from my application to the printer via PrintConnect, Zebra also provides a pdf file guiding people on how to communicate to the app via intents available here however the examples dislpayed on the guide are using a different language.
My question then is how would i go about replicating this (Page 96, Passthrough Intent example) :
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.zebra.printconnect",
"com.zebra.printconnect.print.PassthroughService"));
intent.putExtra("com.zebra.printconnect.PrintService.PASSTHROUGH_DATA", passthroughBytes);
intent.putExtra("com.zebra.printconnect.PrintService.RESULT_RECEIVER", buildIPCSafeReceiver(new
ResultReceiver(null) {
#Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
if (resultCode == 0) { // Result code 0 indicates success
// Handle successful print
} else {
// Handle unsuccessful print
// Error message (null on successful print)
String errorMessage = resultData.getString("com.zebra.printconnect.PrintService.ERROR_MESSAGE");
}
}
}));
Into something acceptable by the react-native-send-intent package such as this:
SendIntentAndroid.openApp("com.mycorp.myapp", {
"com.mycorp.myapp.reason": "just because",
"com.mycorp.myapp.data": "must be a string",
}).then(wasOpened => {});
Thank you for the time you took to read my question.

Ghost script does not reflect the correct information in the PRN file

I create prn code from pdfs and send them to printers in a C# code to automatize printing jobs. To do that, I use ghost script with following parameters.
gswin32c -dNOPAUSE -dBATCH -sDEVICE=laserjet -sOutputFile="c:/temp/out.prn" "NumberedPages.pdf"
This commandline arguments generates a prn file named out.prn. When I send this file to the printer with following C# code, it prints the pdf file successfully.
public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
{
uint returnedValue = 0;
Int32 dwError = 0, dwWritten = 0;
IntPtr hPrinter = new IntPtr(0);
DOCINFOA di = new DOCINFOA();
bool bSuccess = false; // Assume failure unless you specifically succeed.
di.pDocName = "My C#.NET RAW Document";
di.pDataType = "RAW";
// Open the printer.
if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
{
// Start a document.
returnedValue = StartDocPrinter(hPrinter, 1, di);
if (0 != returnedValue)
{
// Start a page.
if (StartPagePrinter(hPrinter))
{
// Write your bytes.
bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
EndPagePrinter(hPrinter);
}
EndDocPrinter(hPrinter);
}
ClosePrinter(hPrinter);
}
// If you did not succeed, GetLastError may give more information
// about why not.
if (bSuccess == false)
{
dwError = Marshal.GetLastWin32Error();
}
return bSuccess;
}
(https://stackoverflow.com/a/29564132/3079364)
I also want to monitor the print jobs if they are successfully printed by the printer or not. But the prn file which is generated by ghost script does not reflects the correct number of pages. See below
Captured from CZ Print job tracker
When I print this document using Word or Adobe, CZ Print job tracker shows the correct number of pages.
Is there any parameter that I can add to ghost script command to correct this information ?
The output file is simply a PCL file, because that's what laserjet devices understand. This does not contain any information about the number of pages in the file.
The reason the print spooler thinks there's only one page is because you haven't told it differently. You have opened a file, started a page, and spewed the content directly to the printer, there is no way for the spooler to know how many pages that stream contains if you don't tell it where each one starts and stops.
You call StartPagePrinter once, so the print spooler (not unreasonably) assumes there is only one page. If you call StartPagePrinter and EndPagePrinter once for each page, then you will get the correct number of pages. Of course, that means knowing where each page begins and ends in the file output by Ghostscript, which you don't know.
Your best bet would be to use the %d format to OutputFile to produce one file per page, then the page counting will be correct.
There doesn't seem to be any way to tell the print spooler how many pages there are when you are sending data directly to the printer. I guess that's not surprising, since the print spooler isn't actually involved.

Opendaylight: how to get MAC address of switch from datapath ID?

I am developing an application for opendaylight Carbon where I need to know the MAC address of the switch. Can I determine this from the DpnId when the switch connects? Thanks.
Not sure which MAC you are referring to. If you are referring MAC address of each ofport of the DPN then you can register listener for FlowCapableNodeConnector model and you can get MAC by calling FlowCapableNodeConnector#getHardwareAddress in add method of listener. And if you are talking about VM/packet Source/destination MAC, then you first you need to punt the packet to controller and then you can use PacketProcessingListener and extract MAC as shown below.
public void onPacketReceived(PacketReceived notification) {
final short tableId = notification.getTableId().getValue();
final byte[] data = notification.getPayload();
Ethernet res = new Ethernet();
try {
res.deserialize(data, 0, data.length * NetUtils.NumBitsInAByte);
} catch (Exception e) {
LOG.warn("PacketInHandler: Failed to decode Packet ", e);
return;
}
try {
Packet pkt = res.getPayload();
LOG.info("Packet type is ->{}", pkt.getClass().getName());
if (pkt instanceof IPv4) {
IPv4 ipv4 = (IPv4) pkt;
byte[] srcMac = res.getSourceMACAddress();
byte[] dstMac = res.getDestinationMACAddress();
}
}
}
The DPID uniquely identifies the switch. The MAC address is generally not exposed.
Moreover, the switch itself generally does not have a MAC address
(they may have tens of MAC addresses for different functions/interfaces).
Switches work at a lower level, though, they work with MAC addresses.

Brother Label Printer c# program with visual studio 2012 for windows8

I want to make a simple program which prints sth (Just wnt it to write sth )
I ve added Interop.bpac.dll (Found it from samples bin folder)
I ve wrote this code
private void buttonTry_Tapped_1(object sender, TappedRoutedEventArgs e)
{
bpac.DocumentClass doc = new DocumentClass();
if(doc.Open("templateFile.lbx"))
{
doc.GetObject("field1").Text = "Hello";
doc.GetObject("field2").Text = "World";
doc.StartPrint("", PrintOptionConstants.bpoDefault);
doc.PrintOut(1, PrintOptionConstants.bpoDefault);
doc.EndPrint();
doc.Close();
}
}
And it gives an error "Interop type 'bpac.DocumentClass' can not be embedded.Use the applicable interface instead." For now Im trying to print QL700 I ll try some other thermal receipt printers later
And also I couldnt get the templateFile.lbx whats that and where does the program search this file?
Thanks :)
Change Embed Interop Types to False

Arduino: UDP sending yields extra characters

At the moment I have an Arduino board with an Ethernet Shield connected to a router. My computer connects to this router via Wi-Fi. My board and my computer send UDP messages back and forth to each other. My computer is a client, and the board is a server. However I noticed, that when I send a longer UDP message from my computer, and then a shorter UDP message, the Arduino accepts the shorter message, then followed by remaining bits from the longer message.
For instance: if I send "Hello World" from my computer, followed with "Test"; the Arduino will not read the second message as "Test", but rather: "Testo World".
I thought perhaps in was a problem from the Arduino end first. The Arduino stores the messages temporarily in an array called packetBuffer. I tried clearing this buffer before I receive a new message each time. The buffer would clear, but then I would receive the faulty message again.
So I assume the culprit is the computer, the client. On the computer end I have a processing sketch that sends the UDP messages. The example below is not the sketch itself; however it is by far a simpler example that still provides the exact symptoms as I described with my original sketch.
import hypermedia.net.*;
UDP udp; // define the UDP object
void setup() {
udp = new UDP( this, 6000 ); // Create a new datagram connection on port 6000
//udp.log( true ); // <-- printout the connection activity
udp.listen( true ); // and wait for incoming message
}
void keyPressed() {
String IPaddress = "192.168.1.177"; // The remote IP address
int port = 8888; // The destination port
if (keyCode == UP)
{
udp.send("Test", IPaddress, port );
}
else
if (keyCode == DOWN)
{
udp.send("Hello World", IPaddress, port );
}
}
void receive( byte[] data ) { // <-- default handler
//void receive( byte[] data, String IPaddress, int port ) { // <-- extended handler
for(int i=0; i < data.length; i++)
print(char(data[i]));
println();
}
How could I get the sketch to send the right messages?
Of course I am more than willing to provide more information.
There wasn't a direct solution to this problem; so I ended up resorting to a work around. The work around involves dynamically adding zeros to all strings sent to the Arduino so there is always 10 characters sent.
For instance:
If I am to send "Hello Bot", the actual string sent is "Hello Bot0". If I sent an additional message like "Test" after that, the string sent to the Arduino would be "Test000000". The additional zeros would cover up the overlapping characters. One problem with this work around is that I had to prepare the Arduino to accept the zeros also. This work around is also kind of messy for the code. It does work though.
Here's a snippet of code from the computer (client) side. The Arduino code obviously just had to be adjusted to account for the zeros.
public void Send() { //bang button named "Send" activates function
String txtSend = comField.getText(); //Grab text from a textbox to be sent
int txtSendLength = txtSend.length();
for(int i = 0; i < 10-txtSendLength; i++){ //Add zeros until it has 10 char
txtSend = txtSend + "0";
}
udp.send(txtSend, ip, port);
comField.clear(); //Clear the textbox
}
Behold, the very simple and crude solution!
I believe your issue is with properly clearing the buffer. I found a line of code that goes through and clears each character, since it is a character array. There is theoretically no buffer to clear once it is read.
Use:
for(int i=0;i<UDP_TX_PACKET_MAX_SIZE;i++) packetBuffer[i] = 0;
Once you read the data, and that will clear the array. I also found out that when trying to do logic on the data that was received, in order to control some output, I needed to first convert the packetBuffer to a string. After that, all seemed to work correctly.
Hope that help.