dongzongzi0379 2018-07-06 01:24
浏览 228

为什么需要第二个文件写入golang中的pos打印机才能输出?

I have an Epson TM-T88III pos printer on a Linux machine hooked up on a USB port. All is working relative good, It gets recognised, I can open the device, I can echo "Hello World" on the commandline to the printer and it prints without problem.

However in golang when I open the device with os.OpenFile and write some test sequences either as byteslice or string it only prints that sequence after I do another write.

Can somebody explain if I miss something in what I need to do? I am a relative beginner in golang but I do program in other languages.


I open with:

f, err := os.OpenFile("/dev/usb/lppos",os.O_RDWR,0755)
if err != nil {
    panic(err)
}
defer f.Close()

Then I define some esc commands and catch them in a byteslice (i use the escpos package of panjjo for that):

p := escposc.New()

p.Init()
p.SetSmooth(1)
p.SetFontSize(2, 3)
p.SetFont("A")
p.Write("test ")
p.SetFont("B")
p.Write("test2 ")
p.Formfeed()

p.SetFont("B")
p.SetFontSize(1, 1)

p.SetEmphasize(1)
p.Write("halle")
p.Formfeed()

p.SetUnderline(1)
p.SetFontSize(4, 4)
p.Write("halle")

p.SetReverse(1)
p.SetFontSize(2, 4)
p.Write("halle")
p.Formfeed()

p.FormfeedN(5)

p.Cut()

_,b:=p.Readbyte()

p.End()

Then I write the catched slice b to the device:

n,err:=f.Write(b)
if err != nil {
    panic(err)
}

At this point nothing happens, and it took me a while to find out that if I do a last:

f.WriteString(" ")

Then all sequences get printed as should, with all the styling, linefeeds and cut. And all is well, but not without that last WriteString. Oh yes, It does need the space or other character, writing an empty string does not work.

I also tried to write a string of commands instead of a byteslice, but I need the same 2nd WriteString or it wont output on the paper roll.

  • 写回答

4条回答 默认 最新

  • dpn517111 2018-07-06 12:47
    关注

    You have to do f.Sync(). From the docs: Sync commits the current contents of the file to stable storage. (https://golang.org/src/os/file_posix.go?s=3352:3379#L11). And after you end using the device, you may have to do f.Close().

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名