Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everyone,

I have looked everywhere for help concerning this issue, but haven't found anything helpful. Currently, I have a code in Visual Studio written to trigger a code to run in Arduino when told to do so. The code in arduino runs a luminosity sensor. Since I can't have the Serial monitor open while doing this I would like to print the data from the sensor into my visual studio. I am not quite sure the proper syntax for this so any help would be appreciated.

Visual studio code
C++
// 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 \"go\" to measure");
			// get answer
			answer=Console::ReadLine();
			//check that user typed one of the options
			if(String::Compare(answer,"go")==0)
				arduino->WriteLine("A"); // send A to arduino
			else
				Console::WriteLine(answer+" was not an option");
		
		}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;
}


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
int trigger; //define incoming trigger value

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() > 0)
  {
    trigger=Serial.read();
    if (trigger == '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");
    }
}
Posted
Comments
Vedat Ozan Oner 1-Jul-14 13:23pm    
any serial terminal is enough for your need. you don't have to write code to communicate with arduino in your case. arduino ide includes a serial communication tool with arduino and you can use it.
Sergey Alexandrovich Kryukov 1-Jul-14 14:06pm    
There is no such think as "Visual Studio console". Visual Studio has nothing to do with the applications you develop using it. :-)
You are not writing data to Visual Studio, you are writing it to a console of your application. What's the problem?
—SA
Member 10917244 1-Jul-14 14:15pm    
The reason I am writing a code in Visual Studio is because later this will be applied to a camera code that will trigger the Arduino. So what I need is to be able to see the data that the sensor produces, which I currently can't do.
Vedat Ozan Oner 1-Jul-14 15:02pm    
you can check out my article here: http://www.codeproject.com/Articles/776132/Service-based-Arduino-Data-Logger. it's beyond your need, but I think it helps.

1 solution

If you want text to appear inside the Visual Studio Output window, Diagnostics::Debug::WriteLine does the trick. Otherwise, use Console:WriteLine (which you seem to be using).

Looking at your code, you've got two problems.

1. You aren't reading anything from the serial port.
2. You're calling Console::ReadLine which blocks.

The easiest thing would be to create a separate thread to deal with the serial port.

QueueUserWorkItem will do here.

http://msdn.microsoft.com/en-us/library/kbf0f1ct%28v=vs.110%29.aspx[^]

Code your worker thread to exit when some boolean variable has been set (or cleared).

The main thread can set/clear this flag when the program is ready to exit.

You'll need to figure out a way for the main console thread and the serial port thread to exchange data.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900