csn_dll.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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_Close(handle uintptr) {
  55. if csnDll != nil && handle != 0 {
  56. csnDll.NewProc("Port_ClosePort").Call(handle)
  57. }
  58. }
  59. // === Print functions ===
  60. func csn_Reset() {
  61. if csnDll != nil {
  62. csnDll.NewProc("Pos_Reset").Call()
  63. }
  64. }
  65. func csn_FeedLine() {
  66. if csnDll != nil {
  67. csnDll.NewProc("Pos_FeedLine").Call()
  68. }
  69. }
  70. func csn_FeedLines(n int) {
  71. if csnDll != nil {
  72. csnDll.NewProc("Pos_Feed_N_Line").Call(uintptr(n))
  73. }
  74. }
  75. func csn_Align(value int) { // 0=左, 1=中, 2=右
  76. if csnDll != nil {
  77. csnDll.NewProc("Pos_Align").Call(uintptr(value))
  78. }
  79. }
  80. func csn_FullCut() {
  81. if csnDll != nil {
  82. csnDll.NewProc("Pos_FullCutPaper").Call()
  83. }
  84. }
  85. func csn_Text(text string, widthTimes, heightTimes, fontType, fontStyle int) {
  86. if csnDll == nil {
  87. return
  88. }
  89. // Pos_Text(const wchar_t *prnText, int nLan, int nOrgx, int nWidthTimes, int nHeightTimes, int FontType, int nFontStyle)
  90. wstr, _ := syscall.UTF16FromString(text)
  91. csnDll.NewProc("Pos_Text").Call(
  92. uintptr(unsafe.Pointer(&wstr[0])),
  93. 0, // nLan: 0=default
  94. 0, // nOrgx
  95. uintptr(widthTimes),
  96. uintptr(heightTimes),
  97. uintptr(fontType),
  98. uintptr(fontStyle))
  99. csn_FeedLine()
  100. }
  101. func csn_QRCode(data string, width int) {
  102. if csnDll == nil {
  103. return
  104. }
  105. wstr, _ := syscall.UTF16FromString(data)
  106. csnDll.NewProc("Pos_Qrcode").Call(
  107. uintptr(unsafe.Pointer(&wstr[0])),
  108. uintptr(width), 0, 4) // nVersion=0(auto), nErrLevel=4
  109. }
  110. func csn_Barcode(data string, barcodeType int, unitWidth, unitHeight, fontStyle, fontPos int) {
  111. if csnDll == nil {
  112. return
  113. }
  114. dataBytes := append([]byte(data), 0)
  115. csnDll.NewProc("Pos_Barcode").Call(
  116. uintptr(unsafe.Pointer(&dataBytes[0])),
  117. uintptr(barcodeType), 0, uintptr(unitWidth), uintptr(unitHeight),
  118. uintptr(fontStyle), uintptr(fontPos))
  119. }