PDA

View Full Version : Serial Port Communication



soldstatic
21st June 2006, 17:05
I'm trying to communicate to a serial port RF receiver (specifically the W800RF32A) which is supposed to receive events from wireless X10 devices.

Within the function that is supposed to read the port I have:

ErrorText->setText("Clicked");

string line;
ifstream myfile ("/dev/ttyS0");

ErrorText->append("Created file object...");

if (myfile.is_open())
{
ErrorText->append("file is open...");

while ( !myfile.eof() )
{
ErrorText->append("not at end...");
// getline (myfile,line);
ErrorText->append("got line...");
ErrorText->append(line);
cout << line << endl;

}

ErrorText->append("End of file, closing");
myfile.close();
ErrorText->append("closed");

}

else cout << "Unable to open file";

You'll notice the commented getline function. The code can compile and run fine as long as I don't have the getline or similar function in there (and I've tried almost every alternative I could find for "getlin" because I know getline isnt going to work [no terminating character etc]).

Now when I look around for QT and stuff about serial ports everything is all QextSerial or some other class, but I've been told on linux you don't need that and you can simply read the serial port like a text file....... hence the above approach.

(btw I have a version of the above that uses QFile, it too behaves the same way)

So my questions:
1)Do I need another class to handle the serial port in linux?
2)If not, then what function should I use to get the incoming characters?? (to replace the commented line)

wysota
21st June 2006, 19:09
1)Do I need another class to handle the serial port in linux?
You can use QExtSerial or QFile


2)If not, then what function should I use to get the incoming characters?? (to replace the commented line)
QFile::getch() or fgetc()

If you don't have line terminators and you don't know the length of input, you have to read char by char. Of course if you do know the length, you can use QFile::readLine() and limit the number of characters to read.

soldstatic
21st June 2006, 20:53
You can use QExtSerial or QFile


QFile::getch() or fgetc()

If you don't have line terminators and you don't know the length of input, you have to read char by char. Of course if you do know the length, you can use QFile::readLine() and limit the number of characters to read.

I didn't really want to use the another class, so I was going for the file approach. I have a Qfile version of the above, but i'll post that if I can't get getch to work.

good point w/ the readline and limiting the number of characters.

soldstatic
21st June 2006, 21:44
Here's my other attempt with the QFile and getch.


void Tech::pushButton1_clicked()
{
{
QFile file( "/dev/ttyS0" ); // Read the text from a file
if ( file.open( IO_ReadOnly ) ) {
ErrorText->append( "File opened, trying to read...");
QTextStream stream( &file );
QString line;
char *a;

while(!file.atEnd())
{
a = file.getch();
line.append( a );
}
file.close();
ErrorText->append( line) ;
ErrorText->append( "File closed");
}
else
{
ErrorText->setText("Could not open file.");
}
}

Not sure why but it gives me compile error:

X10.ui.h:247: error: invalid conversion from `int' to `char*'

I'm sure that compile error is somethin stupid though... Still, if you see what's givin me that error lemme know please... :-/

Thanks muchos for the help guys, this has been frustrating me forever.. and by forever i mean almost 4 weeks...

wysota
21st June 2006, 23:10
getch returns an int (which is castable to char). The one below is fine:


char character = file.getch();

soldstatic
21st June 2006, 23:37
oh duh thanks. i knew it was dumb. i had it cast as a char * and not char.
thanks!

i'll test it out tomorrow and let you know!

soldstatic
22nd June 2006, 17:05
OK the getch worked with Qfile. Using a null modem cable hooked up to another computer it worked just fine. Now I just need to figure out how to call the function when there is information there that isn't null and how to get this serial device to actually work. At least my program is functional now as far as pure serial communication is concerned.

Thanks alot I really appreciate it!!!

-Andy


QFile file( "/dev/ttyS0" ); // Read the text from a file
if ( file.open( IO_ReadOnly ) ) {
ErrorText->append( "File opened, trying to read...");
QTextStream stream( &file );
QString line;
char a;
ErrorText->append( "made everything at end?");
while(!file.atEnd())
{
ErrorText->append( "Not at end, reading and appending...");
a = file.getch();
line.append( a );
std::cout << a;
}
ErrorText->append( "at end, closing...");
file.close();
ErrorText->append( line) ;
ErrorText->append( "File closed");
}
else
{
ErrorText->setText("Could not open file.");
}