-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/labstack/echo" | ||
"net/http" | ||
"os" | ||
"time" | ||
) | ||
|
||
// 创建打印任务的RESTFul控制器 | ||
// 方法:POST | ||
// Body:JSON->{HTML:··} | ||
func CreateMission(c echo.Context) (err error) { | ||
// 将body中的HTML文本解析到对象中 | ||
uRequest := new(CreateItem) | ||
if err = c.Bind(uRequest); err != nil { | ||
return | ||
} | ||
fmt.Println("origin HTML TEXT:", uRequest.HTML) | ||
// 翻译HTML文本 | ||
res := Translation(uRequest.HTML) | ||
// 创建打印元对象 | ||
var item PrintItem | ||
|
||
// 遍历HTML翻译文本并加入到打印任务元中 | ||
for _, val := range res { | ||
for _, v := range ToPrintData(val) { | ||
item.LineNum += 1 | ||
item.Line = append(item.Line, v) | ||
} | ||
} | ||
|
||
fmt.Println("打印行数:", item.LineNum) | ||
fmt.Println("打印Item:") | ||
for _, val := range item.Line { | ||
fmt.Println(val) | ||
} | ||
// 打印对象 | ||
m := mission{ | ||
progress: 0, | ||
item: &item, | ||
} | ||
|
||
// 检查串口连接 | ||
if conn == false { | ||
fmt.Println("系统无法找到打印机!请确认打印机连接状态后重新启动客户端!") | ||
return json.NewEncoder(c.Response()).Encode(struct { | ||
Code string `json:"code"` | ||
ResponseMessage string `json:"message"` | ||
ResponsePayload interface{} `json:"payload,omitempty"` | ||
}{ | ||
Code: "NotFound", | ||
ResponseMessage: "创建新打印任务失败!系统找不到打印机设备,请重新确认打印机连接状态后重启客户端!", | ||
}) | ||
} | ||
|
||
// 异步启动打印任务 | ||
go m.On() | ||
|
||
// 创建标准HTTP Response | ||
c.Response().WriteHeader(http.StatusOK) | ||
return json.NewEncoder(c.Response()).Encode(struct { | ||
Code string `json:"code"` | ||
ResponseMessage string `json:"message"` | ||
ResponsePayload interface{} `json:"payload,omitempty"` | ||
}{ | ||
Code: "OK", | ||
ResponseMessage: "创建新打印任务成功!", | ||
}) | ||
|
||
} | ||
|
||
func PingController(c echo.Context) (err error) { | ||
c.Response().WriteHeader(http.StatusOK) | ||
printConn := "Stop" | ||
if conn == true { | ||
printConn = "OK" | ||
} | ||
return json.NewEncoder(c.Response()).Encode(struct { | ||
Code string `json:"code"` | ||
ResponseMessage string `json:"message"` | ||
PrintConnected string `json:"printConnected"` | ||
Version string `json:"version"` | ||
}{ | ||
Code: "OK", | ||
ResponseMessage: "OK", | ||
PrintConnected: printConn, | ||
Version: Version, | ||
}) | ||
} | ||
|
||
func ExitSystemController(c echo.Context) (err error) { | ||
go func() { | ||
time.Sleep(time.Second * 3) | ||
os.Exit(0) | ||
}() | ||
c.Response().WriteHeader(http.StatusOK) | ||
return json.NewEncoder(c.Response()).Encode(struct { | ||
Code string `json:"code"` | ||
ResponseMessage string `json:"message"` | ||
PrintConnected string `json:"printConnected"` | ||
}{ | ||
Code: "OK", | ||
ResponseMessage: "退出成功!", | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/tarm/serial" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
var s *serial.Port | ||
var conn = false | ||
var COM string | ||
var version string | ||
|
||
func Init() { | ||
findCOM() | ||
} | ||
|
||
/** | ||
* 搜索COM串口 | ||
* Warning:该段代码可能存在隐藏BUG | ||
* 在双线连接时无法搜索COM3和COM5的区别 | ||
* ping及返回值疑似无效 | ||
*/ | ||
func findCOM() { | ||
fmt.Println("正在查找打印机,开始扫描系统COM口。") | ||
for i := 1; i <= 18; i++ { | ||
COMStr := "COM" + strconv.Itoa(i) | ||
fmt.Println("正在扫描", COMStr) | ||
c := &serial.Config{Name: COMStr, Baud: 115200, ReadTimeout: time.Second * 5} | ||
// 开启串口端口 | ||
var err error | ||
s, err = serial.OpenPort(c) | ||
// 检查串口是否能被打开 | ||
if err != nil { | ||
fmt.Println(COMStr, "扫描失败!该COM口无设备!", err) | ||
continue | ||
} | ||
|
||
// 该段代码使用ping的方式尝试连接串口设备,返回值无法正常收取,疑似存在BUG | ||
fmt.Println("在", COMStr, "上发现了设备,正在尝试连接。") | ||
ping := []byte("printerVersion") | ||
_, err = s.Write(ping) | ||
_, err = s.Write(ping) | ||
|
||
// 收取返回值 | ||
time.Sleep(2 * time.Second) | ||
buf := make([]byte, 128) | ||
n, err := s.Read(buf) | ||
if err == nil { | ||
// 无错误:连接成功 | ||
fmt.Println("打印机连接成功!COM口为:", COMStr) | ||
version = string(buf[:n]) | ||
conn = true | ||
COM = COMStr | ||
_ = s.Flush() | ||
break | ||
} else { | ||
// 出现错误,该端口无法使用 | ||
fmt.Println("连接失败!该设备非本软件相关设备!", err) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func Test() { | ||
// //str := dict.Sentence().None() | ||
// log.Println("str:",testStringArray) | ||
// log.Println("part:",resArray) | ||
testString := | ||
` | ||
<p>春晓</p> | ||
<p>孟浩然</p> | ||
<p>春眠不觉晓,</p> | ||
<p>处处闻啼鸟,</p> | ||
<p>夜来风雨声,</p> | ||
<p>花落知多少,</p> | ||
` | ||
|
||
res := Translation(testString) | ||
var item PrintItem | ||
|
||
for _, val := range res { | ||
for _, v := range ToPrintData(val) { | ||
item.LineNum += 1 | ||
item.Line = append(item.Line, v) | ||
} | ||
} | ||
|
||
fmt.Println("打印行数:", item.LineNum) | ||
fmt.Println("打印Item:") | ||
for _, val := range item.Line { | ||
fmt.Println(val) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/tarm/serial" | ||
"log" | ||
"time" | ||
) | ||
|
||
// demo示例程序 | ||
func demo() { | ||
|
||
// Set Serial Port and Baud | ||
|
||
fmt.Println("输入COM口的编号,必须是大写,例如 COM5") | ||
str := "" | ||
_, _ = fmt.Scanf("%s", &str) | ||
fmt.Println(str) | ||
c := &serial.Config{Name: str, Baud: 115200} | ||
|
||
// Open Serial Port | ||
s, err := serial.OpenPort(c) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
//str := []byte("printerVersion") | ||
|
||
ping := []byte("printerVersion") | ||
var prPointZero byte = 0x81 | ||
var prPointOne byte = 0x82 | ||
var prPointTwo byte = 0x83 | ||
var prPointThree byte = 0x84 | ||
var prPointFour byte = 0x85 | ||
var prPointFive byte = 0x86 | ||
var prPointSix byte = 0x87 | ||
var prPointSeven byte = 0x88 | ||
var prSpacePoint byte = 0x89 | ||
var prSpaceWord byte = 0x8A | ||
var prChangeLine byte = 0x8B | ||
var prMarginTop byte = 0x91 | ||
var prMarginBottom byte = 0x92 | ||
var prMarginLeft byte = 0x93 | ||
//var prMarginRight byte =0x94 | ||
//var adPoint byte = 0xA1 | ||
//var adWord byte = 0xA2 | ||
//var adLine byte = 0xA3 | ||
//var adMarginTop byte =0xA4 | ||
//var adMarginBottom byte =0xA5 | ||
//var adMarginLeft byte=0xA6 | ||
//var adMarginRight byte = 0xA7 | ||
var prtInitialize byte = 0xB1 | ||
////var prtCuInitialize byte =0xB2 | ||
var prtFeedPaper byte = 0xB3 | ||
//var prtPause byte = 0xB4 | ||
//var prtResume byte =0xB5 | ||
//var prtStop byte = 0xB6 | ||
//initialization := []byte{1, 2, adPoint, 3, 9, adWord, 5, 5, adLine,6,6,adMarginTop,6,6,adMarginBottom,3,3,adMarginLeft,3,3,adMarginRight,prtInitialize, | ||
// prtFeedPaper,} | ||
normalWord := []byte{prtFeedPaper, prMarginTop, prMarginLeft, prPointZero, prSpacePoint, prPointZero, prSpaceWord, prPointZero, prSpacePoint, prPointZero, prSpaceWord, | ||
prPointSeven, prSpacePoint, prPointThree, prSpaceWord, prPointTwo, prSpacePoint, prPointTwo, prSpaceWord, prPointOne, prSpacePoint, prPointZero, prSpaceWord, | ||
prPointThree, prSpacePoint, prPointTwo, prSpaceWord, prPointFour, prSpacePoint, prPointThree, prSpaceWord, prPointFive, prSpacePoint, prPointZero, prSpaceWord, prSpaceWord, prSpaceWord, prSpaceWord, prSpaceWord, prChangeLine, | ||
|
||
prPointFour, prSpacePoint, prPointSeven, prSpaceWord, prPointThree, prSpacePoint, prPointTwo, prSpaceWord, prPointZero, prSpacePoint, prPointSix, prSpaceWord, prPointTwo, prSpacePoint, prPointSix, prSpaceWord, prPointTwo, prSpacePoint, prPointThree, prSpaceWord, prPointZero, prSpacePoint, prPointSix, prSpaceWord, prPointSeven, prSpacePoint, prPointFour, prSpaceWord, prPointOne, prSpacePoint, prPointFive, prSpaceWord, prPointZero, prSpacePoint, prPointZero, prSpaceWord, prPointZero, prSpacePoint, prPointZero, prSpaceWord, prPointZero, prSpacePoint, prPointZero, prSpaceWord, prPointZero, prSpacePoint, prPointZero, prSpaceWord, prChangeLine, | ||
|
||
prPointSeven, prSpacePoint, prPointThree, prSpaceWord, prPointTwo, prSpacePoint, prPointTwo, prSpaceWord, prPointOne, prSpacePoint, prPointZero, prSpaceWord, prPointFive, prSpacePoint, prPointOne, prSpaceWord, prPointOne, prSpacePoint, prPointFive, prSpaceWord, prPointThree, prSpacePoint, prPointZero, prSpaceWord, prPointFive, prSpacePoint, prPointFour, prSpaceWord, prPointThree, prSpacePoint, prPointThree, prSpaceWord, prPointSix, prSpacePoint, prPointSeven, prSpaceWord, prPointTwo, prSpacePoint, prPointZero, prSpaceWord, prPointThree, prSpacePoint, prPointTwo, prSpaceWord, prPointFour, prSpacePoint, prPointThree, prSpaceWord, prPointFour, prSpacePoint, prPointTwo, prSpaceWord, prChangeLine, | ||
|
||
prPointSix, prSpacePoint, prPointTwo, prSpaceWord, prPointZero, prSpacePoint, prPointFour, prSpaceWord, prPointThree, prSpacePoint, prPointFour, prSpaceWord, prPointThree, prSpacePoint, prPointFive, prSpaceWord, prPointOne, prSpacePoint, prPointTwo, prSpaceWord, prPointThree, prSpacePoint, prPointSix, prSpaceWord, prPointZero, prSpacePoint, prPointTwo, prSpaceWord, prPointTwo, prSpacePoint, prPointTwo, prSpaceWord, prPointZero, prSpacePoint, prPointSix, prSpaceWord, prPointFour, prSpacePoint, prPointFive, prSpaceWord, prPointThree, prSpacePoint, prPointSeven, prSpaceWord, prPointZero, prSpacePoint, prPointFour, prSpaceWord, prPointFour, prSpacePoint, prPointFive, prSpaceWord, prPointThree, prSpacePoint, prPointSeven, prSpaceWord, prChangeLine, | ||
prPointOne, prSpacePoint, prPointTwo, prSpaceWord, prPointSix, prSpacePoint, prPointZero, prSpaceWord, prPointSeven, prSpacePoint, prPointZero, prSpaceWord, prPointTwo, prSpacePoint, prPointFive, prSpaceWord, prPointTwo, prSpacePoint, prPointZero, prSpaceWord, prPointThree, prSpacePoint, prPointOne, prSpaceWord, prPointFour, prSpacePoint, prPointSeven, prSpaceWord, prPointFour, prSpacePoint, prPointFive, prSpaceWord, prPointFour, prSpacePoint, prPointZero, prSpaceWord, prPointOne, prSpacePoint, prPointSix, prSpaceWord, prPointFour, prSpacePoint, prPointSeven, prSpaceWord, prPointOne, prSpacePoint, prPointTwo, prSpaceWord, prChangeLine, | ||
|
||
prPointZero, prSpacePoint, prPointFour, prSpaceWord, prPointTwo, prSpacePoint, prPointSix, prSpaceWord, prPointSix, prSpacePoint, prPointOne, prSpaceWord, prPointZero, prSpacePoint, prPointOne, prSpaceWord, prPointTwo, prSpacePoint, prPointFive, prSpaceWord, prPointThree, prSpacePoint, prPointOne, prSpaceWord, prPointZero, prSpacePoint, prPointOne, prSpaceWord, prPointOne, prSpacePoint, prPointFour, prSpaceWord, prPointTwo, prSpacePoint, prPointFive, prSpaceWord, prPointZero, prSpacePoint, prPointSeven, prSpaceWord, prPointZero, prSpacePoint, prPointOne, prSpaceWord, prPointSeven, prSpacePoint, prPointSeven, prSpaceWord, prPointTwo, prSpacePoint, prPointThree, prSpaceWord, | ||
prChangeLine, prChangeLine, prChangeLine, prChangeLine, prChangeLine, prChangeLine, prChangeLine, prChangeLine, prChangeLine, prChangeLine, prMarginBottom} | ||
// Write a String | ||
prtInit := []byte{prtInitialize} | ||
|
||
n, err := s.Write(ping) | ||
n, err = s.Write(ping) | ||
n, err = s.Write(prtInit) | ||
n, err = s.Write(ping) | ||
n, err = s.Write(ping) | ||
|
||
for i := 0; i < len(normalWord); i++ { | ||
byteBuff := []byte{normalWord[i]} | ||
n, err = s.Write(byteBuff) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if n == 0 { | ||
break | ||
} | ||
buf := make([]byte, 65535) | ||
n, err = s.Read(buf) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
n = 0 | ||
} | ||
fmt.Printf("Write %d Bytes\r\n", n) | ||
time.Sleep(1 * time.Second) | ||
// make and set a buff | ||
|
||
//log.Print("%q", buf[:n]) | ||
//fmt.Printf("Read %d Bytes\r\n", n) | ||
//for i := 0; i < n; i++ { | ||
// //fmt.Printf("buf[%d]=%c ", i, buf[i]) | ||
// fmt.Printf("%c", buf[i]) | ||
//} | ||
//n = 0 | ||
} |