Eivind

Eivind

  • 2.1k
  • 4
  • 5.5k

Unable to access "COM3"

Nov 14 2010 3:36 PM
Hi  (I hope this is the correct place for this post, it is my first.. )

My application:
I am making a program which includes two webcams, one laser and four servos. One cam + laser measures range to "target", second cam tracks the laserdot. I am using Pololu Mini Maestro 6-channel to control the servos via USB. The pololu software simulates a COM port. The port pops up as COM3 in my Control Panel.

My problem:
 If I run the app without debugging it sometimes run fine for some time, then the servos stop responding. If I use the debugger, this  response pops up:
"UnauthorizedAccesException was unhandled by user code"
"Unable to access COM3"

(My program writes it in Norwegian, so I translated it the best way I could)

The debugger points to my class for controlling the servos, which has this code:
using System;
using System.IO.Ports;

namespace LaserstyrtKanon
{
    class servoklasse
    {
        public void MiniSSC(string ComPort, int BaudRate, int ServoNummer, int ServoPos) //Mini-SSC protocol
        {
            const double MIN_POS = 0
                     , MAX_POS = 255;

            if (ServoPos < MIN_POS)
            {
                throw new ArgumentException("Servoposisjonen er mindre enn minimum tillatt!");
            }
            else if (ServoPos > MAX_POS)
            {
                throw new ArgumentException("Servoposisjonen er større enn maksimum tillatt.");
            }
            SerialPort port = new SerialPort(ComPort, BaudRate, Parity.None, 8, StopBits.One);

            port.Open();     //this is where the debugger points to when the problem occurs

            string ServoPosHex = ServoPos.ToString("X");
            byte PosisjonsByte = byte.Parse(ServoPosHex, System.Globalization.NumberStyles.HexNumber);
            string ServoNummerHex = ServoNummer.ToString("X");
            byte ServoNummerByte = byte.Parse(ServoNummerHex, System.Globalization.NumberStyles.HexNumber);
            port.Write(new byte[] { 0xFF, ServoNummerByte, PosisjonsByte }, 0, 3);
            port.Close();

        }//MiniSSC
    }//servoclass
}//namespace

I would really appreciate your help, since I am running out of time on this project.. :)
Regards from Norway

Answers (3)