2025年9月18日木曜日

linux-gpibパッケージをインストールした際にpython-bindingがインストールされていればPyVISAは不要なはず。

 題目の通り。

前回のような手順できちんとlinux-gpibパッケージでpython-bindingがインストールできていれば以下で行けるはず。


  1. import gpib
  2. import time
  3. from datetime import datetime
  4. device = gpib.dev(0, 5)
  5. gpib.timeout(device, gpib.T10s)
  6. log_file = "measurement_log.csv"
  7. with open(log_file, "w") as f:
  8.     f.write("Timestamp,Voltage\n")
  9. try:
  10.     while True:
  11.         gpib.write(device, "MEAS:VOLT?\n")
  12.         response = gpib.read(device, 100).decode().strip()
  13.         timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  14.         with open(log_file, "a") as f:
  15.             f.write(f"{timestamp},{response}\n")
  16.         print(f"{timestamp} -> {response} V")
  17.         time.sleep(1)
  18. except KeyboardInterrupt:
  19.     print("測定を中断しました。")
  20. finally:
  21.     gpib.close(device)
  22.     print("GPIBデバイスを閉じました。")


12行目で.decode().strip()をつけないかたち,すなわち


gpib.write(device."*IDN?\n")
print(gpib.read(device, 100))

としてしまうと,結果が,

b'HEWLETT-PACKARD,34401A,0,1.0\n'

みたいになってしまう。なので,

print(gpib.read(device, 100).decode().strip())

とすれば

HEWLETT-PACKARD,34401A,0,1.0
と返ってくる。

よく使う関数をまとめておく。

関数説明
gpib.dev(board_index, pad)デバイスハンドルを取得
gpib.write(device, command)コマンド送信
gpib.read(device, length)応答読み取り
gpib.close(device)デバイスを閉じる
gpib.timeout(device, value)タイムアウト設定(例: gpib.T10s

0 件のコメント:

コメントを投稿