public DeviceBus(byte comport, int baud) { Comport = comport; _Commport = new CommPort(comport, baud); }
private async Task ExecuteCommand(string command) { await Task.Run(() => CommPort.Send(command)); }
protected void Read(byte[] buffer, int offset, int count) { ExecuteCommOperation("Read", 1, delegate() { CommPort.Read(buffer, offset, count); }); // _commPort.Read(buffer, offset, count); }
protected override void OnStartup(StartupEventArgs e) { if (e.Args.Length != 1) { MessageBox.Show("usage: drillironc [port_name]", "Error", MessageBoxButton.OK, MessageBoxImage.Error); Shutdown(1); return; } string answer; try { p = new CommPort(e.Args[0], string.Empty); p.ReadTimeout = 300; p.WriteTimeout = 300; p.Open(); p.Write("I"); Thread.Sleep(100); answer = p.ReadExisting(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Comm port opening error", MessageBoxButton.OK, MessageBoxImage.Error); Shutdown(2); return; } if (answer != "DRILLIRONC1.0") { MessageBox.Show("Wrong device answer to identify command: " + (answer == null ? string.Empty : answer), "Error", MessageBoxButton.OK, MessageBoxImage.Error); Shutdown(3); return; } drill_title = ConfigurationManager.AppSettings["drill_title"]; program_title = ConfigurationManager.AppSettings["program_title"]; heaterProfiles = new List <HeaterProfile> [3]; for (int i = 0; i < 3; i++) { heaterProfiles[i] = new List <HeaterProfile>(); } for (int i = 1; ; i++) { double multipler, adder; int heaterID; string prefix = "profile" + i; string parameter = prefix + "_title"; string title = ConfigurationManager.AppSettings[parameter]; if (title == null) { break; } parameter = prefix + "_heaterID"; string value = ConfigurationManager.AppSettings[parameter]; if (value == null || !int.TryParse(value, out heaterID) || heaterID < 1 || heaterID > 3) { MessageBox.Show("Wrong " + parameter + " parameter: " + (value == null ? string.Empty : value), "Error", MessageBoxButton.OK, MessageBoxImage.Error); Shutdown(4); return; } parameter = prefix + "_adder"; value = ConfigurationManager.AppSettings[parameter]; if (value == null || !double.TryParse(value, out adder)) { MessageBox.Show("Wrong " + parameter + " parameter: " + (value == null ? string.Empty : value), "Error", MessageBoxButton.OK, MessageBoxImage.Error); Shutdown(4); return; } parameter = prefix + "_multipler"; value = ConfigurationManager.AppSettings[parameter]; if (value == null || !double.TryParse(value, out multipler) || multipler == 0) { MessageBox.Show("Wrong " + parameter + " parameter: " + (value == null ? string.Empty : value), "Error", MessageBoxButton.OK, MessageBoxImage.Error); Shutdown(4); return; } parameter = prefix + "_dual_temp"; value = ConfigurationManager.AppSettings[parameter]; bool dual_temp_enabled = value != null && value.Equals("true", StringComparison.OrdinalIgnoreCase); heaterProfiles[heaterID - 1].Add(new HeaterProfile(title, i, heaterID, multipler, adder, dual_temp_enabled)); } nextCommands = new Queue <DeviceCommand>(); settings = new Dictionary <string, string>(); if (File.Exists(settingsFile)) { foreach (string line in File.ReadAllLines(settingsFile)) { string[] parts = line.Split(' '); if (parts.Length == 2) { settings.Add(parts[0], parts[1]); } } } base.OnStartup(e); }
public RS232() { try { InitializeComponent(); CommPort com = CommPort.Instance; int found = 0; string[] portList = com.GetAvailablePorts(); for (int i = 0; i < portList.Length; ++i) { string name = portList[i]; comboBoxPortName.Items.Add(name); if (name == Settings.Port.PortName) { found = i; } } if (portList.Length > 0) { comboBoxPortName.SelectedIndex = found; } Int32[] baudRates = { 100, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 56000, 57600, 115200, 128000, 256000, 0 }; found = 0; for (int i = 0; baudRates[i] != 0; ++i) { comboBoxBaudRate.Items.Add(baudRates[i].ToString()); if (baudRates[i] == Settings.Port.BaudRate) { found = i; } } comboBoxBaudRate.SelectedIndex = found; comboBoxDataBits.Items.Add("5"); comboBoxDataBits.Items.Add("6"); comboBoxDataBits.Items.Add("7"); comboBoxDataBits.Items.Add("8"); comboBoxDataBits.SelectedIndex = Settings.Port.DataBits - 5; foreach (string s in Enum.GetNames(typeof(Parity))) { comboBoxParity.Items.Add(s); } comboBoxParity.SelectedIndex = (int)Settings.Port.Parity; foreach (string s in Enum.GetNames(typeof(StopBits))) { comboBoxStopBits.Items.Add(s); } comboBoxStopBits.SelectedIndex = (int)Settings.Port.StopBits; foreach (string s in Enum.GetNames(typeof(Handshake))) { comboBoxHandshake.Items.Add(s); } comboBoxHandshake.SelectedIndex = (int)Settings.Port.Handshake; com.StatusChanged += OnStatusChanged; //com.DataReceived += OnDataReceived; com.Open(); } catch (Exception ex) { LogClass.WriteLogFile("Exception:" + ex.ToString()); } }