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($fp, substr($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.



Thursday, March 26, 2015

Transparent background PNG with Google Map

I used to have this problem with using PNG icons on Google Map. It just refused to honor the PNG setting for transparency. This applies to GIF too.

Recently, I have accidentally deleted "New PCN on Bike" map so I had to create a new one from the SHP files which I exported. Again I faced with the issue of icon transparency. Some how I downloaded PNGs from the internet and they all have transparency background enabled. Why then my own creation cannot work?

I tried using graphic editors to see what is the difference between my own PNG and internet ones. Since the PNG are actually icons, I start to search for icon editors instead. No point using a graphic editor that could edit large bitmap files for such a small picture.

One of the icon editor is a shareware but its earlier version is free. This app is called IconFX 1.6. When I open my PNG and compare with other PNG, I noticed that my background is solid color whereas other PNG is "checkered". There is actually no color on the "checkered" area. It is then I realize that I don't even have to set transparency for the PNG files. Just delete the unwanted part and I have transparency. It is as simple as that.

With the recent Google change of heart on the Google Map, icon are now available and more than 5 layers are allowed. The map now looks nicer with many layers.



Wednesday, March 18, 2015

Printing on receipt printer using PHP

Printing in PHP is actually not a right solution. In windows PHP, it does have a dll to do printing. On Mac, and others, you don't have such luck. PHP is server side script which could be anywhere in the world. Why do you need to send print commands at the server? Actually it is usually used in business where the client sent a print copy to the company like for example placing an order. However, the use is very limited. Most of the time the company employee will print using local browser to the local printer.

PHP printing does have its use if it is used within a close vicinity of the user like for example a retail shop. It usually employs one printer for all the requests. The printouts are usually receipts printed in narrow stripes of paper. That's why they are also called "receipt printer". This is especially useful in Point of Sale (POS) system. It is easier to write a web based POS program than a custom application.

Now, it defeats the purpose of having a web based program when you need to install print drivers on the user's computer just to do printing. Why not send print commands directly to the printer? Yes. it is actually done in this way.

There exists a series of printers that does not require drivers. They accept text as input. The early printers usually coded the text using ASCII "ESC" to format the printout. Thus it is usual to refer to this kind of printing as ESC POS because both are closely associated with each other.  Epson is the first one that patented the codes.

With the advance of technology, using a tablet or iPad to do mobile POS is becoming a trend. Device like iPad generally do not have print drivers. It is therefore well suited to run POS systems. The printers that works with mobile devices are usually bluetooth,  WIFI or network printers.

The PHP program to do printing is quite simple. In a few lines of code the printing can be done.

The program starts with

$fs = fsockopen(192.168.0.1.111, 9100);

You can see that the address is actually an Local IP address. 9100 is the normal port used.

The printing starts with an escape code ESC @. This is a command to initiate the printer.

$bytes = fwrite($fs, "\x1B@");

The next series of "fwrite" usually contains a series of ESC codes followed by text to format the text according to the coding commands. There are simply too many such coding commands to be described here.

The last "fwrite"  will be the ESC i - a command to cut paper. Some printers use GS V n to do paper cutting.

It is well known that fwrite don't always send all the data when used on "streams". The term normally refer to sending a series of data (streaming) to remote connected devices. As fwrite does return a value representing the number of bytes sent, it is usually used within a loop where a total byte count is kept and the fwrite is continuous send with the remainder text as value using substr($string, total_byte_sent) as value. You can read about "substr" command in PHP documentation.

After sending all the data, the last step is of course to close the stream

fclose($fs);

It is as simple as that. Actually it took me a week just to write this code as it is the first time I tried printing to streams in PHP.

In real life, the text are usually submitted form data or database so you normally have to use $_POST["var"] or SQL to get the text and then add the ESC codes to format it.

Below is a sample code.

function fwrite_stream($fp, $string) {
    for ($written = 0; $written < strlen($string); $written += $fwrite) {
        $fwrite = fwrite($fp, substr($string, $written));
        if ($fwrite === false) {
            return $written;
        }
    }
    return $written;
}
$fp = fsockopen("192.168.1.3",9100);
if (!$fp){
    die("Cannot open sock");
}
$mytext="Hello this is a test print 13 ";
$string="\x1B@".$mytext."\x1Bd\x07\x1Bi";
$bytes=fwrite_stream($fp, $string);
fclose($fp);
printf('wrote %d bytes',$bytes);




Sunday, February 15, 2015

Internet Privacy - what is that?

Networking used to be totally private. In the early days prior to www you probably can only join BBS (Bulletin Board). Unless you talk a lot, nothing was revealed about you.

Come WWW. It still worked the same until people start to find out about you so that they could send you advertisements. Google, Yahoo etc got their finances doing just that.

How do others find out about you. Simple. Every search you do is recorded. It will show your habit, your liking, your interest. A good profiler can construct a personality trait just from your search habits.

You think that is about all? Think again. It is not just your search that is recorded. Internet service provider practically knows every internet activity that you do over the internet. Internet snoopers can also sniff out network packages pertaining to you. That's why privacy proponents insists that internet access be incognito.

Recently the great news about the killing of Osama bin Laden showed that the house he lived in has no internet or mobile connection. He is probably aware of the snooping capability and thus only rely on human courier.

There are talks about terrorist watch list. How in the world would they form the watch list unless they track your activities. For example, once I looked up information of AK47 (Assault Riffle) because I want to know where is the "safety catch". By just looking up this information, I probably end up in the watch list as normal people don't look up information of that kind. I showed that I have interest in fire arms and thus may link to killing or terrorist activity. This is how scary it is.

To us common people, we don't really need to fear such collection of information unless we are doing something covert and don't want others to know about it. The information collected about us are probably used to send us ADS. Obviously, you will be profiled to see if you have criminal traits or even terrorist traits.

Many privacy advocates don't even want to be profiled. They want to have the freedom to surf the internet incognito. Many internet browsers start to provide "incognito" window ( a browser window that will not record your activity). They even have a "do not track" setting. The former make it slightly less easy for people to track you. The latter is almost useless unless companies respect your setting and don't track you. The end result is just some meager efforts to placate users. There is no stopping others from tracking you especially government agencies. Unless you totally refrain from surfing net, you will be tracked.

Internet users knows that they are being tracked so they have counter measures. Virtual Private Network (VPN) is used to surf net under "total" privacy. You access to the VPN server and then surf from there. Since you only access to the server and the connection is encrypted, it makes it very difficult to track what you do at the server. Obviously, your activity is still know by the VPN server but it is still better than nothing.

Some VPN server even provide free access. I am not that paranoid thus I never subscribe to it. Just do a search for VPN and surf to find the number of providers available.

I really don't care about being profiled as I am just a poor nobody. What I hate is the ADS. Well, I use Firefox that have good plugins that blocks most ads. In Chrome, I don't surf with it. It is just used for mails and social networks (my Firefox even blocks Javascript from running). I do have Facebook adBlock installed plus the not-so-effective ABP. It is good enough for me.




Sunday, February 01, 2015

Communicating with Apple Web Services (GSX) via Filemaker

Web services according to WikiPedia is "a method of communication between two electronic devices over a network." There are websites that provide information as a service. Sites like Google Language API which allows you to translate your website directly without you having to create the translation yourself. This means you could translate your web site to hundreds of languages just with the web services code.

In plain language, web services allows you to call a web site from your website by providing a request, or consuming a request ( a most confusing term), with some data. The web site will then provide you with the result. For example, I send a request to Google Language API with "Japanese" as the required language and "How to speak Japanese" as the data. Google Language API will understand your request and responds with the result as "日本語を話す方法". When you change the language to Korean, and use back the same data, the API will respond with "일본어를하는 방법". Isn't that easier than you writing three different pages each with a different language?

Some web sites provide more than just one functionality. You can actually use the service to perform various actions by using it like a "function" with "parameters". In layman language, it means you need to tell the web service what you want to do with the data provided by you. The web services will understand your request and perform the exact action then return you the result. For example, I can go to a plant information web site and do search, update, create, delete, etc. just by calling the same address with different info.

In a web page, we will normally use Javascript to do the calling and showing the response using the technique call AJAX and a newer technique called JSON as data. It has all the functionality available to do it. You just need to compose what to send and get what is returned.

I have the opportunity to write programs using Filemaker (A database program owned by Apple). It has very good interface that allow very easy creation of database interfaces and web pages. However, its scripting facility is a bit lacking. This is especially true when trying to use the technology above to do translation. You practically have to write a code to do the functionality then write the code that make use of the functionality. That is double the work. Luckily there are plug-in that expand the capability of the application.

The following is a description of what I did to call a Web Service from Apple itself using Filemaker. It is a web service provided by Apple to its agents that provide repair services to its customers. The service provides a number of requests with its associated data. For example, if I need to order a spare part, I will call the web services with the request "orderPart" and "xxxxxx" as part number. Apple will then create an entry in its database that records your request and then send the part to you.

The request is send as a XML that contains the "request action" and the data. The response is also a XML that contains the result of the request. In this way user will see the same display yet the transaction is made in the back ground. The Filemaker program will then display the respond data so that user can see the result of the transaction.

Actually it is easier said than done. There is no such functionality available to talk to web services. Filemaker does have a "insert from URL" function that could call a web site and display the result in a field (a place where you can type in a form). However, it only shows the XML returned from Apple directly which is mostly not understood by user. Its XML facility is also not suited for manipulation with XML in this way. Thus the task is to create a code that could call the web services, send the request in XML format and translate the result to user in a readable format.

The very first task is to create the proper XML. Since there is no such function, it has to be created. XML node is generally in the form "<node>value</node>". As you can see, creating it is not a problem. So instead of typing the "node" and the "value" manually, you just need to call the subscript and pass it with the correct parameters.

Problem is that Filemaker (FMP) cannot pass multiple parameters. It does not have arrays too. There is, however, a "List" function that you can use like "list(value1,value2)". Thus you must combine the two information into one by using "list" before passing it as a parameter. You can even create a sort of array by creating a list within a list.

The XML example above is the basic format. XML, like HTML, also can contain properties. It is in the form "<node property='xxxx'>value</node>". Obviously these nodes can be nested. Setting the property nodes aside (as it is normally not used in "body"). There are four ways to create the XML.

1. The beginning node ("<node>"). With this and the ending node, you can create a nested node to what ever level you want.
2. The ending node ("</node>")
3. The complete node with value ("<node>value</node>").
4. The NULL node ("<node/>")

It is very easy to take the first parameter and treat it as a node by enclosing it with "<" and ">". The value is placed next then the first parameter is used again by enclosing it with "<" and ">
".

For those that only need the node name like 1,2, 4 above. It is constructed accordingly.

The value must not contain characters used by XML itself like "<" and ">". Therefore all the values have to be filtered or modified to ensure that the value does not end up as the XML itself. This is called "escaping". With this we are able to construct any XML body. This script will then be store as a script by itself. It always write to a global variable like $$xml so that it does not even need to return a value. The script then becomes a function that create xml nodes.

How then should we add headers. It is very simple. Since we always access to the same address, the header seldom changes. Why not just store it as a plain variable? This is exactly what I did.

In another script we will assemble the xml step by step. Fist we store the header into the variable $$xml. Then we use "run script" to run the script that creates the xml by passing the appropriate values beginning with a code that define what type of nodes we are creating. Repeat the subscript till the whole xml is complete. The result is then a well formed xml. You could even test the output by storing the $$xml in a field or just a custom dialog to ensure that the xml is correctly form by copying and pasting the result in a xml editor.

With the xml available, the next task is to send it to the Apple web services (GSX). Luckily, this functionality is available as a plug-in. BaseElements has the plug-in that could do a post to a website and get a respond into a variable instead of a input field. This functionality is exactly what we need to talk to the server.

The last part is to get the result out of the xml. This functionality is again not available. However, BaseElements does have the functionality to extract values from the xml. It may not be the functionality that I am looking at but at least it can be done. The best way is to transform the whole response xml into something  that is humanly manageable like an array without having to write long codes to do it. But then with the limitations, it is still better than nothing.

The conclusion is that Filemaker can talk to web services. With the database capability, getting chunks data out of the web services is a possibility.

MBS has an excellent example of communicating with GSX. MBS Blog.

Apple will be changing the format used for sending and receiving data.  Please go to part 2 to see how it could be done.




Friday, January 02, 2015

What is Time?

What actually is time? There are many theories about time. The basic thing about it is the observation from old about the cycle of sunrise and sunset. The cycle is also observed about the season and the stars. They call this observation ‘time’.


Without any equipment, early man roughly divide time into day, night, month, year, seasons etc. As it goes along, there is a need to subdivide to smaller sections called “hour”. Man start to use various method of defining the sections. In the advent of science, clock was invented to provide a easy and portable way of telling time.


It is then men began to wonder about ‘time’ itself. Since everything on earth is measured by length, breath, and height (3 dimension), they began to assume ‘time’ as the fourth dimension and called the four collectively as ‘space-time’.


In my earlier blog I talked about time travel. I did not discuss in length about ‘time’ itself. Here, I will try to explore the concept of time a bit further. Space-time concept has been deep rooted in man’s mind. However, can ‘time’ be really a dimension itself?


A dimension is something that can be measured. Therefore, time dimension should also be measured. However, the method of measuring ‘time’ is simply not real in relation with the other 3 dimensions. You can measure time from the moment you start to the moment you stop. You just can’t measure time from now into the past. Neither can you measure time from now to the future.  The past no longer exist. The future simply hasn’t exist yet. What you have is just ‘now’.


Time travel advocates will say that time dimension exist in multiple instances of ‘now’. The conservation of mass law is broken if this happens. Multiple instances of ‘now’ means the whole ‘now’ instance is replicated into infinity. That is equivalent of creating the whole universe countless times. If matter can be duplicated against the conservation of matter law, the science world would be in chaos.


Time, therefore, is the transition of instances. Matters change its form as it goes along. There will be only one instance of the matter. What we see is the progress of the change (or no change)  and the progress is called ‘time’. What we measured is actually clock cycle of the machine we created and not time itself. There is no 4th dimension. Space-time is simply imagination and not science.


Einstein said, ‘Time has no independent existence apart from the order of events by which we measure it,’ I fully agree.


Einstein later also say  “Speed of time change depending on the speed of the object, and its position in the gravitational field. As speed increases, time is shortened and compressed, and it slows down.”

I don’t quite agree. He is talking about relativity in this case. It does not have any relationship to the fact that time goes on regardless of whether we are moving or not. Since time is not a physical quantity that can be measured, the term “speed” has no meaning except in relative measurement with a clock which should be independent of the movement. If you let the movement interfere with the clock functionality then obviously time changes for you but not for the universe around you. That’s why the clock does not determine ‘time’. ‘Time’ exists before the clock is invented.

I saw an article about NASA use gyroscope in space to measure space-time deviation.  They did measured some deviations. The question is what are they measuring. Pointing to a distance star that was billion light years away and assume that is a constant? 

According to Hubblesite.org "Einstein's theory of general relativity predicts that every object bends light rays through its gravity. This is called gravitational lensing. For our Sun this effect is very weak, but it has been measured. For more massive and distant objects in the Universe much stronger lensing has been seen." What was measured was the light of the stars that reaches earth. So this is one deviation that may cause it.

Gyroscope did point to a consistent point. However, that is only if you rotates it in its place. What if you move the whole gyroscope? Does it points to the same point or does it point to a point that deviates the same distance as it moves?

We all knows that satellites orbits around earth. Earth orbits around the Sun. The sun orbits around the galaxy. The galaxy may also orbits around the universe. Similarly, the star will do exactly the same as earth but at a different orbit. So when you say the gyroscope points to a star. All the deviations will add to the measurement as they don't orbit in perfect circles. It also does not means that they will orbit exactly on the same axis. If it does then you would not observe galaxies merging together.

We were told that the big bang started from one point and is still expanding today. Earth and the distant star are not moving in the same direction (at least it is not known whether they happened to travel in the same direction). This also adds to the deviation.

The measurement period is only 1.5 years. It is not enough to conclusively says that the parameter shifts are constant. 

With all these variables, if you say there is no deviation then it is utterly rubbish. So, does the measurement measure the actual space-time deviation? I am really doubtful.



Thursday, January 01, 2015

Time travel the non-scientific explanation (aka common sense)

People are always intrigued by the idea of time travel. Is it even remotely possible? The following is an effort to try to see the possibility of it in a lay man's way.

First the 'time' element must be explored. What actually is 'time'? Early humans observed the rising and setting of the sun. They also observed the seasonal cycle. Therefore they call the cycle concept 'time'. Time is therefore a concept not a physical property.


As technology improves, people want to have a more precise definition of 'time' cycle. The therefore invented the thing called clock. Clock is supposed to be a machine that measures 'time'. It does resembles the cycle that man observes earlier. But does that actually make 'time' a physical property? It does represents a instrument to define cycles of its motion from the moment it starts to the moment it reaches back to the same position. We probably can call it a cycle of motion. But that does not equates to the 'dimension' (length, breath, and height) that we can measure physically. Generally, time is referred to as the fourth dimension. It is at best man's effort to quantify a measurement of a concept. There are a lot of theories about the space-time (4 dimensions).


With the clock, they invented another concept called 'speed'. The definition of 'speed' is distance divided by time. Its the beginning of mixing physical property with a concept. Obviously people will not regard 'time' as a concept now as they can now 'measure' it. Lets just leave the argument alone as it is not the topic of discussion. 

With the avent of 'speed', peoole start to measure all kind of things. The speed of light is thus derived. The next thing come into mind is that if one could travel faster than the speed of light, one could end up in the future . Thus the concept of time travel comes into being. The next obvious question is whether we could go back in time.


Going back in time already happening right now. The sunlight we see now is actually 8 minutes and 20 sec ago when it happened. We can actually see cosmic activities that happened billion years ago. The real question is going back to our own past. Now if we travel away from the place where we started at 300000 KM per sec (speed of light). We can only see the moment when we started (assuming the speed is 300000 KM in one sec). To see the moment before that, we have to travel much faster than that. At twice the speed of light we barely see the moment one second before hand.


Hold on. Now that we are 0.6 million KM away. How big is the picture that we can see? If you use a standard 50 mm camera (see the image the same size as real one) to take a picture of the starting position, you can practically see nothing. We actually need a huge telescope to see the starting point. How big is the telescope? I let you do the calculation yourself. Did I mention that it takes a few seconds to focus properly?

On the other hand, since the light has not reach us from the cosmic event, we are actually in their 'future' right? So if we travel fast enough towards it, can we reach it at the state in the past? It is equivalent to say that if you fires a gun at me but then I travel fast enough I can reach you before you do it. That is a interesting question. Can you reach a physical event before it happens? Common sense will tell you that it is not possible. No matter how fast we travel, we take time to do it. By the time we reach the object, the event is already gone. Seeing an event is not equal to be in the same physical event. That's why I say 'time' is a concept.

Let us now focus on the idea of travelling forward to the future. Well, before the light start to move, it does not exist. Just like before you switch on the light, you see no light. It is as simple as that. So, even when we tried to travel faster than speed, it still does not exist. 

Obviously some one will argue that what we are seeing now is actually in the past. It will remain an unknown at best.

We see in motion pictures that people travel to the past/future and interacts with the events in the past. Well, that remains only in fiction stories. Remember my very first message that 'time' is a concept? It is not real and cannot coexist with the present.

My concept of time is therefore 'a measure of event that occurred by comparing it with a device that has a constant cycle". It is not a physical property. We use time to predict what is the physical condition after a certain cycle that occurred. Like for example, I travel at 15 KPH for one hour. I can predict where am I after the cycle is completed. 

Time travel is therefore purely imagination. It should remain a flawed science fiction.