---
title: '熱印表機使用方法'
disqus: hackmd
---
熱印表機使用方法
===
## 下載驅動程式
到WinPos公司官網下載WP-K833 thermal printer的驅動程式,有兩個,兩個都下載。官網網址如下:
http://www.winpos.com.tw/admin/download_tc/front/download.php

## 安裝驅動程式
1. 首先執行USB_CDC_Drivers
2. 執行POSPrinter_Drivers,在下表選擇"以手動設定新增本機印表機或網路印表機"

3. 找到印表機連接到本機的哪個PORT,選取該PORT並安裝。
4. 按下試印紐,如果有成功印出東西表示驅動程式安裝成功。
## 程式碼簡易教學
我把取票機範例專案放在NAS的/volume1/git/Printer裡
在FormWPelnvoiceTest.cs中有影印出取票單的範例,以下將講解該程式碼架構
```C#=
new FormWPeInvoiceTest("王小名", "0456", "COM3");
```
> 這是呼叫影印號碼牌的class,裡面的兩個參數分別是病患的名字與號碼編號,若沒有名字則輸入空字串,以及連接印表機的本機連接阜。
```C#=
static SerialPort com = new SerialPort();
// initial com parameter
com.PortName = comname;
com.BaudRate = 9600;
com.Parity = Parity.None;
com.DataBits = 8;
com.StopBits = StopBits.One;
com.Handshake = Handshake.None;
string caption = "COM Port Error";
string message = "";
// check com hardware handshaking
com.Open();
```
> com用來控制連接印表機的連接阜,在這個例子中印表機連接到本機的COM3連接阜,因此PortName取作"COM3"。
```C#=
// printer command define ------------------------------------------------------
const string SelectPageMode = "\x1B" + "\x2";
const string PrintAndCancelPageMode = "\x1B" + "\x3";
const string PrintNvImage = "\x1C" + "p" + "\x1" + "\x0";
// Position Control Functions ------------------------------------------------------
const string SetAbsolutePrintPosition16Dots = "\x1B" + "$" + "\x10" + "\x0";
const string SetAbsolutePrintPosition40Dots = "\x1B" + "$" + "\x28" + "\x0";
byte[] SetAbsolutePrintPosition208Dots = { 0x1B, (byte)'$', 208, 0 };
byte[] SetAbsolutePrintPosition248Dots = { 0x1B, (byte)'$', 248, 0 };
// cut Control Functions ------------------------------------------------------
const string PartialCut1 = "\x1B" + "m";
const string FeedPartialCut1 = "\x1D" + "VB" + "\x0";
// Font Control Functions ------------------------------------------------------
const string SelectGlobalFontW1H1 = "\x1D" + "!" + "\x0";
const string SelectGlobalFontW1H2 = "\x1D" + "!" + "\x01";
const string SelectGlobalFontW2H2 = "\x1D" + "!" + "\x11";
const string SelectGlobalFontW3H3 = "\x1D" + "!" + "\x22";
const string SelectFontBoldMode = "\x1B" + "E" + "\x1";
const string CancelFontBoldMode = "\x1B" + "E" + "\x0";
// Paper Feed Functions ------------------------------------------------------
const string PaperFeed24Dots = "\x1B" + "J" + "\x18";
const string PaperFeed12Dots = "\x1B" + "J" + "\x0C";
const string PaperBackFeed100Dots = "\x1B" + "K" + "\x64";
const string PaperBackFeed31Dots = "\x1B" + "K" + "\x1F";
const string PaperBackFeed72Dots = "\x1B" + "K" + "\x48";
// Align Format Functions ------------------------------------------------------
const string SelectAlignLeft = "\x1B" + "a" + "\x0";
const string SelectAlignCenter = "\x1B" + "a" + "\x1";
const string SelectAlignRight = "\x1B" + "a" + "\x2";
// 1D Barcode Function -----------------------------------------------------------
const string SelectBarCodeWidthDot1 = "\x1D" + "w" + "\x1";
const string SelectBarCodeHighDot48 = "\x1D" + "h" + "\x32";
const string PrnBarCode39EndByNull = "\x1D" + "k" + "\x4";
// QR Code Function -----------------------------------------------------------
const string SelectQrCodeSize3Dot = "\x1D" + "(k" + "\x3\x0\x31\x43\x3";
const string SelectQrCodeSize4Dot = "\x1D" + "(k" + "\x3\x0\x31\x43\x4";
const string SelectQrCodeErrorCorrectionLevelL = "\x1D" + "(k" + "\x3\x0\x31\x45\x30";
const string SelectQrCodeVersion6 = "\x1D" + "(k" + "\x3\x0\x31\x76\x6";
const string QrCodeDataStoreLeading = "\x1D" + "(k" + "\x3\x0\x31\x50\x30";
const string QrDataPrint = "\x1D" + "(k" + "\x3\x0\x31\x51\x30";
```
> 這些都是用來控制印表機的指令,例如SelectGlobalFontW2H2就是用來調整字體大小為兩倍的指令,而那些字串的意義可以在官網給的操作手冊上查到。下突為官方調整字體大小的指令說明。

```c#=
// 以下資料需在整頁模式運作 -----------------------------------------------
string ss = "\n\n\n\n";
ss += SelectPageMode; //整頁模式
ss += SelectAlignCenter; //文字置中
ss += PrintNvImage; //列印logo, logo須事先下載, 若無logo則忽略
ss += SelectGlobalFontW2H2; //文字兩倍寬兩倍高
ss += "佳里奇美醫院檢驗科\n";
ss += "抽血站採檢號\n\n\n";
ss += SelectGlobalFontW3H3; //文字兩倍寬兩倍高
ss += SelectFontBoldMode; //文字加粗
ss += numbers + "\n";
ss += names + "\n\n\n";
ss += CancelFontBoldMode; //文字取消加粗
ss += SelectGlobalFontW2H2; //文字兩倍寬兩倍高
ss += "號碼非採檢順序\n請依叫號耐心等候\n";
ss += "錯過號碼,請重新取票\n\n\n";
ss += SelectGlobalFontW1H1; //文字回復正常
ss += System.DateTime.Now.ToString();
ss += PrintAndCancelPageMode; //列印並取消整頁模式
ss += "\n\n\n\n\n\n"; //送行
ss += FeedPartialCut1; //切紙
byte[] bb = System.Text.Encoding.GetEncoding("Big5").GetBytes(ss);
com.Write(bb, 0, bb.Length);
```
> 上面是列印出所設計字串的程式碼,把要印的東西編成字串後,再以BIG5加密,最後再用COM3輸出。