iT邦幫忙

0

實作刷條碼後於使用者端條碼機列印條碼-第三章

  • 分享至 

  • xImage
  •  

WinForms (C#) 監聽文字檔案條碼列印程式
※以下程式碼為與 chatGPT 討論後,根據產出內容進行調整之程式

程式我大部分分為幾個部分:

  1. 監聽文字檔案
  2. 條碼機列印指令 (TSPL、EZPL、後續有新增ROG等,總之可以以function的形式擴充,但ROG的部分因為不在職已經不太可考了/images/emoticon/emoticon02.gif)
  3. 列印程式
  4. 初始化加載

監聽文字檔案

 // 建立初始化方法
private void InitializeFileWatcher()
{
    //介接程式刷條碼後列印
    //產生 FileSystemWatcher 物件
    fileWatcher = new FileSystemWatcher
    {
        Path = @"C:\temp\", // 指定檔案的目錄(可依實際情況調整)
        Filter = "No.txt", // 指定監控的檔案
        NotifyFilter = NotifyFilters.LastWrite // 監控檔案的修改時間
    };

    // 綁定事件處理
    fileWatcher.Changed += OnFileChanged; //OnFileChanged 為下面的方法,請看更下方程式碼
    fileWatcher.EnableRaisingEvents = true; 
}

fileWatcher.Changed: 當 C:\temp\No.txt 這個文字檔案內容發生變更時會觸發事件並執行 OnFileChanged 方法

private void OnFileChanged(object sender, FileSystemEventArgs e)
 {
     // 檢查是否已在較短時間內處理過相同檔案 (此部分可有可無)
     DateTime currentReadTime = DateTime.Now;
     if ((currentReadTime - lastReadTime).TotalMilliseconds < 1000)
     {
         return; // 忽略重複觸發
     }

     lastReadTime = currentReadTime;

     for (int retry = 0; retry < 3; retry++)
     {
         try
         {
             // 初始化變數
             string No = string.Empty;
          

             // 使用 StreamReader 逐行讀取檔案 (BIG5)
             using (var reader = new StreamReader(e.FullPath, Encoding.GetEncoding("BIG5")))
             {
                 No = reader.ReadLine()?.Trim() ?? string.Empty;
             }

             // 但段是否有讀到 No 資料
             if (!string.IsNullOrWhiteSpace(No))
             {
                 Invoke((MethodInvoker)delegate
                 {
                     // 顯示資料在 TextBox 或其他 UI 元素
                     textBox2.Text = No// 顯示 readyNo

                     // 列印邏輯
                     string printerName = SelectPriorityPrinter();
                     if (!string.IsNullOrEmpty(printerName))
                     {


                         if (printerName == "TSC NIS Barcode")
                         {
                             PrintCommand(printerName, GenerateTSPLCommand(No));
                         }
                         else if (printerName == "Godex NIS Barcode")
                         {
                             PrintCommand(printerName, GenerateEZPLCommand(No));
                         }
                         else if (printerName == "TSC TTP-345") //1140127新增
                         {
                             PrintCommand(printerName, GenerateTSPLCommandTTP345(No));
                         }

                     }
                     else
                     {
                         MessageBox.Show("未設定 TSC NIS Barcode 或 Godex NIS Barcode 條碼印表機!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                     }
                 });

                 break; // 成功讀取後退出重試
             }
         }
         catch (IOException)
         {
             Thread.Sleep(500); // 等待一段時間後重試
         }
     }
 }

程式進入OnFileChanged function 後會讀取printerName印表機名稱`,然後根據所設定的印表機名稱呼叫列印class 中的那一個function,最後完成列印。

這個專案時隔半年以上正是整理完分享,也不是甚麼太艱深的東西,不過因為是在職時配合實務上去開發,經過實際測試確定可用,但正式上線不可能這麼簡單,也只是一個小小 junior 工程師,透過更笨的 chatGPT 查程式然後 google確認後完成的一個小作品,雖然後續有設計常駐程式定期會清理文字檔案,不過當時主管偏向入資料庫可管,因此這的小作品仍有許多調整的空間,但就單純分享~ 請大佬們見諒/images/emoticon/emoticon01.gif


圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言