Arduino for C++ SerialClass

Looking at this post.
http://playground.arduino.cc/Interfacing/CPPWindows

I would like to do an action in my c++ program when a specific byte is received.
Considering the test main.cpp given i have been able to output the desired received byte, however, i havent found a way to actually compare it to anything because it is set up like this char array.

Anyone that knows how to compare it properly to fx. a string?

// Updated with code

Sorry then.

Arduino code is pretty much just the basic debounce example and then i added that i has to print a binary value as well as switch an LED. There is no problem with this part.

void loop()
{
  reading = digitalRead(inPin);

  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH){
      state = LOW;
      Serial.println(0, BIN); 
    }
    else {
      state = HIGH;
      Serial.println(1, BIN);
    }
    
    time = millis();    
  }

  digitalWrite(outPin, state);

  previous = reading;
}

This is the C++ code which i need help with. This way of outputting the “incomingData” doesnt spam your console because it limits it. However, you still cant just compare the incomingData with a string value because the char array is weird somehow? o.O
What i would like to do is be able to make an action based on wheater or not it reads the 0 or the 1 bit.

int main() {
	Serial* SP = new Serial("\\\\.\\COM3");
	if (SP->IsConnected())
	{
		printf("We're connected");
		cout << endl;
	}

	char incomingData[5] = "";
	int readResult = 0;

	while (SP->IsConnected())
	{
		readResult = SP->ReadData(incomingData);
		incomingData[readResult] = 0;
		cout << incomingData;
		Sleep(5);
	}
}

Psst. We can't see YOUR code.

Psst, i literally linked to another post with the code i used and said that im using the example in the bottom.

christopherkas:
Psst, i literally linked to another post with the code i used and said that im using the example in the bottom.

Psst ... make it easy for people to help you by posting the code here so they don't have to waste time going somewhere else and perhaps looking at the wrong thing.

Also, I have zero confidence (based on time-wasting experience) that people use an example without a single modification unless I see the code they have uploaded to their Arduino.

...R
Serial Input Basics

Sorry then.

Arduino code is pretty much just the basic debounce example and then i added that i has to print a binary value as well as switch an LED. There is no problem with this part.

void loop()
{
  reading = digitalRead(inPin);

  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH){
      state = LOW;
      Serial.println(0, BIN); 
    }
    else {
      state = HIGH;
      Serial.println(1, BIN);
    }
    
    time = millis();    
  }

  digitalWrite(outPin, state);

  previous = reading;
}

This is the C++ code which i need help with. This way of outputting the “incomingData” doesnt spam your console because it limits it. However, you still cant just compare the incomingData with a string value because the char array is weird somehow? o.O
What i would like to do is be able to make an action based on wheater or not it reads the 0 or the 1 bit.

int main() {
	Serial* SP = new Serial("\\\\.\\COM3");
	if (SP->IsConnected())
	{
		printf("We're connected");
		cout << endl;
	}

	char incomingData[5] = "";
	int readResult = 0;

	while (SP->IsConnected())
	{
		readResult = SP->ReadData(incomingData);
		incomingData[readResult] = 0;
		cout << incomingData;
		Sleep(5);
	}
}

I'm still confused. You have not posted a complete Arduino program. I presume the other piece of code is intended to run on your PC.

I don't understand why you are using BIN in the Arduino program. Wouldn't it be much simpler just to send the character '1' or '0' as appropriate? And if you really want to send a binary value then you should use Serial.write()

What is this intended to do

readResult = SP->ReadData(incomingData);
 incomingData[readResult] = 0;

It appears to be taking in a value and using that value to index an array and set value at that location to 0. Wouldn't it be much simpler just to write all the array positions to 0 and not bother with the input?

The Sleep() in your PC program is probably not helping either.

I suspect you will get much better advice if you tell us what the project is all about. There may be a better way.

...R

Robin2:
I'm still confused. You have not posted a complete Arduino program. I presume the other piece of code is intended to run on your PC.

I don't understand why you are using BIN in the Arduino program. Wouldn't it be much simpler just to send the character '1' or '0' as appropriate? And if you really want to send a binary value then you should use Serial.write()

What is this intended to do

readResult = SP->ReadData(incomingData);

incomingData[readResult] = 0;



It appears to be taking in a value and using that value to index an array and set value at that location to 0. Wouldn't it be much simpler just to write all the array positions to 0 and not bother with the input?

The Sleep() in your PC program is probably not helping either.

I suspect you will get much better advice if you tell us what the project is all about. There may be a better way.


...R

Do you honestly want to see my setup as well? It has nothing to do with my problem. Just as how i send my bit doesnt have anything to do with it either. It all works as intended.

The code you pointed out is taken from the link and the meaning of it is too. I told you the meaning is that i want to trigger an action based on which bit i receive. Simply and if-statement.

What i want to do is control my C++ based program via fx. keypresses registered on the arduino.

Robin2:
And if you really want to send a binary value then you should use Serial.write()

plus a line terminator.... so what you proposed and what is being done are not identical.

@christopherkas

you are trying to modify the PC side to allow Arduino to send other "commands" yes?

BulldogLowell:
plus a line terminator.... so what you proposed and what is being done are not identical.

@christopherkas

you are trying to modify the PC side to allow Arduino to send other "commands" yes?

I want to be able to send different values (one whole byte) and then read which byte is send from the arduino.
Then on the PC side i want to do different tasks based on which byte is received.

christopherkas:
I want to be able to send different values (one whole byte) and then read which byte is send from the arduino.
Then on the PC side i want to do different tasks based on which byte is received.

I’d start there. Put together your sketch that creates those events, try it by looking at the serial monitor and verify that you see the various characters transmitted by serial. Once you know you have that working (i.e. I push the ‘#’ key and a ‘#’ character is sent, you can look at updating the PC program to look for other inputs.

BulldogLowell:
I'd start there. Put together your sketch that creates those events, try it by looking at the serial monitor and verify that you see the various characters transmitted by serial. Once you know you have that working (i.e. I push the '#' key and a '#' character is sent, you can look at updating the PC program to look for other inputs.

As previously said all the arduino based code is working as intended and i have no problems with that. On the PC i can also already read the bytes sent properly, however i have problems acting upon them. I cant just ask if the char array is equal to a certain string.
This is what i need help with.

[quote author=christopherkas link=msg=3210556 date=1491568988]
As previously said all the arduino based code is working as intended and i have no problems with that.

The code you posted transmits a zero or a one, so naturally I'm a bit confused.

christopherkas:
On the PC i can also already read the bytes sent properly, however i have problems acting upon them. I cant just ask if the char array is equal to a certain string.

There is plenty of C++ knowledge here, I am sure that if you posted what happens from the event (e.g. a char arrives by serial) to the response (what is seen in your console logging of the response) you may be able to get some help.

BulldogLowell:

There is plenty of C++ knowledge here, I am sure that if you posted what happens from the event (e.g. a char arrives by serial) to the response (what is seen in your console logging of the response) you may be able to get some help.

[/quote]

yes, 0 and 1 as in bits.

[quote author=christopherkas link=msg=3210650 date=1491572061]
yes, 0 and 1 as in bits.

[/quote]

???

your arduino code (that you are not using which is posted above, but not the one you are actually using which works but you haven't shown to us) doesn't send bits, it is sending a byte followed by a new line.

In order to get help tying your shoes, you need to get both your foot and your shoe in front of your mommy.

BulldogLowell:

???

your arduino code (that you are not using which is posted above, but not the one you are actually using which works but you haven't shown to us) doesn't send bits, it is sending a byte followed by a new line.

In order to get help tying your shoes, you need to get both your foot and your shoe in front of your mommy.

[/quote]

I dont know why you think i dont use the code i posted, and why it wouldnt work. It always has worked and i never stated otherwise. You people just keep coming in here telling me what i need to do different in the part that is actually working instead of what i cant make work.

[quote author=christopherkas link=msg=3210685 date=1491573603]
I dont know why you think i dont use the code i posted, and why it wouldnt work. It always has worked and i never stated otherwise. You people just keep coming in here telling me what i need to do different in the part that is actually working instead of what i cant make work.

[/quote]

“You people” are trying to help you…
“You people” may be “keep coming in here telling what need to do” because you are asking for help…

why not oblige your free helpers and provide them what they request?

BulldogLowell:

"You people" are trying to help you... 

“You people” may be “keep coming in here telling what need to do” because you are asking for help…

why not oblige your free helpers and provide them what they request?

[/quote]

I dont see how it can be considered helping to claim that im not using the posted code.
Ive provided everything that was asked for. Do you have any knowledge that might help me solve my problem?

christopherkas:
Do you honestly want to see my setup as well? It has nothing to do with my problem. Just as how i send my bit doesnt have anything to do with it either. It all works as intended.

The code you pointed out is taken from the link and the meaning of it is too. I told you the meaning is that i want to trigger an action based on which bit i receive. Simply and if-statement.

Fine. If you prefer to treat this as a struggle rather than a co-operation I will leave you in piece.

...R