PDA

View Full Version : serial error



hiuao
16th April 2007, 11:20
I use qextserial for serial programing,but when I run it and put into the port name,there are endless of "errror" on the screen.
Please take you a little time,help me!
the codes as the following:

sp = new QextSerialPort;
sp->setBaudRate( QextSerialPort::BAUD9600);
sp->setParity( QextSerialPort::PAR_NONE);
sp->setDataBits( QextSerialPort:: DATA_8);
sp->setStopBits( QextSerialPort::STOP_1);
qDebug("ConstructorDestructorTest");
qDebug("-------------------------");
qDebug("+++ Created");
if( !(sp->open(QIODevice::ReadWrite)))
{
qDebug("+++ Could not open");
cout<<"input port"<<endl;
char *POR;
cin>>POR;
QString PORTNAME1 = QString(POR);
sp->setName(PORTNAME1);

if( PORTNAME1 == sp->name() )
{
qDebug("+++ set port name");
if( sp->open() )
{
qDebug("+++ Opened");
sp->close();
qDebug("+++ Closed");

}
else {
qDebug("--- Could not open");
}
}
else {
qDebug("--- failed to set port name");
}
}
else {
qDebug("--- Opened");
sp->close();
qDebug("+++ Closed");
}

high_flyer
16th April 2007, 11:28
1. please post the errors you get.
2. Make sure you are using QExtSerialPort that is built with Qt4
3. Please use code tags when you post code.

marcel
16th April 2007, 11:30
You do not test the second time you open the port (and set the name( if the open is successful ).

Also, post the errors you get.

hiuao
16th April 2007, 13:28
Yes,I use QExtSerial and when I run the program,it shows :
ConstructorDestructorTest
-------------------------
+++ Created
+++ Could not open
input port
(after I input such as /dev/ttyS0)
error
error
error
error
......
Whether there is something wrong with my card?But I have tested it in windows,it is all right.

high_flyer
16th April 2007, 14:18
input port
(after I input such as /dev/ttyS0)
error
error
error
error

In the code you poseted, there is no output "error"...
Where is it coming from (in your code)?

high_flyer
16th April 2007, 14:28
Your code is messy.
So lets try to go step by step.
Oh and make sure you have rights to read and write to the serial ports.
Try this:


sp = new QextSerialPort;
sp->setBaudRate( QextSerialPort::BAUD9600);
sp->setParity( QextSerialPort::PAR_NONE);
sp->setDataBits( QextSerialPort:: DATA_8);
sp->setStopBits( QextSerialPort::STOP_1);
sp->setName("/dev/ttyS0");
if( !(sp->open(QIODevice::ReadWrite)))
{
qDebug()<<"could not open"<<sp->name();
}
if(sp->isOpen())
{
qDebug()<<"opened ok";
sp->close();
}

hiuao
17th April 2007, 05:14
Your code is messy.
So lets try to go step by step.
Oh and make sure you have rights to read and write to the serial ports.
Try this:


sp = new QextSerialPort;
sp->setBaudRate( QextSerialPort::BAUD9600);
sp->setParity( QextSerialPort::PAR_NONE);
sp->setDataBits( QextSerialPort:: DATA_8);
sp->setStopBits( QextSerialPort::STOP_1);
sp->setName("/dev/ttyS0");
if( !(sp->open(QIODevice::ReadWrite)))
{
qDebug()<<"could not open"<<sp->name();
}
if(sp->isOpen())
{
qDebug()<<"opened ok";
sp->close();
}

Oh,high_flyer,there is no wrong when I compile it.The "error" appears when I run the program.And I have done as you said,but when I run the program it is waiting,nothing appears.The day before yesterday,it can run nevertheless it can't read data.

hiuao
17th April 2007, 05:21
I have opened it!But still I can't read data.When I run the program it shows:
opened ok
+++ Created first
+++ Created second
ReadLineTest
-------------
+++ Opened first
+++ Opened second
+++ Transmitted
then waiting here.

bool MySerialPort3::WriteReadTest()
{
bool result = FALSE;
sp1 = new QextSerialPort("/dev/ttyS0");
qDebug("+++ Created first");
sp1->setBaudRate( QextSerialPort::BAUD9600);
sp1->setParity( QextSerialPort::PAR_NONE);
sp1->setDataBits( QextSerialPort:: DATA_8);
sp1->setStopBits( QextSerialPort::STOP_1);

sp2 = new QextSerialPort("/dev/ttyS0");
qDebug("+++ Created second");
sp2->setBaudRate( QextSerialPort::BAUD9600);
sp2->setParity( QextSerialPort::PAR_NONE);
sp2->setDataBits( QextSerialPort:: DATA_8);
sp2->setStopBits( QextSerialPort::STOP_1);
qDebug("ReadLineTest");
qDebug("-------------");

if( sp1->open(QIODevice::ReadWrite) )
{
qDebug("+++ Opened first");

if( sp2->open(QIODevice::ReadWrite) )
{
qDebug("+++ Opened second");

char data[] = { 0x00, 0x7F, 0x0A, 0xFF, 0x00, 0x0D, 0x7F, 0x80, 0xFF };
if( sp1->writeBlock( data, 8 ) == 8 )
{
qDebug("+++ Transmitted");

char r_data[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0xAB};
while( sp2->bytesAvailable() < 8 );
if( 4 == sp2->readLine( r_data, 5 )
&& (uchar)r_data[0] == 0x00
&& (uchar)r_data[1] == 0x7F
&& (uchar)r_data[2] == 0x0A
&& (uchar)r_data[3] == 0x00
&& 6 == sp2->readLine( r_data, 6 )
&& (uchar)r_data[0] == 0xFF
&& (uchar)r_data[1] == 0x00
&& (uchar)r_data[2] == 0x0D
&& (uchar)r_data[3] == 0x7F
&& (uchar)r_data[4] == 0x80
&& (uchar)r_data[5] == 0x00 )
{
qDebug("+++ Received");
result = TRUE;
} else {
qDebug("--- Could not receive");
printf("the read data = %s\n",r_data);
}
} else {
qDebug("--- Could not transmit");
}
} else {
qDebug("--- Could not open second");
}

sp1->close();
qDebug("+++ Closed first");
sp2->close();
qDebug("+++ Closed second");
}
else {
qDebug("--- Could not open first");
}

return result;
}

high_flyer
17th April 2007, 10:54
while( sp2->bytesAvailable() < 8 );

This is bad practice, since you are "chocking" the CPU.
The reason it waits here is either the while loop maked the CPU so busy it never tends to see if there are new bytes available, or, there are really no new bytes available.
Use minicom or similar to make sure that the sending port does indeed sends data.
And I would use a QTimer to poll the serial port for available bytes.
Oh - and PLEASE use code tags for code! it is really hard to read the code this way.

hiuao
17th April 2007, 13:00
while( sp2->bytesAvailable() < 8 );

This is bad practice, since you are "chocking" the CPU.
The reason it waits here is either the while loop maked the CPU so busy it never tends to see if there are new bytes available, or, there are really no new bytes available.
Use minicom or similar to make sure that the sending port does indeed sends data.
And I would use a QTimer to poll the serial port for available bytes.
Oh - and PLEASE use code tags for code! it is really hard to read the code this way.
Thank you high_flyer for your advice!I use
int len = sp1->write( data, 4 );
int len2=sp2->read(r_data,4);
find out it writes 4 chars but reads 0 chars.What can I do?

high_flyer
17th April 2007, 13:16
It could be that the amount of data is smaller than the inner serial buffer.
try using flush() after you write or try writing more data.

hiuao
17th April 2007, 13:42
It could be that the amount of data is smaller than the inner serial buffer.
try using flush() after you write or try writing more data.

The result is the same as above.I think there is something wrong with my MOXA card's driven function.I plan to reinstall it.Because only "/dev/ttyS0" can write data,am I right?

high_flyer
17th April 2007, 13:51
did you try to use some teminal application like minicom?
Can you show your current code?

hiuao
17th April 2007, 13:58
did you try to use some teminal application like minicom?
Can you show your current code?

No,I don't use any other terminal,the above is the whole code.I just test it, hopping it can read and write data.

high_flyer
17th April 2007, 14:05
I suugest you first test your serial ports using a terminal program.
This will allow you to know if there is a problem with your hardware.
If you can read/write to your ports with a terminal program then you know the problem is in your code.
Did you make sure you have read and write rights to the /dev/ttySX?

hiuao
17th April 2007, 14:55
I suugest you first test your serial ports using a terminal program.
This will allow you to know if there is a problem with your hardware.
If you can read/write to your ports with a terminal program then you know the problem is in your code.
Did you make sure you have read and write rights to the /dev/ttySX?
Thank you high_flyer.I accept your advice,I will use minicom for testing.
Thank you very much!;)