Browse Source

打印服务,串口打印

master
娄文智 3 years ago
parent
commit
b163fa8f85
5 changed files with 92 additions and 39 deletions
  1. +34
    -29
      common/Utils.go
  2. +1
    -0
      config/config.go
  3. +5
    -3
      config/config.yaml
  4. +17
    -5
      db/Printertab.go
  5. +35
    -2
      engine/printer.go

+ 34
- 29
common/Utils.go View File

@ -1,10 +1,12 @@
package common
import (
"LAPP_PRN_Service/db"
"container/list"
"errors"
"fmt"
"github.com/lianggx6/goutf16"
"github.com/tarm/serial"
"io/ioutil"
"log"
"math/rand"
@ -328,32 +330,6 @@ func SetDefaultPrinter(printerName string) error {
return nil
}
// 发送到打印机
//func SendFileToPrinter(filePath string,printerName string) error {
// var (
// dll = syscall.NewLazyDLL(EnsureDir("config/TSCLIB.dll"))
// )
// //调用文件
// //打开 打印机 端口.
// open := dll.NewProc("openport")
//
// //关闭连接
// closefile := dll.NewProc("closeport")
// defer closefile.Call()
//
// printer, _ := syscall.UTF16PtrFromString(printerName)
// info1, info2, err := open.Call(uintptr(unsafe.Pointer(printer)))
// glog.InfoExtln("Printer", "info1: ", info1)
// glog.InfoExtln("Printer", "info2: ", info2)
// glog.InfoExtln("Printer", "err: ", err)
// //发送文件
// sendfile := dll.NewProc("sendfile")
// filePathstr, _ := syscall.UTF16PtrFromString(filePath)
//
// sendfile.Call(uintptr(unsafe.Pointer(filePathstr)))
//
// return nil
//}
// 发送到打印机
func SendFileToPrinter(filePath string, ip string, port int) error {
var (
@ -367,22 +343,51 @@ func SendFileToPrinter(filePath string, ip string, port int) error {
closefile := dll.NewProc("closeport")
defer closefile.Call()
ipstr,_ := syscall.BytePtrFromString(ip)
ipstr, _ := syscall.BytePtrFromString(ip)
ret, _, msg := open.Call(uintptr(unsafe.Pointer(ipstr)), uintptr(port))
if ret == 0 {
return msg
}
//发送文件
sendfile := dll.NewProc("sendfile")
filePathstr,_ := syscall.BytePtrFromString(filePath)
filePathstr, _ := syscall.BytePtrFromString(filePath)
ret, _, msg =sendfile.Call(uintptr(unsafe.Pointer(filePathstr)))
ret, _, msg = sendfile.Call(uintptr(unsafe.Pointer(filePathstr)))
if ret == 0 {
return msg
}
return nil
}
// 发送到打印机
func SendFileBySerial(bytesFile []db.ByteFile, SerialName string, SerialPort int) error {
var (
err error
bytesRow db.ByteFile
)
c := &serial.Config{Name: SerialName, Baud: SerialPort}
s, err := serial.OpenPort(c)
if err != nil {
return err
}
defer s.Close()
//发送之前清空缓冲区
buffer := make([]byte, 8000)
_, err = s.Read(buffer)
if err != nil {
return err
}
for _, bytesRow = range bytesFile {
if _, err = s.Write(bytesRow.ByteLine); err != nil {
return err
}
}
return nil
}
func ShellExecute(hwnd HWND, lpOperation, lpFile, lpParameters, lpDirectory string, nShowCmd int) error {
var op, param, directory uintptr
if len(lpOperation) != 0 {


+ 1
- 0
config/config.go View File

@ -26,6 +26,7 @@ type EnvConfig struct {
TemplatePath string `yaml:"templatepath"`
ReadTaskInterval int `yaml:"readtaskinterval"`
PrinterType string `yaml:"printertype"`
Printers string `yaml:"printers"`
Inbox string `yaml:"inbox"`
Outbox string `yaml:"outbox"`
Day int `yaml:"day"`


+ 5
- 3
config/config.yaml View File

@ -1,8 +1,8 @@
plantnr: 100
dbtype: mssql
server: localhost
server: 101.201.121.115
#server: CCN-MESDB
ip: 127.0.0.1
ip: 101.201.121.115
#ip: 192.168.3.22
user: sa
password: Leit2020
@ -19,7 +19,9 @@ templatepath: Template
# 打印任务读取频率
readtaskinterval: 5
# 支持打印机类型
printertype: N,L,E,D
printertype: N,L,E,D,S #D是TSC打印机,S是串口打印机
# 支持打印机
printers : "COM"
inbox: Template\\Inbox
outbox: Template\\Outbox
# 间隔时间


+ 17
- 5
db/Printertab.go View File

@ -29,6 +29,8 @@ type Printer struct {
PrinterType string `xorm:"varchar(1) 'PrinterType' not null" json:"Printer-PrinterType"`
PrinterName string `xorm:"varchar(100) 'PrinterName' not null" json:"Printer-PrinterName"`
IpAddress string `xorm:"varchar(15) 'IpAddress' not null" json:"Printer-IpAddress"`
SerialName string `xorm:"varchar(15) 'SerialName' not null" json:"Printer-SerialName"`
SerialPort int `xorm:"int 'SerialPort' not null" json:"Printer-SerialPort"`
Active int `xorm:"smallint 'Active' not null" json:"Printer-Active"`
PrinterSpec string `xorm:"varchar(30) 'PrinterSpec' not null" json:"Printer-PrinterSpec"`
PageSize string `xorm:"varchar(18) 'PageSize' not null" json:"Printer-PageSize"`
@ -99,15 +101,25 @@ func (t *Printer) GetPrinterList(conf *conf.EnvConfig) ([]Printer, error) {
var (
printertablist []Printer
pt []string
printers []string
)
e := G_DbEngine
pt = strings.Split(conf.PrinterType, ",")
err := e.Where("PlantNr = ? and Active = ?", conf.PlantNr, 1).In("PrinterType", pt).Find(&printertablist)
if err != nil {
glog.Infoln("failed to query Printer for type: %s due to: %v", pt, err)
return nil, err
printers = strings.Split(conf.Printers, ",")
if len(printers) > 0 {
err := e.Where("PlantNr = ? and Active = ?", conf.PlantNr, 1).In("PrinterId", printers).In("PrinterType", pt).Find(&printertablist)
if err != nil {
glog.Infoln("failed to query Printer for type: %s due to: %v", pt, err)
return nil, err
}
} else {
err := e.Where("PlantNr = ? and Active = ?", conf.PlantNr, 1).In("PrinterType", pt).Find(&printertablist)
if err != nil {
glog.Infoln("failed to query Printer for type: %s due to: %v", pt, err)
return nil, err
}
}
return printertablist, nil
}


+ 35
- 2
engine/printer.go View File

@ -18,6 +18,8 @@ type Printer struct {
PrinterType string // 打印机类型,N=网络端口打印,L=文件打印
Ip string // 打印机IP地址
Port int // 打印机端口号
SerialName string // 串口名称
SerialPort int // 串口号
Interval int // 打印机打印间隔时间,单位:秒
conn net.Conn // 打印机连接句柄
InTaskChan chan db.PrintTask // 任务接入管道
@ -33,6 +35,8 @@ func (p *Printer) Create(printertab db.Printer) {
p.Ip = printertab.IpAddress
p.Port = printertab.Port
p.Interval = printertab.PrintInterval
p.SerialName = printertab.SerialName
p.SerialPort = printertab.SerialPort
p.Printertab = printertab
p.InTaskChan = make(chan db.PrintTask, 1000)
@ -156,7 +160,7 @@ func (p *Printer) printByDLL(conf *conf.EnvConfig, saveChan chan db.PrintTask) {
continue
}
// 通过windows文件输出方式打印
if err = common.SendFileToPrinter(t.TaskFile, p.Ip,p.Port); err != nil {
if err = common.SendFileToPrinter(t.TaskFile, p.Ip, p.Port); err != nil {
glog.InfoExtln("Printer", "Failed to by dll: ", err)
continue
}
@ -188,6 +192,31 @@ func (p *Printer) printByExcel(conf *conf.EnvConfig, printChan chan db.PrintTask
}
}
func (p *Printer) printBySerial(conf *conf.EnvConfig, saveChan chan db.PrintTask) {
var (
t db.PrintTask
err error
bytesFile []db.ByteFile
)
for {
t = <-p.InTaskChan
if bytesFile, err = t.GenBytesFile(conf); err != nil {
glog.InfoExtln("Printer", "Failed to generate bytes for task : ", t)
continue
}
// 通过串口形式打印
if err = common.SendFileBySerial(bytesFile, p.SerialName, p.SerialPort); err != nil {
glog.InfoExtln("Printer", "Failed to by dll: ", err)
continue
}
saveChan <- t
if p.Interval > 0 {
time.Sleep(time.Duration(p.Interval) * time.Second)
}
}
}
// 创建打印机对象
func CreatePrinter(printertab db.Printer) (p *Printer) {
p = &Printer{}
@ -214,7 +243,7 @@ func StartPrinter(p *Printer, conf *conf.EnvConfig, printChan, outChan chan db.P
go func() {
p.printByFile(conf, outChan)
}()
case "D": // 输出文件打印
case "D": // TSC打印机输出文件打印
go func() {
p.printByDLL(conf, outChan)
}()
@ -222,6 +251,10 @@ func StartPrinter(p *Printer, conf *conf.EnvConfig, printChan, outChan chan db.P
go func() {
p.printByExcel(conf, printChan)
}()
case "S": // 网口打印机
go func() {
p.printBySerial(conf, printChan)
}()
}
}


Loading…
Cancel
Save