POS小票打印機分為熱敏和針式倆種。
打印紙的寬度分為58毫米、76毫米和80毫米三種。
打印接口分為:串口、並口、USB和網口(以太網)。
熱敏打印機速度較快,打印的時候噪音少,針打可以使用多聯紙自動復印。
熱敏打印機價格一般比較便宜,不需要頻繁地更換色帶。
並口打印機,可直接"端口輸出",不需要安裝打印機的驅動程序。
幾乎所有的POS小票打印機都可以兼容EPSON的ESC POS打印機指令。
const
// 末尾走紙幾行
c_run_paper_lines = 6;
// ESC指令 開錢箱
c_OpenMoneyBoxCommand = CHR(27) + CHR(112) + CHR(0) + CHR(17) + CHR(8);
// ESC指令 自動切紙
c_cut_paper = CHR(29) + CHR(86) + CHR(66) + CHR(0);
type // usb接口票打用
TOutBufPassThrough = record
nDataLen: word;
sEscData: array [0 .. 1024] of AnsiChar;
end;
// usb接口開錢箱
procedure OpenUSBMoneyBox;
var
prt: TPrinter;
esc: TOutBufPassThrough;
begin
try
prt := Printers.Printer;
prt.beginDoc;
esc.nDataLen := Length(c_OpenMoneyBoxCommand);
strpCopy(esc.sEscData, c_OpenMoneyBoxCommand);
windows.ExtEscape(prt.Handle, PASSTHROUGH, sizeOf(esc), @esc, 0, nil);
prt.endDoc;
except
end;
end;
// usb接口切紙
procedure usbCutPaper;
var
prt: TPrinter;
esc: TOutBufPassThrough;
begin
try
prt := Printers.Printer;
prt.beginDoc;
esc.nDataLen := Length(c_cut_paper);
strpCopy(esc.sEscData, c_cut_paper);
windows.ExtEscape(prt.Handle, PASSTHROUGH, sizeOf(esc), @esc, 0, nil);
prt.endDoc;
except
end;
end;
procedure TfrmReprint.Print80;
var
sPort: string;
RPrinter: TextFile;
i: Integer;
sBill, sBarcode, sXH, sPortType: string;
MyList: TStringList;
BillId: string;
sTmp: string;
iTmp: Integer;
sMoney: string;
sGoodName: string;
iLen: Integer;
sTmp2: string;
begin
// 生成一個小票的文本文件
sBill := ExtractFilePath(Application.ExeName) + 'bill.txt';
AssignFile(RPrinter, sBill);
Rewrite(RPrinter);
try
// 店名
Writeln(RPrinter, ' ' + UserInfo.ShopName);
Writeln(RPrinter, '機號 收款員 交易流水號');
sTmp := UserInfo.MachineId + ' ' + UserInfo.UserCode;
iTmp := 32 - Length(sTmp);
i := Length(cdsMaster.FieldByName('saleno').Text);
while i < iTmp do
begin
BillId := BillId + ' ';
i := i + 1;
end;
BillId := BillId + cdsMaster.FieldByName('saleno').Text;
Writeln(RPrinter, sTmp + BillId);
Writeln(RPrinter, '印小票時間:' + FormatDatetime('yyyy-mm-dd hh:nn', now));
Writeln(RPrinter, '-------------------------------------');
cdsDetail.First;
while not cdsDetail.Eof do
begin
// 序號
sXH := cdsDetail.FieldByName('Sequence').Text;
while Length(sXH) < 2 do
begin
sXH := sXH + ' ';
end;
// 金額
sMoney := FormatFloat('0.00', cdsDetail.FieldByName('amount').AsFloat);
i := Length(sMoney);
sTmp := '';
while i < 9 do
begin
sTmp := sTmp + ' ';
i := i + 1;
end;
sMoney := sTmp + sMoney;
// 商品名稱
sGoodName := cdsDetail.FieldByName('goodsName').Text;
iLen := Length(sGoodName);
while iLen < 9 do
begin
sGoodName := sGoodName + ' ';
iLen := iLen + 1;
end;
Writeln(RPrinter, sXH + ' ' + sGoodName + cdsDetail.FieldByName('qty')
.Text + '*' + FormatFloat('0.00', cdsDetail.FieldByName('buyPrice')
.AsFloat) + sMoney);
cdsDetail.Next;
end;
Writeln(RPrinter, '-------------------------------------');
Writeln(RPrinter, '金額:' + FormatFloat('0.00',
cdsMaster.FieldByName('BalanceAmount').AsFloat));
Writeln(RPrinter, sTmp2);
Writeln(RPrinter, ' 謝謝惠顧!');
// 末尾走紙 行數
for i := 1 to c_run_paper_lines do
Writeln(RPrinter, '');
finally
CloseFile(RPrinter);
end;
if SameText(UserInfo.PrintPort, 'lpt') then // 直接並口輸出 不要安裝票打驅動
begin
// 讀取文本文件打印小票
sPort := 'LPT1';
MyList := TStringList.Create;
try
AssignFile(RPrinter, sPort);
try
Rewrite(RPrinter);
MyList.LoadFromFile(sBill);
for i := 0 to MyList.Count - 1 do
begin
Writeln(RPrinter, MyList.Strings[i]);
end;
// 開錢箱
write(RPrinter, c_OpenMoneyBoxCommand);
write(RPrinter, c_cut_paper);
CloseFile(RPrinter);
except
// 如果LPT1端口不存在,會報錯:the specified file not found
// 有些主板不提供LPT並口,不屏蔽錯誤,無法收銀
end;
finally
MyList.Free;
end;
end
else if SameText(UserInfo.PrintPort, 'usb') then // 需要安裝票打驅動
begin
try
RichEdit1.Font.Size := 12;
RichEdit1.Lines.Clear;
RichEdit1.Lines.LoadFromFile(sBill);
RichEdit1.Print('');
if UserInfo.openMoneyBox = 1 then
OpenUSBMoneyBox;
except
on e: Exception do
SysLog.WriteLog('TfrmReprint.Print80' + e.Message);
end;
end;
end;