Monday, January 25, 2010

dos to *nix text file

One of annoyances when working in both Windows and Linux OS is the different format of the text files created.
In Linux OS, when you open a text file (using vi) which happened to be created within Windows OS, you will see a lot of "^M" character. Really annoying.

In the past I used dos2unix to convert, but I don't use it anymore since I could just use sed, instead.

Here's the command.

$ sed -i -e 's/\r//g' file_name


To make it even easier, I created a shell script which contain that command, like the following:

#!/bin/bash
sed -i -e 's/\r//g' $*


... and you're ready to go.

Tuesday, January 19, 2010

Serialize Form to JSON

I came across a handy javascript function to serialize form to JSON, taken from here

$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};


add above script to your document, then if you are using jQuery you can just do something like the following:

$("form#data").serializeObject();


and as per need, simply use json2.js to convert the (JSON) form object into string:
JSON.stringify($("form#invite_users").serializeObject());

Monday, January 11, 2010

Python and RS323 (ESC/POS) Receipt Printer

Currently, I am working a bit with RS323 (ESC/POS) Receipt Printer. The most interesting part working with it is I do not need to have a driver. This is very helpful because I am planning to use it with Linux OS.
I decided to use Python, so anytime I need to switch to Windows OS, nothing need to be changed in the code part.

Since the printer is using RS323 connection, I need to find the simplest way of communicating with that device in Python. With no effort I found pySerial. Taken from the website itself: This module encapsulates the access for the serial port. It provides backends for Python running on Windows, Linux, BSD (possibly any POSIX compliant system), Jython and IronPython (.NET and Mono). The module named "serial" automatically selects the appropriate backend.

After installing pySerial, I started to play around with it. It is really simple and straight-forward.
It took me sometimes to understand ESC/POS commands, since I was totally not familiar with it.

Just to give some idea on how simple it is, here is an example:

import serial
ser = serial.Serial(port='/dev/ttyS0', baudrate=19200)
ser.portstr #check which serial port
ser.write('Hello World!\n')
ser.close()


I spent sometimes trying to figure out how to work with ESC/POS. One of the issue is the baud rate setting. This must be configured properly otherwise the printer will not print the data properly.

Another thing is that the printer will not print anything if you don't tell it to print data in it's buffer. One way to do this is terminate any string/data you want to print with '\n' (or ASCII decimal 10).

Next step is to know what ESC/POS commands available for your type of printer. I downloaded some docs from the vendor's website, but unfortunately there are only minimum info about ESC/POS programming.
Fortunately, ESC/POS commands is pretty much a standard, so I found some other documents from different vendor, by googling it, and got more informations.

Another interesting thing is that when I tried to print several lines of data in Linux, somehow only partial string in certain line that was printed. Got me confused for a bit, then I realized that this is printer buffer issue. The buffer in the printer is too small for the amount and speed of the data I sent to it. In this case adjustment need to be done, so the printer buffer won't get overloaded and discarded the incoming data.

I don't really like current receipt printer that I work on. Minimum documentations, vendor specific power supply cable, and some other stuff.
I haven't search much about other receipt printer from other vendors, but I am pretty sure that there are some that are more portable to work with.