C# Libnodave Siemens LOGO! 0BA7

Libnodave — LOGO 0BA7

  • Netzwerkfähige LOGO 0BA7
  • Visual C# Express
  • Libnodave Devel Archiv
  • libnodave.dll
  • libnodave.net.dll
  • LOGO Netzwerkeinstellungen VM/Ethernet Verbindung/Server
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 LOGOTest_libnodave
{
    public partial class Form1 : Form
    {
        static libnodave.daveOSserialType fds;
        static libnodave.daveInterface di;
        static libnodave.daveConnection dc;
        static int rack = 0; // LOGO
        static int slot = 0; // LOGO
        int LOGOInputs = 0, LOGOOutputs = 0, res;

        public Form1()
        {
            InitializeComponent();
            connectlogo();
            timer1.Interval = 500;
            timer1.Start();
        }

        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(102, "192.168.1.111");

            fds.wfd = fds.rfd;

            if (fds.rfd > 0)
            {

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

                di.setTimeout(1000000);

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

                dc.connectPLC();
            }
        }

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

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

                    if (res == 0)
                    {

                        LOGOInputs = dc.getU8();

                        cbIn1.Checked = CheckBitSet((byte)LOGOInputs, 0);
                        cbIn2.Checked = CheckBitSet((byte)LOGOInputs, 1);
                        cbIn3.Checked = CheckBitSet((byte)LOGOInputs, 2);
                        cbIn4.Checked = CheckBitSet((byte)LOGOInputs, 3);
                        cbIn5.Checked = CheckBitSet((byte)LOGOInputs, 4);
                        cbIn6.Checked = CheckBitSet((byte)LOGOInputs, 5);
                        cbIn7.Checked = CheckBitSet((byte)LOGOInputs, 6);
                        cbIn8.Checked = CheckBitSet((byte)LOGOInputs, 7);

                    }
                    else
                        Console.WriteLine("Error " + res + " " + libnodave.daveStrerror(res));

                    // Ausgänge lesen
                    res = dc.readBytes(libnodave.daveOutputs, 0, 0, 2, null);

                    if (res == 0)
                    {
                        LOGOOutputs = dc.getU8();

                        cbOut1.Checked = CheckBitSet((byte)LOGOOutputs, 0);
                        if (cbOut1.Checked) { tbOut1.BackColor = Color.Yellow; } else { tbOut1.BackColor = Color.Green; };
                        cbOut2.Checked = CheckBitSet((byte)LOGOOutputs, 1);
                        if (cbOut2.Checked) { tbOut2.BackColor = Color.Yellow; } else { tbOut2.BackColor = Color.Green; };
                        cbOut3.Checked = CheckBitSet((byte)LOGOOutputs, 2);
                        if (cbOut3.Checked) { tbOut3.BackColor = Color.Yellow; } else { tbOut3.BackColor = Color.Green; };
                        cbOut4.Checked = CheckBitSet((byte)LOGOOutputs, 3);
                        if (cbOut4.Checked) { tbOut4.BackColor = Color.Yellow; } else { tbOut4.BackColor = Color.Green; };

                    }
                    else
                        Console.WriteLine("Error " + res + " " + libnodave.daveStrerror(res));
        }

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

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            readlogo();
        }

    }
}