csn_dll.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 { return "" }
  20. buf := make([]byte, 256)
  21. ret, _, _ := csnDll.NewProc("Port_EnumUSB").Call(uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf)))
  22. if ret == 0 { return "" }
  23. return string(buf[:ret])
  24. }
  25. func Csn_EnumPRN() string {
  26. if csnDll == nil { return "" }
  27. buf := make([]byte, 256)
  28. ret, _, _ := csnDll.NewProc("Port_EnumPRN").Call(uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf)))
  29. if ret == 0 { return "" }
  30. return string(buf[:ret])
  31. }
  32. func csn_OpenUSB(name string) (uintptr, error) {
  33. if csnDll == nil {
  34. return 0, fmt.Errorf("DLL 未加载")
  35. }
  36. nameBytes := append([]byte(name), 0)
  37. ret, _, _ := csnDll.NewProc("Port_OpenUSBIO").Call(
  38. uintptr(unsafe.Pointer(&nameBytes[0])))
  39. if ret == 0 {
  40. return 0, fmt.Errorf("打开 USB 打印机失败: %s", name)
  41. }
  42. return ret, nil
  43. }
  44. func csn_OpenPRN(name string) (uintptr, error) {
  45. if csnDll == nil {
  46. return 0, fmt.Errorf("DLL 未加载")
  47. }
  48. nameBytes := append([]byte(name), 0)
  49. ret, _, _ := csnDll.NewProc("Port_OpenPRNIO").Call(
  50. uintptr(unsafe.Pointer(&nameBytes[0])))
  51. if ret == 0 {
  52. return 0, fmt.Errorf("打开 Windows 打印机失败: %s", name)
  53. }
  54. return ret, nil
  55. }
  56. func csn_SetPort(handle uintptr) {
  57. if csnDll != nil && handle != 0 {
  58. csnDll.NewProc("Port_SetPort").Call(handle)
  59. }
  60. }
  61. func csn_Close(handle uintptr) {
  62. if csnDll != nil && handle != 0 {
  63. csnDll.NewProc("Port_ClosePort").Call(handle)
  64. }
  65. }
  66. // === Print functions ===
  67. func csn_Reset() {
  68. if csnDll != nil {
  69. csnDll.NewProc("Pos_Reset").Call()
  70. }
  71. }
  72. func csn_FeedLine() {
  73. if csnDll != nil {
  74. csnDll.NewProc("Pos_FeedLine").Call()
  75. }
  76. }
  77. func csn_FeedLines(n int) {
  78. if csnDll != nil {
  79. csnDll.NewProc("Pos_Feed_N_Line").Call(uintptr(n))
  80. }
  81. }
  82. func csn_Align(value int) { // 0=左, 1=中, 2=右
  83. if csnDll != nil {
  84. csnDll.NewProc("Pos_Align").Call(uintptr(value))
  85. }
  86. }
  87. func csn_FullCut() {
  88. if csnDll != nil {
  89. csnDll.NewProc("Pos_FullCutPaper").Call()
  90. }
  91. }
  92. // WriteData 直接发送 RAW 字节到当前激活的打印机端口
  93. func Csn_WriteData(data []byte) int {
  94. if csnDll == nil { return 0 }
  95. ret, _, _ := csnDll.NewProc("WriteData").Call(
  96. uintptr(unsafe.Pointer(&data[0])),
  97. uintptr(len(data)),
  98. 3000) // timeout 3s
  99. return int(ret)
  100. }
  101. func csn_Text(text string, widthTimes, heightTimes, fontType, fontStyle int) {
  102. if csnDll == nil {
  103. return
  104. }
  105. // Pos_Text(const wchar_t *prnText, int nLan, int nOrgx, int nWidthTimes, int nHeightTimes, int FontType, int nFontStyle)
  106. wstr, _ := syscall.UTF16FromString(text)
  107. csnDll.NewProc("Pos_Text").Call(
  108. uintptr(unsafe.Pointer(&wstr[0])),
  109. 0, // nLan: 0=default
  110. 0, // nOrgx
  111. uintptr(widthTimes),
  112. uintptr(heightTimes),
  113. uintptr(fontType),
  114. uintptr(fontStyle))
  115. csn_FeedLine()
  116. }
  117. func csn_QRCode(data string, width int) {
  118. if csnDll == nil {
  119. return
  120. }
  121. wstr, _ := syscall.UTF16FromString(data)
  122. csnDll.NewProc("Pos_Qrcode").Call(
  123. uintptr(unsafe.Pointer(&wstr[0])),
  124. uintptr(width), 0, 4) // nVersion=0(auto), nErrLevel=4
  125. }
  126. func csn_Barcode(data string, barcodeType int, unitWidth, unitHeight, fontStyle, fontPos int) {
  127. if csnDll == nil {
  128. return
  129. }
  130. dataBytes := append([]byte(data), 0)
  131. csnDll.NewProc("Pos_Barcode").Call(
  132. uintptr(unsafe.Pointer(&dataBytes[0])),
  133. uintptr(barcodeType), 0, uintptr(unitWidth), uintptr(unitHeight),
  134. uintptr(fontStyle), uintptr(fontPos))
  135. }