Nov 20, 2019


Self-Defense: Can You Protect Yourself and Avoid the Slammer?
"Sell-Defense: Can You Protect Yourself and Avoid the Slammer?" is taken from the chapter by that name in Miller's Court (1982). Miller's title poses a question of interest to everyone concerned about violent crime. Some of what the law says regarding your right to defend yourself may surprise you. What happens to you, for instance, if the assailant you bop in panic, falls, hits his head, and dies?

Self-defense flourishes in periods and places in which the law is weak or nonexistent. In ancient China, Buddhist monks, harried by bandits beyond the control of the central government, developed the techniques that were to become karate and Kungfu as a means of unarmed self-defense. And in the American Old West, where the law stopped at least one town back toward the east, men wore Colt forty-fives strapped to their sides as part of their normal dress.
The right of self-defense, however, becomes severely restricted as civilization closes in. In a modern, law-abiding society, you do not have the right to “settle affair” for yourself; rather you must look to the official organs of the government — the police and the courts — for protection from the lawless behavior of others. Indeed, Thomas Hobbes, one of the more morose English political philosophers (in a line worthy of comic Rodney Dangerfield, Hobbes once wrote that life is “poor, nasty, brutish, and short”), conceived the information of the state as taking place when each man relinquishes his sword to the king, who then becomes the sole possessor of power and the source of justice and safety for all.
Unfortunately, as we all know, the cops can’t be everywhere at once. So, the law will let you take certain measures to protect yourself... Just what you can do - without ending up in the slammer - is the subject of this essay.

Imagine you're a woman living alone in a big city. Lately, a number of people in your apartment building have been mugged right in the neighborhood - the worse place is an unlighted section of street near where you get off the bus from work. Since you often stay late at the office, you have no choice but to walk there after dark. A neighborhood committee has approached the police about getting more protection, but the police say that they already are spread thin and can't spare the manpower. To make matters worse, the city is in financial trouble and will be laying off officers, so in the future you can count on even less protection.
It's becoming clear to you that the neighborhood isn't going to get any safer, so you begin thinking about what you can do to protect yourself. Your thoughts grow more serious when you learn that the woman who lives across the hall was badly beaten by a mugger near the bus stop. Now you're really determined, but what's the first move? Should you learn karate, or buy a gun or can of Mace? Are any of these measures illegal - will they get you into trouble? Now that you're actually considering self-defense, you realize you really don't know what you're allowed to do.
So, the first thing you do is ask a lawyer friend what your rights are. He tells you that the law differs from state to state, but that the basic rule is that you can use reasonable force to repeal real or apparent attack, The amount of force that is "reasonable" differs according to the circumstances, but you're generally justified in using the same amount of force against your assailant as he is using, or proposes to use, against you. And you are allowed to use deadly force (which is force likely to kill, even if it doesn't succeed) against an attack that could kill you or cause you serious bodily harm.
In some states, however, there is a duty to retreat before resorting to deadly force: You must make an effort to escape, if it appears that you can do so safely, before you are entitled to use it. The classic case is the fight at the local saloon, in which the aggressor pulls a knife on the other man, who responds by pulling out a gun. Can he shoot if the knife wielder advances upon him? Not in a duty-to-retreat state if there's a back door to the saloon and he can safely run away. As you can see, the duty to retreat places a higher premium on human life than on human dignity. Thus, it is not surprising that the states that allow you to "stand your ground" are generally found in the South and West regions where the notion of "a man's right to be a man" is more deeply rooted than it is in the East.
Even in states that require you to retreat before using deadly force, there's an exception to the rule: You're generally entitled to stand your ground in your own home if you are menaced by an intruder. This sterns from the old idea that your home is your castle, and that you should not have to retreat from the one place that is indisputably yours. But even in this context, society’s desire to preserve life and discourage “frontier justice" has led a few courts to limit the defense of the home by force.
So now that you know the rules, what do you do next? You enroll in a self-defense class given especially for women. You learn how to chop throats and gouge eyes and stomp insteps and throw all sorts of exotic punches and kicks. You work out diligently, practice hard, and after eight weeks you're given your diploma, which officially declares you a mistress of unarmed combat.
Now, let's suppose a week after graduation from self-defense -school, you get off the bus late one night and start walking home. Suddenly a man comes up and grabs you and starts to wrestle you to the ground. You use your newly acquired expertise to knock him flat on his back. Obviously, you can't be convicted of assault. This is a clear case of self-defense, since you acted to protect yourself from physical attack. A court most likely would conclude that you used reasonable force to repel the attacker, especially since you did not employ deadly force.
Let's say that when you knock him down, he hits his head on the pavement and dies. Does that change anything? The answer is that it probably doesn't, since his death was not a result that could reasonably have been expected to follow from the amount of force you used in the course of legitimate self-defense. In other words, you did not use deadly force, even if death in fact resulted. Assuming that your use of force was justified, the law won't hold you responsible for any unforeseen injury to your opponent
But what if the man hadn't actually gotten close enough to grab you? What if he had suddenly emerged from the shadows, and without waiting for more, you had delivered a kick that sent him sprawling? Suppose it turns out he's a perfectly respectable citizen who just wanted to ask you for directions?
The rule is that you can use force against what reasonably appears to be an attack, even if you were mistaken and weren't really being threatened. For example, if someone points a fake gun at you as a practical joke, you can use force, in this case even deadly force, if under the circumstances you reasonably believed that the gun was real and that the person was about to shoot you. Similarly, if you reasonably believed that the man approaching you was about to attack you, then you would be entitled to protect yourself, even if it turned out that your "assailant" only wanted directions. The key, of course, is whether you were reasonable in believing you were under attack. You are not allowed to let fly every time someone comes near you, or bumps you, or says something unpleasant.
So what happens when you flatten the man who wanted directions? If he did no more than walk toward you, you probably were not justified in using force. He would have had to have done more for you to say you believed he was about to attack you - for example, if he had said something, or had made a threatening gesture, or followed you for a ways before approaching you. But unless you can supply some reason for your "belief" that you were under attack, you can be charged with the crime of assault, and also can be sued by the man you knocked down for money damages to compensate him for his injuries.
How do you react to that? Not surprisingly, you might say that you are an unarmed woman and that if you wait for a potential assailant to make his intentions it may well be too late for you to defend yourself. There may be some truth to your argument, but in addition to your safety, the law has to consider well-being of innocent people who might want to use the city streets. Suppose that you weren't the woman who was approached on the street but were the wife or close friend of the mart who minted directions. How would you feel about his being clobbered by someone who was so jumpy she struck him before even attempting to find out what he wanted?

Thus far we've considered unarmed resistance. But what about weapon? And under what conditions can you use deadly force?
Let's say, you've been going to self-defense school for eights weeks, but so far, the most you can break with your mighty chop is a Hershey bar. Worse yet, your instructor tells you that it takes up to two years before most people can be considered proficient in unarmed combat, and that in your case he thinks that's an extremely optimistic projection. You begin to doubt you’Il ever be a mistress of unarmed combat. So, you decide you'd better get a weapon. There's an old penknife under the sink, it's rusty and the blade just falls open, but it's the best you've got, so you drop it into your handbag.
The next night you have to work late at the office, and it's past midnight when you get off the bus and have to walk through the same dark stretch of street. A man comes out of the darkness toward you. You increase your pace, but he keeps coming. He grabs you by the hair and neck and starts to drag you into a vacant lot. In a panic you reach into your bag, pull out the knife, which drops open, and stab blindly at the man. He lets go and staggers away. You run to the nearest telephone and call the police. When they come, you lead them back to the vacant lot, where your assailant is found lying dead of knife wounds. At the police station he's identified as a convicted rapist, out on parole, who is suspected of having attacked two other women in the past month.
Justified self-defense? The answer is not absolutely clear. There's no question that you were entitled to defend yourself from the attack, but the question is whether you were justified in using deadly force. As stated earlier, the general rule is that you can resort to deadly force only if you reasonably fear your attacker will kill you or cause you serious bodily harm. In our imaginary case, it is not definite what he intended to do, since you killed him before he got very far. It's possible that he only intended to rob you, which would not have justified your using deadly force unless he threatened serious harm to you in the course of the robbery.
You argue that if you wait for him to prove he's making a deadly attack, it may be too late for you to make any defense at all. You say it's one thing to require you to wait to see if he means to attack you, but quite another to expect you to “evaluate” the seriousness of an assault that's underway. You point out that as a woman confronted on a dark street by a man, who usually I will be significantly stronger than you are, you don't have the luxury of testing out the ferocity of his attack - you have to act forcefully and quickly. if you are going to have any sort of chance. You also point out that the man's history indicates that rape, not robbery, was his purpose.
Maybe so, but the law treats the use of deadly force as a very serious matter and permits it only in extremely limited circumstances. Your relative physical weakness does not automatically justify a quick resort to deadly force, although it can be a factor in determining if you acted "reasonably” under the circumstances. One difficulty with your case is that you responded with deadly force to an unarmed attack, and there is an old rule that an attack with fists or hands threatens only an “ordinary assault,” and thus does not justify a defense with a weapon.
By now you may be a bit annoyed at the law's concern about the attacker rather than the victim. How do you respond?
Well, you might say, (1) a man can do serious harm to a woman with just his fists, and (2) the man's prior record, plus the nature of the attack, indicate he was going to rape you. But let's face it, you didn't know anything about that record. Should that make a difference? Unfortunately — for you — it does. The attacker's history of rape-technically cannot help you, since the question of whether you used excessive force has to be determined according to what you knew at the time of the attack. This is the other side of the rule that an apparent attack, even if not real, justifies resistance: Only appearances seem to count. Thus, you cannot claim to be exonerated on the basis of facts you didn't know at the time.
Of course, his being a convicted rapist may help you in that the prosecutor probably will not bring a criminal action against you. The prosecutor is not obliged to charge everyone he thinks might have committed a crime; he uses what is called "prosecutorial discretion" to select which of the many potential cases should be taken to criminal court. Indeed, this discretion may be the reason why there have been relatively few cases in which a woman has been prosecuted for killing a man who tried to rape her. One result is that there is very little law on the question of whether rape constitutes serious bodily injury for purposes of allowing defense with deadly force. Although it's generally assumed that it does, there can't be a definite answer until more cases are decided (thus establishing clear precedent to be followed in other cases) or statutes are enacted to codify this assumption. Of course, even if it were determined that rape alone doesn't justify resistance with deadly force, the victim usually could claim. She had no way of knowing her assailant wouldn't go beyond the sexual assault and seriously injure her.
But enough theory— let's get, back to your case. What would happen if the prosecutor decided to bring you to trial? There's a good chance you'd be acquitted. In light of all the circumstances — a woman alone at night in a dangerous part of the city, the superior strength of your attacker, and the violence of the assault — a jury could easily conclude that you were justified in using deadly force. But be warned; Juries are notoriously unpredictable; moreover, they probably won't be told about the man’s rape record, since it's not relevant to how you reacted. Thus there's a chance they might decide that you were too quick to resort to deadly force, since the assailant was never definitely shown to have threatened you with death or serious bodily harm, and you could end up convicted of manslaughter.
Keep in mind that in only slightly different circumstances you would not be entitled to use deadly force - for example, if the man had threatened only to take your purse or knock you down or even to molest you. These assaults may be me with force, but not force likely to kill. Remember also that in many states you have a duty to-retreat, if it appears you can do so in safety before using deadly force. In an urban environment, the duty to retreat, which is really a duty to avoid deadly confrontation regardless of right or wrong, might take a different form from fleeing the scene of the commotion. Thus if the attack we've been considering had taken place in daytime on a crowded street, a court might say that you had an obligation to yell for help or physically resist before stabbing your assailant, since it is likely that someone would have helped you or that he would have been frightened off.
Finally, the use of deadly force is particularly risky in cases in which the assailant’s acts are ambiguous. Earlier we concluded that you could be charged with assault if you knocked down a man who just wanted directions. Your case would be much worse if you had stabbed him, not only because you would be up for manslaughter - or perhaps murder – instead of assault, but also because you would have a harder time justifying your apprehension as excusing your acts. If the law hesitates to let you assume that a man who walks up to you at night on a deserted street means to assault you, it will be even less inclined to let you assume he was going to kill you or cause you serious bodily harm. The fact of life is that if you're going to use a weapon, you'd better be sure you really have to.

The imaginary case illustrates the problem in applying the traditional law of self-defense to cases involving women. The law has grown up around quarrels between men and assumes the defender and his assailant to be of roughly equal size and strength. It also assumes that an attack without a weapon is not likely to result in death or serious bodily injury, which has given rise to the rule, already mentioned, that deadly force usually is not justified against an unarmed attack.
All this ignores the fact that a woman may be unable effectively to resist an attack by even an unarmed man without resort to weapons, and that she may be seriously injured by a man using only his fists. Still, the policy or severely restricting the use of deadly force makes changes in the law unlikely, although it is possible that the disparity in strength between attacker and defender will be considered more carefully in the future in deciding what was "reasonable" resistance under the circumstances.
Indeed, one recent case suggests that women should not be held to the same standard as men in deciding if they responded too quickly or with too much force, on the theory that they are prone to be fearful of attack because of the passive role traditionally assigned to them. It is questionable, however, that courts will accept this subjective analysis, since the law feels more comfortable with objective standards, which often rely on notions of "reasonableness."
Moreover, pressure to establish a separate standard for women is seen by some as inconsistent, at least psychologically, with the current movement to secure a constitutional amendment guaranteeing equal rights for women. It's somewhat analogous to the ambivalent reaction that greeted the United States Supreme Court's decision upholding the constitutionality of male-only draft registration.
Let's go back to the point at which you had just returned to the vacant lot with the police and round your assailant lying dead of knife wounds. Let's say that everyone agrees you acted in justified self-defense. Are you free to go home? Not necessarily. You could find yourself charged with carrying a concealed weapon. Every state regulates the possession or carrying of certain weapons. In New York, for example, it is illegal to own a gravity knife — one that opens by the weight of the blade. Since that old knife of yours opens that way, in New York you could be charged with a crime.
But everyone agrees that you used the knife hi justified self-defense, so how can you be charged with breaking the law? The answer is that the law considers defending yourself and carrying the knife to be "severable transactions,” which means they are looked at separately in judging whether you have committed a crime. As to stabbing your attacker, you acted in justified self-defense. But carrying the knife, whether you use it or not, is illegal, and you can be prosecuted even if it's agreed that you needed the knife to protect yourself.
The law on what weapons are illegal to possess or carry differs from state to state; local governments such as cities and towns may impose additional restrictions. In general, weapons such as gravity knives, switchblades, blackjacks, and especially firearms, are prohibited or strictly regulated. And you can't count on soft treatment just because you're a law-abiding citizen - at least one state has passed a law making a minimum jail term of one-year mandatory for anyone caught with an unlicensed firearm. So be careful – before you acquire any sort of weapon, you should find out if owning it is a crime or if it must be licensed.
-->

Jun 9, 2014

Hàm đổi số (đọc số) ra chữ bằng VB6, Java và C#.NET

Hàm đổi số hay đọc số ra chữ thường dùng trong các báo cáo và xử lý tiền tệ. Hiện nay code share trên mạng cũng khá nhiều.
Đợt rồi, có ông bạn nhờ, tiện thể mình làm tip này chia sẽ luôn.
Bao gồm code cho Java, C# và VB6 :))
Code này có thể đọc luôn cả trường hợp có dấu thập phân, và có thể chỉnh giới hạn độ dài số tùy ý, miễn là nằm trong phạm vi double

Ý tưởng xử lý cơ bản như sau: Xé nhỏ thành từng bộ 3 số và đọc cụm 3 số này sau đó ghép lại.
Có thắc mắc hay vướng mắc  gì xin để lại comment, thanks

Link download (bấm vào link chờ 5 giây sau đó chọn "Skip Add" để download)
Code VB6:
Code C#: http://p.pw/babk9B
Code Java: http://p.pw/babk99

Jun 27, 2010

All of unlocking HDD Password

Unlocking a password protected harddisk

During development of the Rockbox firmware, on several occations the harddisk has become locked, i.e. password protected. This results in the Archos displaying:

Part. Error
Pls Chck HD

We are still not 100% sure why it happened. Theories range from low-power conditions to accidental chip select failure. It has also happened for normal users, using the standard Archos-supplied firmware, although it was more frequent for us developers.

Note: None of us developers have experienced this problem since march 2002.

We do however know how to unlock the disk:

Windows/DOS unlock

Note: This requires taking the Archos apart, which will void your warranty!

  1. Grab atapwd (written by Alex Mina)
  2. Create a bootable DOS floppy disk, and put atapwd.exe on it
  3. Remove the harddisk from your Archos and plug it into a laptop (or a standard PC, using a 3.5" => 2.5" IDE adapter)
  4. Boot from the floppy and run atapwd.exe
  5. Select the locked harddrive and press enter for the menu
  6. For Fujitsu disks: Choose "unlock with user password", then "disable with user password". The password is empty, so just press enter at the prompt.
  7. For Toshiba and Hitachi disks, if the above doesn't work: Choose "unlock with master password", then "disable with master password". The password is all spaces.
  8. Your disk is now unlocked. Shut down the computer and remove the disk.

Big thanks to Magnus Andersson for discovering the Fujitsu (lack of) user password!

There is also a program for win32, ArchosUnlock.exe, that creates a linux boot disk with the below mentioned patched isd200 driver.

Linux unlock

For those of us using Linux, we have written an isd200 driver patch for unlocking the disk. This modified driver will automatically unlock the disk when you connect your Archos via USB, so you don't have to do anything special. Apply the patch to a 2.4.18 linux kernel tree.

Still locked?

If the above suggestions don't work, here's some background info about the disk lock feature:

The disk lock is a built-in security feature in the disk. It is part of the ATA specification, and thus not specific to any brand or device.

A disk always has two passwords: A User password and a Master password. Most disks support a Master Password Revision Code, which can tell you if the Master password has been changed, or it it still the factory default. The revision code is word 92 in the IDENTIFY response. A value of 0xFFFE means the Master password is unchanged.

A disk can be locked in two modes: High security mode or Maximum security mode. Bit 8 in word 128 of the IDENTIFY response tell you which mode your disk is in: 0 = High, 1 = Maximum.

In High security mode, you can unlock the disk with either the user or master password, using the "SECURITY UNLOCK DEVICE" ATA command. There is an attempt limit, normally set to 5, after which you must power cycle or hard-reset the disk before you can attempt again.

In Maximum security mode, you cannot unlock the disk! The only way to get the disk back to a usable state is to issue the SECURITY ERASE PREPARE command, immediately followed by SECURITY ERASE UNIT. The SECURITY ERASE UNIT command requires the Master password and will completely erase all data on the disk. The operation is rather slow, expect half an hour or more for big disks. (Word 89 in the IDENTIFY response indicates how long the operation will take.)

Apr 10, 2010

Dotnet framework 3.5 SP1 as prerequisite

Q:
i have vs.net 2008 and i have installed sp1 ( for vs and the .net framework3.5) but when i try to add the .net framework 3.5 sp1 as a prerequisite i go the following errors when i build the setup program

there are 57 serrors : here are some sample

Error1The install location for prerequisites has not been set to 'component vendor's web site' and the file 'DotNetFX35SP1\dotNetFX20\aspnet.msp' in item '.NET Framework 3.5 SP1' can not be located on disk. See Help for more information.C:\Documents and Settings\Sami\My Documents\Visual Studio 2008\Projects\WindowsApplication1\Setup1\Setup1.vdprojSetup1

A:
The reason is that we shipped the .NET Frameworks 3.5 SP1 package as Homesite Only, i.e. only supports "Install Prerequisites from Component Vendor's Website". We did this to keep the size of VS 2008 SP1 from being too large.

We did put info in the SP1 readme on how to get the files put on your machine to enable the samesite scenario. Look for "Enable Samesite".

http://download.microsoft.com/download/A/2/8/A2807F78-C861-4B66-9B31-9205C3F22252/VS2008SP1Readme.htm

Enable Samesite for the .NET Framework 3.5 SP1 bootstrapper package

If the .NET Framework 3.5 SP1 bootstrapper package is selected in the Prerequisite dialog box for a Setup project or in ClickOnce publishing, and also the "Download prerequisites from the same location as my application" option is selected, the following build error is shown:

The install location for prerequisites has not been set to 'component vendor's web site' and the file 'dotNetFx35setup.exe' in item 'Microsoft.Net.Framework.3.5.SP1' cannot be located on disk.

To resolve this issue:

Update the Package Data

1. Open the [Program Files]\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 folder or %ProgramFiles(x86)%\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 on x64 operating systems
2. Edit the Product.xml file in Notepad.
3. Paste the following into the < PackageFiles > element:



4. Find the element for < PackageFile Name="dotNetFX30\XPSEPSC-x86-en-US.exe" and change the PublicKey value to: 3082010A0282010100A2DB0A8DCFC2C1499BCDAA3A34AD23596BDB6CBE2122B794C8EAAEBFC6D526C232118BBCDA5D2CFB36561E152BAE8F0DDD14A36E284C7F163F41AC8D40B146880DD98194AD9706D05744765CEAF1FC0EE27F74A333CB74E5EFE361A17E03B745FFD53E12D5B0CA5E0DD07BF2B7130DFC606A2885758CB7ADBC85E817B490BEF516B6625DED11DF3AEE215B8BAF8073C345E3958977609BE7AD77C1378D33142F13DB62C9AE1AA94F9867ADD420393071E08D6746E2C61CF40D5074412FE805246A216B49B092C4B239C742A56D5C184AAB8FD78E833E780A47D8A4B28423C3E2F27B66B14A74BD26414B9C6114604E30C882F3D00B707CEE554D77D2085576810203010001
5. Find the element for < PackageFile Name="dotNetFX30\XPSEPSC-amd64-en-US.exe" and change the PublicKey value to the same as in step 4 above
6. Save the product.xml file



Download and Extract the Core Installation Files

1. Navigate to the following URL: http://go.microsoft.com/fwlink?LinkID=118080
2. Download the dotNetFx35.exe file to your local disk.
3. Open a Command Prompt window and change to the directory to which you downloaded dotNetFx35.exe.
4. At the command prompt, type:
dotNetFx35.exe /x:.
This will extract the Framework files to a folder named “WCU” in the current directory.
5. Copy the contents of the WCU\dotNetFramework folder and paste them in the %Program Files%\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 folder (%ProgramFiles(x86)%\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 on x64 operating systems). Note: Do not copy the WCU\dotNetFramework folder itself. There should be 5 folders under the WCU folder, and each of these should now appear in the DotNetFx35SP1 folder. The folder structure should resemble the following:
o DotNetFx35SP1 (folder)
* dotNetFX20 (folder
* dotNetFX30 (folder)
* dotNetFX35 (folder)
* dotNetMSP (folder)
* TOOLS folder)
* en (or some other localized folder)
* dotNetFx35setup.exe (file)



You may now delete the files and folders you downloaded and extracted in steps 2 and 4.

Apr 6, 2010

Sending a bit image to an Epson TM-T88III receipt printer using C# and ESC/POS

by Nicholas Piasecki on December 9th, 2009

At Skiviez/WFS, our “low-volume fulfillment requests” (which usually simply means “retail orders”) get a simple receipt printed out via an Epson TM-T88III receipt printer.

TM-T88III Receipt Printer

TM-T88III Receipt Printer

You’ve probably seen the TM-T88III before, though the current model is the TM-T88IV–they’re ubiquitous and especially common in restaurants. We chose it because the receipt prints quickly (which reduces the probability that a stack of order receipts gets missorted such that an employee puts the wrong receipt in the wrong parcel), is thermal so there is no ink to replace (which reduces the cost of consumables), uses standard thermal receipt paper (which is cheap, and we can always run out to Staples if we run out), and, perhaps most importantly, we found one in a box in the back of the warehouse.

At the top of the retail receipt, I print out the Skiviez logo. This is stored in a non-volatile area of memory in the printer (called NV RAM in EPSON parlance), and I dole out a command that essentially says “print out the image stored in slot #1,” having previously uploaded the bitmap via the printer driver’s flash utility. This worked great for as long as we only shipped Skiviez orders and only had one TM-T88III printer. But with the advent of WFS, however, Skiviez now ships orders (we call them “fulfillment requests”) for multiple e-commerce stores, each requiring a different logo to appear at the top of the receipt. So I now had two options:

  • I could manually upload the logos for all of the merchants that we ship for into all of the receipt printers that we use, making sure to upload each merchant image into the same NV memory slot for each printer.
  • I could store the merchant’s logo in our database, figure out how to use the “select bit image mode” receipt printer command, and just send the image data just-in-time whenever I generated a receipt.

The second option has obvious maintenance benefits, so that’s the path I started hacking my way down.

A brief primer on ESC/POS and data

Like the Zebra LP2844 and its brethren, the TM-T88III comes with a Windows printer driver that allows Windows and applications to see it as any other GDI-based printer. You can print a Word document to the TM-T88III and it will print, albeit slowly, and it’ll look like crap. That’s because, as with any other GDI printer, the Word document gets rendered as a bitmap and the entire big-ass bitmap gets sent to the poor little receipt printer which then prints it 24 dots at a time. A better method is to use the printer for its intended purpose and use the native printer language to generate the receipt. This allows us to select printer fonts that are specifically designed for the printer’s resolution and speed characteristics and use other advanced features of the printer such as the paper cutter.

That printer language is called ESC/POS, which is entirely unpronounceable and a stupid name. The “POS” stands for “Point of Sale” since this printer belongs to a class of devices commonly known as point of sale devices–cash drawers, VFDs, barcode scanners, and the like. The “ESC” stands for “escape” because the printer treats any data that is sent to it as text–passed straight through–unless it is escaped with a special character to indicate that a command instruction follows in the input stream. The most common escape character used is the ASCII ESC character, and so we have “ESC/POS” as our language name.

If you’ve just picked up one of these printers on eBay, you may find that getting documentation on this language is hard to come by because you need to get a copy of the “ESC/POS Application Programming Guide” from your “EPSON Authorized Dealer,” whoever the hell that is. (Again, we found ours in a box in the back of the warehouse, so my “authorized dealer” is long gone.) Still, some Googling will give you more or less complete and current copies of these guides, which I’ve mirrored here. Go ahead and download them:

(I particularly like how the programming guide has “proprietary” and “confidential” stamped all over it and then proceeds to describe how great EPSON is for “taking initiative” for “expandability” and “universal applicability” on page 7 of the same document. You can’t make this crap up–it’s full of oxymorons.)

Having just come from writing in the EPL2 printer language for our Zebra LP2844 and Zebra ZP 500 Plus thermal label printers, I found the ESC/POS language to be confusing. My confusion mostly stemmed partly from the way the documentation was written and partly because I was used to the way EPL2 worked. Let’s take a look at the documentation for the bit image command that I’m trying to use:

Documentation Snipped

If this were EPL2, I would actually send the document as a string to the printer, so if a command needed the integer 33 as a parameter, I would send the string “33″ (two bytes of data). In ESC/POS, each parameter is a single byte, so if I want to send 33 as a parameter for m in the bit image command above, then I need to send 33 in a single byte: that is, 0b100001, which is 25 + 20 = 32 + 1 = 33.

The reason that this is super confusing is that other parameters in the documentation are specified to be ASCII characters, such as the ‘*’ parameter in the bit image command above. This is because low-level programmers, such as those who designed the ESC/POS language, tend to blur the lines between data types: it’s all bytes at the end of the day. As a result, if you’re using C#, you might be tempted to use a StringBuilder to build up your document to send to the printer. Don’t do it! You’ll inevitably get confused by its overloads. Let’s take that m = 33 parameter as an example:

var sb = new StringBuilder();


// ASCII escape
sb.Append((char)27);

// ASCII '*' for the bit image command
sb.Append('*');

// oops! this appends the string "33" in two bytes, doesn't work
sb.Append(33);

// this is what I want, but semantically weird
sb.Append((char)33);

A much better and less confusing in the long run solution is to eschew any notion that we are dealing with text. Instead, let’s use the semantics that we are sending bytes of raw binary data directly to the printer:

using (var ms = new MemoryStream())

using (var bw = new BinaryWriter(bw))
{
bw.Write(AsciiControlChars.Escape);
bw.Write('*'); // bit-image mode
bw.Write((byte)33); // 24-dot double-density
}

But remember that cast to byte! Otherwise, you’ll get a C# int, which is four bytes, written to the stream, when we only wanted one. Yes, that means that any parameter that an ESC/POS command takes has a maximum value of 255.

You could get by on the StringBuilder method for a while and not have it burn you until you try the bit image command because all the characters that you’re appending happen to be less than 128–greater than that and you start getting into bytes that don’t map to a Unicode character for quirky historical reasons, such as the range 128 to 159, and you’ll be pulling your hair out as to why some of your data is getting “lost.” Just use the BinaryWriter method. You can thank me later.

Why is the bit image command designed this way?

The m parameter in the bit image command as defined in the previous section has 4 values, and each value changes the way we construct the image data bytes and the way the printer interprets them. The first three values are of dubious value, so let’s throw them out for now. We’ll focus on m = 33, which means “24-dot double density mode.” So keep this in mind and set this aside.

When we think of drawing a bitmap in high level software, we usually think of drawing it pixel by pixel, left to right, top to bottom. So the order that the dots get printed as indicated by the programming guide seems strange:

Relationship between image data and the print result

That is, the dots are rendered from top to bottom, then left to right.

Why in the blue hell do I need to rearrange my nice and neat array of bitmap data into this Connect Four scheme of dots? It all comes down to the size of the print head.

For our mental model, the thermal print head in the receipt printer is physically 1 dot wide by 24 dots high where there are 203 dots per inch (square pixels). It moves left to right across the receipt paper, burning up to 24 dots in the vertical (y) direction for each dot in the horizontal (x) direction.

Since we’re talking about a thermal printer here, there’s no concept of gray scale tones–we’ve either burned a dot into the paper (black) or we haven’t. So if we’re sending image data to the printer in bytes, it would make sense to say that a bit set to 1 means “burn a dot” and a bit set to “0″ means do nothing.

But a byte is only 8 bits wide, and we have 24 dots to burn in each “slice” of the image. 24 just happens to be divisible by 8, so we can send 3 bytes of data for each slice to represent our 24 dots. (If the bitmap I want to draw is taller than 24 dots/pixels, then I need to send multiple bit image commands, effectively doing multiple stripes of the image that are 24 dots high; more on this later.)

So, now, the diagram in the programming guide makes sense: we’re not burning 1 dot at time, we’re burning a vertical stripe that is 1 dot wide and 24 dots high at a time, and then moving to the right. So our printer reads our first 3 bytes of image data, burns the dots specified by those 24 bits, then reads the next 3 bytes, and so on.

Aside: Converting the bitmap to monochrome

Right. So a merchant has given me a *.bmp file, and I need to convert that into array of bits that I can send to the receipt printer. It’s a good bet that the bitmap that the merchant sent me for the logo is not monochrome (that is, every pixel is either 100% black or 100% white).

So what we can do is look at each pixel in the bitmap, determine its luma, and if that’s below a certain threshold, count that pixel as black. How did I write the code to do this? I looked the formula up via the intertubes and modified it to fit my needs:

private static BitmapData GetBitmapData(string bmpFileName)

{
using (var bitmap = (Bitmap)Bitmap.FromFile(bmpFileName))
{
var threshold = 127;
var index = 0;
var dimensions = bitmap.Width * bitmap.Height;
var dots = new BitArray(dimensions);

for (var y = 0; y < bitmap.Height; y++)
{
for (var x = 0; x < bitmap.Width; x++)
{
var color = bitmap.GetPixel(x, y);
var luminance = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);
dots[index] = (luminance < threshold);
index++;
}
}

return new BitmapData()
{
Dots = dots,
Height = bitmap.Height,
Width = bitmap.Width
};
}
}

BitmapData is just a little struct that contains the three properties that you see here. After all, once I have my BitArray with 1 indicating black dots and 0 indicating white dots, I don’t need the original Bitmap instance anymore.

Just to clarify, this means that we’re holding onto a BitArray of monochrome data in the Dots property that conceptually looks something like this:

Conceptual Overview

Marshalling the monochrome data

Now for the trickiest part of all. We need to take our BitArray of monochrome data, divide it up into 8-dot chunks, represent those 8-dot chunks as bytes, and send them to the printer in the order required by the bit image command, discussed above. That is, I’ll need to send the data as bytes in this order:

Capture

So the printer will draw them one vertical stripe of three bytes each at a time. (If you don’t get it, print out that image, cut up the bytes, and then arrange them in the order shown in the diagram from the EPSON documentation. It’ll instantly make sense once you see it.) The X’s in the diagram correspond to bits that aren’t in our original image that we have to send anyway as padding. Remember, since we selected the 24-dot double density mode, the printer is going to draw a 1 x 24 dot slice as it moves the print head. My example bitmap is only 4 pixels tall, so I have to send 20 zero bits as padding.

And thus we would end up with our pretty image.

Unfortunately, there is some math involved in translating the bits in our BitArray into these bytes that we need to send. Assuming bw is our reference to a BinaryWriter, here’s the code that does just that:

// So we have our bitmap data sitting in a bit array called "dots."

// This is one long array of 1s (black) and 0s (white) pixels arranged
// as if we had scanned the bitmap from top to bottom, left to right.
// The printer wants to see these arranged in bytes stacked three high.
// So, essentially, we need to read 24 bits for x = 0, generate those
// bytes, and send them to the printer, then keep increasing x. If our
// image is more than 24 dots high, we have to send a second bit image
// command to draw the next slice of 24 dots in the image.

// Set the line spacing to 24 dots, the height of each "stripe" of the
// image that we're drawing. If we don't do this, and we need to
// draw the bitmap in multiple passes, then we'll end up with some
// whitespace between slices of the image since the default line
// height--how much the printer moves on a newline--is 30 dots.
bw.Write(AsciiControlChars.Escape);
bw.Write('3'); // '3' just means 'change line height command'
bw.Write((byte)24);

// OK. So, starting from x = 0, read 24 bits down and send that data
// to the printer. The offset variable keeps track of our global 'y'
// position in the image. For example, if we were drawing a bitmap
// that is 48 pixels high, then this while loop will execute twice,
// once for each pass of 24 dots. On the first pass, the offset is
// 0, and on the second pass, the offset is 24. We keep making
// these 24-dot stripes until we've run past the height of the
// bitmap.
int offset = 0;

while (offset < data.Height)
{
// The third and fourth parameters to the bit image command are
// 'nL' and 'nH'. The 'L' and the 'H' refer to 'low' and 'high', respectively.
// All 'n' really is is the width of the image that we're about to draw.
// Since the width can be greater than 255 dots, the parameter has to
// be split across two bytes, which is why the documentation says the
// width is 'nL' + ('nH' * 256).
bw.Write(AsciiControlChars.Escape);
bw.Write('*'); // bit-image mode
bw.Write((byte)33); // 24-dot double-density
bw.Write(width[0]); // width low byte
bw.Write(width[1]); // width high byte

for (int x = 0; x < data.Width; ++x)
{
// Remember, 24 dots = 24 bits = 3 bytes.
// The 'k' variable keeps track of which of those
// three bytes that we're currently scribbling into.
for (int k = 0; k < 3; ++k)
{
byte slice = 0;

// A byte is 8 bits. The 'b' variable keeps track
// of which bit in the byte we're recording.
for (int b = 0; b < 8; ++b)
{
// Calculate the y position that we're currently
// trying to draw. We take our offset, divide it
// by 8 so we're talking about the y offset in
// terms of bytes, add our current 'k' byte
// offset to that, multiple by 8 to get it in terms
// of bits again, and add our bit offset to it.
int y = (((offset / 8) + k) * 8) + b;

// Calculate the location of the pixel we want in the bit array.
// It'll be at (y * width) + x.
int i = (y * data.Width) + x;

// If the image (or this stripe of the image)
// is shorter than 24 dots, pad with zero.
bool v = false;
if (i < dots.Length)
{
v = dots[i];
}

// Finally, store our bit in the byte that we're currently
// scribbling to. Our current 'b' is actually the exact
// opposite of where we want it to be in the byte, so
// subtract it from 7, shift our bit into place in a temp
// byte, and OR it with the target byte to get it into there.
slice |= (byte)((v ? 1 : 0) << (7 - b));
}

// Phew! Write the damn byte to the buffer
bw.Write(slice);
}
}

// We're done with this 24-dot high pass. Render a newline
// to bump the print head down to the next line
// and keep on trucking.
offset += 24;
bw.Write(AsciiControlChars.Newline);
}

// Restore the line spacing to the default of 30 dots.
bw.Write(AsciiControlChars.Escape);
bw.Write('3');
bw.Write((byte)30);

This code looks confusing, and is, but I’ve tried to document it with explanatory comments in line.

Sending the document to the printer

Finally, there is the task of sending this array of bytes directly to the printer. I’ve talked about this before and the code is uninteresting, requiring some P/Invokes into native APIs. Essentially, you pass in the name of the printer as it appears in the Control Panel, the array of bytes from our BinaryWriter, and you’re all set:

private static void Print(string printerName, byte[] document)

{
NativeMethods.DOC_INFO_1 documentInfo;
IntPtr printerHandle;

documentInfo = new NativeMethods.DOC_INFO_1();
documentInfo.pDataType = "RAW";
documentInfo.pDocName = "Bit Image Test";

printerHandle = new IntPtr(0);

if (NativeMethods.OpenPrinter(printerName.Normalize(), out printerHandle, IntPtr.Zero))
{
if (NativeMethods.StartDocPrinter(printerHandle, 1, documentInfo))
{
int bytesWritten;
byte[] managedData;
IntPtr unmanagedData;

managedData = document;
unmanagedData = Marshal.AllocCoTaskMem(managedData.Length);
Marshal.Copy(managedData, 0, unmanagedData, managedData.Length);

if (NativeMethods.StartPagePrinter(printerHandle))
{
NativeMethods.WritePrinter(
printerHandle,
unmanagedData,
managedData.Length,
out bytesWritten);
NativeMethods.EndPagePrinter(printerHandle);
}
else
{
throw new Win32Exception();
}

Marshal.FreeCoTaskMem(unmanagedData);

NativeMethods.EndDocPrinter(printerHandle);
}
else
{
throw new Win32Exception();
}

NativeMethods.ClosePrinter(printerHandle);
}
else
{
throw new Win32Exception();
}
}

The P/Invoke declarations can be found in the sample code at the end of the article, and the documentation is available on MSDN. But, again, there’s nothing sexy going on here.

Conclusions and delusions

And that’s a wrap. I always find it interesting when I encounter a problem at work–which is a small business by any definition of the phrase–and the Google indicates that no one has ever encountered or documented this problem before. And it’s these types of integration problems that I find fascinating; after years of playing in castles in the sky, dealing with databases and exceptions and HTML forms, it’s nice to know that I can dust off the corners of my brain and my college degree and deal with bits and bytes when I need to. Writing software that makes hardware do stuff is always fun, too.

And after hours of watching the receipt printer spew out lines and lines of gibberish, there’s nothing like the feeling of seeing the bitmap print out and knowing that, finally, things have simply started to work.

Good luck!

Download the code used in this article.