csn_dll.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package service
  2. import (
  3. "fmt"
  4. "syscall"
  5. "unsafe"
  6. )
  7. // CSN Printer SDK — Go wrapper for CsnPrinterLibs.dll (x64)
  8. var csnDll *syscall.LazyDLL
  9. func InitCSN(dllPath string) error {
  10. csnDll = syscall.NewLazyDLL(dllPath)
  11. // Test load
  12. if err := csnDll.Load(); err != nil {
  13. return fmt.Errorf("加载 CsnPrinterLibs.dll 失败: %w", err)
  14. }
  15. return nil
  16. }
  17. // === Port functions ===
  18. func Csn_EnumUSB() string {
  19. if csnDll == nil {
  20. return ""
  21. }
  22. buf := make([]byte, 256)
  23. ret, _, _ := csnDll.NewProc("Port_EnumUSB").Call(
  24. uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf)))
  25. if ret == 0 {
  26. return ""
  27. }
  28. return string(buf[:ret])
  29. }
  30. func csn_OpenUSB(name string) (uintptr, error) {
  31. if csnDll == nil {
  32. return 0, fmt.Errorf("DLL 未加载")
  33. }
  34. nameBytes := append([]byte(name), 0)
  35. ret, _, _ := csnDll.NewProc("Port_OpenUSBIO").Call(
  36. uintptr(unsafe.Pointer(&nameBytes[0])))
  37. if ret == 0 {
  38. return 0, fmt.Errorf("打开 USB 打印机失败: %s", name)
  39. }
  40. return ret, nil
  41. }
  42. func csn_Close(handle uintptr) {
  43. if csnDll != nil && handle != 0 {
  44. csnDll.NewProc("Port_ClosePort").Call(handle)
  45. }
  46. }
  47. // === Print functions ===
  48. func csn_Reset() {
  49. if csnDll != nil {
  50. csnDll.NewProc("Pos_Reset").Call()
  51. }
  52. }
  53. func csn_FeedLine() {
  54. if csnDll != nil {
  55. csnDll.NewProc("Pos_FeedLine").Call()
  56. }
  57. }
  58. func csn_FeedLines(n int) {
  59. if csnDll != nil {
  60. csnDll.NewProc("Pos_Feed_N_Line").Call(uintptr(n))
  61. }
  62. }
  63. func csn_Align(value int) { // 0=左, 1=中, 2=右
  64. if csnDll != nil {
  65. csnDll.NewProc("Pos_Align").Call(uintptr(value))
  66. }
  67. }
  68. func csn_FullCut() {
  69. if csnDll != nil {
  70. csnDll.NewProc("Pos_FullCutPaper").Call()
  71. }
  72. }
  73. func csn_Text(text string, widthTimes, heightTimes, fontType, fontStyle int) {
  74. if csnDll == nil {
  75. return
  76. }
  77. // Pos_Text(const wchar_t *prnText, int nLan, int nOrgx, int nWidthTimes, int nHeightTimes, int FontType, int nFontStyle)
  78. wstr, _ := syscall.UTF16FromString(text)
  79. csnDll.NewProc("Pos_Text").Call(
  80. uintptr(unsafe.Pointer(&wstr[0])),
  81. 0, // nLan: 0=default
  82. 0, // nOrgx
  83. uintptr(widthTimes),
  84. uintptr(heightTimes),
  85. uintptr(fontType),
  86. uintptr(fontStyle))
  87. csn_FeedLine()
  88. }
  89. func csn_QRCode(data string, width int) {
  90. if csnDll == nil {
  91. return
  92. }
  93. wstr, _ := syscall.UTF16FromString(data)
  94. csnDll.NewProc("Pos_Qrcode").Call(
  95. uintptr(unsafe.Pointer(&wstr[0])),
  96. uintptr(width), 0, 4) // nVersion=0(auto), nErrLevel=4
  97. }
  98. func csn_Barcode(data string, barcodeType int, unitWidth, unitHeight, fontStyle, fontPos int) {
  99. if csnDll == nil {
  100. return
  101. }
  102. dataBytes := append([]byte(data), 0)
  103. csnDll.NewProc("Pos_Barcode").Call(
  104. uintptr(unsafe.Pointer(&dataBytes[0])),
  105. uintptr(barcodeType), 0, uintptr(unitWidth), uintptr(unitHeight),
  106. uintptr(fontStyle), uintptr(fontPos))
  107. }