Delta PLC Read/Write internal relay M in C#

using System.Reflection;[DllImport("kernel32.dll", CharSet = CharSet.Auto)]static extern IntPtr LoadLibrary(string dllPath);[DllImport("kernel32.dll", CharSet = CharSet.Auto)]static extern bool FreeLibrary(IntPtr hDll);// Serial Communication[DllImport("DMT.dll", CharSet = CharSet.Auto)]static extern int OpenModbusSerial(int conn_num, int baud_rate, int data_len, char parity, int stop_bits, int modbus_mode);[DllImport("DMT.dll", CharSet = CharSet.Auto)]static extern void CloseSerial(int conn_num);// MODBUS Address Calculation[DllImport("DMT.dll", CharSet = CharSet.Auto)]static extern int DevToAddrW(string series, string device, int qty);// Wrapped MODBUS Funcion : 0x02[DllImport("DMT.dll", CharSet = CharSet.Auto)]static extern int ReadInputsW(int comm_type, int conn_num, int slave_addr, int dev_addr, int qty, UInt32[] data_r, StringBuilder req, StringBuilder res);// Wrapped MODBUS Funcion : 0x05[DllImport("DMT.dll", CharSet = CharSet.Auto)]static extern int WriteSingleCoilW(int comm_type, int conn_num, int slave_addr, int dev_addr, UInt32 data_w, StringBuilder req, StringBuilder res);
System.IntPtr hDMTDll = LoadLibrary(AppDomain.CurrentDomain.BaseDirectory + "DMT.dll"); 
// explicitly link to DMT.dll
int conn_num = 1; //COM Port (COM1)delegate void DelegateClose(int conn_num); // function pointer for disconnectionDelegateClose CloseModbus = new DelegateClose(CloseSerial);CloseModbus = CloseSerial;
int modbus_mode = 1; // 1:ASCII , 2:RTUint baud_rate = 9600;int data_len = 7;char parity = 'E'; // Evenint stop_bits = 1;int plc_node = 1; //Modbus node numberint status = OpenModbusSerial(conn_num, baud_rate, data_len, parity, stop_bits, modbus_mode);
StringBuilder req = new StringBuilder(1024);StringBuilder res = new StringBuilder(1024);if (status > 0){int addr = DevToAddrW("DVP", "M21", 1); //Get int address of internal relay M21int sonuc = WriteSingleCoilW(0, conn_num, plc_node, addr, 0, req, res);if (sonuc == -1){MessageBox.Show("Error");}else{MessageBox.Show("Success");}}
UInt32[] data_from_dev = new UInt32[1];data_from_dev[0] = 0;UInt32[] data_to_dev = new UInt32[1];data_to_dev[0] = 0;int addr = DevToAddrW("DVP", "M23", 1);int sonuc = ReadInputsW(0, conn_num, plc_node, addr, 1, data_from_dev, req, res);if (sonuc != -1){MessageBox.Show("Read Success");if (data_from_dev[0] == 1){MessageBox.Show("M23 is true");}}




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why Nasa Migrate TO AWS

Deploy your Meteor Web and Android App using MUP

WhatsApp Bug Bounty: Bypassing biometric authentication using voip

The rules of Airdrop for quiz

Installing Spark and PySpark on your Mac

Understanding python dictionaries

Some Basic functions in PyTorch

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


More from Medium

C Language “hello world” Program Explained Step by Step All Statements

Public IP vs Private IP address

Stardew Valley Mod (SeasonSwitcher)

Checking Specific TCP Port Access to a Remote Machine