cant find connected serial devices through usb using c# - c#

ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_SerialPort");
foreach (ManagementObject queryObj in searcher.Get())
{
String mod = queryObj["Name"].ToString();
MessageBox.Show("Modem: " + mod + "\n");
if (queryObj["Name"].ToString().Contains(Modem)) // i.e "ST-Ericsson ... "
{
com = System.Convert.ToString(queryObj["DeviceID"]);
break;
}
I have two modems connected. Both the devices are listed in Device manager. But only ST-Ericsson modem is found. Qualcomm modem, eventhough, it is listed in Device manager is not returning its name and device id which I need in order to setup the communication. Any one has any idea why ?

Related

UserName from 'Win32_ComputerSystem' of windows machine is always null in remote machine

I am using the method below to get logged in user name in windows(it's being used in windows service). It works well(MY-Username(Pc-Name)) on my machine but when I install it to some other windows machine it returns only Name((Name)) value since UserName is null, whats might cause this?
Unfortunately I am not able to install VS in that remote machine, hope still there is way to fix it
private string GetUserName()
{
string result = "";
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT UserName, Name FROM Win32_ComputerSystem"))
{
foreach (ManagementObject mo in searcher.Get())
{
if (mo["UserName"] != null)
result = mo["UserName"].ToString();
if (mo["Name"] != null)
result += " (" + mo["Name"].ToString() + ")";
}
}
return result;
}

Determining the COM Port of a (USB enabled) Customer Display

I have connected customer Display with POS application. I am using serial port class for display message on customer Display. To message I need to know with com port this USB display are using. I have searched allowed there are many examples for getting all com pot for that computer but I am able to find any help to get particular com port no e.g COM93,COM01 or COM2.
I have tried following program and also I have tried Microsoft program WMI code creator.
// Get a list of serial port names.
string[] ports = SerialPort.GetPortNames();
Console.WriteLine("The following serial ports were found:");
// Display each port name to the console.
foreach (string port in ports)
{
Console.WriteLine(port);
}
Console.ReadLine();
SerialPort sp = new SerialPort();
sp.PortName = "COM93";------------- How i can find this no automatic
sp.BaudRate = 9600;
sp.Parity = Parity.None;
sp.DataBits = 8;
sp.StopBits = StopBits.One;
sp.Open();
sp.Write("\f");
sp.WriteLine("***Velkommen***");
sp.Close();
sp.Dispose();
sp = null;
I have tried following solution as well, to use this as well for this I get access denied so I need to edit registory to give rights that I dot wana do I want solution programmatically
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSSerial_PortName");
public string Get_RegistryInfo(string VID, string PID)
{
try
{
RegistryKey rk1 = Registry.LocalMachine;
// HKEY_LOCAL_MACHINE
RegistryKey rk2 = rk1.OpenSubKey("HARDWARE\\\\DEVICEMAP\\\\SERIALCOMM");
// HKEY_LOCAL_MACHINE\HARDWARE\\\\DEVICEMAP\\\\SERIALCOMM
VendorID = VID;
ProduktID = PID;
string pattern = string.Format("^VID_{0}.PID_{1}", VID, PID);
Regex _rx = new Regex(pattern, RegexOptions.IgnoreCase);
string rk2_SubKeyNames = null;
foreach (string rk2_SubKeyNames_loopVariable in rk2.GetValueNames())
{
rk2_SubKeyNames = rk2_SubKeyNames_loopVariable;
if (rk2_SubKeyNames == "\\Device\\ProlificSerial0")
{
COM_Port = rk2.GetValue(rk2_SubKeyNames).ToString();
}
}
return COM_Port;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return COM_Port;
}
}

Listing of detailed COM port names using VisualStudio 2013 C#

I want to list the active COM ports with detailed names (like in Windows Device Manager).
I'm using this code and it works, but it is very slow. It can take up to 45 seconds to get a list of 5 COM ports!
Am I doing anything wrong here or is there a faster way to do this?
I know there are several postings about this, but I haven't found the right answer yet.
private void UpdateSerialPorts(RichTextBox _txtBox)
{
foreach (string portName in System.IO.Ports.SerialPort.GetPortNames())
{
string query = String.Format("SELECT * FROM Win32_PnPEntity WHERE Caption LIKE '%{0}%'", portName);
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", query);
ManagementObjectCollection coll = searcher.Get();
if (coll.Count > 0)
{
foreach (ManagementBaseObject collObj in coll)
{
ManagementBaseObject obj = collObj;
_txtBox.AppendText(portName + " " + obj.GetPropertyValue("Caption").ToString() + "\r\n");
}
}
else
{
_txtBox.AppendText(new SerialPort(portName).ToString() + "\r\n");
}
}
}

To set default printer connected in a network using its IP address [duplicate]

I would like to determine the IP address of a printer, using C# (.NET 2.0). I have only the printer share name as set up on the Windows OS, in the format \\PC Name\Printer Name. The printer is a network printer, and has a different IP address to the PC. Does anyone have any pointers?
Thanks in advance for your help.
Regards, Andy.
Just adding an another solution here using .Net Framework 4.0 or higher
Using System.Printing
var server = new PrintServer();
var queues = server.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections });
foreach (var queue in queues)
{
string printerName = queue.Name;
string printerPort = queue.QueuePort.Name;
}
Check this question: How to get Printer Info in C#.NET?. I think that you have to get the property PortName from the WMI properties.
I know this is an old post, but I had the same issue where I was able to get the Printer Port name, but not the IP. In my case I couldn't rely on the Port Name being IP_[IP Address] but found how to get hold of the actual IP from the port name.
Windows stores the information about ports in the registry under
HKLM\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\[port name]
This key contains the values set up in the port configuration page, including IP address and port number.
A quick C# example to get the IP address
using Microsoft.Win32;
RegistryKey key = Registry.LocalMachine.OpenSubKey(#"System\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\" + printerPortName, RegistryKeyPermissionCheck.Default, System.Security.AccessControl.RegistryRights.QueryValues);
if (key != null)
{
String IP = (String)key.GetValue("IPAddress", String.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames);
}
Using WIN32_Printer class is not enough here. It should be combined with Win32_TCPIPPrinterPort class.
Below is the code which should help:
static void Main(string[] args)
{
var scope = new ManagementScope(#"\root\cimv2");
scope.Connect();
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
var results = searcher.Get();
Console.WriteLine("Network printers list:");
foreach (var printer in results)
{
var portName = printer.Properties["PortName"].Value;
var searcher2 = new ManagementObjectSearcher("SELECT * FROM Win32_TCPIPPrinterPort where Name LIKE '" + portName + "'");
var results2 = searcher2.Get();
foreach (var printer2 in results2)
{
Console.WriteLine("Name:" + printer.Properties["Name"].Value);
//Console.WriteLine("PortName:" + portName);
Console.WriteLine("PortNumber:" + printer2.Properties["PortNumber"].Value);
Console.WriteLine("HostAddress:" + printer2.Properties["HostAddress"].Value);
}
Console.WriteLine();
}
Console.ReadLine();
}
string printerName = "POS-80C";
LocalPrintServer server = new LocalPrintServer();
PrintQueue printQueue = server.GetPrintQueue(printerName);
string portName = printQueue.QueuePort.Name;
string portNumber = "";
string hostAddress = "";
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_TCPIPPrinterPort where Name LIKE '" + portName + "'");
var results = searcher.Get();
foreach (var printer in results)
{
portNumber = (printer.Properties["PortNumber"].Value).ToString();
hostAddress = (printer.Properties["HostAddress"].Value).ToString();
}
Is this printer set up in a network which has Active Directory?
Or is this on your own local network with just a switch and your printer plugged into it?
If it is the former, then you should be able to query for it based on the "printer name". This article show how to get c# .net to connect to the AD. But this does require some knowledge of AD servers in your network.
This solution seems a bit long to me, but may be a good starting point?
Based on the link How to get Printer Info in .NET? (Thanks, Panos, I was already looking at the link!), I have the following solution from Panos's answer:
using System.Management;
...
string printerName = "YourPrinterName";
string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}'", printerName);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection coll = searcher.Get();
foreach (ManagementObject printer in coll)
{
string portName = printer["PortName"].ToString();
if(portName.StartsWith("IP_"))
{
Console.WriteLine(string.Format("Printer IP Address: {0}", portName.Substring(3)));
}
}
Obviously, this only works if the port name for the printer is given in the format "IP_IPAddress", which is I believe is the default.

Loop on serial ports

I'm using c# FW 4.0.
I want to iterate on all the serial ports in my computer and get the full name of each one of them.
For example, I would like to see "Prolific USB-to-Serial Comm Port(COM6)" and not just COM6.
This is my current code which gives me only the COM/1/6 etc...
string[] ports = SerialPort.GetPortNames();
foreach (string port1 in ports)
{
MessageBox.Show(port1);
}
You could make use of WMI, take a look at the WMI Reference
Answer by Juanma, Here.
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\WMI",
"SELECT * FROM MSSerial_PortName");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("MSSerial_PortName instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("InstanceName: {0}", queryObj["InstanceName"]);
Console.WriteLine("-----------------------------------");
Console.WriteLine("MSSerial_PortName instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("PortName: {0}", queryObj["PortName"]);
//If the serial port's instance name contains USB
//it must be a USB to serial device
if (queryObj["InstanceName"].ToString().Contains("USB"))
{
Console.WriteLine(queryObj["PortName"] + "
is a USB to SERIAL adapter/converter");
}
}
}
catch (ManagementException e)
{
MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
}

Categories

Resources