PDA

View Full Version : how to open the serial port of my pc from the qt app



prasad1001
26th February 2014, 10:46
Hello all,
I was writing code for RFID reader interfacing for my pc. RFID reader is responding fine with my minicom program ...but from the application code it is saying unable to open port.

Here is my code...


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
textEdit=ui->textEdit;

qDebug() << "Number of serial ports:" << QSerialPortInfo::availablePorts().count();

foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
{
qDebug()<<"Name: "<<info.portName();
qDebug()<<"Description: "<<info.description();
qDebug()<<"Manufactures: "<<info.manufacturer();


QSerialPort *port = new QSerialPort(info);
if (port->open(QIODevice::ReadWrite)) {
qDebug() << "Baud rate:" << port->baudRate();
qDebug() << "Data bits:" << port->dataBits();
qDebug() << "Stop bits:" << port->stopBits();
qDebug() << "Parity:" << port->parity();
qDebug() << "Flow control:" << port->flowControl();
qDebug() << "Read buffer size:" << port->readBufferSize();
port->close();
} else {
qDebug() << "Unable to open port, error code" << port->error();
}

}

}

The output i am getting is
---------------------------------
Number of serial ports: 1
Name: "ttyS0"
Description: ""
Manufactures: ""
Unable to open port, error code 2
--------------------------------------------

Thanks in advance.

Scorp2us
26th February 2014, 14:19
if you use miniterm or some other program can you open that device? You might need to adjust the permissions on the device.


QSerialPort::NoError 0 No error occurred.
QSerialPort::DeviceNotFoundError 1 An error occurred while attempting to open an non-existing device.
QSerialPort::PermissionError 2 An error occurred while attempting to open an already opened device by another process or a user not having enough permission and credentials to open.
QSerialPort::OpenError 3 An error occurred while attempting to open an already opened device in this object.
QSerialPort::ParityError 4 Parity error detected by the hardware while reading data.
QSerialPort::FramingError 5 Framing error detected by the hardware while reading data.
QSerialPort::BreakConditionError 6 Break condition detected by the hardware on the input line.
QSerialPort::WriteError 7 An I/O error occurred while writing the data.
QSerialPort::ReadError 8 An I/O error occurred while reading the data.
QSerialPort::ResourceError 9 An I/O error occurred when a resource becomes unavailable, e.g. when the device is unexpectedly removed from the system.
QSerialPort::UnsupportedOperationError 10 The requested device operation is not supported or prohibited by the running operating system.
QSerialPort::UnknownError 11 An unidentified error occurred.

prasad1001
27th February 2014, 05:03
Thanks for the Quick reply sir,
The device (rfid reader) is opening and giving data to minicom of my pc...
Now how to set the permission access from Qt app..i.e how to open the port, can u please explain in brief...

Thank you

ChrisW67
27th February 2014, 06:03
There are two possibilities straight from the error message:

Attempting to open an already opened device by another process. If there is another process with the port already open then this might happen. Minicom should not be running when you try to run this.
User not having enough permission and credentials to open. The permissions are those of the file representing the serial port: probably "/dev/ttyS0" but it may be in a subdirectory of /dev. Often only root and a group called "uucp" have write access to the device.


$ ls -l /dev/ttyS0
crw-rw---- 1 root uucp 4, 64 Feb 27 07:15 /dev/ttyS0
$ groups
wheel audio cdrom video games cdrw users wireshark vboxusers truecrypt uucp
Your Linux user should be generally placed in the "uucp" group (recommended) although you can alter the serial port permissions directly (not recommended).

Here is a User Management (https://help.ubuntu.com/10.04/serverguide/user-management.html) tutorial.
Here is a Linux File Permissions (http://www.linux.org/threads/file-permissions-chmod.4094/) tutorial.

prasad1001
27th February 2014, 07:09
Thanks for the quick reply sir,
First of all sorry for my english.

I was running my code in 'root' and it is getting data from serial port....
I written the code for reading data as shown below,
Qt code:

if(port->waitForReadyRead(5000)){
QByteArray readdata=port->readAll();
//textEdit->append(readdata);
while(port->waitForReadyRead(1000)){
readdata +=port->readAll();
}

textEdit->append(readdata);
}

It is reading one time and displaying on the textEdit.. but i want to monitor and read the data from the port continuously whenever the data available...what should i have to do...
Thanks in advance...

anda_skoa
27th February 2014, 09:01
Don't "wait for readyRead", connect a slot to the readyRead() signal and read the data in the slot.

Btw, running a GUI program as root is usually a bad idea. At some point you should invest a bit of time to get the permissions right for a normal user account.

Cheers,
_