42 #include "babymegclient.h"
50 #include <QtNetwork/QtNetwork>
58 connect(
this,SIGNAL(DataAcq()),
this,SLOT(run()));
59 tcpSocket =
new QTcpSocket(
this);
61 connect(tcpSocket,SIGNAL(readyRead()),
this,SLOT(
ReadToBuffer()));
64 connect(
this, SIGNAL(error(
int,QString)),
65 this, SLOT(DisplayError(
int,QString)));
66 name = QHostInfo::localHostName();
69 QString domain = QHostInfo::localDomainName();
70 if (!domain.isEmpty())
71 name = name + QChar(
'.') + domain;
73 if (name!=QString(
"localhost"))
74 name = QString(
"localhost");
75 qDebug()<<
"- " + name;
77 SocketIsConnected =
false;
79 DataAcqStartFlag =
false;
87 BabyMEGClient::~BabyMEGClient()
96 myBabyMEGInfo = pInfo;
101 void BabyMEGClient::DisplayError(
int socketError,
const QString &message)
103 switch (socketError){
104 case QAbstractSocket::RemoteHostClosedError:
106 case QAbstractSocket::HostNotFoundError:
107 qDebug()<<
"The host was not found. Please check the host name and the port number";
109 case QAbstractSocket::ConnectionRefusedError:
110 qDebug()<<
"The connection was refused by the peer. Make sure the server is running?";
113 qDebug()<<
"Error: " << message;
122 for (
int i=0;i<2;i++)
129 memcpy((
char *)&value,b,4);
138 QByteArray b = QByteArray::fromRawData((
char *)&a,4);
140 for (
int i=0;i<2;i++)
157 for (
int i=0;i<4;i++)
164 memcpy((
char *)&value,b,8);
174 QByteArray data = QByteArray::fromRawData((
char *)&a,8);
175 qDebug() << data.toHex();
185 SocketIsConnected =
false;
187 qDebug()<<
"Client is started!";
189 if (!this->isRunning())
194 for(
int i=0;i<5;i++){
195 tcpSocket->connectToHost(name,port,QIODevice::ReadWrite);
196 if (tcpSocket->waitForConnected(5000))
198 SocketIsConnected =
true;
199 qDebug(
"Connect to BabyMEG Server ... Ok");
201 qDebug()<<
"Send the initial parameter request";
202 if (tcpSocket->state()==QAbstractSocket::ConnectedState)
211 qDebug(
"Connect to BabyMEG Server ... Fail");
212 qDebug(
"Try another time connection");
214 qDebug(
"Please check the babyMEG server: if started");
224 if(SocketIsConnected && tcpSocket->state()==QAbstractSocket::ConnectedState)
233 qDebug() <<
"SendCommandToBabyMEGShortConnection";
234 tcpSocket->connectToHost(name,port,QIODevice::ReadWrite);
235 if (tcpSocket->waitForConnected(10000))
238 qDebug()<<
"Connection is built.";
240 if(tcpSocket->state()==QAbstractSocket::ConnectedState)
242 qDebug()<<
"Send String [" << s <<
"]";
249 tcpSocket->waitForBytesWritten();
260 qDebug()<<
"Send Command";
261 if(SocketIsConnected && tcpSocket->state()==QAbstractSocket::ConnectedState)
264 tcpSocket->write(
"COMD");
265 tcpSocket->waitForBytesWritten();
270 tcpSocket->write(Scmd);
271 tcpSocket->write(
"SLM");
272 tcpSocket->waitForBytesWritten();
286 int numBytes = tcpSocket->bytesAvailable();
289 dat = tcpSocket->read(numBytes);
297 qDebug()<<
"[Empty dat: error]"<<tcpSocket->errorString();
310 if(buffer.size()>= 8){
311 QByteArray CMD = buffer.left(4);
312 QByteArray DLEN = buffer.mid(4,4);
318 if (tmp <= (buffer.size() - 8))
326 else if (CMD ==
"DATR")
328 else if (CMD ==
"COMD")
330 else if (CMD ==
"QUIT")
332 else if (CMD ==
"COMS")
334 else if (CMD ==
"QUIS")
341 QByteArray PARA = buffer.left(tmp);
342 qDebug()<<
"[INFO]"<<PARA;
345 buffer.remove(0,tmp);
346 qDebug()<<
"INFO has been received!!!!";
361 QByteArray RESP = buffer.left(tmp);
362 qDebug()<<
"5.Readbytes:"<<RESP.size();
365 buffer.remove(0,tmp);
372 tcpSocket->disconnectFromHost();
373 if(tcpSocket->state() != QAbstractSocket::UnconnectedState)
374 tcpSocket->waitForDisconnected();
375 SocketIsConnected =
false;
376 qDebug()<<
"Disconnect Server";
377 qDebug()<<
"Client is End!";
378 qDebug()<<
"You can close this application or restart to connect Server.";
383 QByteArray RESP = buffer.left(tmp);
384 qDebug()<<
"5.Readbytes:"<<RESP.size();
388 buffer.remove(0,tmp);
395 tcpSocket->disconnectFromHost();
396 if(tcpSocket->state() != QAbstractSocket::UnconnectedState)
397 tcpSocket->waitForDisconnected();
398 SocketIsConnected =
false;
399 qDebug()<<
"Disconnect Server";
403 qDebug()<<
"Unknow Type";
417 QByteArray DATA = buffer.left(tmp);
418 qDebug()<<
"5.Readbytes:"<<DATA.size();
421 buffer.remove(0,tmp);
424 qDebug()<<
"Next Block ..." << numBlock;
438 while (buffer.size()>=(tmp+8))
440 CMD1 = buffer.left(4);
441 qDebug()<<
"CMD"<< CMD1;
444 DLEN1 = buffer.mid(4,4);
446 qDebug() <<
"[2]First 4 bytes + length" << CMD1 <<
"["<<CMD1.toHex()<<
"]";
447 qDebug() <<
"[2]Command[" << CMD1 <<
"]";
448 qDebug() <<
"[2]Body Length[" << tmp1 <<
"]";
451 DATA1 = buffer.left(tmp1);
454 buffer.remove(0,tmp1);
455 qDebug()<<
"End of DataPackeage" << buffer.left(3);
456 qDebug()<<
"[2]Rest buffer [buffer size() =" << buffer.size()<<
"]";
458 qDebug()<<
"[2]Next Block ..." << numBlock;
462 qDebug()<<
"[CMD1]"<<CMD1.toHex();
465 qDebug()<<
"[ReadNextBlock:buffer size]"<<buffer.size();
481 if (tcpSocket->state()==QAbstractSocket::ConnectedState)
486 WrtNum = tcpSocket->write(array,4);
489 qDebug()<<
"Error for sending a command";
491 if(WrtNum != array.size())
493 qDebug()<<
"Uncorrectly sending";
496 tcpSocket->waitForBytesWritten();
498 qDebug()<<
"[Done: Send Command]"<<array<<
"[Send bytes]"<<WrtNum;
503 qDebug()<<
"Not in Connected state";
515 void BabyMEGClient::run()
BabyMEGClient(int myPort, QObject *parent=0)
void MGH_LM_Parse_Para(QByteArray cmdstr)
void SendCommandToBabyMEG()
The BabyMEGClient class provides a TCP/IP communication between Qt and Labview.
void ReadNextBlock(int tmp)
void HexDisplay(double a)
void SendCommandToBabyMEGShortConnection(QByteArray s)
void SetInfo(BabyMEGInfo *pInfo)
double MGH_LM_Byte2Double(QByteArray InByte)
int MGH_LM_Byte2Int(QByteArray InByte)
void SendCommand(QString s)
QByteArray MGH_LM_Int2Byte(int a)
void DispatchDataPackage(int tmp)
void MGH_LM_Send_DataPackage(QByteArray DATA)
void MGH_LM_Send_CMDPackage(QByteArray DATA)