《通用小票打印程序》由会员分享,可在线阅读,更多相关《通用小票打印程序(6页珍藏版)》请在金锄头文库上搜索。
1、怎样做出通用的 pos 小票打印程序 POS 小票打印机分为热敏和针式俩种。打印纸的宽度分为 58 毫米、76 毫米和 80 毫米三种。打印接口分为:串口、并口、USB 和网口(以太网)。热敏打印机速度较快,打印的时候噪音少,针打可以使用多联纸自动复印。热敏打印机价格一般比较便宜,不需要频繁地更换色带。并口打印机,可直接端口输出,不需要安装打印机的驱动程序。几乎所有的 POS 小票打印机都可以兼容 EPSON 的 ESC POS 打印机指令。const/ 末尾走纸几行c_run_paper_lines = 6;/ ESC 指令 开钱箱c_OpenMoneyBoxCommand = CHR(27
2、) + CHR(112) + CHR(0) + CHR(17) + CHR(8);/ ESC 指令 自动切纸c_cut_paper = CHR(29) + CHR(86) + CHR(66) + CHR(0);type / usb 接口票打用TOutBufPassThrough = recordnDataLen: word;sEscData: array 0 . 1024 of AnsiChar;end;/ usb 接口开钱箱procedure OpenUSBMoneyBox;varprt: TPrinter;esc: TOutBufPassThrough;begintryprt := Pri
3、nters.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;exceptend;end;/ usb 接口切纸procedure usbCutPaper;varprt: TPrinter;esc: TOutBufPassThrough;begintryprt :=
4、 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;exceptend;end;procedure TfrmReprint.Print80;varsPort: string;RPrinter: TextFile;i: Integer;sBill, sBarcode, sXH, sPor
5、tType: 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.ShopNa
6、me);Writeln(RPrinter, 机号 收款员 交易流水号);sTmp := UserInfo.MachineId + + UserInfo.UserCode;iTmp := 32 - Length(sTmp);i := Length(cdsMaster.FieldByName(saleno).Text);while i iTmp dobeginBillId := BillId + ;i := i + 1;end;BillId := BillId + cdsMaster.FieldByName(saleno).Text;Writeln(RPrinter, sTmp + BillId)
7、;Writeln(RPrinter, 印小票时间: + FormatDatetime(yyyy-mm-dd hh:nn, now);Writeln(RPrinter, -);cdsDetail.First;while not cdsDetail.Eof dobegin/ 序号sXH := cdsDetail.FieldByName(Sequence).Text;while Length(sXH) 2 dobeginsXH := sXH + ;end;/ 金额sMoney := FormatFloat(0.00, cdsDetail.FieldByName(amount).AsFloat);i
8、:= Length(sMoney);sTmp := ;while i 9 dobeginsTmp := sTmp + ;i := i + 1;end;sMoney := sTmp + sMoney;/ 商品名称sGoodName := cdsDetail.FieldByName(goodsName).Text;iLen := Length(sGoodName);while iLen 9 dobeginsGoodName := sGoodName + ;iLen := iLen + 1;end;Writeln(RPrinter, sXH + + sGoodName + cdsDetail.Fie
9、ldByName(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
10、_paper_lines doWriteln(RPrinter, );finallyCloseFile(RPrinter);end;if SameText(UserInfo.PrintPort, lpt) then / 直接并口输出 不要安装票打驱动begin/ 读取文本文件打印小票sPort := LPT1;MyList := TStringList.Create;tryAssignFile(RPrinter, sPort);tryRewrite(RPrinter);MyList.LoadFromFile(sBill);for i := 0 to MyList.Count - 1 dobeg
11、inWriteln(RPrinter, MyList.Stringsi);end;/ 开钱箱write(RPrinter, c_OpenMoneyBoxCommand);write(RPrinter, c_cut_paper);CloseFile(RPrinter);except/ 如果 LPT1 端口不存在,会报错:the specified file not found/ 有些主板不提供 LPT 并口,不屏蔽错误,无法收银end;finallyMyList.Free;end;endelse if SameText(UserInfo.PrintPort, usb) then / 需要安装票打驱动begintryRichEdit1.Font.Size := 12;RichEdit1.Lines.Clear;RichEdit1.Lines.LoadFromFile(sBill);RichEdit1.Print();if UserInfo.openMoneyBox = 1 thenOpenUSBMoneyBox;excepton e: Exception doSysLog.WriteLog(TfrmReprint.Print80 + e.Message);end;end;end;