#include "ntrip_client.h" #include ntrip_client::ntrip_client(std::string strip,std::string strport,std::string strMountPoint, std::string strUserName,std::string strPassWord, std::string strCOMName,int nBaudRate ) { mstrip = strip; mstrport = strport; mstrMountPoint = strMountPoint; mstrUserName = strUserName; mstrPassWord = strPassWord; mstrCOMName = strCOMName; mnBaudRate = nBaudRate; socket_ = new QTcpSocket(this); // socket_->connectToHost("rtk.ntrip.qxwz.com",8002); connect(socket_, SIGNAL(connected()), this, SLOT(connectedSlot())); connect(socket_, SIGNAL(disconnected()), this, SLOT(disconnectedSlot())); connect(socket_, SIGNAL(readyRead()), this, SLOT(readyReadSlot())); connect(socket_, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(errorSlot(QAbstractSocket::SocketError))); mpTimerNtrip = new QTimer(this); connect(mpTimerNtrip,SIGNAL(timeout()),this,SLOT(onTimerNtrip())); mpTimerNtrip->setInterval(1000); mpTimerNtrip->start(); mpTimerPort = new QTimer(this); connect(mpTimerPort,SIGNAL(timeout()),this,SLOT(onTimerPortOpen())); mpTimerPort->start(100); } void ntrip_client::run() { } void ntrip_client::connectedSlot() { if(socket_->state() == QAbstractSocket::ConnectedState) { qDebug("main is connected.\n"); mFindCMState = 3; } else { qDebug("main is not connected.\n"); } isConnected_ = true; } void ntrip_client::disconnectedSlot() { // qDebug("connect server fail."); if(mFindCMState != -1)mFindCMState = 0; isConnected_ = false; socket_->close(); } void ntrip_client::readyReadSlot() { char * str; QByteArray message = socket_->readAll(); // qDebug(message.data()); switch (mFindCMState) { case 4: str = (char *)message.data(); if(strstr(str,"ICY 200 OK") > 0) { mFindCMState = 5; qDebug("Login succcess"); } else { if(strstr(str,"HTTP 401 Unauthorized") > 0) { mFindCMState = -1; qDebug("Authorize Fail. message is %s",message.data()); } // mFindCMState = 0; } break; case 5: #ifdef DEBUG_NTRIP_MSG ShowNtripMsg(message); #else qDebug("ntrip msg len is %d",message.length()); #endif if(mbPortOpen) { if(-1 == m_serialPort_RTK->write(message)) { qDebug("Write Serial Port Fail."); m_serialPort_RTK->close(); mbPortOpen = false; mpTimerPort->start(100); } } break; default: break; } // qDebug("len = %d",message.length()); /* char strX[3000],strX2[10]; int i; int nLen = message.length(); snprintf(strX,3000,""); for(i=0;ierrorString()); disconnectedSlot(); } void ntrip_client::onTimerNtrip() { switch (mFindCMState) { case -1: mpTimerPort->stop(); mpTimerNtrip->stop(); qDebug("User Pass Word Error."); if(mbPortOpen)m_serialPort_RTK->close(); break; case 0: socket_->connectToHost(mstrip.data(),atoi(mstrport.data())); mFindCMState = 1; break; case 2: break; case 3: SendUserPass(); mFindCMState = 4; break; case 4: break; case 5: SendGPGGA(); break; default: break; } } void ntrip_client::SendUserPass() { char strSend[1000]; // snprintf(strSend,1000,"GET / HTTP/1.0\r\nUser-Agent: NTRIP GNSSInternetRadio/1.4.10\r\nAccept: */*\r\nConnection: close\r\n\r\n"); char strLine[100]; char strX[1000]; snprintf(strX,1000,"%s:%s",mstrUserName.data(),mstrPassWord.data()); QByteArray ba; ba.append(strX);qDebug(ba.toBase64().data()); snprintf(strSend,1000,"GET /%s HTTP/1.0\r\n",mstrMountPoint.data()); snprintf(strLine,100,"User-Agent: NTRIP GNSSInternetRadio/1.4.10\r\n");strncat(strSend,strLine,1000); snprintf(strLine,100,"Accept: */*\r\n");strncat(strSend,strLine,1000); snprintf(strLine,100,"Connection: close\r\n");strncat(strSend,strLine,1000); snprintf(strLine,100,"Authorization: Basic %s\r\n",ba.toBase64().data());strncat(strSend,strLine,1000); snprintf(strLine,100,"\r\n");strncat(strSend,strLine,1000); socket_->write(strSend,strlen(strSend)); socket_->flush(); } void ntrip_client::SendGPGGA() { if(mbPortOpen) { QByteArray ba = m_serialPort_RTK->read(1000); if(ba.length() > 0) { socket_->write(ba); socket_->flush(); } } else { #ifdef DEBUG_NTRIP_MSG char strSend[1000]; snprintf(strSend,1000,"$GPGGA,102342.00,3948.6248006,N,11633.1924828,E,1,13,1.5,46.4085,M,-9.452,M,99,AAAA*72\r\n"); socket_->write(strSend,strlen(strSend)); socket_->flush(); #endif } /* char strSend[1000]; snprintf(strSend,1000,"$GPGGA,102342.00,3948.6248006,N,11633.1924828,E,1,13,1.5,46.4085,M,-9.452,M,99,AAAA*72\r\n"); socket_->write(strSend,strlen(strSend)); socket_->flush();*/ } void ntrip_client::onTimerPortOpen() { if(mbPortOpen)return; m_serialPort_RTK = new QSerialPort(); m_serialPort_RTK->setPortName(mstrCOMName.data()); m_serialPort_RTK->setBaudRate(mnBaudRate); m_serialPort_RTK->setParity(QSerialPort::NoParity); m_serialPort_RTK->setDataBits(QSerialPort::Data8); m_serialPort_RTK->setStopBits(QSerialPort::OneStop); m_serialPort_RTK->setFlowControl(QSerialPort::NoFlowControl); m_serialPort_RTK->setReadBufferSize(0); if(m_serialPort_RTK->open(QSerialPort::ReadWrite)) { mbPortOpen = true; mpTimerPort->stop(); } else { qDebug("Open RTK Port Fail. Port Name is %s BaudRate is %d ",mstrCOMName.data(),mnBaudRate); mbPortOpen = false; delete m_serialPort_RTK; mpTimerPort->setInterval(10000); } } void ntrip_client::ShowNtripMsg(QByteArray ba) { if(ba.size()<1)return; char * strout = 0; std::shared_ptr str_ptr = std::shared_ptr(new char[ba.size()*3 + 100]); strout = str_ptr.get(); snprintf(strout,3000,"Ntrip msg len is %d info is: ",ba.length()); char strtem[10]; int nlen = ba.length(); int i; for(i=0;i