如何知道连接了哪个串口外部设备并从该设备读取数据?
How to know on which serial port external device is connected and read data from that device?
我连接了 1 个生成随机数的设备,我想通过我的 Windows 应用程序.
读取该数字
我正在使用此代码:
public static void Main()
{
SerialPort mySerialPort = new SerialPort("COM19");
mySerialPort.BaudRate = 115200;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
mySerialPort.Open();
Console.WriteLine("Press any key to continue...");
Console.WriteLine();
Console.ReadKey();
mySerialPort.Close();
}
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Debug.Print("Data Received:");
Debug.Print(indata);
}
使用这段代码,我成功获取了数据。
但这里的问题是我硬编码了来自设备管理器的 com port(Com19)
我已经检查了我的设备连接到哪个端口,所以我硬编码了它,但我不想这样做。
相反,我会给用户 1 dropdown
,用户将只能看到用户设备连接到的端口。因此,当从这个连接的设备获取数据时,我将使用该用户选择的下拉端口。
我是 windows 应用程序的新手,从未做过任何与串口相关的事情。
当我将 arduino 连接到 com 端口时。我已经使用了这段代码(假设 arduino 返回的文本里面有 "Info from Arduino"):
SerialPort currentPort; // global variables
bool ArduinoPortFound = false;
...
try
{
string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
{
currentPort = new SerialPort(port, 9600);
if (ArduinoDetected())
{
ArduinoPortFound = true;
break;
}
else
{
ArduinoPortFound = false;
}
}
}
catch { }
......
private bool ArduinoDetected()
{
try
{
currentPort.Open();
System.Threading.Thread.Sleep(1000);
string returnMessage = currentPort.ReadLine();
currentPort.Close();
if (returnMessage.Contains("Info from Arduino"))
{
return true;
}
else
{
return false;
}
}
catch (Exception e)
{
return false;
}
}
更新:
或者你也可以使用 Windows Management Instrumentation (WMI)
这是关于它的文章:
How to programmatically find a COM port by friendly name
ManagementObjectCollection mReturn;
ManagementObjectSearcher mSearch;
mSearch = new ManagementObjectSearcher("Select * from Win32_SerialPort");
mReturn = mSearch.Get();
Combobox cboPort = new Combobox();
foreach (ManagementObject mObj in mReturn)
{
cboPort.Items.Add(mObj["Name"].ToString());
}
修改你的代码如下
SerialPort mySerialPort = new SerialPort(cboPort.Text);
希望对您有所帮助,根据您的要求进行修改。
我连接了 1 个生成随机数的设备,我想通过我的 Windows 应用程序.
读取该数字我正在使用此代码:
public static void Main()
{
SerialPort mySerialPort = new SerialPort("COM19");
mySerialPort.BaudRate = 115200;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
mySerialPort.Open();
Console.WriteLine("Press any key to continue...");
Console.WriteLine();
Console.ReadKey();
mySerialPort.Close();
}
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Debug.Print("Data Received:");
Debug.Print(indata);
}
使用这段代码,我成功获取了数据。
但这里的问题是我硬编码了来自设备管理器的 com port(Com19)
我已经检查了我的设备连接到哪个端口,所以我硬编码了它,但我不想这样做。
相反,我会给用户 1 dropdown
,用户将只能看到用户设备连接到的端口。因此,当从这个连接的设备获取数据时,我将使用该用户选择的下拉端口。
我是 windows 应用程序的新手,从未做过任何与串口相关的事情。
当我将 arduino 连接到 com 端口时。我已经使用了这段代码(假设 arduino 返回的文本里面有 "Info from Arduino"):
SerialPort currentPort; // global variables
bool ArduinoPortFound = false;
...
try
{
string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
{
currentPort = new SerialPort(port, 9600);
if (ArduinoDetected())
{
ArduinoPortFound = true;
break;
}
else
{
ArduinoPortFound = false;
}
}
}
catch { }
......
private bool ArduinoDetected()
{
try
{
currentPort.Open();
System.Threading.Thread.Sleep(1000);
string returnMessage = currentPort.ReadLine();
currentPort.Close();
if (returnMessage.Contains("Info from Arduino"))
{
return true;
}
else
{
return false;
}
}
catch (Exception e)
{
return false;
}
}
更新: 或者你也可以使用 Windows Management Instrumentation (WMI)
这是关于它的文章: How to programmatically find a COM port by friendly name
ManagementObjectCollection mReturn;
ManagementObjectSearcher mSearch;
mSearch = new ManagementObjectSearcher("Select * from Win32_SerialPort");
mReturn = mSearch.Get();
Combobox cboPort = new Combobox();
foreach (ManagementObject mObj in mReturn)
{
cboPort.Items.Add(mObj["Name"].ToString());
}
修改你的代码如下
SerialPort mySerialPort = new SerialPort(cboPort.Text);
希望对您有所帮助,根据您的要求进行修改。