using System; using System.Collections.Generic; using System.Linq; using System.Text; using ElmoMotionControlComponents.Drive.EASComponents; using System.Threading; using System.Net; using System.IO; using ElmoMotionControlComponents.Drive.EASComponents.UploadsAndDownloads; namespace DriveDotNetUploadPersonalityAndSendCommands { class Program { static IDriveCommunication communication; static ManualResetEvent waitEvent = new ManualResetEvent(false); static void Main(string[] args) { Console.WriteLine("Which communication type do you need? 1. UDP, 2. USB, 3. RS232"); int result = int.Parse(Console.ReadLine()); IDriveCommunicationInfo commInfo = null; IDriveErrorObject errorObject; switch (result) { case 1: { Console.WriteLine("Please enter the drive IP address"); string ipAddress = Console.ReadLine(); Console.WriteLine("Please enter the host IP address"); string hostAddress = Console.ReadLine(); commInfo = DriveCommunicationFactory.CreateUDPCommunicationInfo(IPAddress.Parse(ipAddress), IPAddress.Parse(hostAddress)); Console.WriteLine("Established UDP communication for IP address " + ipAddress + ", host IP address: " + hostAddress); break; } case 2: { Console.WriteLine("Please enter COM port name"); string comPort = Console.ReadLine(); commInfo = DriveCommunicationFactory.CreateUSBCommunicationInfo(comPort); Console.WriteLine("Established USB communication for COM port " + comPort); break; } case 3: { Console.WriteLine("Please enter COM port name"); string comPort = Console.ReadLine(); Console.WriteLine("Please enter baud rate"); uint baudRate = uint.Parse(Console.ReadLine()); commInfo = DriveCommunicationFactory.CreateRS232CommunicationInfo(comPort, baudRate); Console.WriteLine("Established RS232 communication for COM port " + comPort + " and baud rate " + baudRate); break; } } IDriveErrorObject err; communication = DriveCommunicationFactory.CreateCommunication(commInfo); if (!communication.Connect(out err)) { Console.WriteLine(err); return; } Console.WriteLine("What do you want to do?\n 1. Create personality model from existing file \n 2. Upload Personality from the drive and create personality model \n 3. Skip Personality model creation"); result = int.Parse(Console.ReadLine()); IUploadDownloadModel model = null; switch (result) { case 1: { string path = null; Console.WriteLine("Type Personality file path:"); path = Console.ReadLine(); if (communication.CreatePersonalityModel(path, out errorObject)) Console.WriteLine("Personality Model was created!"); else { if (errorObject == null) Console.WriteLine("Cannot create Personality Model"); else Console.WriteLine("Cannot create Personality Model. " + errorObject.ToString()); } break; } case 2: { model = communication.UploadPersonality(Path.Combine(Environment.CurrentDirectory, "Personality.xml"), out errorObject); if (model == null) { Console.WriteLine("Cannot upload Personality. " + errorObject.ErrorDescription); break; } model.OnStart += model_OnStart; model.OnProgress += model_OnProgress; model.OnFinish += model_OnFinish; model.OnFailed += model_OnFailed; model.OnCancel += model_OnCancel; model.Start(out errorObject); waitEvent.WaitOne(); break; } case 3: { break; } default: { return; } } SendCommand(); } static void SendCommand() { string command = null, response; IDriveErrorObject errorObject; while (true) { Console.WriteLine("Type a name of command you wish to send"); command = Console.ReadLine(); if (communication.SendCommandAnalyzeError(command, out response, out errorObject, 500)) Console.WriteLine("[" + command + " sent successfully with response] " + response); else { if (errorObject == null) Console.WriteLine(command + " sending failed"); else Console.WriteLine(command + " sending failed with error " + errorObject.ToString()); } } } #region Upload Personality Events static void model_OnFailed(object sender, EventArgs e) { Console.WriteLine("Failed event: Description=" + (sender as IUploadDownloadModel).Description); waitEvent.Set(); } static void model_OnCancel(object sender, EventArgs e) { Console.WriteLine("Cancel event: Description=" + (sender as IUploadDownloadModel).Description); waitEvent.Set(); } static void model_OnFinish(object sender, EventArgs e) { Console.WriteLine("Finish event: Status = " + (sender as IUploadDownloadModel).OperationStatus); IDriveErrorObject errorObject; if ((sender as IUploadDownloadModel).Communication.CreatePersonalityModel((sender as IUploadDownloadModel).StorePath, out errorObject)) Console.WriteLine("Personality Model was created!"); else { if (errorObject == null) Console.WriteLine("Cannot create Personality Model"); else Console.WriteLine("Cannot create Personality Model. " + errorObject.ToString()); } waitEvent.Set(); } static void model_OnProgress(object sender, EventArgs e) { if ((sender as IUploadDownloadModel).Percent == -1) { Console.WriteLine("Progress Indeterminate. Status = " + (sender as IUploadDownloadModel).OperationStatus + " Description = " + (sender as IUploadDownloadModel).Description); return; } if ((sender as IUploadDownloadModel).Percent > 0) Console.SetCursorPosition(0, Console.CursorTop - 1); Console.WriteLine("Progress = " + (sender as IUploadDownloadModel).Percent + " Status = " + (sender as IUploadDownloadModel).OperationStatus + " Description = " + (sender as IUploadDownloadModel).Description); } static void model_OnStart(object sender, EventArgs e) { Console.WriteLine("Start event"); } #endregion Upload Personality Events } }