private bool IsValidWriteSetting(string line, out BusinessLogic.DataParameter wrParam) { wrParam = new BusinessLogic.DataParameter(); if (ValidateWriteCallBack == null || ValidateWriteValueCallBack == null) { return(false); } var factors = line.Split(','); bool isSuccess = false; if (factors.Count() == 1) { if (ValidateWriteCallBack(factors[0], out wrParam)) { isSuccess = true; } } else if (factors.Count() == 2) { int index; if (int.TryParse(factors[0], out index)) { if (ValidateWriteValueCallBack(index, factors[1], out wrParam)) { isSuccess = true; } } } return(isSuccess); }
private bool ValidateDumpConfigrations(string text, out BusinessLogic.DataParameter param) { param = new BusinessLogic.DataParameter(); var factors = text.Split(','); if (factors.Count() != 2) { return(false); } var addressText = factors[0]; var sizeText = factors[1]; if (string.IsNullOrEmpty(addressText)) { return(false); } if (addressText.Length < 2) { return(false); } var header = addressText.Substring(0, 2); if (header != "0x") { return(false); } addressText = addressText.Remove(0, 2); int address = 0; if (!int.TryParse(addressText, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out address)) { return(false); } if (string.IsNullOrEmpty(sizeText)) { return(false); } int size = 0; if (!int.TryParse(sizeText, out size)) { return(false); } param.Address = (uint)address; param.Size = (uint)size; return(true); }
private bool UpdateConfigurationParameter(BindingList <DataSetting> settings, out List <BusinessLogic.DataParameter> parameters) { parameters = new List <BusinessLogic.DataParameter>(); bool isSuccess = true; foreach (var setting in settings) { if (setting.Check == true) { //Add data; var tmp = new BusinessLogic.DataParameter(); UInt64 address = 0; uint offset = 0; uint size = 0; try { address = Convert.ToUInt64(setting.Address, 16); if (!uint.TryParse(setting.Offset, out offset)) { offset = Convert.ToUInt32(setting.Offset, 16); } address += offset; if (address >= (UInt64)UInt32.MaxValue) { address = (UInt64)UInt32.MaxValue; } size = uint.Parse(setting.Size); } catch (Exception) { isSuccess = false; break; } tmp.Address = (uint)address; tmp.Size = size; parameters.Add(tmp); } } return(isSuccess); }
private void requestButton_Click(object sender, EventArgs e) { string addressText = addressTextBox.Text; string sizeText = sizeTextBox.Text; long address = 0; int size = 0; bool isSuccess = false; if (subViewCtrl.IsCommunicationActive == false) { return; } var empty = new List <byte>(); mainHexBox.ByteProvider = new DynamicByteProvider(empty); if (!string.IsNullOrEmpty(addressText)) { if (addressText.Length >= 2) { var header = addressText.Substring(0, 2); if (header == "0x") { addressText = addressText.Remove(0, 2); if (long.TryParse(addressText, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out address)) { if (tms320c28xEndianRadioButton.Checked) { address = address * 2; } hexboxAddress = address; isSuccess = true; } } } } if (!isSuccess) { return; } isSuccess = false; if (!string.IsNullOrEmpty(sizeText)) { if (int.TryParse(sizeText, out size)) { isSuccess = true; } } if (!isSuccess) { return; } var param = new BusinessLogic.DataParameter(); param.Address = (uint)address; param.Size = (uint)size; subViewCtrl.Logic.DumpConfigParameter = param; subViewCtrl.Logic.ClearWaitingTasks(); subViewCtrl.Logic.EnqueueTask(BusinessLogic.CommunicationTasks.Dump); subViewCtrl.Logic.EnqueueTask(BusinessLogic.CommunicationTasks.Logging); subViewCtrl.Logic.CancelCurrentTask(); }
private string AnalyzeCommand(string message) { string failedText = "Failed"; string okText = "OK"; string busyText = "Busy"; string emptyText = "Empty"; string command = string.Empty; string parameters = string.Empty; string answer = failedText; var splitIndex = message.IndexOf(' '); if (splitIndex != -1) { command = message.Substring(0, splitIndex); parameters = message.Substring(splitIndex + 1, (message.Length - command.Length - 1)); } else { command = message; } switch (command) { case CommandResource: var config = new Configuration(); if (ValidateCommunicationResource(parameters, out config)) { if (logic.UpdateResource(config)) { answer = okText; } } break; case CommandOpen: answer = busyText; if (requestTask != BusinessLogic.CommunicationTasks.Open) { requestTask = BusinessLogic.CommunicationTasks.Open; if (!logic.IsCommAvailable) { logic.ClearWaitingTasks(); logic.EnqueueTask(BusinessLogic.CommunicationTasks.Open); logic.CancelCurrentTask(); } } else { if (logic.TaskState != BusinessLogic.CommunicationTasks.Open) { requestTask = BusinessLogic.CommunicationTasks.Nothing; if (logic.IsCommAvailable) { answer = okText; } else { answer = failedText; } } } break; case CommandClose: if (requestTask == BusinessLogic.CommunicationTasks.Open) { requestTask = BusinessLogic.CommunicationTasks.Nothing; } logic.ClearWaitingTasks(); logic.EnqueueTask(BusinessLogic.CommunicationTasks.Close); logic.CancelCurrentTask(); answer = okText; break; case CommandInitialize: logic.EnqueueTask(BusinessLogic.CommunicationTasks.Initialize); DeviceVersion = string.Empty; answer = okText; break; case CommandGetVersion: if (string.IsNullOrEmpty(DeviceVersion)) { answer = busyText; } else { answer = okText + " " + DeviceVersion; } break; case CommandRegister: if (AddDataSetting(parameters)) { answer = okText; } break; case CommandAdapt: RegisterLogConfigCallBack?.Invoke(viewSettingBuffer); viewSettingBuffer = new ViewSetting(); answer = okText; break; case CommandPage: if (logic.TaskState == BusinessLogic.CommunicationTasks.Logging) { answer = busyText; } else { int pageValue; if (!int.TryParse(parameters, out pageValue)) { answer = failedText; } else { if (ChangePageCallBack != null) { if (!ChangePageCallBack(pageValue)) { answer = failedText; } else { ClearLogTextDataBuff(); answer = okText; } } } } break; case CommandTimeStep: int timeStep; if (int.TryParse(parameters, out timeStep)) { if (timeStep < BusinessLogic.TimeStepMin) { timeStep = BusinessLogic.TimeStepMin; } else if (timeStep > BusinessLogic.TimeStepMax) { timeStep = BusinessLogic.TimeStepMax; } ChangeTimeStepCallBack?.Invoke(timeStep); logic.LogTimeStep = (uint)timeStep; answer = okText; } break; case CommandLogStart: logic.EnqueueTask(BusinessLogic.CommunicationTasks.Config); logic.EnqueueTask(BusinessLogic.CommunicationTasks.Logging); ClearLogTextDataBuff(); answer = okText; break; case CommandLogStop: if (logic.TaskState == BusinessLogic.CommunicationTasks.Logging) { logic.ClearWaitingTasks(); logic.CancelCurrentTask(); } answer = okText; break; case CommandLogGet: string data; if (!logTextDataBuff.TryDequeue(out data)) { if (logic.TaskState == BusinessLogic.CommunicationTasks.Logging) { answer = busyText; } else { answer = emptyText; } } else { answer = okText + " " + data; } break; case CommandLogWrite: if (logic.TaskState == BusinessLogic.CommunicationTasks.Logging) { var wrParam = new BusinessLogic.DataParameter(); if (IsValidWriteSetting(parameters, out wrParam)) { logic.EditValue(wrParam); answer = okText; } } break; case CommandLogSize: if (logic.TaskState == BusinessLogic.CommunicationTasks.Logging) { answer = busyText; } else { int size; if (int.TryParse(parameters, out size)) { if (size > 0) { ClearLogTextDataBuff(); logDataBuffSize = size; answer = okText; } } } break; case CommandDumpSet: if (requestTask == BusinessLogic.CommunicationTasks.Dump) { answer = busyText; } else { var param = new BusinessLogic.DataParameter(); if (ValidateDumpConfigrations(parameters, out param)) { requestTask = BusinessLogic.CommunicationTasks.Dump; logic.DumpConfigParameter = param; logic.ClearWaitingTasks(); logic.EnqueueTask(BusinessLogic.CommunicationTasks.Dump); logic.CancelCurrentTask(); answer = okText; } } break; case CommandDumpGet: if (logic.TaskState == BusinessLogic.CommunicationTasks.Dump) { answer = busyText; } else { requestTask = BusinessLogic.CommunicationTasks.Nothing; if (DumpDataBuff.Count <= 0) { answer = emptyText; } else { answer = okText + " "; int count = 0; while (count < 16) { byte abyte; if (DumpDataBuff.TryDequeue(out abyte)) { answer += abyte.ToString("X2"); count++; } else { break; } } } } break; case CommandBypassRequest: if (requestTask == BusinessLogic.CommunicationTasks.Bypass) { answer = busyText; } else { if (System.Text.RegularExpressions.Regex.IsMatch(parameters, @"\A\b[0-9a-fA-F]+\b\Z") && (parameters.Length % 2) == 0) { requestTask = BusinessLogic.CommunicationTasks.Bypass; logic.BypassRequest.Enqueue(parameters); logic.ClearWaitingTasks(); logic.EnqueueTask(BusinessLogic.CommunicationTasks.Bypass); logic.CancelCurrentTask(); answer = okText; } } break; case CommandBypassResponse: if (logic.TaskState == BusinessLogic.CommunicationTasks.Bypass) { answer = busyText; } else { requestTask = BusinessLogic.CommunicationTasks.Nothing; string outputText; if (logic.BypassResponse.TryDequeue(out outputText)) { answer = okText + " " + outputText; } else { answer = emptyText; } } break; default: break; } return(answer); }