Communication with VS c++ 20100

Hello, i am trying to set un a communication between an arduino and C++ on the VS ide.

I found this class http://www.tetraedre.ch/advanced/serial.php and i managed to send characters to the arduino, i guess you could send integers as well but i have not tried it yet, i am not very experienced in programing. The problem came when i tried to send a char from the arduino to the c++ program. This is the Arduino code, the analog read is a potentiometer

void setup() {
 
  Serial.begin(9600);
}


void loop() {
  
  int x = analogRead(A0);
  if(x > 200){
  
  Serial.println("G");
}
  else
  Serial.println("H");
  
  delay(1);       
}

and this is the C++ code

#include <sstream>
#include <String>
#include <iostream>

#include "Tserial.h"

using namespace std;

int main(int argc, char* argv[])
{
    char c;
    //int i;
    //int n, old_n;
    Tserial *com;
    char buffer[20];

    com = new Tserial();
    if (com!=0)
    {
        com->connect("\\\\.\\COM26", 9600, spNONE);

       

       
            c = (char) com->getChar();
            printf("%c",c);
      
			
			
      

	system("PAUSE");
	  // ------------------
        com->disconnect();

        // ------------------
        delete com;
        com = 0;
	}
    return 0;
}

Some sort of communication is beeing established but i do not get anything on the cmd, the tx led on the arduino is not blinking but if i set the incorrect COM in the c++ code it gives me a symbol , and if i set the correct one it hangs as if it is waiting for the arduino to send something.

But the fac is that is if i send a char from the c++ to the arduino it will receve it, so the communication works i just have to figure out how to make it work, with your help :slight_smile:

Communication with VS c++ 20100

You are going to have to wait a few years for that to happen... 8)

You are establishing a connection to the Arduino, causing it to reset. As soon as that connection is established, while the Arduino is still rebooting, you read one character from the serial port, regardless of whether there is anything to read, and then close the port, causing the Arduino to reset again.

I'm sure that you can figure out what you need to do.

yeah VS C++ 20100 i don't think we'll be using computers by then

As i said i am not very experienced in programing, so you are saying that i should close the "if" bracket before closing the port or using insetad of "if" statement a "while" or "for" statement, and close the port just at the end?

Are you sure that the Arduino is reseting? Because if i start the C++ program first and then upload the Arduino code, the arduino ide will tell me that something else is using that port.

As i said i am not very experienced in programing, so you are saying that i should close the "if" bracket before closing the port or using insetad of "if" statement a "while" or "for" statement, and close the port just at the end?

You need to separate opening the connection from sending the data - at least by using a delay between the connect and the send of the data. A user interface, with separate connect and end buttons would be even better. Then, add an event handler to deal with the arrival of serial data.

if (com!=0)
    {
        com->connect("\\\\.\\COM26", 9600, spNONE),
		printf(" i have a connection"),
		com->sendChar('g');}
		
   Sleep(2000);

       while(com!=0){
            c = (int) com->getChar(),
            printf("%c",c);
	   
  }
			
	   if ( com=0){
		   printf("i lost the connection");
		   
	   system("PAUSE");
	   
		}

so i will send a char before listening to the serial, i from the arduino i will only send data if i got something on the serial available like this:

void setup() {
  pinMode(led, OUTPUT);
  
  Serial.begin(9600);
}


void loop() {
  
  int x = analogRead(A0);
  
  if(Serial.available() > 0){
     led = HIGH;
    
  
  Serial.print(8);
}
  
  
  delay(1);        
}

but still no luck, i mean i see the led blinking but nothong on the c++ cmd only the "got connection" msg but at least now i don't get the "lost connection" msg, but if i unplug the arduino and there is no com 26 then i get some symbols.... so i'm lost

if(Serial.available() > 0){
     led = HIGH;
    
  
  Serial.print(8);
}

led is a variable holding a pin number. Why do you want to change the number of the pin?

        com->connect("\\\\.\\COM26", 9600, spNONE),
		printf(" i have a connection"),
		com->sendChar('g');}
		
   Sleep(2000);

The correct order is open, snooze, send, not open, send, snooze.

i set the led to blink only to know that the arduino received something this is the entire code:

  int led = 13;

void setup() {
  pinMode(led, OUTPUT);
  
  Serial.begin(9600);
}


void loop() {
  
  int x = analogRead(A0);
  
  if(Serial.available() > 0){
    led = HIGH;
    
  
  Serial.print(8);
}
  
  
  delay(1);        // delay in between reads for stability
}

and i tried puting the delay before sending the char but the rezult is the same

if (com!=0)
    {
        com->connect("\\\\.\\COM26", 9600, spNONE);
			Sleep(10000);
		printf(" i have a connection");
		com->sendChar('g');}
		
   
       while(com!=0){
            c = (int) com->getChar();
				if (c !=0){
				cout << ("%c",c)<< endl;
				}

but i wonder i i set the led to be HIGH why does it only blink? does that mean that the connection is lost after that?

thusarix:
i set the led to be HIGH

 led = HIGH;

No, you assigned the value HIGH (1) to the variable which holds the LED pin number. That does nothing to the state of the LED, it just corrupts your data.

If you want to make the output HIGH, you need to call digitalWrite().

so yeah i got the led thing. But i removed it from the code and from the c++ code i removed the send.char so the code will establish a connection than sleep for 12 sec to wait for the arduino to reset and than wait for data on the serial. but had no change

thusarix:
had no change

Oh dear. Do you think there’s something wrong with your code?

clearly there is something wrong with my code but clearly i can not figure out what exactly...

i know that this c++ class is working because i can send data... why can't i receive it?.... if you don't wnat to tell me the solution at least point me in the right direction

thusarix:
clearly there is something wrong with my code but clearly i can not figure out what exactly...

Apparently that was too subtle, so let me try it this way:

If you want help solving a problem with your code, you need to POST YOUR CODE.