Modbusの利用 (5) E820-AIO アナログ入力①
EBYTEのE820-AIOは、Modbus/RTUのインターフェースをもつデータ収集装置です。現在検索するとV2.0というモデルが見つかります。Windowsから利用します。
●E820-AIOのおもなスペック
- 電源電圧 8~28V
- 動作電流 40mA
- 入力電圧範囲 0~5V
- 入力チャネル 4
- Modbusアドレス デフォルト1(1~250で設定可)
- RS232/485通信速度 デフォルト9600bps(1200~115200設定可)
- 動作温度範囲 -40~+65℃
●Modbusアドレスと変数
Holding Register(保持レジスタ)にデータが収録されています。保持レジスタのアドレスは40000番台から始まりますが、この装置はその13番目からデータが入っています。
Address | Byte | variable names | Type | Description |
---|---|---|---|---|
40013 | 2 | AI1 input | Read only | Unit 0.001mA/0.001V |
40014 | 2 | AI2 input | Read only | |
40015 | 2 | AI3 input | Read only | |
40016 | 2 | AI4 input | Read only | |
40029 | 2 | reference voltage for channel 1 | Read/write | Default 2400,Max. 3300,Min 2000,Unit 0.001V |
40030 | 2 | reference voltage for channel 2 | Read/write | |
40031 | 2 | reference voltage for channel 3 | Read/write | |
40032 | 2 | reference voltage for channel 4 | Read/write | |
40033 | 2 | ModBus address | Read/write | 1-250(0xff is the monitoring address) |
40034 | 2 | Baud rate | Read/write | see more in baud rate table 40035 2 parity bit Read/write see more in parity bit table |
40036 | 2 | Lower limit register | Read/write | 0-65535(Lower limit register must be less than the Upper limit register . After the configuration, channel 1-4 will be the range |
40037 | 2 | Upper limit register | Read/write | of upper and lower limits. |
40038 | 2 | Conversion value for channel 1 | Read only | The original data value transformed by the analog quantity of channel 1 |
40039 | 2 | Conversion value for channel 2 | Read only | The original data value transformed by the analog quantity of channel 2 |
40040 | 2 | Conversion value for channel 3 | Read only | The original data value transformed by the analog quantity of channel 3 |
40041 | 2 | Conversion value for channel 4 | Read only | The original data value transformed by the analog quantity of channel 4 |
Baud rate 3;9600(デフォルト)、Parity bit 0;No parity(デフォルト)
●接続
Windows10のUSBポートに、USB-RS485変換ボードを挿し込みます。デバイスマネージャで、COMポートが増設されたポートを確認します。COM8でした。
USB-RS485アダプタ、E820-AIOのどちらにも終端抵抗はついていないイレギュラな接続で実験をしています。
●ツールCAS Modbus Scannerでアクセス
CAS Modbus Scannerを次のように設定します。「Read Holding registers starting at 40013」を選択し、Pollボタンをクリックします。
COM 8:9600,N,8,1.0
- Device:1
- Read Holding registers starting at 40013
offset=13で、2489を読み出しています。ここはE820-AIOのチャネル1で、単位は0.001Vです。したがって、2.489Vを読み出しました。
チャネル1にはTL431の基準電圧源の出力をつないでいます。テスタで測ると2.48Vでした。
●WindowsからPython3でアクセス
pip install modbus_tk |
でライブラリmodbus-tkをインストールします。
pip list |
でインストールされたライブラリの一覧が見れます。
HOLDING REGISTERSの12番目を読み出し、1000で割って電圧を求めます。sys.path.appendは、MuエディタからPython3.8をアクセスするために必要です。コマンドプロンプトで実行するときには不要です。serialはpyserialライブラリをインストールしています。Addressはスレーブ・アドレスで、E820-AIOは、デフォルトで'1'に設定されています。
HOLDING REGISTERSは、ファンクション03 Read Holding Registerで読み出します。ファンクションはこちらの記事を参照してください。
import sys, time sys.path.append('C:\\Users\\ユーザ名\\AppData\\Local\\Programs\\Python\\Python38\\Lib\\site-packages') import serial import modbus_tk.defines as cst from modbus_tk import modbus_rtu PORT = 'COM8' Address = 1 master = modbus_rtu.RtuMaster(serial.Serial(port=PORT, baudrate=9600, bytesize=8, parity='N', stopbits=1, xonxoff=0)) master.set_timeout(1.0) master.set_verbose(True) #print("connected") A1 = master.execute(Address, cst.READ_HOLDING_REGISTERS, 12, 1)[0] print(A1/1000.0,'V')
●スレーブ・アドレスを変更
デフォルトのスレーブ・アドレス'1'を'5'に変更します。HOLDING REGISTERSの内容を書き換えるにはファンクション06 Preset Single Registerを使います。
import sys sys.path.append('C:\\Users\\ユーザ名\\AppData\\Local\\Programs\\Python\\Python38\\Lib\\site-packages') import serial import modbus_tk.defines as cst from modbus_tk import modbus_rtu PORT = 'COM8' Address = 1 master = modbus_rtu.RtuMaster(serial.Serial(port=PORT, baudrate=9600, bytesize=8, parity='N', stopbits=1, xonxoff=0)) master.set_timeout(1.0) master.set_verbose(True) master.execute(Address, cst.WRITE_SINGLE_REGISTER, 32, output_value=5) print('changed slave address > 5')
実行後、前記のプログラムを動かすとエラーになります。Address を5に変更すると正常に電圧を読み出したので、スレーブ・アドレスの変更は正しく実行できたようです。