How to use Random Write, Random read ? ( on SimpleMC)
How to use is Like “MX Component (Mitsubishi Electric)” .
以下のソースコードサンプルでは DWORD 版 (WriteDeviceRandom2DWord, ReadDeviceBlock2DWORD,) の
説明はしてませんが、基本的な使い方は同じです。 DWORD で受け取る(Read) 場合は UShort ではなく、UInteger 型になります。
VB.NET
Dim ins = New SimpleMC.SimpleMC()
ins.ActHostAddress = "192.168.1.10" 'PLC IP Address ( Adjust PLC Setting)
ins.ActPortNumber = 2000 ' PLC Port ( Adjust PLC Setting)
ins.BinaryASCII = EnumBinaryASCII.Binary 'Binary Setting ( Adjust PLC Setting)
'ins.BinaryASCII = EnumBinaryASCII.ASCII 'Binary Setting ( Adjust PLC Setting)
ins.MCProtocol = EnumMCProtocol.フレーム4E ' Ethernet 4E Protocol ( in case of connect via the Ethernet between PC and PLC)
' ins.MCProtocol = 78 ' Ethernet 4E
ins.TIMEOUT = 100 ' 100× (10msec)
' ########### Initial Setting end #####################
' in case of Random write through the array
Dim obj(2, 1) As Object : obj(0, 0) = "D0" : obj(0, 1) = 33 : obj(1, 0) = "D3" : obj(1, 1) = 55 : obj(2, 0) = "D7" : obj(2, 1) = 77
ins.WriteDeviceRandom2(3, obj)
' in case of Random write through the function arguments
ins.WriteDeviceRandom2(3, "D10", 22, "D13", 44, "D17", 66)
'in case of Random read through the function arguments
Dim dat(2) As UShort
dat = ins.ReadDeviceRandom2("D0", "D3", "D7").UShortArray
MessageBox.Show(dat(0).ToString + ":" + dat(1).ToString + ":" + dat(2).ToString)
' in case of Random read through the array
Dim arg(2) As Object : arg(0) = "D10" : arg(1) = "D13" : arg(2) = "D17"
dat = ins.ReadDeviceRandom2(arg).UShortArray
MessageBox.Show(dat(0).ToString + ":" + dat(1).ToString + ":" + dat(2).ToString)
C#
SimpleMC.SimpleMC ins = new SimpleMC.SimpleMC();
ins.ActHostAddress = "192.168.1.10"; // PLC IP Address ( Adjust PLC Setting)
ins.ActPortNumber = 2000; // PLC Port ( Adjust PLC Setting)
ins.BinaryASCII = (EnumBinaryASCII.Binary != 0); //Binary Setting ( Adjust PLC Setting)
//ins.BinaryASCII = (EnumBinaryASCII.Binary == 0); //ASCII Setting ( Adjust PLC Setting)
ins.MCProtocol = (byte)EnumMCProtocol.フレーム4E; // Ethernet 4E Protocol ( in case of connect via the Ethernet between PC and PLC)
// ins.MCProtocol = 78; // Ethernet 4E Protocol ( in case of connect via the Ethernet between PC and PLC)
ins.TIMEOUT = 100; // 100× (10msec)
// ########### Initial Setting end #####################
// in case of Random write through the array
object[,] obj = new object[3, 2]; obj[0, 0] = "D0"; obj[0, 1] = 33; obj[1, 0] = "D3"; obj[1, 1] = 55; obj[2, 0] = "D7"; obj[2, 1] = 77;
ins.WriteDeviceRandom2(3, obj);
// in case of Random write through the function arguments
ins.WriteDeviceRandom2(3, "D10", 22, "D13", 44, "D17", 66);
//in case of Random read through the function arguments
ushort[] dat= new ushort[3];
dat = ins.ReadDeviceRandom2("D0", "D3", "D7").UShortArray;
MessageBox.Show(dat[0] + ":" + +dat[1] + ":" + dat[2]);
// in case of Random read through the array
object[] arg = new object[3]; arg[0] = "D10"; arg[1] = "D13"; arg[2] = "D17";
dat = ins.ReadDeviceRandom2(arg).UShortArray;
MessageBox.Show(dat[0] + ":" + +dat[1] + ":" + dat[2]);
結果
実際に値が書き込まれているかは、 GX Works 2 のデバイスにモニタを確認してください。