public override void Write([Implicit] StepBro.Core.Execution.ICallContext context, string text) { if (m_port.IsOpen) { if (context != null && context.LoggingEnabled) { var s = text.Trim(' ', '\r', '\n', '\t').EscapeString(); if (s.Length > 120) { s = "Write \"" + s.Substring(0, 120) + "\"..."; } else { s = "Write \"" + s + "\""; } context.Logger.Log("SerialPort", s); } m_port.Write(text); } else { if (context != null) { context.ReportError("Write, but port is not open."); } } }
protected override bool DoOpen(StepBro.Core.Execution.ICallContext context) { try { m_port.Open(); } catch (Exception ex) { if (s_OpenPortFailureExplorer != null) { s_OpenPortFailureExplorer(context, ex); } //if (context != null && context.LoggingEnabled) //{ // using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\cimv2", // "SELECT * FROM Win32_PnPEntity WHERE ClassGuid=\"{4d36e978-e325-11ce-bfc1-08002be10318}\"")) // { // // Add all available (COM)-ports to the combobox // foreach (ManagementObject queryObj in searcher.Get()) // { // context.Logger.Log("SerialPort.Open", "Available port: " + (queryObj["Caption"] as string)); // } // } //} throw; } try { m_port.DiscardInBuffer(); } catch (Exception ex) { context.ReportError("Error discarding in-buffer.", exception: ex); } return(m_port.IsOpen); }