[英]Sending images over TCP from labVIEW to QT
I am trying to capture images taken from a camera connected to a myRIO and send them over a TCP/IP connection from labVIEW to a QT GUI application. 我正在尝试捕获从连接到myRIO的相机拍摄的图像,并通过TCP / IP连接将它们从labVIEW发送到QT GUI应用程序。
My problem is that QT keeps throwing a heap pointer exception and crashing when I read the data. 我的问题是QT在读取数据时不断抛出堆指针异常并崩溃。 Expression: is_block_type_valid(header->_block_use)
表达式:is_block_type_valid(header-> _ block_use)
I believe this could be because the data being sent is over 35k bytes, so I tried to read the data in separate chunks, but alas am still getting the error. 我相信这可能是因为正在发送的数据超过35k字节,所以我尝试以单独的块读取数据,但是可惜仍然出现错误。
Below is my function that gets called on readyRead() being emitted: 以下是我发出的readyRead()调用的函数:
void TCPHandler::onRead() {
QByteArray byteArray;
QByteArray buffer;
QByteArray dataSize = mainSocket->read(5); //read the expected amount of bytes incoming (about 35000)
while (buffer.size() < dataSize.toInt()) {
int bytesLeft = dataSize.toInt() - buffer.size();
if (bytesLeft < 1024) {
byteArray = mainSocket->read(bytesLeft);
}
else {
byteArray = mainSocket->read(1024);
}
buffer.append(byteArray);
}
QBuffer imageBuffer(&buffer);
imageBuffer.open(QIODevice::ReadOnly);
QImageReader reader(&imageBuffer, "JPEG");
QImage image;
if(reader.canRead())
image = reader.read();
else {
emit read("Cannot read image data");
}
if (!image.isNull())
{
image.save("C:/temp");
}
else
{
emit read(reader.errorString());
}}
In the LabVIEW code I send the size of the bytes being sent first, then the raw image data: 在LabVIEW代码中,我先发送要发送的字节大小,然后发送原始图像数据:
EDIT: Connect for the slot. 编辑:连接插槽。 Also should have mentioned this is running in a separate thread to the Main GUI.
还应该提到,这是在主GUI的单独线程中运行的。
TCPHandler::TCPHandler(QObject *parent)
: QObject(parent),
bytesExpected(0)
{
mainSocket = new QTcpSocket(this);
connect(mainSocket, SIGNAL(readyRead()), this, SLOT(onRead()));
connect(mainSocket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error), this, &TCPHandler::displayError);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.