hi ,
i need to develop an application in qt such that it reads the datas whenever data comes from the serial port.Can i use QFile for this ? or is there any other classes.
thanks in advance,
saravanan
hi ,
i need to develop an application in qt such that it reads the datas whenever data comes from the serial port.Can i use QFile for this ? or is there any other classes.
thanks in advance,
saravanan
yeah , i googled regarding that and i also downloaded the Qextserial package and compiled that for my linux box too. but after that i dnt know how to use it ? i mean there is no executables created. there are only libraries created.i dnt know what to do with that created libraries. i also checked in the link you sent. there only for windows version of qextserial port is given.
can anybody say me how to use it ?
QExtSerial is a library which you can use in your application.
I did not used it, but they should have API documentation on their site.
Probably you will have to include some headers and the port initialization and communication should be pretty straightforward.
regards
hi,
i had downloaded and compiled that for my host. But there are only library files created. also after there is no documentation explaining as how to use this with the qt or qwt package. i mean what should be done with this library etc? so i tried my own way. the following is my code:.Qt Code:
sample::sample() { mouse=open("/dev/ttyS0",O_RDONLY | O_NDELAY); cout<<" mouse:"<<mouse<<endl; QSocketNotifier *note; connect(note,SIGNAL(activated(int)),this,SLOT(data())); } void sample::data() { n=read(mouse,buffer,1); printf("n:%d\n",n); for(int i=0;i<n;i++) printf("buffer:%d\t",buffer[i]); cout<<"end of reading"<<endl; }To copy to clipboard, switch view to plain text mode
this is working when i send some characters from another system connected to this serial port. but when i connect a device to the serial port. the datas are not read successfully. The characteristics of the device is this: it will send some 5 byte data to the serial port. i need to read that datas as soon as it comes in the serial port.
can anyone say me why is that when we i enter the datas are read correctly and when a device sends its not recieved correctly?
can anyone provide me suggestions or solutions for this ?Am i correct ? or is there anyother way for reading the serial port as the datas come ? also if anyone knows how to use the qextserial can you send me the links or docs regarding that ?
I still can't get it why you aren't using syscalls for this issue. I haven't programmed any sockets/ports in Windows, but I can give you enough hints for any posix-complient environment (unixes: aix, hpux, etc., linux, *bsds, probably mac os).
If you want to attatch to a device, use the open() function to open the file (the old Unix filosophy that everything is a file works perfectly here).
To read some data, use read().
To poll for available data, use select().
To close the buffer when it's not available anymore, use close().
There is a nice example of using select in its manual page, at least for a linux environment.
I do usually wrap the logic using external devices in separate threads and communicate with signals (Qt4's emit is thread-safe, to my knowleage). A simple example is the following:
Qt Code:
{ Q_OBJECT private: int f_mouse; protected: virtual void run() //logic here. poll for data, if found, emit a signal. { fd_set rfds; struct timeval tv; int retval; while ( isRunning() ) { /* Watch stdin (fd 0) to see when it has input. */ FD_ZERO(&rfds); FD_SET(0, &rfds); /* Wait up to five microseconds. */ tv.tv_sec = 0; tv.tv_usec = 5; retval = select( f_mouse, &rfds, NULL, NULL, &tv ); /* Don’t rely on the value of tv now! */ if ( retval == -1 ) { perror( "select()" ); } else { if ( retval ) { printf( "Data is available now.\n" ); /* FD_ISSET(0, &rfds) will be true. */ emit data_available(); } else { printf( "No data within five seconds.\n" ); } } } } public: /** Basic constructor, tries to open the device. */ { f_mouse = open( "/dev/mouse", O_RDONLY ); if ( f_mouse < 3 ) // 1 and 2 are standart output and error output { throw DeviceException( "my_mouse_class::my_mouse_class(): Mouse not available." ); } } ~my_mouse_class() { if ( f_mouse > 2 ) //sanity check { close( f_mouse ); //close the file descriptor } } //read some data ssize_t read( void * buffer, ssize_t count ) { return ::read( f_mouse, buffer, count ); } signals: void data_available(); };To copy to clipboard, switch view to plain text mode
I have omitted the headers since they could be retrieved upon issuing the help on each of the functions, presenting in its dedicated manual page (for example, to read the full help of select, use "man select"). Please note that the above example contains code from the glibc's manual page, which is licensed under LGPL, so if you are using it in a commersial application, please modify it.
hi everyone ,
i had compiled my application with the qextserialport package by specifying the INCLUDEPATH and LIBS in the project file(".pro"). but when i execute it , it says "Segmentation fault " error.
can anyone provide me any suggestions or solutions for this ?
segfaults are caused by very bad mistakes in code . So we need to see the code before we can give you a solution.
sar_van81
For what its worth I have used QextSerialPort and it works well.
Below is the snippet of code used to setup the port. "portname1" is determined by the OS being used at the time.
Hope this is of some use to you....
Qt Code:
void Progarm::setupSerial() { serialPort = new QextSerialPort( portname1 ); serialPort->setBaudRate( QextSerialPort::BAUD9600 ); serialPort->setDataBits( QextSerialPort::DATA_8 ); serialPort->setStopBits( QextSerialPort::STOP_1 ); serialPort->setParity( QextSerialPort::PAR_NONE ); serialPort->setFlowControl( QextSerialPort::FLOW_OFF ); serialPort->setTimeout( 0, 500 ); if (!serialPort->isOpen() ) { "Could not open the serial port\n" "\n" "Please check connections and try again\n" ); label->setText( "Serial port error" ); return; } }To copy to clipboard, switch view to plain text mode
Regards, B1.
B1:
the following is my code:
.Qt Code:
{ setMinimumSize(640,480 ); Posix_QextSerialPort *note= new Posix_QextSerialPort("/dev/ttyS0"); note->setBaudRate(Posix_QextSerialPort::BAUD115200); note->setParity(Posix_QextSerialPort::PAR_NONE); note->setDataBits(Posix_QextSerialPort::DATA_8); note->setStopBits(Posix_QextSerialPort::STOP_1); note->open(IO_ReadOnly); int n; n=note->bytesWaiting(); if(n!=0) printf("\nreading the buffer datas \n"); }To copy to clipboard, switch view to plain text mode
Also can you say me how to read the datas from the serial port . i saw that only int Posix_QextSerialPort::getch() is available. is there any function to read a block of datas for example say 10bytes ?
saravanan
sar_van81,
This is what I use in a simple app...bear in mind I am still learning too!!
Qt Code:
void Program::dataListener() { // see how many bytes available from the serial port rec = serialPort->bytesAvailable(); if (rec > 0 ) { if ( rec > 256 ) rec = 256; z = serialPort->read( buffer, rec); buffer[z] = '\0'; tempmsg = buffer; } dispData(); }To copy to clipboard, switch view to plain text mode
There may be (and probably are!) better ways of doing this but for me it did what I wanted. You can also use serialPort->getChar(buffer) and read the serial port character by character and then process the buffer accordingly.
Hope this helps.
B1.
B1:
Ok. let me try this.but the application does not open successfully.it prompts me for "segmenattion fault error".if i comment the following lines:
.Qt Code:
Posix_QextSerialPort *note= new Posix_QextSerialPort("/dev/ttyS0"); note->setBaudRate(BAUD115200); note->setParity(PAR_NONE); note->setDataBits(DATA_8); note->setStopBits(STOP_1); note->open(IO_ReadOnly);To copy to clipboard, switch view to plain text mode
the program executes successfully. Did you experience any problem like this ?
Am i missing some thing more ?
Why are you commenting out these lines? with out them you don't have an initialized serial port objet.
Also, QextSerialPort is made crossplatform, so you don't need to explicitly use Posix_QextSerialPort (this wont compile under windows) - there are defines in the code that will select the correct version for you.
Just use QextSerialPort.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
high_flyer:
if i use QextSerialPort instead of Posix_QextSerialPort, it shows me the following error:
home/qtprograms/qwt/qextserial/qextserialport-0.8.0/win_qextserialport.h:11:21: error: windows.h: No such file or directory
/home/qtprograms/qwt/qextserial/qextserialport-0.8.0/win_qextserialport.h:57:7: warning: no newline at end of file
In file included from tux.cpp:21:
/home/qtprograms/qwt/qextserial/qextserialport-0.8.0/qextserialport.h:26:7: warning: no newline at end of file
/home/qtprograms/qwt/qextserial/qextserialport-0.8.0/win_qextserialport.h:50: error: ‘HANDLE’ does not name a type
/home/qtprograms/qwt/qextserial/qextserialport-0.8.0/win_qextserialport.h:51: error: ‘COMMCONFIG’ does not name a type
/home/qtprograms/qwt/qextserial/qextserialport-0.8.0/win_qextserialport.h:52: error: ‘COMMTIMEOUTS’ does not name a type
make: *** [tux.o] Error 1
.
So only i used Posix_QextSerialPort. Should i define somewhere about _TTY_POSIX ? Also as i said before when i include them in my code its prompting me segmentation fault.
Exactly - You should add DEFINES += _TTY_POSIX in your pro file (or if you are using KDevelop it has a field for that in the qmake configuration).So only i used Posix_QextSerialPort. Should i define somewhere about _TTY_POSIX ?
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
high_flyer:
that parameter has already been set when i untar the qextserial package. should i specify that in my application pro file ?
Let me explain what i have done so far :
1). Downloaded the qextserial package , untarred and entered qmake ,make. there were the following libraries created:
libqextserialport.so libqextserialport.so.1 libqextserialport.so.1.0 libqextserialport.so.1.0.0
2). Then created a directory ,copied my source files. entered the header files and the libraries for qextserial in my project file. then entered make.
is this procedure correct ? else can you say me the correct one ?
yes.should i specify that in my application pro file ?
It should work.is this procedure correct ? else can you say me the correct one ?
But I would not copy the header files and libs, I would link to them or user the -L and -I in my make file.
But that is a subjective private taste matter, your way should work just as well.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
hi ,
i specified in my project file also and it compiled successfully.but still the same error comes . Is there any permission i need to set to serial port ? i'l post my full code here if any one find any error please point me :
Qt Code:
#include <qwidget.h> #include <qapplication.h> #include <stdlib.h> #include <qextserialport.h> { public: private: int mouse; int mouseidx; }; { setMinimumSize(640,480 ); QextSerialPort *note= new QextSerialPort("/dev/ttyS0"); note->setBaudRate(BAUD115200); note->setParity(PAR_NONE); note->setDataBits(DATA_8); note->setStopBits(STOP_1); note->open(IO_ReadOnly); } int main( int argc, char **argv ) { Widget connect1; a.setMainWidget( &connect1 ); connect1.show(); return a.exec(); }To copy to clipboard, switch view to plain text mode
Last edited by high_flyer; 7th May 2007 at 10:54. Reason: missing [code] tags
I guess you mean the segmentation fault.compiled successfully.but still the same error comes .
Your code has the following problems:
You are allocating the serial port on to a local pointer.Qt Code:
QextSerialPort *note= new QextSerialPort("/dev/ttyS0");To copy to clipboard, switch view to plain text mode
You will not be able to access the serial port outside the constructor.
'note' needs to be a member variable.
Where is the code that handels the reading/writing to the serial port?
theLSB:
I did not add the read/write operation. i thougth first let me open the port sucessfuly. then,read that.
Do you mean that i need to declare it under protected and use it in the constructor ?'note' needs to be a member variable
Bookmarks