Read Data Block of S7 300 PLC using C#

            S7Client client = new S7Client();
            int result = client.ConnectTo("192.168.0.3", 0, 2);

            if (result != 0)
            {
                Console.WriteLine(client.ErrorText(result));
            }
            else
            {
                byte[] dbBuffer = new byte[12];
                result = client.DBRead(1, 0, 12, dbBuffer);
                var var1 = S7.GetIntAt(dbBuffer, 0);
                var var2 = S7.GetRealAt(dbBuffer, 2);
                var var4 = S7.GetDWordAt(dbBuffer, 8);
                Console.WriteLine(var1);
                Console.WriteLine(var2);
                Console.WriteLine(var4);
            }

 

References
https://github.com/fbarresi/Sharp7/wiki
https://github.com/S7NetPlus/s7netplus/wiki
http://gmiru.com/article/s7comm/
https://wiki.wireshark.org/S7comm
https://plc4x.incubator.apache.org/protocols/s7/index.html

Connect Step 7 PLC-Sim to Scadas using NetToPLCsim

You should use Nettoplcsim in this way

  • restart computer
  • before starting any Siemens software, start Nettoplcsim with Admin rights, and let Nettoplcsim stop/start the Siemens service to capture the TCP port 102. Let Nettoplcsim in this state (open with no configuration)
  • start Step 7 (or TIA Portal if you want to use this), then start Plcsim and download your project
  • configure Plcsim station in Nettoplcsim, and start Nettoplcsim server

References
https://www.mesta-automation.com/nettoplcsim-how-to-connect-step-7-plc-sim-to-scadas/
http://nettoplcsim.sourceforge.net/
https://sourceforge.net/p/nettoplcsim/discussion/912717/thread/777cf7c4/?limit=25

Write a Siemens S7 plc driver with C# and Sharp7

Connection to the plc

// Create and connect the client
var client = new S7Client();
int result = client.ConnectTo("127.0.0.1", 0, 1);
if(result == 0)
{
    Console.WriteLine("Connected to 127.0.0.1");
}
else
{
    Console.WriteLine(client.ErrorText(result));
}
 
// Disconnect the client
client.Disconnect();

Read from DB

Console.WriteLine("\n---- Read DB 1");
 
byte[] db1Buffer = new byte[18];
result = client.DBRead(1, 0, 18, db1Buffer);
if(result != 0)
{
    Console.WriteLine("Error: " + client.ErrorText(result));
}
int db1dbw2= S7.GetIntAt(db1Buffer, 2);
Console.WriteLine("DB1.DBW2: " + db1dbw2);
 
double db1ddd4 = S7.GetRealAt(db1Buffer, 4);
Console.WriteLine("DB1.DBD4: " + db1ddd4);
 
double db1dbd8 = S7.GetDIntAt(db1Buffer, 8);
Console.WriteLine("DB1.DBD8: " + db1dbd8);
 
double db1dbd12 = S7.GetDWordAt(db1Buffer, 12);
Console.WriteLine("DB1.DBD12: " + db1dbd12);
 
double db1dbw16 = S7.GetWordAt(db1Buffer, 16);
Console.WriteLine("DB1.DBD16: " + db1dbw16);

Write to DB

db1Buffer = new byte[12];
const int START_INDEX = 4;
S7.SetRealAt(db1Buffer, 4 - START_INDEX, (float)54.36);
S7.SetDIntAt(db1Buffer, 8 - START_INDEX, 555666);
S7.SetDWordAt(db1Buffer, 12 - START_INDEX, 123456);
result = client.DBWrite(1, START_INDEX, db1Buffer.Length, db1Buffer);
if (result != 0)
{
    Console.WriteLine("Error: " + client.ErrorText(result));
}

ReadMultiVar

// Read multi vars
 
var s7MultiVar = new S7MultiVar(client);
byte[] db1 = new byte[18];
s7MultiVar.Add(S7Consts.S7AreaDB, S7Consts.S7WLByte, 1, 0, 18, ref db1);
byte[] db3 = new byte[18];
s7MultiVar.Add(S7Consts.S7AreaDB, S7Consts.S7WLByte, 3, 0, 18, ref db3);
result = s7MultiVar.Read();
if (result != 0)
{
    Console.WriteLine("Error on s7MultiVar.Read()");
}
db1dbw2 = S7.GetIntAt(db1, 2);
Console.WriteLine("DB1.DBW2.0 = {0}", db1dbw2);
 
db1ddd4 = S7.GetRealAt(db1, 4);
Console.WriteLine("DB1.DBW4.0 = {0}", db1ddd4);
 
db1dbd8 = S7.GetDIntAt(db1, 8);
Console.WriteLine("DB1.DBW8.0 = {0}", db1dbd8);
 
db3dbw2 = S7.GetIntAt(db3, 2);
Console.WriteLine("DB3.DBW2.0 = {0}", db3dbw2);
 
db3dbd4 = S7.GetRealAt(db3, 4);
Console.WriteLine("DB3.DBW4.0 = {0}", db3dbd4);
 
db3dbd8 = S7.GetDIntAt(db3, 8);
Console.WriteLine("DB3.DBW8.0 = {0}", db3dbd8);

WriteMultiVars

// Write multi vars
s7MultiVar = new S7MultiVar(client);
const int DB1_START_INDEX = 2;
db1 = new byte[10];
S7.SetIntAt(db1, 2 - DB1_START_INDEX, 50);
S7.SetRealAt(db1, 4 - DB1_START_INDEX, (float)36.5);
S7.SetDIntAt(db1, 8 - DB1_START_INDEX, 123456);
s7MultiVar.Add(S7Consts.S7AreaDB, S7Consts.S7WLByte, 1, DB1_START_INDEX, db1.Length, ref db1);
 
const int DB3_START_INDEX = 2;
db3 = new byte[10];
S7.SetIntAt(db3, 2 - DB3_START_INDEX, -50);
S7.SetRealAt(db3, 4 - DB3_START_INDEX, (float)-25.36);
S7.SetDIntAt(db3, 8 - DB3_START_INDEX, -123456);
s7MultiVar.Add(S7Consts.S7AreaDB, S7Consts.S7WLByte, 3, DB3_START_INDEX, db3.Length, ref db3);
result = s7MultiVar.Write();
if (result != 0)
{
    Console.WriteLine("Error on s7MultiVar.Read()");
}

References
https://www.mesta-automation.com/how-to-write-a-siemens-s7-plc-driver-with-c-and-sharp7/
https://github.com/mesta1/Sharp7-example/blob/master/Sharp7Example/Program.cs

Measuring a 4-20ma Input With a Voltage Input Device

we will assume that a 0-10Vdc input will be used to measure 4-20ma signal.

Ohms law states: R=V/I where V is the Voltage, I is the current and R is the resistance

R=10V/.020A = 500 Ohms

When 20ma flows through a 500 Ohm resistor, it will drop 10 volts

When 4ma flows through a 500 Ohm resistor, it will drop 2 volts

Therefore, 4-20ma signal through a 500 ohm resister will drop 2 to 10 volts

PLC
if you’re using instrument with 4-20mA and connect it to:

1. range 0-20mA – your value of the instrument will be from that 5529 to 27648, value from 0 to cca 5500 will be not showed, but it depends on instrument.

2. range 4-20mA – your value of instrument will be from 0 to 27648.

References
https://www.omega.co.uk/techref/das/4-20ma.html
https://support.industry.siemens.com/tf/WW/en/posts/analog-input-evaluation/146749?page=0&pageSize=10
https://www.omega.com/en-us/resources/data-acquisition-measuring-420-input-with-voltage-device

How can the CPU analog inputs of the SIMATIC S7-1200 also measure currents of 0-20mA

s7-1200_cpu_analoginputs_current_01

Fig. 01: Connection of a sensor to a voltage source as a 2-conductor connection

s7-1200_cpu_analoginputs_current_02

Fig. 02: Connection of a sensor to a voltage source as a 3-conductor connection

s7-1200_cpu_analoginputs_current_03

Fig. 03: Connection of a sensor to a voltage source as a 4-conductor connection

s7-1200_cpu_analoginputs_current_04

Fig. 04: Connection of a measuring transducer with voltage output to a current source as a 4-conductor connection

References :
https://support.industry.siemens.com/cs/document/67396370/how-can-the-cpu-analog-inputs-of-the-simatic-s7-1200-also-measure-currents-of-0-20ma?dti=0&lc=en-WW