Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [Arduino] Serial Communication Linux

This thread is locked; no one can reply to it. rss feed Print
[Arduino] Serial Communication Linux
Ariesnl
Member #2,902
November 2002
avatar

Hey everyone,
I started to play with Arduino (UNU)
I wanted to setup some test for sewrial communication with my pc.
I wrote a small and simple program to send some characters. the Arduino side seems to work fine, The serial monitor of the IDE shows the text the Arduino is sending, and also the test appears in "Screen" ( Linux minitoring program.
However trying to connect with my C++ program using LibSerial fails every time...
<Bad file descriptor>

Any Ideas ?

Thanks !

#SelectExpand
1#include <libserial/SerialStream.h> 2#include <iostream> 3 4using namespace LibSerial; 5 6SerialStream g_StreamSerial; 7 8bool g_blClose = false; 9 10int main() 11{ 12 g_StreamSerial.Open("/dev/ttyACMO"); 13 if (g_StreamSerial.good()) 14 { 15 g_StreamSerial.SetBaudRate(BaudRate::BAUD_9600); 16 if (g_StreamSerial.good()) 17 { 18 char ch; 19 20 while(!g_blClose) 21 { 22 23 g_StreamSerial >> ch; 24 std::cout << ch; 25 } 26 } 27 else 28 { 29 std::cout << "Could not set baud rate"; 30 } 31 32 33 } 34 else 35 { 36 std::cout << "Could not open port"; 37 } 38 39 g_StreamSerial.Close(); 40 return 0; 41}

Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard)
Current project: [Star Trek Project ] Join if you want ;-)

Dizzy Egg
Member #10,824
March 2009
avatar

Are you running it with sudo priveliges?

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

Ariesnl
Member #2,902
November 2002
avatar

No, but would that be necessary ?
Would be inconvenient to use sudo every time running this...

Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard)
Current project: [Star Trek Project ] Join if you want ;-)

Peter Hull
Member #1,136
March 2001

Maybe try .IsOpen() instead of .good() and see if an error message becomes apparent?

edit - also are you opening "/dev/ttyACMO" with a letter "O" or "/dev/ttyACM0" with a zero? I think the latter is correct.

Ariesnl
Member #2,902
November 2002
avatar

Makes no difference, and yes the last char is a zero ;-)

Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard)
Current project: [Star Trek Project ] Join if you want ;-)

Peter Hull
Member #1,136
March 2001

Ariesnl said:

yes the last char is a zero

OK because it isn't in the code you posted.

How do you know the error is "Bad file descriptor"? (EBADF presumably?)

If you try to just open() that file does it work?

Dizzy Egg
Member #10,824
March 2009
avatar

Have you given yourself permission to acces the ports?

i.e.

sudo usermod -a -G dialout <your username>
sudo usermod -a -G plugdev <your username>

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

Peter Hull
Member #1,136
March 2001

Surely if it works from screen and from the Arduino IDE, the permissions are OK?

Go to: