private static void ListComDetails()
        {
            using (ManagementClass i_Entity = new ManagementClass("Win32_PnPEntity"))
            {
                foreach (ManagementObject i_Inst in i_Entity.GetInstances())
                {
                    Object o_Guid = i_Inst.GetPropertyValue("ClassGuid");
                    if (o_Guid == null || o_Guid.ToString().ToUpper() != "{4D36E978-E325-11CE-BFC1-08002BE10318}")
                    {
                        continue;                         // Skip all devices except device class "PORTS"
                    }
                    String s_Caption  = i_Inst.GetPropertyValue("Caption").ToString();
                    String s_Manufact = i_Inst.GetPropertyValue("Manufacturer").ToString();
                    String s_DeviceID = i_Inst.GetPropertyValue("PnpDeviceID").ToString();
                    String s_RegPath  = "HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Enum\\" + s_DeviceID + "\\Device Parameters";
                    String s_PortName = Microsoft.Win32.Registry.GetValue(s_RegPath, "PortName", "").ToString();

                    int s32_Pos = s_Caption.IndexOf(" (COM");
                    if (s32_Pos > 0)                     // remove COM port from description
                    {
                        s_Caption = s_Caption.Substring(0, s32_Pos);
                    }

                    ConsoleOutput.Success("Port Name:    " + s_PortName);
                    ConsoleOutput.Success("Description:  " + s_Caption);
                    ConsoleOutput.Success("Manufacturer: " + s_Manufact);
                    ConsoleOutput.Success("Device ID:    " + s_DeviceID);
                    ConsoleOutput.Success("-----------------------------------");
                }
            }
        }
Example #2
0
        private async Task <CommandResponse> SendCommand(string command, ResponseType needsResponse)
        {
            if (await EnsurePortIsOpen())
            {
                _port.DiscardInBuffer();
                _port.DiscardOutBuffer();

                requestIndex++;
                try
                {
                    ConsoleOutput.Info(string.Format("[{0:0000}] SERIAL: [{1}] Sending command", requestIndex, command));
                    _port.Write(command);
                }
                catch (Exception ex)
                {
                    ConsoleOutput.Error(string.Format("[{0:0000}] SERIAL: [{1}] Failed to send command. {2}", requestIndex, command, ex.Message));
                    return(new CommandResponse(string.Empty, false, $"Unable to write to {_portName}. " + ex.Message));
                }

                try
                {
                    switch (needsResponse)
                    {
                    case ResponseType.NoResponse:
                    {
                        ConsoleOutput.Info(string.Format("[{0:0000}] SERIAL: [{1}] No response needed for command", requestIndex, command));
                        return(new CommandResponse(string.Empty, true));
                    }

                    case ResponseType.DigitResponse:
                    {
                        ConsoleOutput.Info(string.Format("[{0:0000}] SERIAL: [{1}] Expecting single digit response for command, waiting...", requestIndex, command));
                        string response = new string((char)_port.ReadChar(), 1);
                        ConsoleOutput.Success(string.Format("[{0:0000}] SERIAL: [{1}] Received single digit response '{2}'", requestIndex, command, response));
                        return(new CommandResponse(response, true));
                    }

                    case ResponseType.FullResponse:
                    {
                        ConsoleOutput.Info(string.Format("[{0:0000}] SERIAL: [{1}] Expecting #-delimited response for Command, waiting...", requestIndex, command));
                        string response = _port.ReadTo("#");
                        ConsoleOutput.Success(string.Format("[{0:0000}] SERIAL: [{1}] Received response '{2}'", requestIndex, command, response));
                        return(new CommandResponse(response, true));
                    }
                    }
                }
                catch (Exception ex)
                {
                    ConsoleOutput.Error(string.Format("[{0:0000}] SERIAL: [{1}] Failed to receive response to command. {2}", requestIndex, command, ex.Message));
                    return(new CommandResponse(string.Empty, false, $"Unable to read response to {command} from {_portName}. {ex.Message}"));
                }

                return(new CommandResponse(string.Empty, false, "Something weird going on..."));
            }
            else
            {
                ConsoleOutput.Error(string.Format("[{0:0000}] SERIAL: Failed to open port {1}", requestIndex, _portName));
                return(new CommandResponse(string.Empty, false, $"Unable to open {_portName}"));
            }
        }
Example #3
0
 public void Disconnect()
 {
     if (_port.IsOpen)
     {
         ConsoleOutput.Info("SERIAL: Port is open, sending shutdown command [:Qq#]");
         _port.Write(":Qq#");
         ConsoleOutput.Info("SERIAL: Closing port...");
         _port.Close();
         _port = null;
         ConsoleOutput.Success("SERIAL: Disconnected...");
     }
 }
Example #4
0
        private async Task <bool> EnsurePortIsOpen()
        {
            if (!_port.IsOpen)
            {
                try
                {
                    ConsoleOutput.Info(string.Format("SERIAL: Port {0} is not open, attempting to open...", _portName));
                    _port.Open();

                    ConsoleOutput.Info(string.Format("SERIAL: Checking if buffer contains data", _portName));
                    string preCheck = _port.ReadExisting();
                    if (!string.IsNullOrEmpty(preCheck))
                    {
                        ConsoleOutput.Error(string.Format("SERIAL: Possible problem, data already in buffer: {0}", preCheck));
                        ConsoleOutput.Warning("SERIAL: Flushing serial buffers...");
                        _port.DiscardInBuffer();
                        _port.DiscardOutBuffer();
                    }
                    else
                    {
                        ConsoleOutput.Success("SERIAL: No exising serial data in buffer, all good...");
                    }
                    await Task.Delay(750); // Arduino resets on connection. Give it time to start up.

                    ConsoleOutput.Info("SERIAL: Port is open, sending initial [:I#] command..");
                    _port.Write(":I#");

                    ConsoleOutput.Success("SERIAL: Connected!");
                    return(true);
                }
                catch (Exception ex)
                {
                    ConsoleOutput.Error(string.Format("SERIAL: Failed to open the port. {0}", ex.Message));
                    return(false);
                }
            }


            ConsoleOutput.Warning("SERIAL: Flushing serial buffers...");
            _port.DiscardInBuffer();
            _port.DiscardOutBuffer();
            Console.WriteLine("SERIAL: Port {0} is open, continuing...", _portName);
            return(true);
        }