Eivind

Eivind

  • 0
  • 4
  • 5.6k

Trouble accessing COM port (C# MFC)

Nov 15 2010 2:43 AM

*** Never mind, I figured it out! ***


Forgive me if what I write looks a little "off", I have been programming for over 12 hours straight.. Also this is my second post on the topic, but I misunderstood the meaning of COM in the other forum.. :P

Project: Windows Form App

Two webcams, one laser, four servos and a Pololu Maestro 6 channel USB device. One cam and the laser measures range, second webcam automatically tracks laserdot. Pololu software simulates two COM ports (used to send commands to the servos).

Problem:

"An exception of type 'System.UnauthorizedAccessException' occurred in System.dll but was not handled in user code" "Additional information: Ingen tilgang til porten COM3". ("Ingen tilgang til porten COM3" -translated: "Access denied to port COM3")

Without Debugging: The application runs, then freezes after some time, no error shown. (The time varies)

With Debugging: The application runs for a short time, then the above error pops up.  (Time also varies)

Here is the code for the servoclass:

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.Handshake = Handshake.None;
            port.Open();            <----this is where the Debugger points the error at ----           
            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
    }//servoklasse
}//namespace

Sam Hobbs has already given me a few tips, but I hope YOU can clarify this for me..I have searched many many forums without result.


*** Never mind, I figured it out! ***