How to send Font via Bluetooth to Zebra printer - c#

I must send a Font file to my printer Zebra RW420 via bluetooth. Im using Zebra Windows Mobile SDK, but can't find any way to send and store it on printer. I could do it manually by Label Vista but It must be done in 200+ printers.
Anyone have any suggestion or know what method from the SDK I could use?
Thanks in advance.

CISDF is the correct answer, it's probably the checksum value that you are computing that is incorrect. I put a port sniffer on my RW420 attached to a USB port and found this to work. I actually sent some PCX images to the printer, then used them in a label later on.
! CISDF
<filename>
<size>
<cksum>
<data>
There is a CRLF at the end of the 1st four lines. Using 0000 as the checksum causes the printer to ignore any checksum verification (I found some really obscure references to this in some ZPL manuals, tried it and it worked). <filename> is the 8.3 name of the file as it will be stored in the file system on the printer and <size> is the size of the file, 8 characters long and formatted as a hexadecimal number. <cksum> is the two's complement of the sum of the data bytes as the checksum. <data> is, of course, the contents of the file to be stored on the printer.
Here is the actual C# code that I used to send my sample images to the printer:
// calculate the checksum for the file
// get the sum of all the bytes in the data stream
UInt16 sum = 0;
for (int i = 0; i < Properties.Resources.cmlogo.Length; i++)
{
sum += Convert.ToUInt16(Properties.Resources.cmlogo[ i]);
}
// compute the two's complement of the checksum
sum = (Uint16)~sum;
sum += 1;
// create a new printer
MP2Bluetooth bt = new MP2Bluetooth();
// connect to the printer
bt.ConnectPrinter("<MAC ADDRESS>", "<PIN>");
// write the header and data to the printer
bt.Write("! CISDF\r\n");
bt.Write("cmlogo.pcx\r\n");
bt.Write(String.Format("{0:X8}\r\n", Properties.Resources.cmlogo.Length));
bt.Write(String.Format("{0:X4}\r\n", sum)); // checksum, 0000 => ignore checksum
bt.Write(Properties.Resources.cmlogo);
// gracefully close our connection and disconnect
bt.Close();
bt.DisconnectPrinter();
MP2Bluetooth is a class we use internally to abstract BT connections and communications - you have your own as well, I'm sure!

You can use the SDK to send any kind of data. A Zebra font is just a font file with a header on it. So if you capture the output cpf file from Label Vista, you can send that file from the SDK. Just create a connection, and call write(byte[]) with the contents of the file

Related

How to extract image from TCP Stream

I need your help.
I was creating an application in c# that converts the data from the IP camera to an image (JPEG).
I was able to convert the image using the below code:
hex = "FFD8FFDB008400130D0F1.........";/// supply this with the attached hex dump.
byte[] image = HexString2Bytes(hex);
File.WriteAllBytes("visio.png", image);
Process.Start("visio.png");
private static byte[] HexString2Bytes(string hexString)
{
int bytesCount = (hexString.Length) / 2;
byte[] bytes = new byte[bytesCount];
for (int x = 0; x < bytesCount; ++x)
{
bytes[x] = Convert.ToByte(hexString.Substring(x * 2, 2), 16);
}
return bytes;
}
Sometimes I get a better image as expected:https://ibb.co/pxrwn6p
but sometimes I get a distorted image after converting https://ibb.co/9twx5ZT.
I was wondering if there is a problem with the conversion or the way I save the image.
because as per the supplier what I need to do is to directly save the image from the stream.
but since I receive it as a byte and I still need to convert it maybe there is something wrong with my codes.
the image also starts with ÿØÿÛ FF D8 and ends with ÿ Ùÿÿÿÿ (FF D9 FF FF FF FF)
here's the hex dump from their sample app:
https://drive.google.com/file/d/1CMlQ0xaVjM0jfU5A4MB-_HwK54dUMTOr/view?usp=sharing
using their test application the image can be captured and converted the image perfectly.
captured image using their application:https://ibb.co/2KgyLTc
using the hex from the sniff and convert it using my code:
converted image using my code:https://ibb.co/G0WMjht
sample source code:
please bare with my codes because currently this is only my test app before integrating this feature to another app.
https://drive.google.com/file/d/1Ux7zsR39IVNyd1wrBxQPQKA6yM4YnwJN/view?usp=sharing
Thank You in advance.
Looking at the hex-dump it looks like some kind of XML file with embedded image data. Trying to convert this directly to an image will most likely not work, you would need to parse the XML-data to extract the actual image file. But it looks like you have a valid Jpeg header, so I would guess you have found the start of the image at least. But you probably also need to check the length property from the XML-data to find the length of the image-data block.
However, the datablock looks like it contains large sections of zeros, this should not be present in a jpeg file, so it might indicate some data corruption. Possibly from the way the network data is captured.
I would expect cameras to use some higher level protocol than raw TCP. Like Real Time Streaming Protocol, GigE vision, or mjpeg over http. I have not seen any camera that require you to process a raw TCP streams. But since you do not show how the data is fetched it is difficult to tell if there is any mistakes in that code.

Printing from Datecs FML 10KL

I am using .NET CF and my task is to print fiscal and non-fiscal receipt.
So I need to connect to the FML 10 KL via bluetooth.
I am using SerialPort to do this, but after sending commands nothing happens.
I tried sending the commands like this :
byte[] buf = new byte[218];
using (StreamWriter writer = new StreamWriter(inPort.BaseStream))
{
writer.Write(buf);
//inPort.NewLine = "\n";
//var msg = inPort.ReadLine();
}
I populate "buf" with my command.
After that I try to read the responce but everytime I get timeout.Also I tried to "write" with text and not byte array, but I get the same result.
If anyone can give me some advice that would be great.
Most serial devices including Bluetooth SPP and Socket controlled devices do not support Unicode. If you have a string, you need to use Encoding ASCII (or UTF8) getBytes to get a byte arry suitable for the serial or direct socket connection. If you do not care for this, you may get a byte sequence of {0x00, 0x41} (Unicode) for an 'A' instead of the needed {0x41} only.
If you try to print something, verify that the code you send is valid by writing the data to a file and send the file as it is using a terminal application (former HyperTerminal).
Most printers support a Dump mode. Verify that the code you build is transmitted un-altered to the printer by using the dump mode and compare with the validated code that you used to print.
The Operating System and the target device may use a buffer. Ensure the buffer is flushed and then close the Stream before disconnecting.
With the Serial Port class ensure the device and the class use the same parameters, ie both 8Bit etc. For Bluetooth SPP the baud rate is adopted, you may use 115200 or 57600 without failing.
I found a Class that supports the Datecs Fiscal Printing Protocol: https://github.com/wqweto/UcsFiscalPrinters/blob/master/Samples/Demo1/Program.cs you should probably use this or look at how the class does print on the Datecs.
I figured out my problem.It was the connection to the mobile printer.
I am connecting and writing like this:
SerialPort inport = new SerialPort("COM5", 115200, Parity.None, 8,
StopBits.One);
inport.Write(buf, 0, buf.Length);
Thread.Sleep(1000);
I need to use Thread.Sleep because the buffer is getting full and some of data is not getting printed.

Sending file to print not working using System.Drawing.Printing

I'm trying to send a file to print without opening it trough Adobe as suggested by several Answers here; instead, I'm using the PrintQueue library (from System.Drawing.Printing).
What I've accomplished so far:
I have the correct PrintQueue referenced as pq:
PrintQueue pq; //Assume it's correct. The way to get here it isn't easy and it is not needed for the question.
// Call AddJob
PrintSystemJobInfo myPrintJob = pq.AddJob();
// Write a Byte buffer to the JobStream and close the stream
Stream myStream = myPrintJob.JobStream;
Byte[] myByteBuffer = ObjectIHave.ToArray(); //Ignore the ObjectIhave, it actually is Linq object which is correct as well.
myStream.Write(myByteBuffer, 0, myByteBuffer.Length);
myStream.Close();
As I understood from the Microsoft Library it's correctly done but it is not working. Any ideas?
EDIT: Debugging the code I can see that something is being send to the printer but it seems the file is not sent.
You need to render your PDF to the printer. Calling the shell print verb on the file would be the ideal means to do that. If you insist on doing the low level rendering yourself then I recommend using a library like Ghostscript.NET and choose the mswinpr2 device as output.
The mswinpr2 device uses MS Windows printer drivers, and thus should work with any printer with device-independent bitmap (DIB) raster capabilities. The printer resolution cannot be selected directly using PostScript commands from Ghostscript: use the printer setup in the Control Panel instead.
See SendToPrinterSample.cs for example:
string printerName = "YourPrinterName";
string inputFile = #"E:\__test_data\test.pdf";
using (GhostscriptProcessor processor = new GhostscriptProcessor())
{
List<string> switches = new List<string>();
switches.Add("-empty");
switches.Add("-dPrinted");
switches.Add("-dBATCH");
switches.Add("-dNOPAUSE");
switches.Add("-dNOSAFER");
switches.Add("-dNumCopies=1");
switches.Add("-sDEVICE=mswinpr2");
switches.Add("-sOutputFile=%printer%" + printerName);
switches.Add("-f");
switches.Add(inputFile);
processor.StartProcessing(switches.ToArray(), null);
}
If the file has to be printed in both sides you just need to add:
switches.Add("-dDuplex");
switches.Add("-dTumble=0");
You can not just write PDF bytes to a print job. The printer doesn't know how to handle it. The RAW data you send to the printer must describe the document in a printer language specific to the printer. That's what the printer driver does.
You can not print a PDF by just sending it to the printer. You need some piece of software that renders the PDF and then sends the rendered image to the printer.
As the documentation states:
Use this method to write device specific information, to a spool file, that is not automatically included by the Microsoft Windows spooler.
I enhanced the important part of this information.

C# Parameter is not Valid Exception

I am trying to get Avatar from google talk.
I received packet from goole talk server like:
<presence from="xxxxxxxxxxxxx#gmail.com/MessagingA3e8c9465" to="xxxxxxxxxx#gmail.com/Jabber.NetF5D1AB65">
<show>away</show>
<caps:c ver="1.1" node="http://www.android.com/gtalk/client/caps" xmlns:caps="http://jabber.org/protocol/caps" />
<x xmlns="vcard-temp:x:update">
<photo>6373f2ccdf12ef06292ca2257dc0bdc9aa1040c2</photo>
</x>
I thought the hex vale of '<photo>' tag is the avatar (display image) of the contact. (Please correct me if I am wrong.)
I converted that value to byte[]
and used following code to display the image.
pictureBox1.Image = Image.FromStream(new MemoryStream(byte_array));
// byte_array is byte[] converted from hex value.
It raises exception saying:
Parameter is not valid.
I am using the following function to covert from hex to byte[]:
private static byte[] HexString2Bytes(string hexString)
{
int bytesCount = (hexString.Length) / 2;
byte[] bytes = new byte[bytesCount];
for (int x = 0; x < bytesCount; ++x)
{
bytes[x] = Convert.ToByte(hexString.Substring(x * 2, 2), 16);
}
return bytes;
}
I tries many ways but same result.
I also tried to convert the hex value to uppercase, but no luck, same result.
I am using .net 3.5 on windows 8.1 machine.
Thanks
Updated:
Thanks to every one for their comments and answer.
I was wrong the hex value was not avatar (display image).
I sent 'iq' request to server and it gives the avatar.
Thanks a lot.
Happy Coding.
http://www.xmpp.org/extensions/xep-0153.html says following:
Next, the user's client computes the SHA1 hash of the avatar image data itself (not the base64-encoded version) in accordance with RFC 3174 [4]. This hash is then included in the user's presence information as the XML character data of the child of an element qualified by the 'vcard-temp:x:update' namespace, as shown in the following example:
Example 3. User's Client Includes Avatar Hash in Presence Broadcast
So, basically hex value of '' tag is not the avatar, but SHA1 hash of the avatar image.
The hex value that you see is not the display image of the contact. It is a hash of the display image. The logic to get the display image is as follows.
After login on the XMPP client, you start receiving presence messages from the XMPP server.
In the presence message, you receive the hash of the avatar.
Check your local storage, if you have a binary image against the received hash.
If you have a binary image against the hash, then display the avatar on your client from the local storage.
If you do not have a binary image against the hash, send a request for v-card to the XMPP server, for the user against which you received the presence.
On receiving the v-card response, you will find the hash and the display image binary. Store this in some local storage.
For details on the XMPP packets Read section 3.2 on http://www.xmpp.org/extensions/xep-0153.html#retrieve
According to this, the photo is Base64-encoded. So you simple need to call Convert.FromBase64String to get the byte array from the photo element InnerText.

How to convert a png file to .GRF file used for zebra printer

I use Zebraprinter for printing the labels. My printer is 203dpi. For last couple of days i was searching in internet and i found there are Zebraprint utilities.. to convert to DFR format.. which sucks.. they are not fully explaining how to do this.. They just says convert to ~DG format. any print it, which is not happening!!
Rather I would like to convert a png file to a .GRF file and send to the printer for printing.. IS there any deadly available free software in internet which does my needs,
Also, i tired to develop a software which does the job for printing the letters.. which is wiring fine. i don't know how to print pictures using this printer.
I need to convert this image https://imageshack.com/i/pb0BArbep to .GRF format. How can i do all this under a single button press.. Any helps..
Thanks a lot.
Code snippet:-
private void button2_Click(object sender, EventArgs e)
{
string s = Print();
PrintFactory.sendTextToLPT1(s);
}
private string Print()
{
string s = "";
s += "^XA^LH"+ text.textbox + ".GRF,1,1^FS";
s += "^FO250,294^FD^FS";
s += "^XZ";
return s;
}
It's been a month since this was asked, so I don't know if an answer is still needed or not, but I'll have a go at it. I've actually been doing a lot of research on ZPL lately, (one of the reasons I came across this question), and I had to do something similar. With a 203 dpi printer as well, actually. I'm not sure how to convert a PNG to GRF, but I was able to print out a graphic using just a PNG:
^XA
^MNY
^LL203
~DYE:{name},P,P,{file size},,{data}
^XZ
The ^MN has to do with Media Tracking, and you may have to change it a bit to suit your needs, depending on the label. Same thing with ^LL, which specifies the label length. For an 8 dots/mm (203 dpi), the value you use as an argument is calculated by 203.2 * length of label in inches. After that, there's a few values to plug into ~DY (Download graphics, page 112 on the manual), the first of which is the name you want to use to reference the file. I didn't add a file extension, as the printer seemed to do that for me, since I specified it was a PNG in the arguments. The second is the size, in bytes, of the PNG file. And lastly, the actual data from the file in the form of ASCII hex. Now with the file being saved on the printer, I was then able to print the graphic in a script using:
^IME:{name}.PNG
^FS
Note: After uploading the file to the printer, I was able to confirm it did save as a PNG file by connecting to the printer VIA IP in a browser and going to "Directory Listing". I'm not sure if all those printers have this feature, but the one I used did. If it does, you can use this to confirm that the file properly uploaded. (It should be in Onboard Flash)
Hope this helps!
The manual I used is here.
I also came across numerous other StackOverflow questions that helped out a bit, and a few threads on other forums. One question that was also in C# that helped me quite a bit can be found here.

Categories