Go Down

Topic: usb communication with c++ (Read 2351 times) previous topic - next topic

naeemuzz

kindly help me to get data from the uno to my lap working on ubuntu 12.04. i am creating a program interfacing a camera in c++. i want to include a segment ie to get data from uno and use it manipulate the configurations of camera.kindly help...

Erdin

The Arduino Uno creates a (virtual) serial port on the PC.
That serial port is used to upload a sketch and also to read from and write to the Arduino Uno.

http://arduino.cc/en/reference/serial

There are many ways to use that serial port on the PC, for example in C++ but also the command line and some use python.

naeemuzz

ok..thanks for the help :)..i have tried the following..
configured baudrate in terminal by
Quote
stty -F /dev/ttyACM0 9600

then i created a c++ program(exp.cpp) in my computer
Quote
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string str;
fstream f;
f.open("/dev/ttyACM0");
while(1)
{
   while (f >> str)
   {
      cout << str;
   }
}
return 0;
}


and uploaded this program into arduino uno
Quote

int led = 13;
void setup()

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

void loop()
{
  digitalWrite(led, HIGH);
  Serial.write("led");
  delay(1000);         
  digitalWrite(led, LOW);
  delay(1000);           
}

finally compiled the exp.cpp and got nothing...please consider me as newbie to usb communication

naeemuzz

well i changed the c++ program as
Quote
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char ch;
ifstream f;
f.open("/dev/ttyACM0");

   while (f.get(ch))
   {
      cout << ch;
   }
return 0;
}

when i compile the program i get
Quote
naaeemuzz@naaeemuzz-Satellite-L655:~/project/samples$ ./samplesled
led
led
led
led
led
led
led
led
led
led
led
led
led
led
led
led
led
led
led
led
led
led
led
led
led
led
naaeemuzz@naaeemuzz-Satellite-L655:~/project/samples$

and this happens in split of a  second and stops.i actually wanted an output similar to that of serial monitor..ie whenever the led blinks and shows out:"led" and else it does not....so what should i do??

Erdin

The Uno keeps sending "led" every 2 seconds. I think that part is good.

Your program on PC ends when there is nothing to receive.
Perhaps you can test if a keyboard button is pressed to end the program.

What kind of compiler do you use for the PC program ?

naeemuzz

i use g++ compiler...i compiled through terminal...lemme try about what you told...


naeemuzz

i programmed cpp file as when it gets input "1",it shows output from uno,still the program stopes  in 2 sec.. :(

Erdin

I tried to make a program in C++.
But it is not reading /dev/ttyACM0. I get a lot of EOF, and nothing else.

naeemuzz


Erdin

I'm sorry, but I don't know.  :~
I wanted to test the communication, but somehow I am not receiving anything with my c++ program.
I have not made a c++ program on the PC for the serial port before.
Normal programs like gtkterm can communicate without problem.

makerman

A quick search on google found these candidates:

http://www.teuniz.net/RS-232/

http://sourceforge.net/projects/cssl/

I have not used them, but you can give them a try.


naeemuzz

well thanx for your support...when i searched in stackoverflow.com i got a solution perfectly working...problem was the part where i configured baudrate..
i used
Quote
stty -F /dev/ttyACM0 sane raw pass8 -echo -hupcl clocal 9600


this works well
source:http://stackoverflow.com/questions/11677639/two-way-c-communication-over-serial-connection

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy