PDA

View Full Version : How can I use QextSerialPort on two different serial ports in the same time?



inger
10th August 2011, 08:10
In my program I want to communicate with two different com ports. From one of them I get alarm signals. This works Ok.:)
But now I try to send and receive data to a GSM terminal for sending SMS messages. I am not able to get response from the terminal to my computer.:(
Do you know if I can use QextSerialPort to communicate with several serial ports in the same program? I use QWidget as base.

Lesiok
10th August 2011, 09:18
Yes, You can create many instances of QExtSerialPort as You need.

inger
11th August 2011, 10:27
I still do not get any answer from com port number two(GSMcomm). My code is like:
The constructor:

EikeTesten::EikeTesten(QWidget *parent) :
QWidget(parent)
{ ....
qDebug("Creating QextSerialPort instance");
CommPort = new QextSerialPort();
CommPort->setPortName(QString("/dev/ttyUSB0"));
CommPort->setDataBits(DATA_8);
CommPort->setParity(PAR_NONE);
CommPort->setFlowControl(FLOW_OFF);
CommPort->setStopBits(STOP_1);
CommPort->setBaudRate(BAUD300);
CommPort->open();
QTimer *receiveTimer = new QTimer(this);
connect(receiveTimer, SIGNAL(timeout()), SLOT(receiveMsg())); // receiveMsg get the message from ttyUSB0 and call saveData();
receiveTimer->start(0);
..
}

in saveData:

{ ....
GSMcomm = new GSMkommunikasjon();
GSMcomm -> sendAlarmSMS(text);
....}

in GSMkommunikasjon constructor:

{/*create and init communications port*/
qDebug("Creating SMSCommPort instance");
SMSCommPort = new QextSerialPort();
SMSCommPort->setPortName(QString("/dev/ttyUSB1"));
SMSCommPort->setDataBits(DATA_8);
SMSCommPort->setParity(PAR_NONE);
SMSCommPort->setFlowControl(FLOW_OFF);
SMSCommPort->setStopBits(STOP_1);
SMSCommPort->setBaudRate(BAUD19200);
SMSCommPort->open();
SMSCommPort->setDtr(0);
SMSCommPort->setDtr(1);
//SMSCommPort->close();
//sendAlarmSMS();

QTimer *receiveTimer2 = new QTimer(this);
connect(receiveTimer2, SIGNAL(timeout()), SLOT(receiveFromSMS()));
receiveTimer2->start(0);
}

in receiveFromSMS()
{
int numBytes = SMSCommPort->bytesAvailable();
if(numBytes > 0)
{
if (numBytes > 80) numBytes = 80;
char buff[80];
int i = SMSCommPort->readData(buff, numBytes);
buff[numBytes] = '\0';
QChar byte0 = buff[0];
QChar byte1 = buff[1];
}}
... // But the program never receive anything
}

Can you please help me?

marcvanriet
11th August 2011, 13:01
Just the basic checks...
- is the baudrate correct
- if you have a scope or so, do you see the DTR line toggling when you open the port ?
- try to connect something else to that port and see if that works
- put a loopback wire on the port (connect RX and TXT) and then send something in your program. It should be received back

Regards,
Marc

nix
11th August 2011, 14:00
Hi,

Maybe it's not the matter, but it seems to me that you start 2 timers with a 0 interval time.
Is it correct? Your CPU should run at full charge, maybe that cause some trouble in the reception? Have you try with a reasonable time interval (250ms for instance)?

Loopback the wire can tell you if you're wrong on the protocol/in the serial configuration or if it's a software problem.

inger
11th August 2011, 15:16
Thank you for your feedback:). I have tried to change the GSM terminal with something other, and then I got some input. The baudrate is correct. So something in my code is a little tricky:(.
I will check the timers:)