Author Topic: Serial Ports not there?  (Read 13864 times)

GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
Serial Ports not there?
« on: June 14, 2012, 11:55:55 am »
I don't really know if this is the right place for this question or not, here goes.  I am writing an Application to talk to a board which is on going.  I am using an ini file to load up some serial port properties all good it's working on my PC at work, take it home works at home install it on the battered lab laptop it throws an issue (I think to with the open sort of the serial ports) as it has a complaints over an array value.  So my question is can this be due to having had quite a few USB comports over the years could it have a vestige of them left, as in control panel the only port is Com 1.  I would expect the following to work

            nameArray = SerialPort.GetPortNames();
            do
            {
                index += 1;
            }
            while (!((nameArray[index] == myComPortName) | (index == nameArray.GetUpperBound(0))));   
COM1 being the only eatery  it would have the nameArray upper bound set to 1 (at address 0) but no!

Glenn

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Serial Ports not there?
« Reply #1 on: June 14, 2012, 01:14:07 pm »
Before incrementing the index, you should check to see if the array has any elements.

Be sure to initialize index to -1 so the first value used is 0.

Detemining what array value causes the exception might help in figuring out what's wrong.

Jan


GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
Re: Serial Ports not there?
« Reply #2 on: June 15, 2012, 05:19:26 am »
Hi,
Thanks for the reply, this is getting odd, I have run several bits of code which use the same code without error (I tend to recycle code to prevent this from happening!).  Between the three PC's it has been run on this is the only one without Visual Studio 2008 installed, however the run time must be installed to allow other programs written with 2008 to work.

  index = 0;
            do
            {
                index += 1;
                //MessageBox.Show("index = " + index.ToString());
               
                cboPort.Items.Add(nameArray[index]);
             
            } while (!((nameArray[index] == myComPortName) || (index == nameArray.GetUpperBound(0))));

This is the code that works and doesn't work! Also I havwe just noticed when I run the test app on my PC it only see's Com6, the first port of a multiport Serial Card. Something odd is happening!
Glenn

GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
Re: Serial Ports not there?
« Reply #3 on: June 15, 2012, 08:43:23 am »
This is crazy I have gone back to basics and written a very basic app to list all the serial ports available six on my desktop, none on the laptop.  If open Com1: and send Hello World! it is recieved on it. It does not appear in the available list.  ??? The code I am using is below:
   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);
        }

GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
Re: Serial Ports not there?
« Reply #4 on: June 15, 2012, 09:33:16 am »
Hmmmm, It appears to work with another Windows7 ThinkPad, maybe the Laptop itself is on its way out!
Glenn
 

GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
Re: Serial Ports not there?
« Reply #5 on: June 18, 2012, 08:03:33 am »
Realized the problem! program was trying to open port with out checking it existed, stupid, stupid mistake!

Glenn