跳到主要內容

命令列無人執行 ​

需求:在測試治具、排程或 CI 上執行腳本,不開視窗,由結束碼判斷結果,並產生報表。

範例說明:兩個 case:Write and read、Status。

語法 ​

text
RoachModbusTool --run-script <腳本.json> [--report <報表.xlsx 或 .csv>] [--loops N] [--case <case 名稱>] [--log <紀錄檔.txt>]
選項說明
--run-script要執行的腳本(必填)
--report執行後輸出報表;副檔名 .xlsx 或 .csv 決定格式
--loops N執行 N 輪,取代腳本裡的設定
--case 名稱每一輪只執行這個 case
--log 檔案把畫面輸出同時寫進文字檔

需要有效的授權或試用期,而且授權要包含 Script 功能。

結束碼 ​

結束碼意思實測
0Release PASSSlave 正常時執行 09-cli.json
1有 FAIL、SKIP 或 DISCONNECT(Release BLOCKED)Slave 停止時執行,兩個 case 都是 DISCONNECT
2無法執行:檔案不存在或格式錯誤、序列埠打不開、Slave 通道無法監聽、選項錯誤檔案不存在、腳本含 goto、--loops x
3沒有授權(Script,或序列埠通道需要的 RTU)—

在 Windows 取得結束碼 ​

程式是 Windows 視窗程式,命令提示字元和 PowerShell 預設不會等它結束,必須明確等待:

bat
start /wait "" RoachModbusTool.exe --run-script 09-cli.json --report out.xlsx
echo %ERRORLEVEL%
powershell
$p = Start-Process RoachModbusTool.exe -ArgumentList '--run-script 09-cli.json --report out.xlsx' `
     -Wait -PassThru -NoNewWindow
$p.ExitCode

批次檔裡可以這樣判斷:

bat
start /wait "" RoachModbusTool.exe --run-script 09-cli.json --report out.xlsx
if %ERRORLEVEL%==0 (echo PASS) else (echo NOT PASS: %ERRORLEVEL%)

Linux 的 AppImage 就是一般的命令列程式:

bash
./RoachModbusTool-x86_64.AppImage --run-script 09-cli.json --report out.xlsx; echo $?

輸出內容 ​

執行紀錄和最後一行統計會輸出到標準輸出(UTF-8)。實際輸出(記錄檔資料夾的使用者名稱已遮掉):

text
Opened 09-cli.json
Loaded Channel A/B from file
Roach Modbus Tool 1.3.3 - running ...\05-script-examples\09-cli.json
Starting...
Log file folder: C:\Users\<name>\AppData\Local\Mr.Roach\Roach Modbus Tool\script_logs
Firmware: examples
On error: Skip remaining
Loop: 1
A ON - Master TCP 127.0.0.1:502  Slave ID 1    B OFF
Connected A
Running 2 case(s) x 1 loop(s)
Running case 1/2: Write and read
15:27:29.857  [Write and read #3, loop 1]  0x0500 = 4660
Finished case 1/2: Write and read  PASS
Running case 2/2: Status
Finished case 2/2: Status  PASS
Write and read: PASS
  A 0x0500 = 0x1234 (expect 0x1234)
Status: PASS
  A 0x0501 = 0x0000 (expect 0x0000)
Done
PASS 2 - FAIL 0 - SKIP 0 - DISCONNECT 0 - Release PASS

設備沒有回應時(結束碼 1),結尾會是:

text
Write and read: DISCONNECT
  A 0x0500 write failed: comm fail: connect failed: connection refused
  skip: on_error skip
Status: DISCONNECT
  A 0x0501 read failed: comm fail: connect failed: connection refused
Done
PASS 0 - FAIL 0 - SKIP 0 - DISCONNECT 2 - Release BLOCKED

--run-script 09-cli.json --case Status 只執行 Status:

text
Running case 2/2: Status
Finished case 2/2: Status  PASS
...
PASS 1 - FAIL 0 - SKIP 0 - DISCONNECT 0 - Release PASS

CSV 報表 ​

--report out.csv --loops 3 的內容(節錄):

text
Firmware,examples
Loops,3
Test cases,2
PASS,6
FAIL,0
SKIP,0
DISCONNECT,0
Release,PASS

loop,time,test_name,verdict
1,2026-11-25 15:27:49,Write and read,PASS
1,2026-11-25 15:27:49,Status,PASS
2,2026-11-25 15:27:49,Write and read,PASS
...

loop,time,case,step,action,channel,function,start,end,actual,expected,verdict,detail,response_ms
1,2026-11-25 15:27:49,Write and read,1,write,A,6,1280,1280,,,PASS,,0.1
1,2026-11-25 15:27:49,Write and read,2,read,A,3,1280,1280,0x1234,0x1234,PASS,v = 4660,0.0
1,2026-11-25 15:27:49,Write and read,3,log,,0,0,0,,,PASS,0x0500 = 4660,
...

CSV 依序有三段:摘要、每個 case 的結果、每一步的結果。start、end 欄是十進位位址(1280 = 0x0500)。

注意事項

  • 沒有操作員:Pause 步驟只記錄後略過,Ask 步驟一律 FAIL。含 Ask 的腳本請在 GUI 執行,或用 --case 只執行不含 Ask 的 case。
  • 執行開始時 TCP 設備沒有回應不會中止執行:每個要通訊的步驟會重新連線,沒有回應的步驟判定 DISCONNECT。要等設備開機,可以在第一步放 Wait Until(compare: false)。
  • 腳本含 goto,或 main_case 不是第一個 case 時,會被拒絕執行(結束碼 2),並說明要怎麼改。

下載範例腳本 ​

範例腳本

先用 Slave 模式在 127.0.0.1:502 按 Listen,再用上文的命令列語法執行。

Roach Modbus Tool · Mr.Roach 獨立開發