Pages: [1]   Go Down
Author Topic: usb communication with c++  (Read 1992 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok..thanks for the help smiley..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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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??
Logged

Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so what do u suggest??
Logged

Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm sorry, but I don't know.  smiley-confuse
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: