I am trying to communicate from visual studio with my arduino, but every time I run my cod I get this error.
An unhandled exception of type 'System.UnauthorizedAccessException' occurred in System.dll
Additional information: Access to the port 'COM3' is denied.
// ArduinoComm.cpp : main project file.
#include "stdafx.h"
using namespace System;
using namespace System::IO::Ports;
int main(array<System::String ^> ^args)
{
String^ answer;
String^ portName;
int baudRate=9600;
Console::WriteLine("Type in a port name and hit ENTER");
portName=Console::ReadLine();
// arduino settings
SerialPort^ arduino;
arduino = gcnew SerialPort(portName, baudRate);
// open port
try
{
arduino->Open();
do
{
// ask on or off
Console::WriteLine("Type \"on\" to turn the light on or \"off\" to turn it off");
// get answer
answer=Console::ReadLine();
//check that user typed one of the options
if(String::Compare(answer,"on")==0)
arduino->WriteLine("1"); // send 1 to arduino
else if(String::Compare(answer,"off")==0)
arduino->WriteLine("0"); // send 0 to arduino
else
Console::WriteLine(answer+" was not an option");
// ask user if he wants to continue
Console::WriteLine("Try again? yes/no");
// get answer
answer=Console::ReadLine();
// clear the screen
Console::Clear();
}while(String::Compare(answer,"yes")==0);
// close port to arduino
arduino->Close();
}
catch (IO::IOException^ e )
{
Console::WriteLine(e->GetType()->Name+": Port is not ready");
}
catch (ArgumentException^ e)
{
Console::WriteLine(e->GetType()->Name+": incorrect port name syntax, must start with COM/com");
}
// end program
Console::Write("Press enter to close the program");
Console::Read();
return 0;
}
I used this code from a website: Arduino Serial Communication With C++ Tutorial
It seems other people got it to work, so I'm not quite sure why I'm getting this error.
Here is my arduino code:
//Some content from SFE_TSL2561 example from SparkFun Electronics
#include <SFE_TSL2561.h>
#include <Wire.h>
//Creat an SFE_TSL2561 object
SFE_TSL2561 light;
//Global variables:
boolean gain; //Gain setting, 0=X1, 1=X16;
unsigned int ms; //Integration ("shutter") time in ms
void setup()
{
//Initialize the Serial port:
Serial.begin(9600);
//Initialize the SFE_TSL2561 library
light.begin();
//If gain=false(0), device is set to low gain(1X)
//If gain=high (1), device is set to high gain (16X)
gain = 0;
//If time = 0, integration will be 13.7ms
//If time = 1, integration will be 101ms
//If time = 2, integration will be 402ms
//If time = 3, use maunal start / stop to perform own integration
unsigned char time = 2;
//SetTiming() will set the third parameter (ms) to the requested
//integration time in ms
Serial.println("Set timing...");
light.setTiming(gain,time,ms);
//Power the sensor (To start taking measurements)
Serial.println("Powerup...");
light.setPowerUp();
}
void loop(){
char measurement=0;
unsigned int data0, data1;
//read Serial port, when camera has triggered
if (Serial.available()=='A')
{
if (light.getData(data0,data1))
{
//getData() return true, communication was successful
Serial.print("data0: ");
Serial.print(data0);
Serial.print(" data1: ");
Serial.print(data1);
//To calculate lux, pass all your settings and readings to the getLux() function
double lux; //resulting lux value
boolean good; //true if neither sensor is saturated
//Perform lux calculation
good=light.getLux(gain,ms, data0,data1,lux);
//Print results
Serial.print(" lux: ");
Serial.print(lux);
if(good) Serial.println(" (good)"); else Serial.println(" (BAD)");
}
else
{
//getData() returned false because of an I2C error
byte error = light.getError();
printError(error);
}
}
}
void printError (byte error)
//This funtion tells the user if there is an I2C error
{
Serial.print("I2C error: ");
Serial.print(error,DEC);
Serial.print(", ");
switch (error)
{
case 0:
Serial.println("success");
break;
case 1:
Serial.println("data too long for transmit buffer");
break;
case 2:
Serial.println("received NACK on address (disconnected?)");
break;
case 3:
Serial.println("received NACK on data");
break;
case 4:
Serial.println("other error");
break;
default:
Serial.println("unknown error");
}
}