diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8367495 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +venv/ +__pycache__/ +spectrum.csv diff --git a/README.md b/README.md new file mode 100644 index 0000000..04ad5fc --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# CM700Reader + +コニカミノルタ 分光測色計 CM-700d の本体保存データ取得プログラム + +### 仕様 + +- COM ポート自動検索(デバイス名参照) +- 全ての本体データを取得 +- CSV ファイルに保存 +- 本体データの削除はしない + +### 準備 + +pip install -r requirement.txt + +### 設定 + +config.py に記載 diff --git a/cm700reader.py b/cm700reader.py new file mode 100644 index 0000000..ccd6697 --- /dev/null +++ b/cm700reader.py @@ -0,0 +1,85 @@ +import sys + +import numpy as np +import serial +import serial.tools.list_ports + +import config + + +# デバイス名でCOMポート番号を検索 +def find_port(device_str): + """ + Find the serial port from the device name. + + Parameters + ---------- + device_str : str + Device name string. + + Returns + ------- + str + Found serial port name or empty string if not found. + """ + ports = list(serial.tools.list_ports.comports()) + found_list = list(filter(lambda x: device_str in x.description, ports)) + if len(found_list) == 0: + return "" + return found_list[0].device + + +# コマンド送受信 +def send_command(ser, command): + ser.write((command + "\r\n").encode()) + data = ser.read(1024) + recv_items = [x.strip() for x in data.decode().split(",")] + if recv_items[0] != config.CODE_OK: + print(f"コマンド{command}に失敗しました") + sys.exit() + return recv_items + + +# メイン +if __name__ == "__main__": + # COMポートを開く + port = find_port(config.DEVICE_NAME) + if port == "": + print(f"COM port of {config.DEVICE_NAME} not found") + sys.exit() + ser = serial.Serial(port, config.BAUD_RATE, timeout=config.TIMEOUT) + if not ser.is_open: + print(f"COMポート{port}を開くことができませんでした") + sys.exit() + print(f"COMポート{port}が開きました") + + # 基本データ受信 + recv = send_command(ser, "IDR") + wavelen = range(int(recv[5]), int(recv[6]) + 1, int(recv[7])) + + recv = send_command(ser, "STR") + num_samples = int(recv[5]) + print(f"本体にあるサンプル数は{num_samples}個です") + + # 測定データ受信 + spectrum = [] + for i in range(num_samples): + recv = send_command(ser, f"SDR,{i+1},{config.MEAS_TYPE_DICT[config.MEAS_TYPE]}") + spectrum.append(recv[1:]) + print(".", end="") + print("done") + + # COMポートを閉じる + ser.close() + + # CSVファイルに保存 + spetrum_array = np.asarray(spectrum, dtype=np.float32) / 10000.0 + header_line = ",".join(map(str, wavelen)) + np.savetxt( + config.SAVE_FILE, + spetrum_array, + delimiter=",", + header=header_line, + comments="", + fmt="%.4f", + ) diff --git a/config.py b/config.py new file mode 100644 index 0000000..d85787b --- /dev/null +++ b/config.py @@ -0,0 +1,7 @@ +DEVICE_NAME = "CM-700" +BAUD_RATE = 115200 +TIMEOUT = 1 +CODE_OK = "OK00" +MEAS_TYPE = "SCE" +MEAS_TYPE_DICT = {"SCI": 1, "SCE": 2} +SAVE_FILE = "spectrum.csv" diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f6c1a1f --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pyserial