/// <summary> /// Handles when data arrives at serial port /// </summary> /// <param name="transaction">Transaction that has been completed</param> private void Port_DataUpdated(Transaction transaction) { switch (transaction.Command) { case Commands.Averaging: { try { device.Averaging = (Averaging)(int.Parse(transaction.Response)); } catch (Exception) { Reset(); } break; } case Commands.Frequency: { try { device.FrequencyIndex = Array.IndexOf(Global.Frequencies, int.Parse(transaction.Response)); } catch (Exception) { Reset(); } break; } case Commands.Mode: { try { device.Mode = (Mode)(int.Parse(transaction.Response)); } catch (Exception) { Reset(); } break; } case Commands.Model: { try { device.Model = (Model)(int.Parse(transaction.Response)); } catch (Exception) { Reset(); } break; } case Commands.Speed: { try { device.Speed = (Speed)(int.Parse(transaction.Response)); } catch (Exception) { Reset(); } break; } case Commands.Trigger: { try { device.Trigger = (Trigger)(int.Parse(transaction.Response)); } catch (Exception) { Reset(); } break; } case Commands.Voltage: { try { device.Voltage = double.Parse(transaction.Response, CultureInfo.InvariantCulture); } catch (Exception) { Reset(); } break; } case Commands.Values: { try { string[] values = transaction.Response.Split(','); // HM8118 sends comma separated values XY parsedValues = new XY(); parsedValues.X = double.Parse(values[0], CultureInfo.InvariantCulture); // HM8118 sends decimal point parsedValues.Y = double.Parse(values[1], CultureInfo.InvariantCulture); device.Values = parsedValues; if (NewValues != null) // raise event { NewValues(device); } } catch (Exception) { Reset(); } break; } case Commands.CompensateOpen: case Commands.CompensateShort: { if (CompensationFinished != null) { CompensationFinished(transaction.Response.Contains("0")); // 0 indicates succesful compensation, -1 indicates failure } port.Flush(); break; } case Commands.Identify: break; case Commands.Reset: break; case Commands.Ready: break; case Commands.MeasureSingle: break; case Commands.SetCompensate: break; case Commands.Wait: break; case Commands.BiasMode: try { device.BiasMode = (BiasMode)(int.Parse(transaction.Response)); } catch (Exception) { Reset(); } break; case Commands.BiasVoltage: try { device.BiasVoltage = double.Parse(transaction.Response, CultureInfo.InvariantCulture); } catch (Exception) { Reset(); } break; case Commands.BiasCurrent: try { device.BiasCurrent = double.Parse(transaction.Response, CultureInfo.InvariantCulture); } catch (Exception) { Reset(); } break; case Commands.ConstantVoltage: try { device.ConstantVoltage = (ConstantVoltage)int.Parse(transaction.Response); } catch (Exception) { Reset(); } break; default: throw new ArgumentOutOfRangeException(); } }