2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

matlabでMODBUS①コンテックCPSN-MCB271-1-041+CPSN-AI-1208LI

Last updated at Posted at 2020-08-29

 コンテックの低価格マイコンCONPROSYSnanoのCPSN-MCB271-1-041本体に、A-Dコンバータ・モジュールCPSN-AI-1208LIを追加した形で、アナログ電圧を収集します。

ファイル名

 MODBUS/TCPが動いているので、matlabのアプリInstrument Control Toolboxについてくる(確証がない)Modbus Explorerを使って情報を見ます。
###Modbus Explorer
 アプリのなかにあるModbus Explorerを起動します。
2020-08-29_12h41_07.png

 左端にある+Deviceをクリックします。
コメント 2020-08-29 124628.png

 Modbus TCP/IPをクリックします。
スクリーンショット 2020-08-29 12.54.40.png

 CPSN-MCB271-1-041には192.168.111.21を振ってあります。見つけてきました。Register Type(ファンクション)からInput Registerを選び、Readの虫眼鏡のアイコンをクリックすると、Read Valueの欄に2559を読み出しました。
 ファンクションなどは、こちらの記事を参照してください。
  https://www.denshi.club/cookbook/wire/rs485/modbus/modbus-2-l.html

 その右にあるConfirm Parametersをクリックします。すると、次の画面になり、自動でデータを読み取って、グラフを描きます。
スクリーンショット 2020-08-29 13.08.48.png

 真ん中の画面Read Registersの左端にあるSelectのReg1をチェックを入れ、上部メニューのGenerate Scriptをクリックすると、ライブエディタが開きます。名前を付けて保存します。

スクリーンショット 2020-08-29 13.13.56.png

###コードを書く
 読み取った2559は、10進です。
 コンテックCPSN-MCB271-1-041のA-Dコンバータモジュール、
リファレンスマニュアル(ハードウェア編)|CPSN-AI-1208LI/CPSN-AI-2408LI[日本語]
を参考に、アナログ入力コネクタの1ピンAI0と5ピンAGNDにTL431の出力をつないでいます。
 コンテックCPSN-MCB271-1-041の
リファレンスマニュアル(ソフトウェア編) | CPSN-MCB271-S1-041/CPSN-MCB271-1-041 [日本語]
には、アナログA-Dコンバータ・モジュールCPSN-AI-1208LI(12ビット)のデータ形式が書かれています。デフォルトでは、±10Vの入力です。この時、入力電圧+0.000000は0x800です。1LSBは0.004883Vです。

 Vin0 = double((modbusData.Reg_1 - hex2dec('0x800') ) * 0.004883);

で、電圧に変換できます。マイナスの値は検証していません。

###プログラム
20回測定してプロットでグラフを描きます。

% Create a Modbus Object.
m = modbus('tcpip', '192.168.111.21');
m.Timeout = 20;

% Save the Server ID specified.
serverId = 1;

% Input Registers
% Read 1 Input Register of type 'int16' starting from address 1.
count = 20;
V0 = (count);
for i=1:count
    modbusData.Reg_1 = read(m, 'inputregs', 1, 1, serverId, 'int16');
    Vin0 = double((modbusData.Reg_1 - hex2dec('0x800') ) * 0.004883);
    fprintf('Vin0 = %.6f \n', Vin0);
    V0(i) = Vin0;
    pause(0.1);
end

plot(V0);

% Clear the Modbus Object created.
clear m

% Clear the Server ID.
clear serverId

 グラフです。

スクリーンショット 2020-08-29 14.35.40.png

###資料;Webベースのアクセス
 コンテックのマイコンには、Webからアクセスできます。デフォルトのパスワードはmcb271です。Modbusの読み出し画面です。
スクリーンショット 2020-08-29 14.18.59.png

 A-Dコンバータ・モジュールの設定もここからできます。
スクリーンショット 2020-08-29 14.20.54.png

###資料;Modbusスキャナ
 アナログ・デバイセズ社のWebページ
  https://wiki.analog.com/resources/eval/user-guides/eval-adicup3029/reference_designs/demo_plc_modbus
で紹介されていたQModMasterを利用すると、モノによってはエラーがあってもアクセスできます。

スクリーンショット 2020-08-29 14.45.37.png

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?