不存在的串行端口... [英] Serial Ports that don't exists...

查看:235
本文介绍了不存在的串行端口...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用串行端口连接到内部设计的板上.在尝试使用用户可设置的参数使其尽可能灵活时,它没有预定的串行端口(例如Com1),因此我使用以下代码

Hi All,

I am using the serial port to connect to a board designed in house. In trying to get it as flexible as possible using user settable parameters it can''t have a predetermined serial port (such as Com1) so I am using the below code

private void button4_Click(object sender, EventArgs e)
     {
         string[] ArrayPortNames = null;
         string myComPortName = null;
         int index = -1;

         ArrayPortNames = SerialPort.GetPortNames();
         do
         {
             index += 1;
         } while (!((ArrayPortNames[index] == myComPortName) | (index == ArrayPortNames.GetUpperBound(0))));
         if (index == ArrayPortNames.GetUpperBound(0))
             myComPortName = ArrayPortNames[0];

         for (int i = 0; i < index; i++)
             cboPort.Items.Add(ArrayPortNames[i]);
     }

,但是根据设备管理器,在我使用的笔记本电脑上找不到任何串行端口,但使用USB到9路转换器的硬件是一个Com1(它是一台旧笔记本电脑)我没有再执行任何操作,而是再次运行button4,它现在在找到Com1(而不是Com5)后立即找到,因此无法再次看到Com1.即使我尝试使用Com1时也可以看到它,但它不会让我打开它.我已经在Windows 7 Thinkpad上进行了尝试,并且看来工作正常是PC的串行端口即将退出?

but can''t find any serial ports on the laptop I am using according to device manager there is one Com1 the hardware (it''s an old laptop) using a USB to 9 Way converter, which it finds with no action on my part run button4 again it now finds Com1 (and not Com5) as soon as it is removed it can''t see Com1 again. Even if I try to use Com1 when it can see it it wont let me open it. I have tried on Windows 7 Thinkpad and it appears to work is the opionon that the PC''s serial ports are on the way out ?

推荐答案

我可以"不能弄清楚该代码片段的意图,但看起来并不正确.您能解释一下do while循环和变量myComPortName的用法吗?

我本来会用的(假设cboPort是一个组合框)
I can''t figure out the intention of that code fragment but it doesn''t look right. Can you explain the use of the do while loop and the variable myComPortName?

I would have just used this (assuming cboPort is a combobox)
private void button4_Click(object sender, EventArgs e)
       {
           cboPort.DataSource = SerialPort.GetPortNames();
       }




艾伦.




Alan.


这篇关于不存在的串行端口...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆