c++ - 使用Qt模拟gps数据以与traccar一起使用

标签 c++ qt

我正在尝试使用QT c++编写GPS模拟器。它将用于将数据发送到Traccar跟踪应用程序。 Traccar使用localhost端口5055接收数据包,并使用数据包中的数据绘制位置。我无法成功地通过追踪车读取数据包。 Traccar希望看到HTML格式的数据包。我不确定我的程序是否正确格式化了数据。以// \开头的所有行都显示了我尝试过的内容。

这是将数据写入loalhost端口5055的程序的一部分:

// write to localhost if timer seconds is greater than or equal to desired time
if(scnds >= ui -> updateBox -> text().toDouble()) { 
 //\\   QString sendString = "/?id=" + id$ + "&Lat=" +lat$ + "&Lon=" + lon$ + "&Speed=" + speed$ + "&Course=" + course$ + "\r\n\r\n\r\n\r\n";
 //\\   QString sendString = "/?id=" + id$ + "&Lat=" +lat$ + "&Lon=" + lon$ + "&Speed=" + speed$ + "&Course=" + course$;
 //\\   QString sendString = "id=" + id$ + "&Lat=" +lat$ + "&Lon=" + lon$ + "&Speed=" + speed$ + "&Course=" + course$ + "\r\n\r\n\r\n\r\n";
 //\\   QString sendString = "id=" + id$ + "&Lat=" +lat$ + "&Lon=" + lon$ + "&Speed=" + speed$ + "&Course=" + course$;
    QString sendString = "/?id=123456&lat=40.730610&lon=-73.935600&speed=100&Course=45\r\n\r\n\r\n\r\n";
 //\\   QString sendString = "/?id=123456&lat=40.730610&lon=-73.935600&speed=100&Course=45";
 //\\   QString sendString = "id=123456&lat=40.730610&lon=-73.935600&speed=100&Course=45\r\n\r\n\r\n\r\n";
 //\\   QString sendString = "id=123456&lat=40.730610&lon=-73.935600&speed=100&Course=45";
    QByteArray gpsdata = sendString.toUtf8();//     .toLocal8Bit(); .toUtf8(); .toLatin1();    
    socket = new QTcpSocket(this);
    socket -> connectToHost("localhost",5055);
    if (socket -> waitForConnected(3000)){
        ui -> receivedMessage -> append("Connected");
        socket -> write(gpsdata);
        socket -> waitForBytesWritten(1000);
        socket -> waitForReadyRead(3000);
        ui -> receivedMessage -> append(QString::number(socket -> bytesAvailable()) + " ");
        ui -> receivedMessage -> append(socket -> readAll());
        socket -> close();
        ui -> sentMessage -> setText(gpsdata);
    }
    else {
        ui -> receivedMessage -> append("Not connected");
        ui -> timerLabel -> setNum(0);
    }

tracca日志显示以下内容:
2020-01-18 18:36:41  INFO: [afa16fa9] connected
2020-01-18 18:36:41  INFO: [afa16fa9: osmand < 0:0:0:0:0:0:0:1] HEX: 2f3f69643d313233343536266c61743d33322e35343738266c6f6e3d2d39392e373435322674696d657374616d703d302668646f703d3526616c7469747564653d3130302673706565643d31303020485454502f312e31200d0a0d0a0d0a0d0a
2020-01-18 18:36:41  INFO: [afa16fa9: osmand > 0:0:0:0:0:0:0:1] HEX: 485454502f312e31203430302042616420526571756573740d0a636f6e74656e742d6c656e6774683a20300d0a0d0a
2020-01-18 18:36:41  INFO: [afa16fa9] disconnected

如您所见,traccar日志显示该程序正在连接,但数据尚未解码。我知道Traccar的工作原理是因为我可以从网络浏览器发送经过解码和绘制的数据。

任何建议,将不胜感激。

最佳答案

试试这个。它为我工作:

void MainWindow::on_pushButton_3_clicked()
{
    QString sendString = "/id=123456&lat=50.030610&lon=33.335600&speed=100&Course=45";
    QUrl theurl;
    theurl = "http://127.0.0.1:5055";

    QByteArray gpsdata = sendString.toUtf8();
    qDebug()<<gpsdata;
    socket = new QTcpSocket(this);

    socket -> connectToHost(theurl.host(),5055);
    if (socket -> waitForConnected(3000)){
        socket->write("GET /?id=123456&lat=50.030610&lon=33.335600&speed=100&Course=45 HTTP/1.1\r\n"
                      "host: " + theurl.host().toUtf8() + "\r\n\r\n");
        socket -> waitForBytesWritten(1000);
        socket -> waitForReadyRead(1000);
        QByteArray ba;
        ba.append(socket -> readAll());
        socket -> close();
        qDebug()<<ba;

    }
}

关于c++ - 使用Qt模拟gps数据以与traccar一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59833375/

相关文章:

c++ - 置换函数和交换字符串值的问题

c++ - 从 Outlook 对待办事项/任务/约会执行 CRUD?

C++ 最终类和切片习惯用法

c++ - windows下的Qt Application和window Icon

c++ - Qt : How to get all the classes that are children of a specific class

mysql - Qt 上的 NOW() 函数

c++ - 在Qt中测试Lambda唯一连接