Arduino and C++

Hello, I am having some issues with interfacing c++ with an adrunio uino. So my code connects to the board (as confirmed by the lights on the board) but when ever I try and send/receive code from the board the board either doesn't receive it or it doesn't send. I have tried multiple boards, multiple programs, multiple codes and nothing seems to work. Any help would be much appreciated!


Code uploaded to arduino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  int led1=2;
int getNumber=0; 

void setup() {
  // put your setup code here, to run once:
  pinMode(led1,OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  while (Serial.available()>0) {
    getNumber=Serial.read();
    if(getNumber==49)
      digitalWrite(led1, HIGH);
    else
      digitalWrite(led1, LOW);
    Serial.print(getNumber);
  }
}



C++ basic code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  #include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <windows.h>

using namespace std;

int main()
{
    char sendcode1[]="1";
    char sendcode2[]="2";
    string ss;
    ss ="\\\\.\\COM8" ;
    HANDLE arduinoSerial=0;
    DWORD  bytesread = 1;
    arduinoSerial = CreateFile(ss.c_str() ,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);//creating the comunication with the scale
    if (arduinoSerial == INVALID_HANDLE_VALUE)
    {
       cout<<"COM Communication Error!\nPlease Check the USB connection.\n";
        CloseHandle(arduinoSerial);
    }

    DCB dcbSerialParams = {0};
    COMMTIMEOUTS CommTimeouts= {0};

    dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
    dcbSerialParams.BaudRate=CBR_9600;
    dcbSerialParams.ByteSize=8;
    dcbSerialParams.StopBits=ONESTOPBIT;
    dcbSerialParams.Parity=NOPARITY;

    dcbSerialParams.fBinary = TRUE;
    dcbSerialParams.fDtrControl = DTR_CONTROL_ENABLE;
    dcbSerialParams.fRtsControl = RTS_CONTROL_ENABLE;
    dcbSerialParams.fOutxCtsFlow = TRUE;
    dcbSerialParams.fOutxDsrFlow = TRUE;
    dcbSerialParams.fDsrSensitivity= TRUE;
    dcbSerialParams.fAbortOnError = TRUE;

    CommTimeouts.ReadIntervalTimeout = MAXDWORD;
    CommTimeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
    CommTimeouts.ReadTotalTimeoutConstant = 500;

    SetCommTimeouts (arduinoSerial, &CommTimeouts);//used for the timeouts
    SetCommState(arduinoSerial, &dcbSerialParams);//parameters of the scale
    WriteFile(arduinoSerial, sendcode1 , 1, &bytesread, NULL);
    Sleep(500);
    WriteFile(arduinoSerial, sendcode1 , 1, &bytesread, NULL);
    cout<<"light on\n";
    Sleep(4500);
    WriteFile(arduinoSerial,sendcode2, 1, &bytesread,NULL);
    Sleep(5000);
}


So basically the arduino code says if the board receives a 1 it turns the light on and if it receives any other number it turns the light off. This program works on putty as well. Then when adding the c++ program it connects to the board and then turns the light on, gives a notification, then turns off.
closed account (48T7M4Gy)
Using the first code have you properly selected the right COM port?
yes in arduino, you select the com port in the window and it is the correct com port. The way I know this is that the program works in the serial monitor in the arduino coding language.
closed account (48T7M4Gy)
Do the programs like Blink from the Arduino site work?
on my board, yes. And the arduino code that put above works as well with in the ardunio program and with in putty. :)
closed account (48T7M4Gy)
So, if I understand you correctly you can run your arduino uno with input from the serial monitor or PTTY using the first program but not when you try to use the second program?
Last edited on
That is correct, and I am trying to figure out what is different because i have used basically the same program before to connect to a serial port, and it worked just fine and now this is failing, and i can not figure out why.
closed account (48T7M4Gy)
Well, I've just about run out of time. You're taking too long to answer. My view is if you can't get your second program to work is because you don't program an Arduino with a program main() because it is a controller, not a computer and probably doesn't have enough memory to run windows.h library type stuff anyway.

Arduino's are designed to transmit/receive serial data and I notice from your other posts you are managing data from some sort of weight transducer which is simple if you do it via the arduino capability.

In short, I suspect you're expecting too much from the Arduino. It will manage/process/display and file the data but there are limitations. If you want to do more then maybe you need to connect up and transmit the data to a network computer.

:)
Thank you for trying, but that is not the issue it is just talking to the arduino board, it is not controlling the arduino board. The arduino can handle a lot more than receiving a 1 char code from another program. I have done a lot more with the arduino in other coding languages, but for this I have to use C++, which is why I am here currently.

Also I am not working on that program anymore, I finished that program over a year ago.
Topic archived. No new replies allowed.