public static void Main(string[] args)
        {
            SimaticDevice device = new SimaticDevice("192.168.0.80", SimaticDeviceType.S7300_400);

            PlcDeviceConnection connection = device.CreateConnection();

            connection.Open();

            SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder();

            builder.DataSource = @".\Database.db";

            SQLiteConnection sqlConnection = new SQLiteConnection(
                builder.ToString(),
                parseViaFramework: true);

            sqlConnection.Open();

            SQLiteCommand command = sqlConnection.CreateCommand();

            command.CommandText
                = "insert into Data (ChanceOfRain, WindSpeed, Pressure, Temperature, Forecast) "
                  + "values (@chanceOfRain, @windSpeed, @pressure, @temperature, @forecast)";

            SQLiteParameter sqlChanceOfRain = command.Parameters.Add("@chanceOfRain", DbType.Int16);
            SQLiteParameter sqlWindSpeed    = command.Parameters.Add("@windSpeed", DbType.Int16);
            SQLiteParameter sqlPressure     = command.Parameters.Add("@pressure", DbType.Int16);
            SQLiteParameter sqlTemperature  = command.Parameters.Add("@temperature", DbType.Single);
            SQLiteParameter sqlForecast     = command.Parameters.Add("@forecast", DbType.String);

            #region 1. Way: Sequential Read.
            {
                //// Either use the primitive low level methods of the PLC device connection to
                //// sequential read the desired data areas.

                // Is the weather station online.
                while (connection.ReadBoolean("E 1.0"))
                {
                    sqlChanceOfRain.Value = connection.ReadByte("DB111.DBB 2");        // Chance of rain.
                    sqlWindSpeed.Value    = connection.ReadInt16("DB111.DBW 4");       // Wind speed.
                    sqlPressure.Value     = connection.ReadInt32("DB111.DBD 6");       // Pressure.
                    sqlTemperature.Value  = connection.ReadReal("DB111.DBD 10");       // Temperature.
                    sqlForecast.Value     = connection.ReadString("DB111.DBB 20", 16); // Forecast.

                    command.ExecuteNonQuery();
                    Thread.Sleep(TimeSpan.FromMinutes(30));
                }
            }
            #endregion

            #region 2. Way: Bulk read (with variables).
            {
                //// Or use the higher level methods of the PLC device connection to read a whole
                //// set of variables at once. While this way would be much faster than the previous
                //// one, because the values are read within one transaction instead of querying
                //// each value within a transaction for each request.

                PlcByte   chanceOfRain = new PlcByte("DB111.DBB 2");
                PlcInt16  windSpeed    = new PlcInt16("DB111.DBW 4");
                PlcInt32  pressure     = new PlcInt32("DB111.DBD 6");
                PlcReal   temperature  = new PlcReal("DB111.DBD 10");
                PlcString forecast     = new PlcString("DB111.DBB 20", 16);

                // Is the weather station online.
                while (connection.ReadBoolean("E 1.0"))
                {
                    connection.ReadValues(chanceOfRain, windSpeed, pressure, temperature, forecast);

                    sqlChanceOfRain.Value = chanceOfRain.Value; // Chance of rain.
                    sqlWindSpeed.Value    = windSpeed.Value;    // Wind speed.
                    sqlPressure.Value     = pressure.Value;     // Pressure.
                    sqlTemperature.Value  = temperature.Value;  // Temperature.
                    sqlForecast.Value     = forecast.Value;     // Forecast.

                    command.ExecuteNonQuery();
                    Thread.Sleep(TimeSpan.FromMinutes(30));
                }
            }
            #endregion

            #region 3. Way: Bulk read (with object).
            {
                //// Or use the methods of the PLC device connection at the highest abstraction
                //// layer to read the whole PLC data at once into a user defined PLC object.

                // Is the weather station online.
                while (connection.ReadBoolean("E 1.0"))
                {
                    WeatherData data = connection.ReadObject <WeatherData>();

                    sqlChanceOfRain.Value = data.ChanceOfRain; // Chance of rain.
                    sqlWindSpeed.Value    = data.WindSpeed;    // Wind speed.
                    sqlPressure.Value     = data.Pressure;     // Pressure.
                    sqlTemperature.Value  = data.Temperature;  // Temperature.
                    sqlForecast.Value     = data.Forecast;     // Forecast.

                    command.ExecuteNonQuery();
                    Thread.Sleep(TimeSpan.FromMinutes(30));
                }
            }
            #endregion

            sqlConnection.Close();
            connection.Close();
        }
Esempio n. 2
0
        public static void Main(string[] args)
        {
            SimaticDevice device = new SimaticDevice("192.168.0.80", SimaticDeviceType.S7300_400);

            PlcDeviceConnection connection = device.CreateConnection();

            connection.Open();

            string[]     values = new string[5];
            StreamWriter writer = File.AppendText(@".\Data.csv");

            #region 1. Way: Sequential Read.
            {
                //// Either use the primitive low level methods of the PLC device connection to
                //// sequential read the desired data areas.

                // Is the weather station online.
                while (connection.ReadBoolean("E 1.0"))
                {
                    values[0] = connection.ReadByte("DB111.DBB 2").ToString();  // Chance of rain.
                    values[1] = connection.ReadInt16("DB111.DBW 4").ToString(); // Wind speed.
                    values[2] = connection.ReadInt32("DB111.DBD 6").ToString(); // Pressure.
                    values[3] = connection.ReadReal("DB111.DBD 10").ToString(); // Temperature.
                    values[4] = connection.ReadString("DB111.DBB 20", 16);      // Forecast.

                    writer.WriteLine(string.Join(";", values));
                    writer.Flush();

                    Thread.Sleep(TimeSpan.FromMinutes(30));
                }
            }
            #endregion

            #region 2. Way: Bulk read (with variables).
            {
                //// Or use the higher level methods of the PLC device connection to read a whole
                //// set of variables at once. While this way would be much faster than the
                //// previous one, because the values are read within one transaction instead of
                //// querying each value within a transaction for each request.

                PlcByte   chanceOfRain = new PlcByte("DB111.DBB 2");
                PlcInt16  windSpeed    = new PlcInt16("DB111.DBW 4");
                PlcInt32  pressure     = new PlcInt32("DB111.DBD 6");
                PlcReal   temperature  = new PlcReal("DB111.DBD 10");
                PlcString forecast     = new PlcString("DB111.DBB 20", 16);

                // Is the weather station online.
                while (connection.ReadBoolean("E 1.0"))
                {
                    connection.ReadValues(chanceOfRain, windSpeed, pressure, temperature, forecast);

                    values[0] = chanceOfRain.Value.ToString();  // Chance of rain.
                    values[1] = windSpeed.Value.ToString();     // Wind speed.
                    values[2] = pressure.Value.ToString();      // Pressure.
                    values[3] = temperature.Value.ToString();   // Temperature.
                    values[4] = forecast.Value.ToString();      // Forecast.

                    writer.WriteLine(string.Join(";", values));
                    writer.Flush();

                    Thread.Sleep(TimeSpan.FromMinutes(30));
                }
            }
            #endregion

            #region 3. Way: Bulk read (with object).
            {
                //// Or use the methods of the PLC device connection at the highest abstraction
                //// layer to read the whole PLC data at once into a user defined PLC object.

                // Is the weather station online.
                while (connection.ReadBoolean("E 1.0"))
                {
                    WeatherData data = connection.ReadObject <WeatherData>();

                    values[0] = data.ChanceOfRain.ToString();  // Chance of rain.
                    values[1] = data.WindSpeed.ToString();     // Wind speed.
                    values[2] = data.Pressure.ToString();      // Pressure.
                    values[3] = data.Temperature.ToString();   // Temperature.
                    values[4] = data.Forecast.ToString();      // Forecast.

                    writer.WriteLine(string.Join(";", values));
                    writer.Flush();

                    Thread.Sleep(TimeSpan.FromMinutes(30));
                }
            }
            #endregion

            writer.Close();
            connection.Close();
        }
Esempio n. 3
0
        public static void Main(string[] args)
        {
            SimaticDevice device = new SimaticDevice("192.168.0.80", SimaticDeviceType.S7300_400);

            PlcDeviceConnection connection = device.CreateConnection();

            connection.Open();

            #region 1. Way: Sequential Write/Read.
            {
                //// Either use the primitive low level methods of the PLC device connection to
                //// sequential write the data or read the desired data areas.

                connection.WriteByte("DB111.DBB 2", 15);
                Console.WriteLine("DB111.DBB 2: {0}", connection.ReadByte("DB111.DBB 2"));

                connection.WriteInt16("DB111.DBW 4", 600);
                Console.WriteLine("DB111.DBW 4: {0}", connection.ReadInt16("DB111.DBW 4"));

                connection.WriteInt32("DB111.DBD 6", 280);
                Console.WriteLine("DB111.DBD 6: {0}", connection.ReadInt32("DB111.DBD 6"));

                connection.WriteReal("DB111.DBD 10", 2.46f);
                Console.WriteLine("DB111.DBD 10: {0}", connection.ReadReal("DB111.DBD 10"));

                connection.WriteString("DB111.DBB 20", "4-036300-076816");
                Console.WriteLine("DB111.DBB 20: {0}", connection.ReadString("DB111.DBB 20", 16));
            }
            #endregion

            #region 2. Way: Bulk write/read (with variables).
            {
                //// Or use the higher level methods of the PLC device connection to write/read
                //// a whole set of variables at once. While this way would be much faster than the
                //// previous one, because the values are write/read within one transaction
                //// instead of processing each value within a transaction for each action.

                connection.WriteValues(
                    new PlcByte("DB111.DBB 2", 15),
                    new PlcInt16("DB111.DBW 4", 600),
                    new PlcInt32("DB111.DBD 6", 280),
                    new PlcReal("DB111.DBD 10", 2.46f),
                    new PlcString("DB111.DBB 20", "4-036300-076816", 16));

                object[] values = connection.ReadValues(
                    new PlcByte("DB111.DBB 2"),
                    new PlcInt16("DB111.DBW 4"),
                    new PlcInt32("DB111.DBD 6"),
                    new PlcReal("DB111.DBD 10"),
                    new PlcString("DB111.DBB 20", 16));

                Console.WriteLine("DB111.DBB 2: {0}", values[0]);
                Console.WriteLine("DB111.DBW 4: {0}", values[1]);
                Console.WriteLine("DB111.DBD 6: {0}", values[2]);
                Console.WriteLine("DB111.DBD 10: {0}", values[3]);
                Console.WriteLine("DB111.DBB 20: {0}", values[4]);
            }
            #endregion

            #region 3. Way: Bulk write/read (with object).
            {
                //// Or use the methods of the PLC device connection at the highest abstraction
                //// layer to write/read the whole PLC data at once from a user defined PLC object.

                Data data = new Data();
                data.ByteValue   = 15;
                data.Int16Value  = 600;
                data.Int32Value  = 280;
                data.RealValue   = 2.46f;
                data.StringValue = "4-036300-076816";

                connection.WriteObject(data);
                data = connection.ReadObject <Data>();

                Console.WriteLine("DB111.DBB 2: {0}", data.ByteValue);
                Console.WriteLine("DB111.DBW 4: {0}", data.Int16Value);
                Console.WriteLine("DB111.DBD 6: {0}", data.Int32Value);
                Console.WriteLine("DB111.DBD 10: {0}", data.RealValue);
                Console.WriteLine("DB111.DBB 20: {0}", data.StringValue);
            }
            #endregion

            Console.ReadKey();
        }
Esempio n. 4
0
        public static void Main(string[] args)
        {
            SimaticDevice device = new SimaticDevice("192.168.0.80", SimaticDeviceType.S7300_400);

            PlcDeviceConnection connection = device.CreateConnection();

            connection.Open();

            // NOTE
            // To access XLS files using OLEDB you will need to install
            // "Microsoft Access Database Engine 2010 Redistributable"
            // https://www.microsoft.com/en-us/download/details.aspx?id=13255
            OleDbConnection excelConnection = new OleDbConnection(
                "Provider=Microsoft.ACE.OLEDB.12.0;"
                + @"Data Source=.\Data.xls;"
                + "Extended Properties=Excel 12.0");

            excelConnection.Open();

            // 'Data' represents the Excel Worksheet to write.
            OleDbCommand command = excelConnection.CreateCommand();

            command.CommandText = "insert into [Data$] values (?, ?, ?, ?, ?)";

            OleDbParameter excelChanceOfRain = command.Parameters.Add("@chanceOfRain", OleDbType.UnsignedTinyInt);
            OleDbParameter excelWindSpeed    = command.Parameters.Add("@windSpeed", OleDbType.SmallInt);
            OleDbParameter excelPressure     = command.Parameters.Add("@pressure", OleDbType.Integer);
            OleDbParameter excelTemperature  = command.Parameters.Add("@temperature", OleDbType.Single);
            OleDbParameter excelForecast     = command.Parameters.Add("@forecast", OleDbType.BSTR);

            #region 1. Way: Sequential Read.
            {
                //// Either use the primitive low level methods of the PLC device connection to
                //// sequential read the desired data areas.

                // Is the weather station online.
                while (connection.ReadBoolean("E 1.0"))
                {
                    excelChanceOfRain.Value = connection.ReadByte("DB111.DBB 2");        // Chance of rain.
                    excelWindSpeed.Value    = connection.ReadInt16("DB111.DBW 4");       // Wind speed.
                    excelPressure.Value     = connection.ReadInt32("DB111.DBD 6");       // Pressure.
                    excelTemperature.Value  = connection.ReadReal("DB111.DBD 10");       // Temperature.
                    excelForecast.Value     = connection.ReadString("DB111.DBB 20", 16); // Forecast.

                    command.ExecuteNonQuery();
                    Thread.Sleep(TimeSpan.FromMinutes(30));
                }
            }
            #endregion

            #region 2. Way: Bulk read (with variables).
            {
                //// Or use the higher level methods of the PLC device connection to read a whole
                //// set of variables at once. While this way would be much faster than the previous
                //// one, because the values are read within one transaction instead of querying
                //// each value within a transaction for each request.

                PlcByte   chanceOfRain = new PlcByte("DB111.DBB 2");
                PlcInt16  windSpeed    = new PlcInt16("DB111.DBW 4");
                PlcInt32  pressure     = new PlcInt32("DB111.DBD 6");
                PlcReal   temperature  = new PlcReal("DB111.DBD 10");
                PlcString forecast     = new PlcString("DB111.DBB 20", 16);

                // Is the weather station online.
                while (connection.ReadBoolean("E 1.0"))
                {
                    connection.ReadValues(chanceOfRain, windSpeed, pressure, temperature, forecast);

                    excelChanceOfRain.Value = chanceOfRain.Value; // Chance of rain.
                    excelWindSpeed.Value    = windSpeed.Value;    // Wind speed.
                    excelPressure.Value     = pressure.Value;     // Pressure.
                    excelTemperature.Value  = temperature.Value;  // Temperature.
                    excelForecast.Value     = forecast.Value;     // Forecast.

                    command.ExecuteNonQuery();
                    Thread.Sleep(TimeSpan.FromMinutes(30));
                }
            }
            #endregion

            #region 3. Way: Bulk read (with object).
            {
                //// Or use the methods of the PLC device connection at the highest abstraction
                //// layer to read the whole PLC data at once into a user defined PLC object.

                // Is the weather station online.
                while (connection.ReadBoolean("E 1.0"))
                {
                    WeatherData data = connection.ReadObject <WeatherData>();

                    excelChanceOfRain.Value = data.ChanceOfRain; // Chance of rain.
                    excelWindSpeed.Value    = data.WindSpeed;    // Wind speed.
                    excelPressure.Value     = data.Pressure;     // Pressure.
                    excelTemperature.Value  = data.Temperature;  // Temperature.
                    excelForecast.Value     = data.Forecast;     // Forecast.

                    command.ExecuteNonQuery();
                    Thread.Sleep(TimeSpan.FromMinutes(30));
                }
            }
            #endregion

            excelConnection.Close();
            connection.Close();
        }