Siemens LOGO! vs. Visual Studio Express

Siemens LOGO! 0BA7 auslesen

WICHTIG: Vorher in Parameter VM Zuordnung die im DB gewünschten Werte festlegen
WICHTIG: Netzwerkverbindung mit TSAP 01.00 und mit Operator Panel [x] konfigurieren

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LOGO_Remote
{
    public partial class Form1 : Form
    {
        static libnodave.daveOSserialType fds;
        static libnodave.daveInterface di;
        static libnodave.daveConnection dc;

        string ip = "192.168.1.111"; // Siemens LOGO! 0BA7
        int port = 102; // Siemens LOGO! 0BA7
        int protokoll = libnodave.daveProtoISOTCP;

        static int mpi = 0;     // MPI
        static int rack = 1;    // Racknummer
        static int slot = 0;    // Slotnummer

        int res;
        int LOGODInputs = 0;
        int LOGODOutputs = 0;
        int LOGOAInputs = 0;
        int LOGOAOutputs = 0;
        int LOGODB = 0;
        int LOGOFlags = 0;

        public Form1()
        {
            InitializeComponent();
        }

        public static bool CheckBitSet(byte b, int BitNumber)
        {
            //Kleine Fehlerbehandlung
            if (BitNumber < 8 && BitNumber > -1)
            {
                return (b & (1 << BitNumber)) > 0;
            }
            else
            {
                throw new InvalidOperationException(
                "Der Wert für BitNumber " + BitNumber.ToString() + " war nicht im zulässigen Bereich! (BitNumber = (min)0 - (max)7)");
            }

        }

        public void connectlogo()
        {
            fds.rfd = libnodave.openSocket(port, ip);

            fds.wfd = fds.rfd;

            if (fds.rfd > 0)
            {

                di = new libnodave.daveInterface(fds, "IF1", 0, protokoll, libnodave.daveSpeed187k);

                di.setTimeout(1000000);

                dc = new libnodave.daveConnection(di, mpi, rack, slot);

                dc.connectPLC();
            }
            else
                Console.WriteLine("Connect Error");
        }

        public void disconnectlogo()
        {
            dc.disconnectPLC();
            libnodave.closeSocket(fds.rfd);
        }

        public void readlogodi()
        {
            // Digitale Eingänge lesen
            res = dc.readBytes(libnodave.daveInputs, 0, 0, 1, null);

            if (res == 0)
            {
                LOGODInputs = dc.getU8();
            }
            else
                Console.WriteLine("Error " + res + " " + libnodave.daveStrerror(res));
        }

        public void readlogodo()
        {
            // Digitale Eingänge lesen
            res = dc.readBytes(libnodave.daveOutputs, 0, 0, 1, null);

            if (res == 0)
            {
                LOGODOutputs = dc.getU8();
            }
            else
                Console.WriteLine("Error " + res + " " + libnodave.daveStrerror(res));
        }

        public void readlogoai()
        {
            // Analoge Eingänge lesen
            res = dc.readBytes(libnodave.daveAnaIn, 0, 0, 2, null);

            if (res == 0)
            {
                LOGOAInputs = dc.getU16();
            }
            else
                Console.WriteLine("Error " + res + " " + libnodave.daveStrerror(res));
        }

        public void readlogoao()
        {
            // Analoge Eingänge lesen
            res = dc.readBytes(libnodave.daveAnaOut, 0, 0, 2, null);

            if (res == 0)
            {
                LOGOAOutputs = dc.getU16();
            }
            else
                Console.WriteLine("Error " + res + " " + libnodave.daveStrerror(res));
        }

        public void readlogodb()
        {
            // Analoge Eingänge lesen
            res = dc.readBytes(libnodave.daveDB, 0, 0, 2, null);

            if (res == 0)
            {
                LOGODB = dc.getU16();
            }
            else
                Console.WriteLine("Error " + res + " " + libnodave.daveStrerror(res));
        }

        public void readlogoflags()
        {
            // Merker lesen
            res = dc.readBytes(libnodave.daveFlags, 0, 0, 1, null);

            if (res == 0)
            {
                LOGOFlags = dc.getU8();
            }
            else
                Console.WriteLine("Error " + res + " " + libnodave.daveStrerror(res));
        }


        //Write a sequence of bytes from a buffer to PLC memory. int daveWriteBytes(daveConnection * dc, int area, int DB, int start, int len, void * buffer);
        //Parameters:

        //dc: A pointer to a daveConnection structure representing an established connection.
        //area: A constant that specifies a memory area in the PLC.
        //DB: The number of a data block. Only meaningful if area is daveDB. Use 0 oterwise.
        //start: The address of the first byte in the block.
        //len: The number of bytes to write.
        //buffer: A pointer to some memory space with the writedata. 

        public void writelogoflags()
        {
              // Merker schreiben
              byte[] buf;
              buf = new byte[1];

             int Adr;
             bool Par = false;

             int InputNum = 0; //
             int BitNum = 24;   // 

             if(Par == true)
             {
                 buf[0] = 255; // TRUE
             }
             else
             {
                buf[0] = 0; // FALSE
             }

             Adr = InputNum * 8 + BitNum;

            Console.WriteLine("InputNum: " + InputNum);
            Console.WriteLine("ByteNum: " + BitNum);
            Console.WriteLine("Adr: " + Adr);

             //write Merker M0.0
             res = dc.writeBits(libnodave.daveFlags, 0, Adr, 1, buf);

            if (res == 0)
            {
                //
            }
            else
                Console.WriteLine("Error " + res + " " + libnodave.daveStrerror(res));
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //readlogoai();
            readlogodb();
            progressBar1.Value = LOGODB;
        }

        private void btnInfo_Click(object sender, EventArgs e)
        {
            MessageBox.Show("libnodave & LOGO! 0BA7 -- Tom Lorenz 09/2014");
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            connectlogo();
        }

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            disconnectlogo();
        }

        private void btnReadAI_Click(object sender, EventArgs e)
        {
            readlogoai();
            tbTesttext.Text = LOGOAInputs.ToString();
        }

        private void btnReadAO_Click(object sender, EventArgs e)
        {
            readlogoao();
            tbTesttext.Text = LOGOAOutputs.ToString();
        }

        private void btnReadDI_Click(object sender, EventArgs e)
        {
            readlogodi();
            tbTesttext.Text = LOGODInputs.ToString();
        }

        private void btnReadDO_Click(object sender, EventArgs e)
        {
            readlogodo();
            tbTesttext.Text = LOGODOutputs.ToString();
        }

        private void btnReadDB_Click(object sender, EventArgs e)
        {
            readlogodb();
            tbTesttext.Text = LOGODB.ToString();
        }

        private void btnReadFlags_Click(object sender, EventArgs e)
        {
            readlogoflags();
            tbTesttext.Text = LOGOFlags.ToString();
        }

        private void btnWriteFlags_Click(object sender, EventArgs e)
        {
            writelogoflags();
        }

        private void btnLOGOLightOn_Click(object sender, EventArgs e)
        {
            // Merker schreiben
            byte[] buf;
            buf = new byte[1];

            int Adr;
            bool Par = true;

            int InputNum = 0; //
            int BitNum = 24;   // 

            if (Par == true)
            {
                buf[0] = 255; // TRUE
            }
            else
            {
                buf[0] = 0; // FALSE
            }

            Adr = InputNum * 8 + BitNum;

            //write Merker M25
            res = dc.writeBits(libnodave.daveFlags, 0, Adr, 1, buf);

            if (res == 0)
            {
                //
            }
            else
                Console.WriteLine("Error " + res + " " + libnodave.daveStrerror(res));
        }

        private void btnLOGOLightOff_Click(object sender, EventArgs e)
        {
            // Merker schreiben
            byte[] buf;
            buf = new byte[1];

            int Adr;
            bool Par = false;

            int InputNum = 0; //
            int BitNum = 24;   // 

            if (Par == true)
            {
                buf[0] = 255; // TRUE
            }
            else
            {
                buf[0] = 0; // FALSE
            }

            Adr = InputNum * 8 + BitNum;

            //write Merker M25
            res = dc.writeBits(libnodave.daveFlags, 0, Adr, 1, buf);

            if (res == 0)
            {
                //
            }
            else
                Console.WriteLine("Error " + res + " " + libnodave.daveStrerror(res));
        }

    }
}

Siemens S7-300 Eingänge auslesen

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

namespace S7300_Statistik
{
    public partial class Form1 : Form
    {

        static libnodave.daveOSserialType davesocket;
        static libnodave.daveInterface daveiface;
        static libnodave.daveConnection daveconnection;

        //string ip = "192.168.1.113"; // Siemens LOGO! 0BA7
        //int port = 102; // Siemens LOGO! 0BA7
        //int portokoll = libnodave.daveprotoTCP;

        //string ip = "192.168.0.236"; // S7-300 IP Altbau
        //int port = 1099; // S7-300 IP Altbau
        //int protokoll = libnodave.daveProtoMPI_IBH;

        string ip = "192.168.1.207";    // S7-300 IP Haustechnik
        int port = 7777;                // S7-300 IP Haustechnik
        int protokoll = 230;            // libnodave.daveProtoNLpro = 230;

        static int rack = 0;    // S7-300 Racknummer
        static int slot = 2;    // S7-300 Slotnummer
        static int mpi = 2;     // S7-300 MPI -- WICHTIG!!
        string logfilename = "";

        //-----------------------------------------------------------------------

        int res=0;
        int daveinputs = 0;
        int connectionestablished = 0;
        double readcounter = 0;
        DateTime recorddate = DateTime.Now;

        public Form1()
        {
            InitializeComponent();
            timermomentankwh.Interval = 1000;
            timerrecord.Interval = 30000;
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            disconnects7300();
        }

        public static bool CheckBitSet(byte b, int BitNumber)
        {
            // Fehlerbehandlung
            if (BitNumber < 8 && BitNumber > -1)
            {
                return (b & (1 << BitNumber)) > 0;
            }
            else
            {
                throw new InvalidOperationException(
                "Der Wert für BitNumber " + BitNumber.ToString() + " war nicht im zulässigen Bereich! (BitNumber = (min)0 - (max)7)");
            }
        }

        public void connects7300()
        {
            connectionestablished = 0;
            tbLog.AppendText("Socket öffnen\n");
            davesocket.rfd = libnodave.openSocket(port, ip);
            tbLog.AppendText("Socket erfolgreich\n");
            davesocket.wfd = davesocket.rfd;

            if (davesocket.rfd > 0)
                {
                tbLog.AppendText("Interface öffnen\n");
                daveiface = new libnodave.daveInterface(davesocket, "IF1", 0, protokoll, libnodave.daveSpeed187k);
                daveiface.initAdapter();
                tbLog.AppendText("Adapter erfolgreich\n");
                daveiface.setTimeout(100000);
                tbLog.AppendText("Interface erfolgreich\n");

                if (res == 0)
                {
                    tbLog.AppendText("Connection öffnen\n");
                    daveconnection = new libnodave.daveConnection(daveiface, mpi, rack, slot);

                    daveconnection.connectPLC();
                    tbLog.AppendText("Connection erfolgreich\n");
                    connectionestablished = 1;
                }
                else
                    tbLog.AppendText("Status: " + res + " " + libnodave.daveStrerror(res));
                }
            else
                {
                    tbLog.AppendText("Verbindungsaufbau abgebrochen\n");
                }
        }

        public void disconnects7300()
        {
            if (connectionestablished == 1)
            {
                daveiface.disconnectAdapter();
                tbLog.AppendText("Disconnect Adapter erfolgreich\n");
                daveconnection.disconnectPLC();
                libnodave.closeSocket(davesocket.rfd);
                tbLog.AppendText("Disconnect PLC erfolgreich\n");
                connectionestablished = 0;
            }
        }

        public void readdbkw()
        {
            readcounter++;
            recorddate = DateTime.Now;
            /*
            // Datenbaustein Momentanwert DB30 DW0 lesen 
            daveinputs = 0;
            res = daveconnection.readBytes(libnodave.daveDB, 30, 0, 4, null);
            tbLog.AppendText("Funktion ReadDB " + readcounter + " DB30-0 erfolgreich\n");

            if (res == 0)
            {
                daveinputs = daveconnection.getU32();
                lblkw0.Text = daveinputs.ToString();

            }
            else
            {
                tbLog.AppendText("Fehler Read\n");
            }
            */

            // Datenbaustein Momentanwert DB30 DW8 lesen --- Easyshow !!!! AKTUELLER WERT
            daveinputs = 0;
            res = daveconnection.readBytes(libnodave.daveDB, 30, 8, 4, null);
            tbLog.AppendText("Funktion ReadDB " + readcounter + " DB30-8 erfolgreich\n");

            if (res == 0)
            {
                daveinputs = daveconnection.getU32();
                lblkw8.Text = daveinputs.ToString();
                pbmomentankw.Value = daveinputs;
            }
            else
            {
                tbLog.AppendText("Fehler Read\n");
            }

            /*
            // Datenbaustein Momentanwert DB1 x lesen --- Easyshow !!!! AKTUELLER WERT
            daveinputs = 0;
            res = daveconnection.readBytes(libnodave.daveDB, 1, 0, 1, null);
            tbLog.AppendText("Funktion ReadDB " + readcounter + " Tick erfolgreich\n");

            daveinputs = daveconnection.getU8();

            if (res == 0)
            {
                if (CheckBitSet((byte)daveinputs,0))
                    {
                        panelkwtick.BackColor = Color.Yellow;
                    }
                else
                    {
                        panelkwtick.BackColor = Color.DarkGray;
                    }
            }
            else
            {
                tbLog.AppendText("Fehler Read\n");
            }
            */
        }

        public void readtime()
        {
            DateTime Uhrzeit = DateTime.Now;

            lblplcyear.Text = Uhrzeit.Year.ToString();
            lblplcmonth.Text = Uhrzeit.Month.ToString();
            lblplcday.Text = Uhrzeit.Day.ToString();
            lblplcstunde.Text = Uhrzeit.Hour.ToString();
            lblplcminute.Text = Uhrzeit.Minute.ToString();
            lblplcsekunde.Text = Uhrzeit.Second.ToString();
            lblplcmillis.Text = Uhrzeit.Millisecond.ToString();
        }

        public void recordkwh()
        {
            // Record kWh
            // 13.12.2013;08:23:42;45

            String appendstring =  lblplcday.Text+"."+lblplcmonth.Text+"."+lblplcyear.Text+";"+lblplcstunde.Text+":"+lblplcminute.Text+":"+lblplcsekunde.Text+";"+lblkw8.Text;

            AppendToTextFile(logfilename, appendstring);
        }

        private void AppendToTextFile(string strPath, string strLines)
        {
            using (StreamWriter streamWriter = new StreamWriter(strPath, true))
            {
                streamWriter.WriteLine(strLines);
            }
        }

        private void InitiateTextFile(string strPath, string strLines)
        {
            using (StreamWriter streamWriter = new StreamWriter(strPath))
            {
                streamWriter.WriteLine(strLines);
            }
        }

        // Actual total zoom value
        int deltaScrollTotal;
        private void kwchart_MouseWheel(object sender, MouseEventArgs e)
        {
            int maxChangeRange = 21;
            int minChangeRange = -1;

            int deltaScroll = e.Delta / Math.Abs(e.Delta);
            deltaScrollTotal += deltaScrollTotal + deltaScroll > minChangeRange
                             && deltaScrollTotal + deltaScroll < maxChangeRange
                              ? deltaScroll : 0;
            // Additional calculation in order to obtain pseudo
            // "positional zoom" feature
            double minXScale = (double)e.X / (double)kwchart.Width;
            double maxXScale = 1 - minXScale;
            double minYScale = (double)e.Y / (double)kwchart.Height;
            double maxYScale = 1 - minYScale;

            // Max and min values into which axis need to be scaled/zoomed
            double maxX = kwchart.ChartAreas[0].AxisX.Maximum
                        - deltaScrollTotal * maxXScale;
            double minX = kwchart.ChartAreas[0].AxisX.Minimum
                        + deltaScrollTotal * minXScale;
            double maxY = kwchart.ChartAreas[0].AxisY.Maximum
                        - deltaScrollTotal * minYScale;
            double minY = kwchart.ChartAreas[0].AxisY.Minimum
                        + deltaScrollTotal * maxYScale;

            kwchart.ChartAreas[0].AxisX.ScaleView.Zoom(minX, maxX);
            kwchart.ChartAreas[0].AxisY.ScaleView.Zoom(minY, maxY);
        }

        private void btnstoplogging_Click(object sender, EventArgs e)
        {
            timermomentankwh.Stop();
        }

        private void btnreadplctime_Click(object sender, EventArgs e)
        {
            // PLC Time auslesen
            daveinputs = 0;
            res = daveconnection.readBytes(libnodave.daveTimer, 0, 110, 2, null);
            tbLog.AppendText("Funktion ReadTimer erfolgreich\n");

            if (res == 0)
            {
                tbLog.AppendText("------Timer101--------\n");
                daveinputs = daveconnection.getU8();
                tbLog.AppendText("Ergebnis T101: " + daveinputs + "\n");
                daveinputs = daveconnection.getU8();
                tbLog.AppendText("Ergebnis T101: " + daveinputs + "\n");
            }
            else
            {
                tbLog.AppendText("Fehler Read\n");
            }
        }

        private void btnstart_Click(object sender, EventArgs e)
        {
            connects7300();
            timermomentankwh.Start();

            kwchart.ChartAreas[0].AxisX.MajorGrid.LineWidth = 1;
            kwchart.ChartAreas[0].AxisY.MajorGrid.LineWidth = 1;
            kwchart.ChartAreas[0].AxisX.MinorGrid.LineWidth = 1;
            kwchart.ChartAreas[0].AxisY.MinorGrid.LineWidth = 1;

            kwchart.ChartAreas[0].AxisY.MajorGrid.Interval = 100;
            kwchart.ChartAreas[0].AxisY.MinorGrid.Interval = 10;

            kwchart.ChartAreas[0].AxisY.MinorGrid.LineColor = Color.LightGray;

            kwchart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
            kwchart.ChartAreas[0].AxisX.MinorGrid.Enabled = false;
            kwchart.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
            kwchart.ChartAreas[0].AxisY.MinorGrid.Enabled = true;

            kwchart.Series["kWh"].Color = Color.DarkGreen;

            kwchart.Series["kWh"].ToolTip = "#VALX{G}, #VALY kW/h";

            kwchart.Series["kWh"].IsXValueIndexed = true;
        }

        private void btnstop_Click(object sender, EventArgs e)
        {
            timerrecord.Stop();
            timermomentankwh.Stop();
            disconnects7300();
            pbmomentankw.Value = 0;
        }

        private void btnrecord_Click(object sender, EventArgs e)
        {
            DateTime logtime = DateTime.Now;

            logfilename = "Logfile_" + logtime.Year.ToString() + logtime.Month.ToString() + logtime.Day.ToString() + logtime.Hour.ToString() + logtime.Minute.ToString() + logtime.Second.ToString() + ".txt";

            InitiateTextFile(logfilename, "Datum;Zeit;kWh");

            readdbkw();
            readtime();
            kwchart.Series["kWh"].Points.AddXY(recorddate.ToOADate(), pbmomentankw.Value);

            timerrecord.Start();
        }

        private void timermomentankwh_Tick(object sender, EventArgs e)
        {
            readdbkw();
            readtime();
        }

        private void timerrecord_Tick(object sender, EventArgs e)
        {
            recordkwh();
            kwchart.Series["kWh"].Points.AddXY(recorddate.ToOADate(), pbmomentankw.Value);
        }

        private void kwchart_MouseMove(object sender, MouseEventArgs e)
        {
            //kwchart.Series["kWh"].ToolTip = "#VALX, #VALY";
        }

        private void btnsavechart_Click(object sender, EventArgs e)
        {
            this.kwchart.SaveImage(logfilename + ".png", System.Drawing.Imaging.ImageFormat.Png);
        }
    }
}

PT100 Widerstandswert in Temperatur umrechnen

aivalue = Widerstandswert
tmpcalc = Temperatur

tmpcalc = ((3.90802*0.1)/(2*5.802*0.00001)) - Math.Sqrt(((0.390802*0.390802)/(4*((5.802*0.00001)*(5.802*0.00001)))) - ((aivalue-100)/(5.802*0.00001)));