1

我正在尝试用 QT c++ 编写 GPS 模拟器。它将用于将数据发送到 Traccar 跟踪应用程序。Traccar 使用 localhost 端口 5055 接收数据包并使用数据包中的数据绘制位置。我没有成功让 traccar 读取数据包。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 可以工作,因为我可以从经过解码和绘制的 Web 浏览器发送数据。

任何建议将不胜感激。

4

1 回答 1

1

试试这个。它对我的工作:

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;

    }
}
于 2020-01-22T18:11:53.487 回答