简单打印机集成并实现监听打印功能 – 三郎君的日常

面试 · 2024年9月9日

简单打印机集成并实现监听打印功能

监听部分

private PrintService printService = new PrintService();
private bool isCollectionChangeListening = true; // 用于控制是否监听集合变化

/// <summary>
/// 实验结果列表
/// </summary>
public ObservableCollection<ExpViewUtil> ExperimentDataItems {
    get { return experimentDataItems; }
    set {
        if (experimentDataItems != null) {
            experimentDataItems.CollectionChanged -= OnExperimentDataItemsChanged;
        }
        experimentDataItems = value;
        if (experimentDataItems != null && isCollectionChangeListening) {
            experimentDataItems.CollectionChanged += OnExperimentDataItemsChanged;
        }
    }
}

/// <summary>
/// 设置是否监听集合变化
/// </summary>
public void SetCollectionChangeListening(bool isListening) {
    isCollectionChangeListening = isListening;
    if (experimentDataItems != null) {
        if (isListening) {
            experimentDataItems.CollectionChanged += OnExperimentDataItemsChanged;
        } else {
            experimentDataItems.CollectionChanged -= OnExperimentDataItemsChanged;
        }
    }
}

/// <summary>
/// 处理集合更改事件
/// </summary>
private void OnExperimentDataItemsChanged(object sender, NotifyCollectionChangedEventArgs e) {
    if (!isCollectionChangeListening) return;
    if (e.Action == NotifyCollectionChangedAction.Add) {
        foreach (ExpViewUtil newItem in e.NewItems) {
            printService.PrintExpData(newItem);
        }
    }
}

      
/// <summary>
/// 模拟的打印方法
/// </summary>
private void PrintExpData2(ExpViewUtil expData) {
    Console.WriteLine($"实验名称: {expData.ExperimentName}, 样本名称: {expData.SampleName}, 细胞类型: {expData.CellType}");
}

打印部分

 public class TSPL_SDK {
     public const CharSet charSet = CharSet.Ansi;

     public IntPtr printer;
     public string MachineType = "HT300";
     public string IOSettings = "USB";
     public int labelWidth = 80;
     public int labelHeight = 60;
     public int speed = 2;
     public int density = 15;
     public int type = 1;
     public int gap = 0;
     public int offset = 0;
     public int Copies = 1;
     public int xPos = 0;
     public int yPos = 0;
     public int iPrintImageMode = 0;
     public int iBrightness = 0;
     public int iContrast = 0;
     public int iHtMode = 0;
     public int iTagNum = 1;

     [DllImport("TSPL_SDK", CharSet = charSet)]
     public static extern int PrinterCreator(ref IntPtr printer, string model);

     [DllImport("TSPL_SDK", CharSet = charSet)]
     public static extern int PortOpen(IntPtr printer, string portSetting);

     [DllImport("TSPL_SDK", CharSet = charSet)]
     public static extern int PortClose(IntPtr printer);

     [DllImport("TSPL_SDK", CharSet = charSet)]
     public static extern int PrinterDestroy(IntPtr printer);

     [DllImport("TSPL_SDK", CharSet = charSet)]
     public static extern int TSPL_Setup(IntPtr printer, int labelWidth, int labelHeight, int speed, int density, int type, int gap, int offset);

     [DllImport("TSPL_SDK", CharSet = charSet)]
     public static extern int TSPL_TextPrint(IntPtr printer, string chText, int iTextLenth, int iXPos, int iYPos, int iXImage, int iYImage, int iFontSize);

     [DllImport("TSPL_SDK", CharSet = charSet)]
     public static extern int TSPL_StartPrint(IntPtr printer, int iXPos, int iYPos, int iPrintImageMode, int iBrightness, int iContrast, int iHtMode, int iTagNum, int iCopies);
 }

 public class PrintService {
     public TSPL_SDK sdk = new TSPL_SDK();

     public void PrintExpData(ExpViewUtil expData) {
         string text =
             $"Sample ID: {expData.ExpUniqueId}," +
             $"Sample Date: {expData.ExpTime}, " +
             $"Results:";
         try {
             int result = TSPL_SDK.PrinterCreator(ref sdk.printer, sdk.MachineType);
             if (result != 0) throw new Exception("Failed to create printer.");

             result = TSPL_SDK.PortOpen(sdk.printer, sdk.IOSettings);
             if (result != 0) throw new Exception("Failed to open port.");

             result = TSPL_SDK.TSPL_Setup(sdk.printer, sdk.labelWidth, sdk.labelHeight, sdk.speed, sdk.density, sdk.type, sdk.gap, sdk.offset);
             if (result != 0) throw new Exception("Failed to setup printer.");

             result = TSPL_SDK.TSPL_TextPrint(sdk.printer, text, text.Length, sdk.xPos, sdk.yPos, 0, 0, 24);
             if (result != 0) throw new Exception("Failed to print text.");

             result = TSPL_SDK.TSPL_StartPrint(sdk.printer, sdk.xPos, sdk.yPos, sdk.iPrintImageMode, sdk.iBrightness, sdk.iContrast, sdk.iHtMode, sdk.iTagNum, sdk.Copies);
             if (result != 0) throw new Exception("Failed to start print.");

         } catch (Exception ex) {
             Console.WriteLine($"打印失败: {ex.Message}");
             Console.WriteLine($"当前实验: {text}");
         } finally {
             if (sdk.printer != IntPtr.Zero) {
                 TSPL_SDK.PortClose(sdk.printer);
                 TSPL_SDK.PrinterDestroy(sdk.printer);
                 sdk.printer = IntPtr.Zero;
             }
         }
     }
 }