问题描述
大家好,
我试图问这个问题,但是在Caffine推动编程马拉松的结尾时,可能没有意义,在客户中肆虐等.我遇到的问题是我需要使用类似
的方法
Hi All,
I tried to ask the question but was at the end of a Caffine fueled programming marathon, possibly not making sense, raging at the customer etc. The issue I have is I need to use a method like
cboPorts.DataSource = SerialPort.GetPortNames;
但是,如果在没有Com端口的PC上打开应用程序会出现错误,则很难找到此错误,因为几乎每台PC都有一个串行端口,并且只能通过只有USB com端口和PC的PC来隔离它.这些在运行之前已被删除,看来我需要做任何其他事情之前检查所有串行端口.
找到串行端口后,我正在执行以下操作
How ever the app errors if it is opened on a PC with no Com Ports, it was hard to find this error as nearly every PC has a Serial Port and it was only possible to isolate it with a PC that only had USB com ports and these were removed before run it seems I need to check for any Serial Ports before I do any thing else.
I was doing the following after I had found the serial Ports
do
{
index += 1;
cboPort.Items.Add(nameArray[index]);
}
while (!((nameArray[index]==myComPortName)||(index == nameArray.GetUpperBound(0))));
我刚刚移动了它,因此这是我现在要测试的Form Load中的第一件事!
这是正确的方法吗?
Glenn
I have have just moved this so it is the first thing in the Form Load I am now going to test it!
Is this the right way is there a right way?
Glenn
推荐答案
string[] portNames = SerialPort.GetPortNames();
if(portNames.Length == 0)
// No ports found.
else
// Do your binding.
这篇关于串口问题更有意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!