Managing fiscal printer from Datecs by browser

I would like to share my experience wth a task I had to do for a project at work. The project is a web application billing software. So it needed to be able to print fiscal notes, bons or whatever. There were several ways of dealing with this stuff – creating a XPCOMM extension for FireFox, using activeX control (supporting just IE) and the java applet way. I examined
the first two approaches and it seemed that they are not supporting all the browsers and environments. Datecs FP 550-05 is the fiscal printer I have to test which has a communication protocol and comes with a ready to use dll file. This supposes the use of activeX – I have no experience with that, no real C experience and it turned out the hardest for me. I skipped this.

Developping XPCOMM extension for FireFox is an interesting stuff to dig in but I had no time for that. I skipped this, too.

The way I went on this was java. I found a very usefull ready to use applet here – JavaScript Serial. There is good explanation on the site but I am a noobie on java. Actually I hate this stuff. I have never liked programming in java. So in addition to the guidelines on the site I would say something more:

  • after initializing the applet use setTimeout for about a second before opening the port
  • program you peice of software so that your are sure that the port will be closed. If not it cannot be opened afterwards. You have to close the browser.

The rest is just implementing the protocol of the device that you have to support. For the FP 550 the protocol was not very clear about packaging the messages. There was a tool to send custom commands coming together with the printer. So I started searching for software the can spy com ports. There was nothing handy but the Advanced Serial Data Logger. It is even free for a trial period which is ok for me :) . So I got it started to decode the messages that were being sent from the custom software of the printer. This way I could get more insight on the protocol that was unclear to me and I got the job done.

I want to note just one critical part. I am writing bytes to the port. So I had problems with the cyrillic characters. The page is utf-8 encoded so I had a function that generates the needed bytes to write on the printer like this:

function stringToBytes ( str )
{
  var ch, st, re = [];
  for (var i = 0; i < str.length; i++ )
  {
    ch = str.charCodeAt(i);  // get char
    if(ch >= 1040 && ch <= 1103)
    {
        re = re.concat( [ch - 912] );
    }
    else
    {
        st = [];                 // set up "stack"
        do {
        st.push( ch & 0xFF );  // push byte to stack
        ch = ch >> 8;          // shift value down by 1 byte
    }
    while ( ch );
    // add stack contents to result
    // done because chars have "wrong" endianness
    re = re.concat( st.reverse() );
  }
 }
 // return an array of bytes

 return re;
}

I found this function on stackoverflow but I had to modify it because of the non-latin characters problem. The movement of 912 is when you use utf8. The encoding is different then you have to move the character bits by different ammount.

This post is also available in: Bulgarian

  1. In addition , thank you for the rich article . Where it is possible to write to get the affection of advice in a way that is absolutely like this anyone overseas ?

  2. I apperceive that affection , such as the post good apprentice , amateur people is the advice .

  3. Post quite acceptable . I stumbled aloft capital to your blog or account for your blog post to say that I accept I enjoyed absolutely just .

  4. Other networks, namely Three and O2 have similar features but are not quite as polished and advanced as EE’s offering. Three and O2 offer apps that let you make calls and send texts, but EE’s integration is system wide on select iOS and Android devices

  5. The FBI’s repeated — leaving law-abiding individuals shouldering all of the burdens on liberty, without any offsetting benefit to public safety. Indeed, the FBI’s Repeated warnings that criminals and terrorists are able to “go dark” behind end-to-end encryption methods proves this very point.

  1. July 17th, 2010