예제 #1
0
        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);

            SQLiteCommand command = sqlConnection.CreateCommand();

            command.CommandText = "select * from Data";

            SQLiteDataReader dataReader = command.ExecuteReader();

            DataTable table = new DataTable();

            table.Load(dataReader);

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

                foreach (DataRow row in table.Rows)
                {
                    connection.WriteByte("DB111.DBB 2", Convert.ToByte(row["NumberOfPages"]));      // Number of pages.
                    connection.WriteInt16("DB111.DBW 4", Convert.ToInt16(row["Resolution"]));       // Resolution in dpi.
                    connection.WriteInt32("DB111.DBD 6", Convert.ToInt32(row["LineHeight"]));       // Line Height in pixels.
                    connection.WriteReal("DB111.DBD 10", Convert.ToSingle(row["Price"]));           // Price.
                    connection.WriteString("DB111.DBB 20", Convert.ToString(row["ArticleNumber"])); // Article Number.

                    connection.WriteBoolean("DB111.DBX 1.0", true);

                    // Wait while printing.
                    while (connection.ReadBoolean("E 1.0"))
                    {
                        Thread.Sleep(TimeSpan.FromMilliseconds(100));
                    }
                }
            }
            #endregion

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

                PlcByte   numberOfPages = new PlcByte("DB111.DBB 2");
                PlcInt16  resolution    = new PlcInt16("DB111.DBW 4");
                PlcInt32  lineHeight    = new PlcInt32("DB111.DBD 6");
                PlcReal   price         = new PlcReal("DB111.DBD 10");
                PlcString articleNumber = new PlcString("DB111.DBB 20", 16);

                PlcBoolean startPrint = new PlcBoolean("DB111.DBX 1.0", true);

                foreach (DataRow row in table.Rows)
                {
                    numberOfPages.Value = Convert.ToByte(row["NumberOfPages"]);     // Number of pages.
                    resolution.Value    = Convert.ToInt16(row["Resolution"]);       // Resolution in dpi.
                    lineHeight.Value    = Convert.ToInt32(row["LineHeight"]);       // Line Height in pixels.
                    price.Value         = Convert.ToSingle(row["Price"]);           // Price.
                    articleNumber.Value = Convert.ToString(row["ArticleNumber"]);   // Article Number.

                    connection.WriteValues(numberOfPages, resolution, lineHeight, price, articleNumber, startPrint);

                    // Wait while printing.
                    while (connection.ReadBoolean("E 1.0"))
                    {
                        Thread.Sleep(TimeSpan.FromMilliseconds(100));
                    }
                }
            }
            #endregion

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

                foreach (DataRow row in table.Rows)
                {
                    PrintJobData data = new PrintJobData();

                    data.NumberOfPages = Convert.ToByte(row["NumberOfPages"]);     // Number of pages.
                    data.Resolution    = Convert.ToInt16(row["Resolution"]);       // Resolution in dpi.
                    data.LineHeight    = Convert.ToInt32(row["LineHeight"]);       // Line Height in pixels.
                    data.Price         = Convert.ToSingle(row["Price"]);           // Price.
                    data.ArticleNumber = Convert.ToString(row["ArticleNumber"]);   // Article Number.

                    connection.WriteObject(data);

                    // Wait while printing.
                    while (connection.ReadBoolean("E 1.0"))
                    {
                        Thread.Sleep(TimeSpan.FromMilliseconds(100));
                    }
                }
            }
            #endregion

            sqlConnection.Close();
            connection.Close();
        }
예제 #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();

            #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();
        }
예제 #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.
            {
                //// Either use the primitive low level methods of the PLC device connection to
                //// sequential write the data.

                foreach (string line in File.ReadAllLines(@".\Data.csv"))
                {
                    string[] values = line.Split(';');

                    connection.WriteByte("DB111.DBB 2", Convert.ToByte(values[0]));     // Number of pages.
                    connection.WriteInt16("DB111.DBW 4", Convert.ToInt16(values[1]));   // Resolution in dpi.
                    connection.WriteInt32("DB111.DBD 6", Convert.ToInt32(values[2]));   // Line Height in pixels.
                    connection.WriteReal("DB111.DBD 10", Convert.ToSingle(values[3]));  // Price.
                    connection.WriteString("DB111.DBB 20", values[4]);                  // Article Number.

                    connection.WriteBoolean("DB111.DBX 1.0", true);

                    // Wait while printing.
                    while (connection.ReadBoolean("E 1.0"))
                    {
                        Thread.Sleep(TimeSpan.FromMilliseconds(100));
                    }
                }
            }
            #endregion

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

                PlcByte   numberOfPages = new PlcByte("DB111.DBB 2");
                PlcInt16  resolution    = new PlcInt16("DB111.DBW 4");
                PlcInt32  lineHeight    = new PlcInt32("DB111.DBD 6");
                PlcReal   price         = new PlcReal("DB111.DBD 10");
                PlcString articleNumber = new PlcString("DB111.DBB 20", 16);

                PlcBoolean startPrint = new PlcBoolean("DB111.DBX 1.0", true);

                foreach (string line in File.ReadAllLines(@".\Data.csv"))
                {
                    string[] values = line.Split(';');

                    numberOfPages.Value = Convert.ToByte(values[0]);     // Number of pages.
                    resolution.Value    = Convert.ToInt16(values[1]);    // Resolution in dpi.
                    lineHeight.Value    = Convert.ToInt32(values[2]);    // Line Height in pixels.
                    price.Value         = Convert.ToSingle(values[3]);   // Price.
                    articleNumber.Value = Convert.ToString(values[4]);   // Article Number.

                    connection.WriteValues(numberOfPages, resolution, lineHeight, price, articleNumber, startPrint);

                    // Wait while printing.
                    while (connection.ReadBoolean("E 1.0"))
                    {
                        Thread.Sleep(TimeSpan.FromMilliseconds(100));
                    }
                }
            }
            #endregion

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

                foreach (string line in File.ReadAllLines(@".\Data.csv"))
                {
                    string[]     values = line.Split(';');
                    PrintJobData data   = new PrintJobData();

                    data.NumberOfPages = Convert.ToByte(values[0]);     // Number of pages.
                    data.Resolution    = Convert.ToInt16(values[1]);    // Resolution in dpi.
                    data.LineHeight    = Convert.ToInt32(values[2]);    // Line Height in pixels.
                    data.Price         = Convert.ToSingle(values[3]);   // Price.
                    data.ArticleNumber = Convert.ToString(values[4]);   // Article Number.

                    connection.WriteObject(data);

                    // Wait while printing.
                    while (connection.ReadBoolean("E 1.0"))
                    {
                        Thread.Sleep(TimeSpan.FromMilliseconds(100));
                    }
                }
            }
            #endregion

            connection.Close();
        }
예제 #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 read.
            OleDbCommand command = excelConnection.CreateCommand();

            command.CommandText = "select * from [Data$]";

            OleDbDataReader dataReader = command.ExecuteReader();

            DataTable table = new DataTable();

            table.Load(dataReader);

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

                foreach (DataRow row in table.Rows)
                {
                    connection.WriteByte("DB111.DBB 2", Convert.ToByte(row[0]));        // Number of pages.
                    connection.WriteInt16("DB111.DBW 4", Convert.ToInt16(row[1]));      // Resolution in dpi.
                    connection.WriteInt32("DB111.DBD 6", Convert.ToInt32(row[2]));      // Line Height in pixels.
                    connection.WriteReal("DB111.DBD 10", Convert.ToSingle(row[3]));     // Price.
                    connection.WriteString("DB111.DBB 20", Convert.ToString(row[4]));   // Article Number.

                    connection.WriteBoolean("DB111.DBX 1.0", true);

                    // Wait while printing.
                    while (connection.ReadBoolean("E 1.0"))
                    {
                        Thread.Sleep(TimeSpan.FromMilliseconds(100));
                    }
                }
            }
            #endregion

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

                PlcByte   numberOfPages = new PlcByte("DB111.DBB 2");
                PlcInt16  resolution    = new PlcInt16("DB111.DBW 4");
                PlcInt32  lineHeight    = new PlcInt32("DB111.DBD 6");
                PlcReal   price         = new PlcReal("DB111.DBD 10");
                PlcString articleNumber = new PlcString("DB111.DBB 20", 16);

                PlcBoolean startPrint = new PlcBoolean("DB111.DBX 1.0", true);

                foreach (DataRow row in table.Rows)
                {
                    numberOfPages.Value = Convert.ToByte(row["NumberOfPages"]);     // Number of pages.
                    resolution.Value    = Convert.ToInt16(row["Resolution"]);       // Resolution in dpi.
                    lineHeight.Value    = Convert.ToInt32(row["LineHeight"]);       // Line Height in pixels.
                    price.Value         = Convert.ToSingle(row["Price"]);           // Price.
                    articleNumber.Value = Convert.ToString(row["ArticleNumber"]);   // Article Number.

                    connection.WriteValues(numberOfPages, resolution, lineHeight, price, articleNumber, startPrint);

                    // Wait while printing.
                    while (connection.ReadBoolean("E 1.0"))
                    {
                        Thread.Sleep(TimeSpan.FromMilliseconds(100));
                    }
                }
            }
            #endregion

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

                foreach (DataRow row in table.Rows)
                {
                    PrintJobData data = new PrintJobData();

                    data.NumberOfPages = Convert.ToByte(row["NumberOfPages"]);     // Number of pages.
                    data.Resolution    = Convert.ToInt16(row["Resolution"]);       // Resolution in dpi.
                    data.LineHeight    = Convert.ToInt32(row["LineHeight"]);       // Line Height in pixels.
                    data.Price         = Convert.ToSingle(row["Price"]);           // Price.
                    data.ArticleNumber = Convert.ToString(row["ArticleNumber"]);   // Article Number.

                    connection.WriteObject(data);

                    // Wait while printing.
                    while (connection.ReadBoolean("E 1.0"))
                    {
                        Thread.Sleep(TimeSpan.FromMilliseconds(100));
                    }
                }
            }
            #endregion

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