ThermalPrinter.cs 19.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
//  ThermalDotNet
//  An ESC/POS serial thermal printer library
//  by yukimizake
//  https://github.com/yukimizake/ThermalDotNet

//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Text;

namespace ThermalDotNet
{
	/// <summary>
	/// ESC/POS serial thermal printer library.
	/// https://github.com/yukimizake/ThermalDotNet
	/// This program is distributed under the GPLv3 licence.
	/// </summary>
	public class ThermalPrinter
	{	
		private SerialPort _serialPort;
		private byte _maxPrintingDots = 7;
		private byte _heatingTime = 80;
		private byte _heatingInterval = 2;
		
		/// <summary>
		/// Delay between two picture lines. (in ms)
		/// </summary>
		public int PictureLineSleepTimeMs = 40;
		/// <summary>
		/// Delay between two text lines. (in ms)
		/// </summary>
		public int WriteLineSleepTimeMs = 0;
		/// <summary>
		/// Current encoding used by the printer.
		/// </summary>
		public string Encoding		{ get; private set; }
		
		/// <summary>
		/// Initializes a new instance of the <see cref="ThermalDotNet.ThermalPrinter"/> class.
		/// </summary>
		/// <param name='serialPort'>
		/// Serial port used by printer.
		/// </param>
		/// <param name='maxPrintingDots'>
		/// Max printing dots (0-255), unit: (n+1)*8 dots, default: 7 ((7+1)*8 = 64 dots)
		/// </param>
		/// <param name='heatingTime'>
		/// Heating time (3-255), unit: 10µs, default: 80 (800µs)
		/// </param>
		/// <param name='heatingInterval'>
		/// Heating interval (0-255), unit: 10µs, default: 2 (20µs)
		/// </param>
		public ThermalPrinter(SerialPort serialPort, byte maxPrintingDots, byte heatingTime, byte heatingInterval)
		{
			_constructor(serialPort,maxPrintingDots,heatingTime,heatingInterval);
		}
		
		/// <summary>
		/// Initializes a new instance of the <see cref="ThermalDotNet.ThermalPrinter"/> class.
		/// </summary>
		/// <param name='serialPort'>
		/// Serial port used by printer.
		/// </param>
		public ThermalPrinter(SerialPort serialPort)
		{
			_constructor(serialPort,_maxPrintingDots,_heatingTime,_heatingInterval);
		}
		
		private void _constructor(SerialPort serialPort, byte maxPrintingDots, byte heatingTime, byte heatingInterval)
		{
			this.Encoding = "ibm850";
			
			_maxPrintingDots = maxPrintingDots;
			_heatingTime = heatingTime;
			_heatingInterval = heatingInterval;

			_serialPort = serialPort;
			Reset();
			SetPrintingParameters(maxPrintingDots,heatingTime,heatingInterval);
			_sendEncoding(this.Encoding);
		}
		
		/// <summary>
		/// Prints the line of text.
		/// </summary>
		/// <param name='text'>
		/// Text to print.
		/// </param>
		public void WriteLine(string text)
		{
			WriteToBuffer(text);
			_writeByte(10);
			System.Threading.Thread.Sleep(WriteLineSleepTimeMs);
		}
		
		/// <summary>
		/// Sends the text to the printer buffer. Does not print until a line feed (0x10) is sent.
		/// </summary>
		/// <param name='text'>
		/// Text to print.
		/// </param>
		public void WriteToBuffer(string text)
		{
			text = text.Trim('\n').Trim('\r');
			byte[] originalBytes = System.Text.Encoding.UTF8.GetBytes(text);
			byte[] outputBytes = System.Text.Encoding.Convert(System.Text.Encoding.UTF8,
				System.Text.Encoding.GetEncoding(this.Encoding),originalBytes);
			_serialPort.Write(outputBytes,0,outputBytes.Length);
		}
		
		/// <summary>
		/// Prints the line of text, white on black.
		/// </summary>
		/// <param name='text'>
		/// Text to print.
		/// </param>
		public void WriteLine_Invert(string text)
		{
			//Sets inversion on
			_writeByte(29);
			_writeByte(66);
			_writeByte(1);
			
			//Sends the text
			WriteLine(text);
			
			//Sets inversion off
			_writeByte(29);
			_writeByte(66);
			_writeByte(0);
			
			LineFeed();
		}
		
		/// <summary>
		/// Prints the line of text, double size.
		/// </summary>
		/// <param name='text'>
		/// Text to print.
		/// </param>
		public void WriteLine_Big(string text)
		{
			const byte DoubleHeight = 1 << 4;
			const byte DoubleWidth = 1 << 5;
			const byte Bold = 1 << 3;
			
			//big on
			_writeByte(27);
			_writeByte(33);
			_writeByte(DoubleHeight + DoubleWidth + Bold);
			
			//Sends the text
			WriteLine(text);
			
			//big off
			_writeByte(27);
			_writeByte(33);
			_writeByte(0);
		}
	
		/// <summary>
		/// Prints the line of text.
		/// </summary>
		/// <param name='text'>
		/// Text to print.
		/// </param>
		/// <param name='style'>
		/// Style of the text.
		/// </param> 
		public void WriteLine(string text, PrintingStyle style)
		{
			WriteLine(text,(byte)style);
		}
		
		/// <summary>
		/// Prints the line of text.
		/// </summary>
		/// <param name='text'>
		/// Text to print.
		/// </param>
		/// <param name='style'>
		/// Style of the text. Can be the sum of PrintingStyle enums.
		/// </param>
		public void WriteLine(string text, byte style)
		{
			byte underlineHeight = 0;
			
			if (_BitTest(style, 0))
			{
				style = _BitClear(style, 0);
				underlineHeight = 1;
			}
			
			if (_BitTest(style, 7))
			{
				style = _BitClear(style, 7);
				underlineHeight = 2;
			}
			
			if (underlineHeight != 0) {
				_writeByte(27);
				_writeByte(45);
				_writeByte(underlineHeight);
			}
			
			//style on
			_writeByte(27);
			_writeByte(33);
			_writeByte((byte)style);
			
			//Sends the text
			WriteLine(text);
			
			//style off
			if (underlineHeight != 0) {
				_writeByte(27);
				_writeByte(45);
				_writeByte(0);
			}
			_writeByte(27);
			_writeByte(33);
			_writeByte(0);
			
		}
		
		/// <summary>
		/// Prints the line of text in bold.
		/// </summary>
		/// <param name='text'>
		/// Text to print.
		/// </param>
		public void WriteLine_Bold(string text)
		{
			//bold on
			BoldOn();
			
			//Sends the text
			WriteLine(text);
			
			//bold off
			BoldOff();
			
			LineFeed();
		}
		
		/// <summary>
		/// Sets bold mode on.
		/// </summary>
		public void BoldOn()
		{
			_writeByte(27);
			_writeByte(32);
			_writeByte(1);
			_writeByte(27);
			_writeByte(69);
			_writeByte(1);
		}
		
		/// <summary>
		/// Sets bold mode off.
		/// </summary>
		public void BoldOff()
		{
			_writeByte(27);
			_writeByte(32);
			_writeByte(0);
			_writeByte(27);
			_writeByte(69);
			_writeByte(0);
		}
		
		/// <summary>
		/// Sets white on black mode on.
		/// </summary>
		public void WhiteOnBlackOn()
		{
			_writeByte(29);
			_writeByte(66);
			_writeByte(1);
		}
		
		/// <summary>
		/// Sets white on black mode off.
		/// </summary>
		public void WhiteOnBlackOff()
		{
			_writeByte(29);
			_writeByte(66);
			_writeByte(0);
		}
		
		/// <summary>
		/// Sets the text size.
		/// </summary>
		/// <param name='doubleWidth'>
		/// Double width
		/// </param>
		/// <param name='doubleHeight'>
		/// Double height
		/// </param>
		public void SetSize(bool doubleWidth, bool doubleHeight)
		{
			int sizeValue = (Convert.ToInt32(doubleWidth))*(0xF0) + (Convert.ToInt32(doubleHeight))*(0x0F);
			_writeByte(29);
			_writeByte(33);
			_writeByte((byte)sizeValue);
		}
		
		///	<summary>
		/// Prints the contents of the buffer and feeds one line.
		/// </summary>
		public void LineFeed()
		{
			_writeByte(10);
		}
		
		/// <summary>
		/// Prints the contents of the buffer and feeds n lines.
		/// </summary>
		/// <param name='lines'>
		/// Number of lines to feed.
		/// </param>
		public void LineFeed(byte lines)
		{
			_writeByte(27);
			_writeByte(100);
			_writeByte(lines);
		}
		
		/// <summary>
		/// Idents the text.
		/// </summary>
		/// <param name='columns'>
		/// Number of columns.
		/// </param>
		public void Indent(byte columns)
		{
			if (columns < 0 || columns > 31) {
				columns = 0;
			}
			
			_writeByte(27);
			_writeByte(66);
			_writeByte(columns);
		}
		
		/// <summary>
		/// Sets the line spacing.
		/// </summary>
		/// <param name='lineSpacing'>
		/// Line spacing (in dots), default value: 32 dots.
		/// </param>
		public void SetLineSpacing(byte lineSpacing)
		{
			_writeByte(27);
			_writeByte(51);
			_writeByte(lineSpacing);
		}
		
		/// <summary>
		/// Aligns the text to the left.
		/// </summary>
		public void SetAlignLeft()
		{
			_writeByte(27);
			_writeByte(97);
			_writeByte(0);
		}
		
		/// <summary>
		/// Centers the text.
		/// </summary>		
		public void SetAlignCenter()
		{
			_writeByte(27);
			_writeByte(97);
			_writeByte(1);
		}
		
		/// <summary>
		/// Aligns the text to the right.
		/// </summary>
		public void SetAlignRight()
		{
			_writeByte(27);
			_writeByte(97);
			_writeByte(2);
		}
		
		/// <summary>
		/// Prints a horizontal line.
		/// </summary>
		/// <param name='length'>
		/// Line length (in characters) (max 32).
		/// </param>
		public void HorizontalLine(int length)
		{
			if (length > 0) {
				if (length > 32) {
					length = 32;
				}
				
				for (int i = 0; i < length; i++) {
					_writeByte(0xC4);
				}
			_writeByte(10);
			}
		}
		
		/// <summary>
		/// Resets the printer.
		/// </summary>
		public void Reset()
		{
			_writeByte(27);
			_writeByte(64);	
			System.Threading.Thread.Sleep(50);
		}
		
		/// <summary>
		/// List of supported barcode types.
		/// </summary>
		public enum BarcodeType
		{
			/// <summary>
			/// UPC-A
			/// </summary>
			upc_a = 0,
			/// <summary>
			/// UPC-E
			/// </summary>
			upc_e = 1,
			/// <summary>
			/// EAN13
			/// </summary>
			ean13 = 2,
			/// <summary>
			/// EAN8
			/// </summary>
			ean8 = 3,
			/// <summary>
			/// CODE 39
			/// </summary>
			code39 = 4,
			/// <summary>
			/// I25
			/// </summary>
			i25 = 5,
			/// <summary>
			/// CODEBAR
			/// </summary>
			codebar = 6,
			/// <summary>
			/// CODE 93
			/// </summary>
			code93 = 7,
			/// <summary>
			/// CODE 128
			/// </summary>
			code128 = 8,
			/// <summary>
			/// CODE 11
			/// </summary>
			code11 = 9,
			/// <summary>
			/// MSI
			/// </summary>
			msi = 10
		}
		
		/// <summary>
		/// Prints the barcode data.
		/// </summary>
		/// <param name='type'>
		/// Type of barcode.
		/// </param>
		/// <param name='data'>
		/// Data to print.
		/// </param>
		public void PrintBarcode(BarcodeType type, string data)
		{
			byte[] originalBytes;
			byte[] outputBytes;
			
			if (type == BarcodeType.code93 || type == BarcodeType.code128)
			{
				originalBytes = System.Text.Encoding.UTF8.GetBytes(data);
				outputBytes = originalBytes;
			} else {
				originalBytes = System.Text.Encoding.UTF8.GetBytes(data.ToUpper());
				outputBytes = System.Text.Encoding.Convert(System.Text.Encoding.UTF8,System.Text.Encoding.GetEncoding(this.Encoding),originalBytes);
			}
			
			switch (type) {
			case BarcodeType.upc_a:
				if (data.Length ==  11 || data.Length ==  12) {
					_writeByte(29);
					_writeByte(107);
					_writeByte(0);
					_serialPort.Write(outputBytes,0,data.Length);
					_writeByte(0);
				}
				break;
			case BarcodeType.upc_e:
				if (data.Length ==  11 || data.Length ==  12) {
					_writeByte(29);
					_writeByte(107);
					_writeByte(1);
					_serialPort.Write(outputBytes,0,data.Length);
					_writeByte(0);
				}
				break;
			case BarcodeType.ean13:
				if (data.Length ==  12 || data.Length ==  13) {
					_writeByte(29);
					_writeByte(107);
					_writeByte(2);
					_serialPort.Write(outputBytes,0,data.Length);
					_writeByte(0);
				}
				break;
			case BarcodeType.ean8:
				if (data.Length ==  7 || data.Length ==  8) {
					_writeByte(29);
					_writeByte(107);
					_writeByte(3);
					_serialPort.Write(outputBytes,0,data.Length);
					_writeByte(0);
				}
				break;
			case BarcodeType.code39:
				if (data.Length > 1) {
					_writeByte(29);
					_writeByte(107);
					_writeByte(4);
					_serialPort.Write(outputBytes,0,data.Length);
					_writeByte(0);
				}
				break;
			case BarcodeType.i25:
				if (data.Length > 1 || data.Length % 2 == 0) {
					_writeByte(29);
					_writeByte(107);
					_writeByte(5);
					_serialPort.Write(outputBytes,0,data.Length);
					_writeByte(0);
				}
				break;
			case BarcodeType.codebar:
				if (data.Length > 1) {
					_writeByte(29);
					_writeByte(107);
					_writeByte(6);
					_serialPort.Write(outputBytes,0,data.Length);
					_writeByte(0);
				}
				break;
			case BarcodeType.code93: //todo: overload PrintBarcode method with a byte array parameter
				if (data.Length > 1) {
					_writeByte(29);
					_writeByte(107);
					_writeByte(7); //todo: use format 2 (init string : 29,107,72) (0x00 can be a value, too)
					_serialPort.Write(outputBytes,0,data.Length);
					_writeByte(0);
				}
				break;
			case BarcodeType.code128: //todo: overload PrintBarcode method with a byte array parameter
				if (data.Length > 1) {
					_writeByte(29);
					_writeByte(107);
					_writeByte(8); //todo: use format 2 (init string : 29,107,73) (0x00 can be a value, too)
					_serialPort.Write(outputBytes,0,data.Length);
					_writeByte(0);
				}
				break;
			case BarcodeType.code11:
				if (data.Length > 1) {
					_writeByte(29);
					_writeByte(107);
					_writeByte(9);
					_serialPort.Write(outputBytes,0,data.Length);
					_writeByte(0);
				}
				break;
			case BarcodeType.msi:
				if (data.Length > 1) {
					_writeByte(29);
					_writeByte(107);
					_writeByte(10);
					_serialPort.Write(outputBytes,0,data.Length);
					_writeByte(0);
				}
				break;
			}
		}
		
		/// <summary>
		/// Selects large barcode mode.
		/// </summary>
		/// <param name='large'>
		/// Large barcode mode.
		/// </param>
		public void SetLargeBarcode(bool large)
		{
			if (large) {
				_writeByte(29);
				_writeByte(119);
				_writeByte(3);
			} else {
				_writeByte(29);
				_writeByte(119);
				_writeByte(2);
			}
		}
		
		/// <summary>
		/// Sets the barcode left space.
		/// </summary>
		/// <param name='spacingDots'>
		/// Spacing dots.
		/// </param>
		public void SetBarcodeLeftSpace(byte spacingDots)
		{
				_writeByte(29);
				_writeByte(120);
				_writeByte(spacingDots);
		}
		
		/// <summary>
		/// Prints the image. The image must be 384px wide.
		/// </summary>
		/// <param name='fileName'>
		/// Image file path.
		/// </param>
		public void PrintImage(string fileName)
		{
			
			if (!File.Exists(fileName)) {
				throw(new Exception("File does not exist."));
			}
			
			PrintImage(new Bitmap(fileName));

		}
		
		/// <summary>
		/// Prints the image. The image must be 384px wide.
		/// </summary>
		/// <param name='image'>
		/// Image to print.
		/// </param>
		public void PrintImage(Bitmap image)
		{
			int width = image.Width;
			int height = image.Height;
			
			byte[,] imgArray = new byte[width,height];
			
			if (width != 384 || height > 65635) {
				throw(new Exception("Image width must be 384px, height cannot exceed 65635px."));
			}
			
			//Processing image data	
			for (int y = 0; y < image.Height; y++) {
				for (int x = 0; x < (image.Width/8); x++) {
					imgArray[x,y] = 0;
					for (byte n = 0; n < 8; n++) {
						Color pixel = image.GetPixel(x*8+n,y);
						if (pixel.GetBrightness() < 0.5) {
							imgArray[x,y] += (byte)(1 << n);
						}
					}
				}	
			}
			
			//Print LSB first bitmap
			_writeByte(18);
			_writeByte(118);
			
			_writeByte((byte)(height & 255)); 	//height LSB
			_writeByte((byte)(height >> 8)); 	//height MSB

			
			for (int y = 0; y < height; y++) {
				System.Threading.Thread.Sleep(PictureLineSleepTimeMs);
				for (int x = 0; x < (width/8); x++) {
					_writeByte(imgArray[x,y]);
				}	
			}
		}
		
		/// <summary>
		/// Sets the printing parameters.
		/// </summary>
		/// <param name='maxPrintingDots'>
		/// Max printing dots (0-255), unit: (n+1)*8 dots, default: 7 (beceause (7+1)*8 = 64 dots)
		/// </param>
		/// <param name='heatingTime'>
		/// Heating time (3-255), unit: 10µs, default: 80 (800µs)
		/// </param>
		/// <param name='heatingInterval'>
		/// Heating interval (0-255), unit: 10µs, default: 2 (20µs)
		/// </param>
		public void SetPrintingParameters(byte maxPrintingDots, byte heatingTime, byte heatingInterval)
		{
			_writeByte(27);
			_writeByte(55);	
			_writeByte(maxPrintingDots);
			_writeByte(heatingTime);				
			_writeByte(heatingInterval);
		}
		
		/// <summary>
		/// Sets the printer offine.
		/// </summary>
		public void Sleep()
		{
			_writeByte(27);
			_writeByte(61);
			_writeByte(0);
		}
		
		/// <summary>
		/// Sets the printer online.
		/// </summary>		
		public void WakeUp()
		{
			_writeByte(27);
			_writeByte(61);
			_writeByte(1);
		}
		
		/// <summary>
		/// Returns a <see cref="System.String"/> that represents the current <see cref="ThermalDotNet.ThermalPrinter"/>.
		/// </summary>
		/// <returns>
		/// A <see cref="System.String"/> that represents the current <see cref="ThermalDotNet.ThermalPrinter"/>.
		/// </returns>
		public override string ToString()
		{
			return string.Format("ThermalPrinter:\n\t_serialPort={0},\n\t_maxPrintingDots={1}," +
				"\n\t_heatingTime={2},\n\t_heatingInterval={3},\n\tPictureLineSleepTimeMs={4}," +
				"\n\tWriteLineSleepTimeMs={5},\n\tEncoding={6}", _serialPort.PortName , _maxPrintingDots,
				_heatingTime, _heatingInterval, PictureLineSleepTimeMs, WriteLineSleepTimeMs, Encoding);
		}
		
		/// <summary>
		/// Returns a printing style.
		/// </summary>
		public enum PrintingStyle
		{
			/// <summary>
			/// White on black.
			/// </summary>
			Reverse = 1 << 1,
			/// <summary>
			/// Updown characters.
			/// </summary>
			Updown = 1 << 2,
			/// <summary>
			/// Bold characters.
			/// </summary>
			Bold = 1 << 3,
			/// <summary>
			/// Double height characters.
			/// </summary>
			DoubleHeight = 1 << 4,
			/// <summary>
			/// Double width characters.
			/// </summary>
			DoubleWidth = 1 << 5,
			/// <summary>
			/// Strikes text.
			/// </summary>
			DeleteLine = 1 << 6,
			/// <summary>
			/// Thin underline.
			/// </summary>
			Underline = 1 << 0,
			/// <summary>
			/// Thick underline.
			/// </summary>
			ThickUnderline = 1 << 7
		}
		
		/// <summary>
		/// Prints the contents of the buffer and feeds n dots.
		/// </summary>
		/// <param name='dotsToFeed'>
		/// Number of dots to feed.
		/// </param>
		public void FeedDots(byte dotsToFeed)
		{
			_writeByte(27);
			_writeByte(74);
			_writeByte(dotsToFeed);
		}
		
		private void _writeByte(byte valueToWrite)
		{
			byte[] tempArray = {valueToWrite};
			_serialPort.Write(tempArray,0,1);
		}
		
		private void _sendEncoding(string encoding)
		{
			switch (encoding)
			{
				case "IBM437":
					_writeByte(27);
					_writeByte(116);
					_writeByte(0);
					break;
				case "ibm850":
					_writeByte(27);
					_writeByte(116);
					_writeByte(1);
					break;				
			}
		}
		
		/// <summary>
        /// Tests the value of a given bit.
        /// </summary>
        /// <param name="valueToTest">The value to test</param>
        /// <param name="testBit">The bit number to test</param>
        /// <returns></returns>
        static private bool _BitTest(byte valueToTest, int testBit)
        {
            return ((valueToTest & (byte)(1 << testBit)) == (byte)(1 << testBit));
        }
		
		/// <summary>
        /// Return the given value with its n bit set.
        /// </summary>
        /// <param name="originalValue">The value to return</param>
        /// <param name="bit">The bit number to set</param>
        /// <returns></returns>
        static private byte _BitSet(byte originalValue, byte bit)
        {
            return originalValue |= (byte)((byte)1 << bit);
        }

        /// <summary>
        /// Return the given value with its n bit cleared.
        /// </summary>
        /// <param name="originalValue">The value to return</param>
        /// <param name="bit">The bit number to clear</param>
        /// <returns></returns>
        static private byte _BitClear(byte originalValue, int bit)
        {
            return originalValue &= (byte)(~(1 << bit));
        }  
	}
}