分享

怎样做出通用的pos小票打印程序

 quasiceo 2016-08-24

怎样做出通用的pos小票打印程序

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;

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多