Advertisement
Guest User

C# project

a guest
Mar 20th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 20.10 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Drawing;
  4. using System.Collections;
  5. using System.ComponentModel;
  6. using System.Windows.Forms;
  7. using System.Data;
  8. using System.Runtime.InteropServices;
  9. using TwinCAT.Ads;
  10. using MySql.Data.MySqlClient;
  11. using System.Timers;
  12.  
  13. namespace Sample07
  14. {
  15.     /// <summary>
  16.     /// Summary description for Form1.
  17.     /// </summary>
  18.     public class Form1 : System.Windows.Forms.Form
  19.     {
  20.         internal System.Windows.Forms.Button btnDeleteNotifications;
  21.         internal System.Windows.Forms.Button btnAddNotifications;
  22.  
  23.         internal System.Windows.Forms.Button btnRead;
  24.         internal System.Windows.Forms.GroupBox GroupBox3;
  25.         internal System.Windows.Forms.TextBox tbComplexStruct_Datavalues;
  26.         internal System.Windows.Forms.Label Label14;
  27.  
  28.         internal System.Windows.Forms.TextBox tbComplexStruct_stringVal;
  29.         internal System.Windows.Forms.Label Label13;
  30.  
  31.  
  32.         internal System.Windows.Forms.TextBox tbComplexStruct_Countervalues;
  33.         internal System.Windows.Forms.Label Label12;
  34.         private IContainer components;
  35.  
  36.         //PLC variable handles
  37.  
  38.         private int hcomplexStruct;
  39.         private ArrayList notificationHandles;
  40.         private System.Windows.Forms.Timer timer1;
  41.  
  42.         private TcAdsClient adsClient;
  43.  
  44.         public Form1()
  45.         {
  46.             //
  47.             // Required for Windows Form Designer support
  48.             //
  49.             InitializeComponent();
  50.  
  51.             //
  52.             // TODO: Add any constructor code after InitializeComponent call
  53.             //
  54.         }
  55.  
  56.         /// <summary>
  57.         /// Clean up any resources being used.
  58.         /// </summary>
  59.         protected override void Dispose(bool disposing)
  60.         {
  61.             if (disposing)
  62.             {
  63.                 if (components != null)
  64.                 {
  65.                     components.Dispose();
  66.                 }
  67.             }
  68.             base.Dispose(disposing);
  69.         }
  70.  
  71.         #region Windows Form Designer generated code
  72.         /// <summary>
  73.         /// Required method for Designer support - do not modify
  74.         /// the contents of this method with the code editor.
  75.         /// </summary>
  76.         private void InitializeComponent()
  77.         {
  78.             this.components = new System.ComponentModel.Container();
  79.             this.btnDeleteNotifications = new System.Windows.Forms.Button();
  80.             this.btnAddNotifications = new System.Windows.Forms.Button();
  81.             this.btnRead = new System.Windows.Forms.Button();
  82.             this.GroupBox3 = new System.Windows.Forms.GroupBox();
  83.             this.tbComplexStruct_Datavalues = new System.Windows.Forms.TextBox();
  84.             this.Label14 = new System.Windows.Forms.Label();
  85.             this.tbComplexStruct_stringVal = new System.Windows.Forms.TextBox();
  86.             this.Label13 = new System.Windows.Forms.Label();
  87.             this.tbComplexStruct_Countervalues = new System.Windows.Forms.TextBox();
  88.             this.Label12 = new System.Windows.Forms.Label();
  89.             this.timer1 = new System.Windows.Forms.Timer(this.components);
  90.             this.GroupBox3.SuspendLayout();
  91.             this.SuspendLayout();
  92.             //  
  93.             // btnDeleteNotifications
  94.             //  
  95.             this.btnDeleteNotifications.Location = new System.Drawing.Point(975, 96);
  96.             this.btnDeleteNotifications.Name = "btnDeleteNotifications";
  97.             this.btnDeleteNotifications.Size = new System.Drawing.Size(112, 23);
  98.             this.btnDeleteNotifications.TabIndex = 13;
  99.             this.btnDeleteNotifications.Text = "Delete Notifications";
  100.             this.btnDeleteNotifications.Click += new System.EventHandler(this.btnDeleteNotifications_Click);
  101.             //  
  102.             // btnAddNotifications
  103.             //  
  104.             this.btnAddNotifications.Location = new System.Drawing.Point(975, 61);
  105.             this.btnAddNotifications.Name = "btnAddNotifications";
  106.             this.btnAddNotifications.Size = new System.Drawing.Size(112, 23);
  107.             this.btnAddNotifications.TabIndex = 12;
  108.             this.btnAddNotifications.Text = "Add Notifications";
  109.             this.btnAddNotifications.Click += new System.EventHandler(this.btnAddNotifications_Click);
  110.             //  
  111.             // btnRead
  112.             //  
  113.             this.btnRead.Location = new System.Drawing.Point(975, 28);
  114.             this.btnRead.Name = "btnRead";
  115.             this.btnRead.Size = new System.Drawing.Size(112, 23);
  116.             this.btnRead.TabIndex = 10;
  117.             this.btnRead.Text = "Read";
  118.             this.btnRead.Click += new System.EventHandler(this.btnRead_Click);
  119.             //  
  120.             // GroupBox3
  121.             //  
  122.             this.GroupBox3.Controls.Add(this.tbComplexStruct_Datavalues);
  123.             this.GroupBox3.Controls.Add(this.Label14);
  124.             this.GroupBox3.Controls.Add(this.tbComplexStruct_stringVal);
  125.             this.GroupBox3.Controls.Add(this.Label13);
  126.             this.GroupBox3.Controls.Add(this.tbComplexStruct_Countervalues);
  127.             this.GroupBox3.Controls.Add(this.Label12);
  128.             this.GroupBox3.Location = new System.Drawing.Point(12, 8);
  129.             this.GroupBox3.Name = "GroupBox3";
  130.             this.GroupBox3.Size = new System.Drawing.Size(957, 280);
  131.             this.GroupBox3.TabIndex = 9;
  132.             this.GroupBox3.TabStop = false;
  133.             this.GroupBox3.Text = "Data Structure";
  134.             this.GroupBox3.Enter += new System.EventHandler(this.GroupBox3_Enter);
  135.             //  
  136.             // tbComplexStruct_Datavalues
  137.             //  
  138.             this.tbComplexStruct_Datavalues.Location = new System.Drawing.Point(105, 53);
  139.             this.tbComplexStruct_Datavalues.Name = "tbComplexStruct_Datavalues";
  140.             this.tbComplexStruct_Datavalues.Size = new System.Drawing.Size(835, 20);
  141.             this.tbComplexStruct_Datavalues.TabIndex = 20;
  142.             this.tbComplexStruct_Datavalues.TextChanged += new System.EventHandler(this.tbComplexStruct_Datavalues_TextChanged);
  143.             //  
  144.             // Label14
  145.             //  
  146.             this.Label14.Location = new System.Drawing.Point(6, 60);
  147.             this.Label14.Name = "Label14";
  148.             this.Label14.Size = new System.Drawing.Size(99, 16);
  149.             this.Label14.TabIndex = 19;
  150.             this.Label14.Text = "Data array:";
  151.             this.Label14.Click += new System.EventHandler(this.Label14_Click);
  152.             //  
  153.             // tbComplexStruct_stringVal
  154.             //  
  155.             this.tbComplexStruct_stringVal.Location = new System.Drawing.Point(105, 105);
  156.             this.tbComplexStruct_stringVal.Name = "tbComplexStruct_stringVal";
  157.             this.tbComplexStruct_stringVal.Size = new System.Drawing.Size(835, 20);
  158.             this.tbComplexStruct_stringVal.TabIndex = 11;
  159.             this.tbComplexStruct_stringVal.TextChanged += new System.EventHandler(this.tbComplexStruct_stringVal_TextChanged);
  160.             //  
  161.             // Label13
  162.             //  
  163.             this.Label13.Location = new System.Drawing.Point(6, 105);
  164.             this.Label13.Name = "Label13";
  165.             this.Label13.Size = new System.Drawing.Size(109, 16);
  166.             this.Label13.TabIndex = 10;
  167.             this.Label13.Text = "stringValues:";
  168.             this.Label13.Click += new System.EventHandler(this.Label13_Click);
  169.             //  
  170.             // tbComplexStruct_Countervalues
  171.             //  
  172.             this.tbComplexStruct_Countervalues.Location = new System.Drawing.Point(105, 160);
  173.             this.tbComplexStruct_Countervalues.Name = "tbComplexStruct_Countervalues";
  174.             this.tbComplexStruct_Countervalues.Size = new System.Drawing.Size(835, 20);
  175.             this.tbComplexStruct_Countervalues.TabIndex = 18;
  176.             this.tbComplexStruct_Countervalues.TextChanged += new System.EventHandler(this.tbComplexStruct_Countervalues_TextChanged);
  177.             //  
  178.             // Label12
  179.             //  
  180.             this.Label12.Location = new System.Drawing.Point(6, 157);
  181.             this.Label12.Name = "Label12";
  182.             this.Label12.Size = new System.Drawing.Size(100, 23);
  183.             this.Label12.TabIndex = 17;
  184.             this.Label12.Text = "Counter array:";
  185.             this.Label12.Click += new System.EventHandler(this.Label12_Click);
  186.             //  
  187.             // timer1
  188.             //  
  189.             this.timer1.Enabled = true;
  190.             this.timer1.Interval = 10000;
  191.             this.timer1.Tick += new System.EventHandler(this.btnRead_Click);
  192.             //  
  193.             // Form1
  194.             //  
  195.             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  196.             this.ClientSize = new System.Drawing.Size(1181, 302);
  197.             this.Controls.Add(this.btnDeleteNotifications);
  198.             this.Controls.Add(this.btnAddNotifications);
  199.             this.Controls.Add(this.btnRead);
  200.             this.Controls.Add(this.GroupBox3);
  201.             this.Name = "Form1";
  202.             this.Text = "Readout PLC";
  203.             this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
  204.             this.Load += new System.EventHandler(this.Form1_Load);
  205.             this.GroupBox3.ResumeLayout(false);
  206.             this.GroupBox3.PerformLayout();
  207.             this.ResumeLayout(false);
  208.  
  209.         }
  210.         #endregion
  211.  
  212.         /// <summary>
  213.         /// The main entry point for the application.
  214.         /// </summary>
  215.         [STAThread]
  216.         static void Main()
  217.         {
  218.             Application.Run(new Form1());
  219.         }
  220.  
  221.         private void Form1_Load(object sender, System.EventArgs e)
  222.         {
  223.             adsClient = new TcAdsClient();
  224.             notificationHandles = new ArrayList();
  225.             try
  226.             {
  227.                 adsClient.Connect(801);
  228.                 adsClient.AdsNotificationEx += new AdsNotificationExEventHandler(adsClient_AdsNotificationEx);
  229.                 btnDeleteNotifications.Enabled = false;
  230.                 //create handles for the PLC variables;
  231.  
  232.                 hcomplexStruct = adsClient.CreateVariableHandle("MAIN.ComplexStruct1");
  233.             }
  234.             catch (Exception ex)
  235.             {
  236.                 MessageBox.Show(ex.Message);
  237.             }
  238.         }
  239.  
  240.         private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  241.         {
  242.             adsClient.Dispose();
  243.         }
  244.  
  245.  
  246.         private void btnRead_Click(object sender, System.EventArgs e)
  247.         {
  248.             Main2();
  249.             try
  250.             {
  251.                 //read by handle
  252.                 //the second parameter specifies the type of the variable
  253.  
  254.                 FillStructControls((ComplexStruct)adsClient.ReadAny(hcomplexStruct, typeof(ComplexStruct)));
  255.             }
  256.             catch (Exception ex)
  257.             {
  258.                 MessageBox.Show(ex.Message);
  259.             }
  260.         }
  261.  
  262.         private void Main2()
  263.         {
  264.             {
  265.                 string cs = @"server=localhost;userid=root;
  266.            password=flex01;database=gen1";
  267.  
  268.                 for (int i = 0; i < 5; i++)
  269.                 {
  270.                     using (var conn = new MySqlConnection(cs))
  271.                     using (var cmd = conn.CreateCommand())
  272.                     {
  273.                         conn.Open();
  274.                         cmd.CommandText = "INSERT INTO actualvalues (a,b,c) VALUES ('structure.datavalues[0],'structure.Datavalues[1]','structure.Datavalues[2]')";
  275.                         cmd.ExecuteNonQuery();
  276.  
  277.                     }
  278.                 }
  279.             }
  280.         }
  281.         private void btnAddNotifications_Click(object sender, System.EventArgs e)
  282.         {
  283.             notificationHandles.Clear();
  284.             try
  285.             {
  286.                 //register notification            
  287.  
  288.                 notificationHandles.Add(adsClient.AddDeviceNotificationEx("MAIN.complexStruct1", AdsTransMode.OnChange, 100, 0, Datavalues, typeof(ComplexStruct)));
  289.             }
  290.             catch (Exception ex)
  291.             {
  292.                 MessageBox.Show(ex.Message);
  293.             }
  294.             btnDeleteNotifications.Enabled = true;
  295.             btnAddNotifications.Enabled = false;
  296.         }
  297.  
  298.         private void btnDeleteNotifications_Click(object sender, System.EventArgs e)
  299.         {
  300.             //delete registered notifications.
  301.             try
  302.             {
  303.                 foreach (int handle in notificationHandles)
  304.                     adsClient.DeleteDeviceNotification(handle);
  305.             }
  306.             catch (Exception ex)
  307.             {
  308.                 MessageBox.Show(ex.Message);
  309.             }
  310.             notificationHandles.Clear();
  311.             btnAddNotifications.Enabled = true;
  312.             btnDeleteNotifications.Enabled = false;
  313.         }
  314.  
  315.         private void adsClient_AdsNotificationEx(object sender, AdsNotificationExEventArgs e)
  316.         {
  317.             TextBox textBox = (TextBox)e.UserData;
  318.             Type type = e.Value.GetType();
  319.             if (type == typeof(string) || type.IsPrimitive)
  320.                 textBox.Text = e.Value.ToString();
  321.             else if (type == typeof(ComplexStruct))
  322.                 FillStructControls((ComplexStruct)e.Value);
  323.         }
  324.  
  325.         private void FillStructControls(ComplexStruct structure)
  326.         {
  327.  
  328.             tbComplexStruct_Datavalues.Text = String.Format(
  329.                 "{0:d}, {1:d}, {2:d}, {3:d}, {4:d}, {5:d}, {6:d}, {7:d}, {8:d}, {9:d}, {10:d}, {11:d}, {12:d}, {13:d}, {14:d}, {15:d}, {16:d}, {17:d}, {18:d}, {19:d}, {20:d}, {21:d}, {22:d}, {23:d}, {24:d},{25:d}, {26:d}, {27:d}, {28:d},{29:d},{30:d},{31:d}, {32:d}, {33:d}, {34:d},{35:d}, {36:d}, {37:d}, {38:d},{39:d}, {40:d}, {41:d}, {42:d}, {43:d}, {44:d},{45:d}, {46:d}, {47:d}, {48:d},{49:d}, {50:d}, {51:d}, {52:d}, {53:d}, {54:d},{55:d}, {56:d}, {57:d}, {58:d},{59:d}, {60:d}, {51:d}, {52:d}, {53:d}, {54:d},{55:d}, {56:d}, {57:d}, {58:d},{59:d}, {60:d}, {61:d}, {62:d}, {63:d}, {64:d},{65:d}, {66:d}, {67:d}, {68:d},{69:d}, {70:d}, {71:d}, {72:d}, {73:d}, {74:d},{75:d}, {76:d}, {77:d}, {78:d},{79:d}, {80:d}, {81:d}, {82:d}, {83:d}, {84:d},{85:d}, {86:d}, {87:d}, {88:d},{89:d}, {90:d}, {91:d}, {92:d}, {93:d}, {94:d},{95:d}, {96:d}, {97:d}, {98:d},{99:d}, {100:d}"
  330.  
  331.                 , structure.Datavalues[0], structure.Datavalues[1], structure.Datavalues[2], structure.Datavalues[3],
  332.                 structure.Datavalues[4], structure.Datavalues[5], structure.Datavalues[6], structure.Datavalues[7],
  333.                 structure.Datavalues[8], structure.Datavalues[9], structure.Datavalues[10], structure.Datavalues[11],
  334.                 structure.Datavalues[12], structure.Datavalues[13], structure.Datavalues[14], structure.Datavalues[15],
  335.                 structure.Datavalues[16], structure.Datavalues[17], structure.Datavalues[18], structure.Datavalues[19],
  336.                 structure.Datavalues[20], structure.Datavalues[21], structure.Datavalues[22], structure.Datavalues[23],
  337.                 structure.Datavalues[24], structure.Datavalues[25], structure.Datavalues[26], structure.Datavalues[27],
  338.                 structure.Datavalues[28], structure.Datavalues[29], structure.Datavalues[30], structure.Datavalues[31],
  339.                 structure.Datavalues[32], structure.Datavalues[33], structure.Datavalues[34], structure.Datavalues[35],
  340.                 structure.Datavalues[36], structure.Datavalues[37], structure.Datavalues[38], structure.Datavalues[39],
  341.                 structure.Datavalues[40], structure.Datavalues[11], structure.Datavalues[42], structure.Datavalues[43],
  342.                 structure.Datavalues[44], structure.Datavalues[45], structure.Datavalues[46], structure.Datavalues[47],
  343.                 structure.Datavalues[48], structure.Datavalues[49], structure.Datavalues[50], structure.Datavalues[51],
  344.                 structure.Datavalues[52], structure.Datavalues[53], structure.Datavalues[54], structure.Datavalues[55],
  345.                 structure.Datavalues[56], structure.Datavalues[57], structure.Datavalues[58], structure.Datavalues[59],
  346.                 structure.Datavalues[60], structure.Datavalues[61], structure.Datavalues[62], structure.Datavalues[63],
  347.                 structure.Datavalues[64], structure.Datavalues[65], structure.Datavalues[66], structure.Datavalues[67],
  348.                 structure.Datavalues[68], structure.Datavalues[69], structure.Datavalues[70], structure.Datavalues[71],
  349.                 structure.Datavalues[72], structure.Datavalues[73], structure.Datavalues[74], structure.Datavalues[75],
  350.                 structure.Datavalues[76], structure.Datavalues[77], structure.Datavalues[78], structure.Datavalues[79],
  351.                 structure.Datavalues[80], structure.Datavalues[81], structure.Datavalues[82], structure.Datavalues[83],
  352.                  structure.Datavalues[84], structure.Datavalues[85], structure.Datavalues[86], structure.Datavalues[87],
  353.                 structure.Datavalues[88], structure.Datavalues[89], structure.Datavalues[90], structure.Datavalues[91],
  354.                 structure.Datavalues[92], structure.Datavalues[93], structure.Datavalues[94], structure.Datavalues[95],
  355.                 structure.Datavalues[96], structure.Datavalues[97], structure.Datavalues[98], structure.Datavalues[99],
  356.                 structure.Datavalues[100]
  357.                 );
  358.  
  359.             tbComplexStruct_stringVal.Text = structure.stringVal;
  360.  
  361.  
  362.             tbComplexStruct_Countervalues.Text = String.Format(
  363.                 "{0:d}, {1:d}, {2:d}, {3:d}, {4:d}, {5:d}, {6:d}, {7:d}, {8:d}, {9:d}, {10:d}"
  364.  
  365.              , structure.Countervalues[0], structure.Countervalues[1], structure.Countervalues[2], structure.Countervalues[3],
  366.              structure.Countervalues[4], structure.Countervalues[5], structure.Countervalues[6], structure.Countervalues[7],
  367.              structure.Countervalues[8], structure.Countervalues[9], structure.Countervalues[10]);
  368.  
  369.         }
  370.  
  371.         private ComplexStruct GetStructFromControls()
  372.         {
  373.             ComplexStruct structure = new ComplexStruct();
  374.             String[] stringArr = tbComplexStruct_Datavalues.Text.Split(new char[] { ',' });
  375.  
  376.             for (short i = 0; i < stringArr.Length; i++)
  377.                 structure.Datavalues[i] = short.Parse(stringArr[i]);
  378.  
  379.  
  380.             structure.stringVal = tbComplexStruct_stringVal.Text;
  381.  
  382.             String[] stringArra = tbComplexStruct_Countervalues.Text.Split(new char[] { ',' });
  383.             for (int i = 0; i < stringArra.Length; i++)
  384.                 structure.Countervalues[i] = int.Parse(stringArra[i]);
  385.  
  386.             return structure;
  387.         }
  388.  
  389.         public object Datavalues { get; set; }
  390.  
  391.         private void tbComplexStruct_Datavalues_TextChanged(object sender, EventArgs e)
  392.         {
  393.         }
  394.  
  395.         private void Label14_Click(object sender, EventArgs e)
  396.         {
  397.         }
  398.  
  399.         private void tbComplexStruct_stringVal_TextChanged(object sender, EventArgs e)
  400.         {
  401.         }
  402.  
  403.         private void tbComplexStruct_Countervalues_TextChanged(object sender, EventArgs e)
  404.         {
  405.         }
  406.  
  407.         private void Label12_Click(object sender, EventArgs e)
  408.         {
  409.         }
  410.  
  411.         private void Label13_Click(object sender, EventArgs e)
  412.         {
  413.  
  414.         }
  415.  
  416.         private void GroupBox3_Enter(object sender, EventArgs e)
  417.         {
  418.  
  419.         }
  420.  
  421.         private void textBox1_TextChanged(object sender, EventArgs e)
  422.         {
  423.  
  424.         }
  425.  
  426.         private void timer1_Tick(object sender, EventArgs e)
  427.         {
  428.  
  429.         }
  430.     }
  431.  
  432.     [StructLayout(LayoutKind.Sequential, Pack = 1)]
  433.     public class ComplexStruct
  434.     {
  435.         //specifies how .NET should marshal the array
  436.         //SizeConst specifies the number of elements the array has.
  437.         [MarshalAs(UnmanagedType.ByValArray, SizeConst = 101)]
  438.         public short[] Datavalues = new short[101];
  439.  
  440.         //specifies how .NET should marshal the string
  441.         //SizeConst specifies the number of characters the string has.
  442.         //'(inclusive the terminating null ).  
  443.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 81)]
  444.         public string stringVal = "";
  445.  
  446.         //SizeConst specifies the number of elements the array has.
  447.         [MarshalAs(UnmanagedType.ByValArray, SizeConst = 11)]
  448.         public int[] Countervalues = new int[11];
  449.     }
  450. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement