C#/XNA - Access to the port 'COM2' is denied.

I´am trying to read the Serial output of the arduino and make it control objects in my game. the problem is that for every port i try i get the Access to the port 'COM2' is denied. I have checked to se if some other application uses the Serial but i can´t find anything.

Here is my Code to get serial data into the game:

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO.Ports;

namespace Xna_serial
    class Serial_reader
        SerialPort _serialPort;
        public string pos = "0";
        public int y = 0;
        public Serial_reader()
            //To settup serial connection
            _serialPort = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);
            _serialPort.PortName = "COM2";
            _serialPort.Handshake = Handshake.None;
            _serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);

            _serialPort.WriteTimeout = 500;


        // Get serial data and convert to INT 32
        void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)

            y = Convert.ToInt32(_serialPort.ReadLine());

And here is the code running on my arduino(to get input from a potensiometer)

const int ReadPin = 5;  

int input = 0;        
int output = 0;        

void setup() {

void loop() {
  input = analogRead(ReadPin);            
  output = map(input, 0, 1023, 0, 600);

All answers are appreciated.

What else do you have running when this program is running? Specifically, is the Arduino Serial Monitor open?

the problem is that for every port i try i get the Access to the port 'COM2' is denied.

This does not make sense. You should not get an error about access to COM2 unless you are trying to access COM2.

Are you able to access the port (any port) to upload a sketch to the Arduino?

The Serial monitor is´nt open and i can upload my sketch to my arduino.
When i open the Serial monitor i can se the output from the arduino.
Also when i try a Serial port that i have nothing attached to i get the same problem.

Ps: I ment that for every serial port i use i get the same error

What is the purpose of this:


... since the docs for Close say this:

The best practice for any application is to wait for some amount of time after calling the Close method before attempting to call the Open method, as the port may not be closed instantly.

That is not the problem. I get the same error when using Firmat.net......