[Arduino] Serial Communication Linux
Ariesnl

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}

Dizzy Egg

Are you running it with sudo priveliges?

Ariesnl

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

Peter Hull

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

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

Peter Hull
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

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>

Peter Hull

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

Thread #618738. Printed from Allegro.cc