1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
|
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
namespace CLIENT
{
/// <summary>
/// Description résumée de Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private SoftWIRE.ErrorCatcher errorCatcher1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
/// <summary>
/// Variable nécessaire au concepteur.
/// </summary>
private System.ComponentModel.Container components = null;
private Form form2 = new Form();
private Thread _thread1;
private Thread threadAffiche;
private System.Windows.Forms.TextBox textBox1;
byte [] _TableauDoubleParam = new byte[256];
byte [] ReceiveSimVitesse = new byte[1024];
byte [] ReceiveSimPosition = new byte[1024];
byte [] ReceiveSimId = new byte[1024];
byte [] ReceiveData = new byte[128];
byte [] ByteParamSend = new byte[128];
byte [] ByteParamReceive = new byte[128];
string [] TableauParam = new string[22];
double [] DataVitesseReceive = new double[1024];
private System.Windows.Forms.Splitter splitter1;
byte [] ReceiveSimIq = new byte[1024];
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.TextBox textBox6;
private SoftWIRE.VI.StripChart stripChart1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
public Socket ClientSocket;
public Form1()
{
//
// Requis pour la prise en charge du Concepteur Windows Forms
//
InitializeComponent();
//
// TODO : ajoutez le code du constructeur après l'appel à InitializeComponent
//
SoftWIRE.Diagram.Initialize(this);
}
/// <summary>
/// Nettoyage des ressources utilisées.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
/// le contenu de cette méthode avec l'éditeur de code.
/// </summary>
private void InitializeComponent()
{
this.errorCatcher1 = new SoftWIRE.ErrorCatcher();
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.splitter1 = new System.Windows.Forms.Splitter();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.textBox6 = new System.Windows.Forms.TextBox();
this.stripChart1 = new SoftWIRE.VI.StripChart();
this.button2 = new System.Windows.Forms.Button();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// errorCatcher1
//
this.errorCatcher1.AllowThrowExceptions = false;
this.errorCatcher1.CancelBlock = false;
this.errorCatcher1.ControlIn = true;
this.errorCatcher1.ControlName = "errorCatcher1";
this.errorCatcher1.DisplayFormat = SoftWIRE.DisplayFormat.MsgBox;
this.errorCatcher1.FileName = "";
this.errorCatcher1.FormName = "Form1";
this.errorCatcher1.Location = new System.Drawing.Point(20000, 20000);
this.errorCatcher1.LogToFile = false;
this.errorCatcher1.Name = "errorCatcher1";
this.errorCatcher1.Namespace = null;
this.errorCatcher1.Size = new System.Drawing.Size(25, 25);
this.errorCatcher1.SoftWIRE_Tag = null;
this.errorCatcher1.TabIndex = 0;
this.errorCatcher1.TabStop = false;
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(0, 176);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(256, 121);
this.listBox1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Connecter";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(128, 8);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 2;
this.textBox1.Text = "192.168.2.10";
//
// splitter1
//
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(3, 310);
this.splitter1.TabIndex = 3;
this.splitter1.TabStop = false;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(16, 40);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 4;
this.textBox2.Text = "1.01";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(16, 64);
this.textBox3.Name = "textBox3";
this.textBox3.TabIndex = 5;
this.textBox3.Text = "100";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(16, 88);
this.textBox4.Name = "textBox4";
this.textBox4.TabIndex = 6;
this.textBox4.Text = "34";
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(16, 112);
this.textBox5.Name = "textBox5";
this.textBox5.TabIndex = 7;
this.textBox5.Text = "15.78";
//
// textBox6
//
this.textBox6.Location = new System.Drawing.Point(16, 136);
this.textBox6.Name = "textBox6";
this.textBox6.TabIndex = 8;
this.textBox6.Text = "1000";
//
// stripChart1
//
this.stripChart1.AllowThrowExceptions = false;
this.stripChart1.AutoScale = SoftWIRE.VI.AutoScale.Both;
this.stripChart1.BackColor = System.Drawing.SystemColors.Control;
this.stripChart1.Border = SoftWIRE.VI.Border.Etched;
this.stripChart1.BufferSize = 2001;
this.stripChart1.CancelBlock = false;
this.stripChart1.Caption = "";
this.stripChart1.ControlIn = true;
this.stripChart1.ControlName = "stripChart1";
this.stripChart1.ControlSurface = SoftWIRE.VI.ControlSurface.Highlighted;
this.stripChart1.DateTimeFormat = SoftWIRE.VI.StripChart.DateLabelFormat.Time_HHMMSS;
this.stripChart1.DateTimeMode = false;
this.stripChart1.DisplaySize = 600;
this.stripChart1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.stripChart1.ForeColor = System.Drawing.SystemColors.ControlText;
this.stripChart1.FormName = "Form1";
this.stripChart1.Location = new System.Drawing.Point(272, 8);
this.stripChart1.MajorGrid = new SoftWIRE.VI.Grid(false, System.Drawing.Color.Gray, System.Drawing.Drawing2D.DashStyle.Solid);
this.stripChart1.MinorGrid = new SoftWIRE.VI.Grid(false, System.Drawing.Color.Gray, System.Drawing.Drawing2D.DashStyle.Solid);
this.stripChart1.Name = "stripChart1";
this.stripChart1.Namespace = null;
this.stripChart1.PlotBackgroundColor = System.Drawing.Color.Linen;
this.stripChart1.Plots = new SoftWIRE.VI.Plot[] {
new SoftWIRE.VI.Plot(1, System.Drawing.Drawing2D.DashStyle.Solid, System.Drawing.Color.Red, true),
new SoftWIRE.VI.Plot(1, System.Drawing.Drawing2D.DashStyle.Solid, System.Drawing.Color.RoyalBlue, true),
new SoftWIRE.VI.Plot(1, System.Drawing.Drawing2D.DashStyle.Solid, System.Drawing.Color.Purple, true),
new SoftWIRE.VI.Plot(1, System.Drawing.Drawing2D.DashStyle.Solid, System.Drawing.Color.Turquoise, true),
new SoftWIRE.VI.Plot(1, System.Drawing.Drawing2D.DashStyle.Solid, System.Drawing.Color.Magenta, true),
new SoftWIRE.VI.Plot(1, System.Drawing.Drawing2D.DashStyle.Solid, System.Drawing.Color.Aqua, true),
new SoftWIRE.VI.Plot(1, System.Drawing.Drawing2D.DashStyle.Solid, System.Drawing.Color.CadetBlue, true),
new SoftWIRE.VI.Plot(1, System.Drawing.Drawing2D.DashStyle.Solid, System.Drawing.Color.LightSlateGray, true)};
this.stripChart1.Size = new System.Drawing.Size(288, 200);
this.stripChart1.SoftWIRE_Tag = null;
this.stripChart1.StepSize = 1;
this.stripChart1.TabIndex = 9;
this.stripChart1.XAxis = new SoftWIRE.VI.ScrollableAxis(4, 3, System.Drawing.Color.Red, System.Drawing.Color.Black, System.Drawing.Color.Black, new System.Drawing.Font("Arial", 8F), 1, "", SoftWIRE.VI.AxisPlacement.Bottom);
this.stripChart1.YAxis = new SoftWIRE.VI.Axis(0, 10, 4, 3, System.Drawing.Color.Red, System.Drawing.Color.Black, System.Drawing.Color.Black, new System.Drawing.Font("Arial", 8F), 1, "", SoftWIRE.VI.AxisPlacement.Left);
//
// button2
//
this.button2.Location = new System.Drawing.Point(272, 216);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(104, 23);
this.button2.TabIndex = 10;
this.button2.Text = "Afficher";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem3});
this.menuItem1.Text = "Paramètres";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "Paramètres du moteur";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.Text = "Paramètres de la simultation";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(568, 310);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button2,
this.stripChart1,
this.textBox6,
this.textBox5,
this.textBox4,
this.textBox3,
this.textBox2,
this.splitter1,
this.textBox1,
this.button1,
this.listBox1});
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Client";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// Point d'entrée principal de l'application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void ThrFunc1()
{
// Traitement effectué par le thread. Calculs est une fonction quelconque de notre Form
IPAddress ip = IPAddress.Parse(textBox1.Text);
IPEndPoint ipEnd = new IPEndPoint (ip,8000);
Socket ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
ClientSocket.Connect(ipEnd);
if(ClientSocket.Connected==true)
{
listBox1.Items.Add("Connecté au serveur " +ClientSocket.LocalEndPoint.ToString());
}
LectureParam();
// Envoi des paramètres au serveur
for(int n=0;n<5;n++)
{
// Je converti le paramètre de String vers un tableau de Byte
ByteParamSend = Encoding.ASCII.GetBytes(TableauParam[n]);
// J'envoi le paramètre sous forme d'un tableau de Byte
ClientSocket.Send(ByteParamSend,0,ByteParamSend.Length,SocketFlags.None);
// Affiche le paramètre dans la ListBox
//listBox1.Items.Add(TableauParam[n]);
// Je me met en Réception pour attendre la confirmation. Je n'utilise pas cette
// confirmation, c'est juste pour synchroniser le dialogue.
ClientSocket.Receive(ByteParamReceive,0,ByteParamReceive.Length,SocketFlags.None);
// Je remplis de caractères vide la Tableau de Réception car si la valeur
// suivante est plus courte en nombre de Byte, je retrouverais les anciens
// byte.
ByteParamSend = Encoding.ASCII.GetBytes(" ");
}
// Boucle de Réception des résultats de la simulation.
// Pour l'instant, je reçois seulement les valeurs de la vitesse, mais pour
// les autres valeurs, il s'agit simplement de répéter la boucle.
for(int n=0;n<600;n++)
{
// Réception de la première valeur sous forme d'un tableau de BYTE
ClientSocket.Receive(ReceiveData,0,ReceiveData.Length,SocketFlags.None);
// Conversion de la valeur en String.
string DataVitesse = Encoding.ASCII.GetString(ReceiveData);
// Envoi de la même valeur pour indiquer au serveur qui la valeur a bien
// été reçue. Il s'agit d'un confirmation qui permettera au server d'envoyer
// la valeur suivante
ClientSocket.Send(ReceiveData,0,ReceiveData.Length,SocketFlags.None);
// Convertir la valeur reçue "string" en une valeur DOUBLE
double DataDouble = Convert.ToDouble(DataVitesse);
// Stocker la valeur dans un tableau de DOUBLE pour pouvoir ensuite
// l'afficher à l'aide d'un graphique. Ce tableau pourra également être
// utilisé pour trouver la valeur maximale ou autre chose.
DataVitesseReceive[n]=DataDouble;
// Affichage des valeurs reçues dans la ListBox
// listBox1.Items.Add(DataDouble.ToString());
}
listBox1.Items.Add("Simulation Terminée");
}
catch(SocketException E)
{
MessageBox.Show(E.Message);
}
}
private void ThrAffiche()
{
for(int n=0;n<600;n++)
{
this.stripChart1.Operate();
this.stripChart1.Value=DataVitesseReceive[n];
}
}
private void StartThread()
{
// ThrFunc est la fonction exécutée par le thread.
_thread1 = new Thread(new ThreadStart(ThrFunc1));
// Démarrage du thread.
_thread1.Start();
}
private void StartThreadAffiche()
{
// ThrFunc est la fonction exécutée par le thread.
threadAffiche = new Thread(new ThreadStart(ThrAffiche));
// Démarrage du thread.
threadAffiche.Start();
}
private void LectureParam()
{
TableauParam[0]=textBox2.Text;
TableauParam[1]=textBox3.Text;
TableauParam[2]=textBox4.Text;
TableauParam[3]=textBox5.Text;
TableauParam[4]=textBox6.Text;
}
private void button1_Click(object sender, System.EventArgs e)
{
StartThread();
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void button2_Click(object sender, System.EventArgs e)
{
StartThreadAffiche();
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
form2.Show();
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
}
}
} |
Partager