我有一个相机,我想把数据发送给它,但是首先我必须把相机的软件(它在我的笔记本电脑上)和我的软件连接起来,所以我需要打开两个插座,获取数据from.the软件,然后把它发送到相机。
sockettest.h:
class SocketTest : public QObject{
Q_OBJECT
public:
explicit SocketTest(QObject *parent = nullptr);
bool start_listen(int);
QTcpSocket *blutechnix;
QTcpSocket *tof;
QTcpServer *server;
void incomingConnection();
signals:
public slots:
void Connect();
private:
};
SocketTest::SocketTest(QObject *parent) : QObject(parent){
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()),this, SLOT(Connect()));
tof = new QTcpSocket(this);
tof->connectToHost("192.168.0.10",10001);
if(tof->waitForConnected(3000))
{
qDebug() << "tof connected";
if(server->listen(QHostAddress::LocalHost, 10000))
{
qDebug() << "Listening...";
qDebug() << server->serverAddress();
qDebug() << server->serverPort();
}
}
else
{
qDebug() << "tof connexion failed";
}
tof->close();
}
sockettest.h:
void SocketTest::Connect(){
qDebug() << "CONNECTED";
blutechnix = server->nextPendingConnection();
blutechnix->waitForReadyRead();
QByteArray array =blutechnix->read(blutechnix->bytesAvailable());
qDebug() << array;
//send all data to the camera
tof->write(array);
tof->waitForBytesWritten();
//make sure that we wrote the right bytes
tof->waitForReadyRead(3000);
qDebug() << "Reading tof" << tof->bytesAvailable();
qDebug() << tof->readAll();
}
void SocketTest::incomingConnection() {
if( ! blutechnix->setSocketDescriptor(server->socketDescriptor()) ) {
QMessageBox::warning( (QWidget *)this->parent(), tr("Error!"), tr("Socket
error!") );
return;
}
}我运行了代码,结果是:
连听..。QHostAddress<"127.0.0.1"> 10000
为什么我看不见数据?为什么不执行Connect()函数?我希望你能帮我。
发布于 2017-08-06 12:11:29
服务器正在侦听端口10000上的本地主机的连接。您可能想要的是连接到tof上的tof信号,并使用tof->readAll()从那里读取数据,并使用tof.write(QByteArray)编写数据。
您不需要QTcpServer,除非您有另一个愿意与您建立连接的程序(即您是服务器)。这是摄像头软件的工作。
https://stackoverflow.com/questions/45460443
复制相似问题