doudeng3008 2018-11-10 00:13
浏览 627
已采纳

在Golang热敏打印机设备上写

I have a thermal printer(ESC/POS) already configured on my linux machine and using the terminal command (as root) I can make it print:

echo "Hello!" > /dev/usb/lp0

However, doing the same procedure in golang nothing happens:

package main

import (
    "fmt"
    "os"
)

func main() {
   fmt.Println("Hello Would!")

   f, err := os.Open("/dev/usb/lp0")

   if err != nil {
       panic(err)
   }

   defer f.Close()

   f.Write([]byte("Hello world!"))
}

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • douzheng5717 2018-11-10 00:50
    关注

    As described in the documentation os.Open() opens a file read-only.

    You would have discovered the problem if you had checked the return from your Write() call. Always check errors. Don't ignore them, even in tiny programs like this; they will give you a clue as to what is wrong.

    To fix the problem, open the device special for writing with os.OpenFile().

    f, err := os.OpenFile("/dev/usb/lp0", os.O_RDWR, 0)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?