csn_dll.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_OpenPRN(name string) (uintptr, error) {
  43. if csnDll == nil {
  44. return 0, fmt.Errorf("DLL 未加载")
  45. }
  46. nameBytes := append([]byte(name), 0)
  47. ret, _, _ := csnDll.NewProc("Port_OpenPRNIO").Call(
  48. uintptr(unsafe.Pointer(&nameBytes[0])))
  49. if ret == 0 {
  50. return 0, fmt.Errorf("打开 Windows 打印机失败: %s", name)
  51. }
  52. return ret, nil
  53. }
  54. func csn_SetPort(handle uintptr) {
  55. if csnDll != nil && handle != 0 {
  56. csnDll.NewProc("Port_SetPort").Call(handle)
  57. }
  58. }
  59. func csn_Close(handle uintptr) {
  60. if csnDll != nil && handle != 0 {
  61. csnDll.NewProc("Port_ClosePort").Call(handle)
  62. }
  63. }
  64. // === Print functions ===
  65. func csn_Reset() {
  66. if csnDll != nil {
  67. csnDll.NewProc("Pos_Reset").Call()
  68. }
  69. }
  70. func csn_FeedLine() {
  71. if csnDll != nil {
  72. csnDll.NewProc("Pos_FeedLine").Call()
  73. }
  74. }
  75. func csn_FeedLines(n int) {
  76. if csnDll != nil {
  77. csnDll.NewProc("Pos_Feed_N_Line").Call(uintptr(n))
  78. }
  79. }
  80. func csn_Align(value int) { // 0=左, 1=中, 2=右
  81. if csnDll != nil {
  82. csnDll.NewProc("Pos_Align").Call(uintptr(value))
  83. }
  84. }
  85. func csn_FullCut() {
  86. if csnDll != nil {
  87. csnDll.NewProc("Pos_FullCutPaper").Call()
  88. }
  89. }
  90. func csn_Text(text string, widthTimes, heightTimes, fontType, fontStyle int) {
  91. if csnDll == nil {
  92. return
  93. }
  94. // Pos_Text(const wchar_t *prnText, int nLan, int nOrgx, int nWidthTimes, int nHeightTimes, int FontType, int nFontStyle)
  95. wstr, _ := syscall.UTF16FromString(text)
  96. csnDll.NewProc("Pos_Text").Call(
  97. uintptr(unsafe.Pointer(&wstr[0])),
  98. 0, // nLan: 0=default
  99. 0, // nOrgx
  100. uintptr(widthTimes),
  101. uintptr(heightTimes),
  102. uintptr(fontType),
  103. uintptr(fontStyle))
  104. csn_FeedLine()
  105. }
  106. func csn_QRCode(data string, width int) {
  107. if csnDll == nil {
  108. return
  109. }
  110. wstr, _ := syscall.UTF16FromString(data)
  111. csnDll.NewProc("Pos_Qrcode").Call(
  112. uintptr(unsafe.Pointer(&wstr[0])),
  113. uintptr(width), 0, 4) // nVersion=0(auto), nErrLevel=4
  114. }
  115. func csn_Barcode(data string, barcodeType int, unitWidth, unitHeight, fontStyle, fontPos int) {
  116. if csnDll == nil {
  117. return
  118. }
  119. dataBytes := append([]byte(data), 0)
  120. csnDll.NewProc("Pos_Barcode").Call(
  121. uintptr(unsafe.Pointer(&dataBytes[0])),
  122. uintptr(barcodeType), 0, uintptr(unitWidth), uintptr(unitHeight),
  123. uintptr(fontStyle), uintptr(fontPos))
  124. }