Saturday, March 28, 2015

Printing to Label Printer

In commerce, shops used to print receipts on a small printer called Receipt Printer. These printers usually do not require a driver. Basically you just treat it as a device and send raw data to it like sending chat messages.

Here I am not talking about what to send to the printer. It is about how to send the raw text to the printer. These printers can generally be connected via Parallel, Serial, USB, WIFI, Bluetooth or Ethernet. The first two is getting rare as most computers and devices now don't have LPT and COM ports.

Modern technology also enable mobile devices like notepads to talk to the printer through wireless communication like Bluetooth or Wifi.

Most shop use a computer/server to manage the Point of Sales system. Therefore, there is always a computing device available. In addition, some may use mobile devices to communicate with the computing device ("server). It is usually the server that does the printing. In some circumstances, the mobile device actually does the printing directly.

In order for the device to talk to the printer, both must be in the same network regardless of whether it is one->one or one->many type of connection. Generally they use a intranet infrastructure if there are two or more devices.

The easiest way is to provide a network system using a standard network router. It need not have a connection to the internet. This method applies to Wifi and Ethernet printer devices.

Another method is to use Bluetooth. This method needs the device to be paired. It does not matter how many can pair with the printer. Some actually does not require pairing. I have little information on this technology.

Once the connection is available, the next task is to use a protocol to talk to the printer.I have not played with Bluetooth enabled printer thus unable to talk about it.

The more common ones usually use Wifi or Ethernet. The two are basically the same except that one is wireless the other one requires a LAN connection.

Since they are connected via network, the best protocol is of course Sockets. To use sockets, devices must be identified by a IP address and a port number. There are plenty of programs that could perform sockets. It will depend on what programming language you use. Just look for "Sockets" in your programming language and you should be able to get plenty of example on how to do it.

No matter what programming language you use, it generally follows the pattern of "opening a connection", "write to the connection" and "close the connection", Here I use PHP as an example.

The very first thing is to connect to the printer. Therefore we use "fsockopen" like ths

$mysock = fsockopen("192.168.0.100", "9100");

The IP address varies according to your network setting. The port number is generally set as 9100. The command allows us to open a connection to the printer.

Once the connection is open, it is treated like any media connection (e.g. file). It is therefore appropriate to use fwrite command (file write) to write to it. Since you generally do not expect a communication to be sent from the printer, you do not need to do fread (file read).

The fwrite command looks like this.

$written = fwrite($mysock, "my text", 7);

It is always good to include the length of the text you are writing. This method of writing to a device is generally referred to as "streaming". When you do streaming with fwrite, there is a peculiar thing that you must always remember. You may write 10000 characters to the printer. Not all the 10000 will be successfully sent. Some will be lost.

Luckily fwrite does returns a number indicating what was successfully sent. You just need to get the returned value and use a substr command to write the rest of the string till all are written. The following code is copied from internet.

function fwrite_stream($fp$string) {
    for (
$written 0$written strlen($string); $written += $fwrite) {
        
$fwrite fwrite($fpsubstr($string$written));
        if (
$fwrite === false) {
            return 
$written;
        }
    }
    return 
$written;
}


I am always amazed as to why fwrite never ensure that all the characters are written. Anyhow, there must be a reason for it.

OK. You now have written the text over to the printer. Obviously you do not want to keep the connection open. The next natural step is to close the connection.

fclose($mysock);

Is it not simple? Actually the hard part is to figure what to sent to the printer. Generally you have to use "ESC" code to achieve this. Most printer actually use Ascii 27 (HEX 1B) to indicate that the text follows is a command instead of pure text. PHP have a nice way of allowing this by another "escape" code. It uses "\x" to indicate that the next two character after it is a HEX code. When PHP sends the text "\x1B" it becomes a single character that is equal to Ascii 27.

I shall not go too deep into this except that most printers expect a start code and a end code. It depends on what code the printer uses. This allow the printer to start interpreting the data and stops all activity accordingly.

If you are thinking of using an iOS device to do it. I suggest you do a deep research first before even trying. Apple does not allow user to even use their own programming codes on iOS devices. Every program that you want to run on iOS has to be code signed (from Apple). This means that you need to pay Apple ($99 a year for personal codes) to write and use your own code in iOS. No wonder people want to "jailbreak" iOS.

Android also requires you to pay but it is a one time payment only. Furthermore, if you do not want to sell your app in Google Play Store, you still can write your own and use it on Android without paying.



No comments:

Post a Comment