using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Net; using System.IO; using ElmoMotionControlComponents.Drive.EASComponents; using ElmoMotionControlComponents.Drive.EASComponents.UploadsAndDownloads; using ElmoMotionControlComponents.Drive.EASComponents.Personality; using ElmoMotionControlComponents.Drive.EASComponents.DriveCAN; namespace DriveDotNetRecording { class Program { static IDriveCommunication communication; static ManualResetEvent waitEvent = new ManualResetEvent(false); static bool status = true; static RecordingOperator recordingOperator; static void Main(string[] args) { Console.WriteLine("Which communication type do you need? 1. UDP, 2. USB, 3. RS232, 4. CAN (Kvaser)"); int result = int.Parse(Console.ReadLine()); IDriveCommunicationInfo commInfo = null; 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; } case 4: { Console.WriteLine("Please enter 1 for Gold, 2 for SimplIQ"); uint nodeTypeInput = uint.Parse(Console.ReadLine()); CANNodeType nodeType = CANNodeType.ElmoGold; if (nodeTypeInput == 2) nodeType = CANNodeType.ElmoSimplIQ; Console.WriteLine("Please enter CAN bus ID"); uint busID = uint.Parse(Console.ReadLine()); Console.WriteLine("Please enter CAN node ID"); ushort nodeID = ushort.Parse(Console.ReadLine()); Console.WriteLine("Please enter baud rate"); string baudRate = Console.ReadLine(); commInfo = DriveCommunicationFactory.CreateCANKvaserCommunicationInfo(baudRate, busID, nodeID, nodeType); break; } } IDriveErrorObject err; communication = DriveCommunicationFactory.CreateCommunication(commInfo); if (!communication.Connect(out err)) { ExitProgram(GetErrorDesc(err)); return; } if (!UploadPersonality(communication)) { ExitProgram(); return; } PerformRecording(communication); ExitProgram(); } static string GetErrorDesc(IDriveErrorObject errorObj) { if (errorObj.ErrorCode != 0) return string.Format("Drive error {0}: {1}", errorObj.ErrorCode, errorObj.ErrorDescription); else return string.Format("Library error {0}: {1}", errorObj.LibraryErrorCode, errorObj.LibraryErrorDescription); } static void ExitProgram(string error = null) { if (!string.IsNullOrEmpty(error)) Console.WriteLine("Program failed with error " + error + ", press any key to stop"); else Console.WriteLine("Program done, press ENTER to stop"); Console.ReadLine(); } #region Upload Personality static bool UploadPersonality(IDriveCommunication communication) { IDriveErrorObject errorObject; string response; if (!Utils.SendCommand(communication, "RR=-1", out response)) return false; if (!Utils.MotorEnable(communication, false)) return false; Console.WriteLine("Uploading personality..."); IUploadDownloadModel model = communication.UploadPersonality(Path.Combine(Path.GetTempPath(), "Personality.xml"), out errorObject); if (model == null) { Console.WriteLine("Cannot upload Personality. " + errorObject.ErrorDescription); return false; } model.OnStart += model_OnStart; model.OnProgress += model_OnProgress; model.OnFinish += model_OnFinish; model.OnFailed += model_OnFailed; model.OnCancel += model_OnCancel; if (!model.Start(out errorObject)) { Console.WriteLine("Cannot start upload Personality: " + GetErrorDesc(errorObject)); return false; } waitEvent.WaitOne(); return status; } #endregion Upload Personality #region Upload Personality Events static void model_OnFailed(object sender, EventArgs e) { Console.WriteLine("Failed event: Description=" + (sender as IUploadDownloadModel).Description); status = false; waitEvent.Set(); } static void model_OnCancel(object sender, EventArgs e) { Console.WriteLine("Cancel event: Description=" + (sender as IUploadDownloadModel).Description); status = false; 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!"); status = true; } else { if (errorObject == null) Console.WriteLine("Cannot create Personality Model"); else Console.WriteLine("Cannot create Personality Model. " + errorObject.ToString()); status = false; } 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 static void PerformRecording(IDriveCommunication communication) { recordingOperator = new RecordingOperator(communication); if (!recordingOperator.StartRecording()) return; recordingOperator.RecordingCompleteEvent += RecordingComplete; waitEvent.Reset(); waitEvent.WaitOne(); } static void RecordingComplete(object sender, EventArgs args) { recordingOperator.RecordingCompleteEvent -= RecordingComplete; waitEvent.Set(); } } }