PDA

View Full Version : Need help setting up a serial port connection



jvwlong
29th June 2012, 20:28
Hello, I am trying to wrtie a program in Ubuntu where, throught the console, I can read and write a serial port. So I have started using the qextserialport library. I found code online and tried to emulate it to make a simple test to see if I can get the program to merely connect to the serial port. But alas my program does not seem to be connecting. And I am at a loss for why it won't open the port. Here is my code



#include <QtCore/QCoreApplication>
#include <3rdparty/qextserialport/src/qextserialport.h>
#include <iostream>
#include <qiodevice.h>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
const char data[]= "e";
QextSerialPort * port = new QextSerialPort("/dev/ttyUSB0");
port->setBaudRate(BAUD115200);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
port->setTimeout(10);
bool res = false;
res = port->open(QIODevice::ReadWrite | QIODevice::Unbuffered);

if(res)
{
cout << "Opened" << endl;
char i = port->write(data);
}
else
{
cout << "Failed to connect" << endl;
}
return a.exec();
}


The console that is opened always displays "Failed to connect"
Can anyone see any very obvious erros?

Thanks
-Jvwlong

Lesiok
30th June 2012, 07:50
Do You have rigths to use /dev/ttyUSB0 ?

jvwlong
2nd July 2012, 16:42
I believe I do? How would I check if I have permission or not?

Lesiok
2nd July 2012, 17:24
I believe I do? How would I check if I have permission or not?

From command line :

ls -l /dev/ttyUSB0

and watch at screen.

jvwlong
2nd July 2012, 17:32
This is the output from that command
"crw-rw---- 1 root dialout 188, 0 Jul 2 09:30 /dev/ttyUSB0"

jvwlong
3rd July 2012, 17:50
Solved. I just needed to be added to the dialout group.