劉德軍
(射陽(yáng)縣人民醫(yī)院 信息科 224300)
摘 要:鏈?zhǔn)酱蛴〖埵侨粘J褂玫囊环N紙張類型,通常用于單聯(lián)或多聯(lián)的票據(jù)。通過(guò)針式打印機(jī)的鏈?zhǔn)捷敿埰鞯乃图?,完成打印任?wù)。但對(duì)于非標(biāo)準(zhǔn)格式的鏈?zhǔn)郊垙埓蛴r(shí)卻存在位置偏差的現(xiàn)象,經(jīng)過(guò)連續(xù)多張打印后,位置偏差相當(dāng)嚴(yán)重,甚至不能忍受。
關(guān)鍵詞:鏈?zhǔn)?;非?biāo)準(zhǔn);紙張;偏差
一、前言
作者在給某單位開(kāi)發(fā)信息系統(tǒng)時(shí),使用到該單位以前使用的定制的鏈?zhǔn)狡睋?jù)紙,其規(guī)格為寬12CM,高為5.5孔距(計(jì)算紙張高度的方法為:連續(xù)紙孔數(shù)×0.5×2.54CM=高度,5.5×0.5×2.54=6.985CM)。開(kāi)始測(cè)試時(shí)沒(méi)發(fā)現(xiàn)紙張打印位置偏差問(wèn)題,但隨著系統(tǒng)的正式使用,打印多張后就發(fā)生了明顯示的偏移,張數(shù)越多偏移越嚴(yán)重,由于該發(fā)票是套打格式的,因此該問(wèn)題是不能忍受的。
二、過(guò)程
經(jīng)過(guò)咨詢打印機(jī)廠家工程師,得知此問(wèn)題是因?yàn)檫B續(xù)紙孔數(shù)為非整數(shù),根據(jù)計(jì)算紙張高度的方法為:連續(xù)紙孔數(shù)×0.5×2.54CM=高度,導(dǎo)致紙張高度為6.985CM(即帶三位小數(shù)),而操作系統(tǒng)紙張的精度為兩位小數(shù),因此要么設(shè)為6.98CM,要么設(shè)置為6.99CM,隨著打印張數(shù)的增加,其偏差也在增加。設(shè)為6.98CM時(shí),位置逐漸向正常位置的上方偏移,而設(shè)為6.98CM時(shí),位置逐漸向正常位置的下方偏移。因此除非修改紙張規(guī)格,否則無(wú)法正常打印。偏移情況如圖示:
三、處理方法
通過(guò)不斷的測(cè)試,作者終于找到一種方法來(lái)解決非標(biāo)鏈?zhǔn)郊埓蛴∥恢闷畹膯?wèn)題。解決思路:既然無(wú)法更改紙張,那么就變通處理,只要在經(jīng)過(guò)一定數(shù)量的紙張偏差后,通過(guò)修正的消除該偏差,即可使打印位置回歸正常位置。
根據(jù)這一思路具體方法是:通過(guò)計(jì)算多張紙累積偏差和,調(diào)用打印機(jī)指令集進(jìn)行誤差修正,從而實(shí)現(xiàn)正常打印。(打印指令集:控制打印機(jī)操作的打印控制語(yǔ)言指令集合。計(jì)算機(jī)通過(guò)打印控制語(yǔ)言,以軟件命令的方法來(lái)控制打印機(jī)操作,解釋執(zhí)行打印數(shù)據(jù),獲得打印結(jié)果的。)
四、詳細(xì)步驟
1、新建VB工程,添加模塊,名稱為mdlPrintControl。
2、mdlPrintControl代碼如下:
Option Explicit
‘聲明對(duì)動(dòng)態(tài)鏈接庫(kù)?(DLL)?中外部過(guò)程的引用
Public Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Public Declare Function EndDocPrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Public Declare Function EndPagePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Public Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, ByVal pDefault As Long) As Long
Public Declare Function StartDocPrinter Lib "winspool.drv" Alias "StartDocPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pDocInfo As DOCINFO) As Long
Public Declare Function WritePrinter Lib "winspool.drv" (ByVal hPrinter As Long, pBuf As Any, ByVal cdBuf As Long, pcWritten As Long) As Long
Public Type DOCINFO
pDocName As String
pOutputFile As String
pDatatype As String
End Type
Public lReturn As Long
Public MyDocInfo As DOCINFO
Public lpcWritten As Long
Public sWrittenData As String
Public lhPrinter As Long
Public Sub PrintRollBack() ‘控制打印機(jī)后退
lReturn = OpenPrinter(Printer.DeviceName, lhPrinter, 0)
If lReturn = 0 Then
MsgBox "未找打印機(jī)!"
Exit Sub
End If
MyDocInfo.pDocName = ""
MyDocInfo.pOutputFile = vbNullString
MyDocInfo.pDatatype = vbNullString
StartDocPrinter lhPrinter, 1, MyDocInfo
sWrittenData = Chr(27) + "j" + Chr(30) ‘回退走紙
WritePrinter lhPrinter, ByVal sWrittenData, Len(sWrittenData), lpcWritten
EndPagePrinter lhPrinter
EndDocPrinter lhPrinter
ClosePrinter lhPrinter
End Sub
3、調(diào)用方法
‘將紙張高度設(shè)為6.98CM
‘根據(jù)實(shí)際使用狀態(tài)調(diào)整代碼中數(shù)字40為合適的數(shù)字即可,
If i Mod 40 = 0 Theni為打印張數(shù)計(jì)數(shù)
DoPrint (i) ‘打印操作
Call PrintRollBack ‘調(diào)用控制打印機(jī)后退
Else
DoPrint (i)
End If
五、體會(huì)與總結(jié)
通過(guò)調(diào)用打印機(jī)指令集,成功修正了因非標(biāo)鏈?zhǔn)郊垖?dǎo)致的打印位置偏差問(wèn)題。對(duì)于控制打印機(jī)后退所需的紙張數(shù)可根據(jù)發(fā)票印刷位置與打印位置能忍受的程度來(lái)定,通過(guò)可調(diào)參數(shù)來(lái)使用時(shí)將更為方便。函數(shù)執(zhí)行后,會(huì)產(chǎn)生一行打印任務(wù)。本例調(diào)試環(huán)境為:Window XP(SP3)+Visual Basic 6.0(SP6),運(yùn)行環(huán)境為:Window 10,打印機(jī)型號(hào)為EPSON LQ-635K。
參考文獻(xiàn)
[1]王懷彬等,Visual Basic程序設(shè)計(jì),機(jī)械工業(yè)出版社,2008.04.
[2]北京愛(ài)普生軟件技術(shù)發(fā)展有限公司,ESC/P、ESC/P2、ESC/PK、ESC/PK2程序設(shè)計(jì)指南,1994.08.