while循环,出现QSocketNotifier: Can only be used with threads started with QThread

星辰韦 2015-11-17 11:40:07
写了个图像处理+写串口的qmake工程,编译没有错误,但是一运行到while(true),就会出现'QSocketNotifier: Can only be used with threads started with QThread',,,而且写串口不成功,外置设备收不到指令。
求教大神

把程序都贴在下面了,感觉是main函数的问题,因为2个类已经在别的程序中验证过了应该没问题
vp开头的函数是个第三方库

/*****main.cpp*****/
#include <iostream>
#include "UEyeOpenCV.hpp"
#include "opencv2/opencv.hpp"
#include <servocontroller.h>

#include <visp/vpPlot.h>
#include <visp/vpDot2.h>
#include <visp/vpDisplay.h>
#include <visp/vpImageIo.h>
#include <visp/vpImageTools.h>
#include <visp/vpDisplayOpenCV.h>

int main(){
/*导入opencv相机校正参数*/
cv::FileStorage fs("out_camera_data.xml", cv::FileStorage::READ);
cv::Mat cameraMatrix, distCoeffs;
fs["Camera_Matrix"] >> cameraMatrix;
fs["Distortion_Coefficients"] >> distCoeffs;
std::cout << "camera matrix: " << cameraMatrix << std::endl
<< "distortion coeffs: " << distCoeffs << std::endl;
cv::Mat image;

/*打开相机*/
UeyeOpencvCam cam = UeyeOpencvCam(752,480);

/*定义显示窗口*/
vpImage<unsigned char> I(480, 752); // Create a gray level image
cv::Mat grayFrame;
vpDisplayOpenCV d;
d.init(I, 0, 0, "Visual servoing on dot");

/*定义追踪点*/
vpDot2 blob;
blob.setGraphics(true);
blob.setGraphicsThickness(2);
vpImagePoint germ;
bool init_done = false;

/*设置com口*/
unsigned short target=1500;
ServoController com;
com.ServoController::createSerialPort();
com.setTarget(target*4);

while(true) {
cv::Mat frame= cam.getFrame(); //取每帧的图像
cv::cvtColor(frame,grayFrame, CV_BGR2GRAY); //转灰度图
cv::undistort(grayFrame,image,cameraMatrix,distCoeffs); //图像校正
cv::Mat image2;
cv::flip(image,image2,-1); //图像倒置
vpImageConvert::convert(image2, I); //转成ViSP库的图像
vpDisplay::display(I); //ViSP库的图像显示

// 等待鼠标点击目标点
if (! init_done) {
vpDisplay::displayText(I, vpImagePoint(10,10), "Click in the blob to initialize the tracker", vpColor::red);
if (vpDisplay::getClick(I, germ, false)) {
blob.initTracking(I, germ);
init_done = true;
}
}
else {
blob.track(I); //点追踪
vpImagePoint mcog; //点中心坐标
mcog=blob.getCog();
int x=mcog.get_i();
if (x<240){
target=target+1;
com.setTarget(target*4);
cv::waitKey(10);
}
else{
target=target-1;
com.setTarget(target*4);
cv::waitKey(10);
}
}
vpDisplay::flush(I);
}
}

//servocontroller.h
/*****servocontroller.h*****/
#ifndef SERVOCONTROLLER_H
#define SERVOCONTROLLER_H

#include <string>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <termios.h>
#include <QDebug>
#include <QString>

class ServoController
{
public:
virtual ~ServoController();
void createSerialPort();
void setTarget( unsigned short target );

private:
static const unsigned short mMinChannelValue = 4000;
static const unsigned short mMaxChannelValue = 8000;
unsigned short target;
unsigned char channelNumber;
QSerialPort* myCom;
QByteArray command;
};

#endif // SERVOCONTROLLER_H

/*****servocontroller.cpp*****/
#include "servocontroller.h"
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <iostream>

void ServoController::createSerialPort()
{
myCom= new QSerialPort();
myCom->setPortName("/dev/ttyACM0");
myCom->open(QIODevice::WriteOnly);
myCom->setBaudRate(9600);
myCom->setDataBits(QSerialPort::Data8);
myCom->setParity(QSerialPort::NoParity);
myCom->setStopBits(QSerialPort::OneStop);
myCom->setFlowControl(QSerialPort::NoFlowControl);
}

ServoController::~ServoController()
{
myCom->close();
}

void ServoController::setTarget(unsigned short target )
{
if (target<mMinChannelValue ){
target=mMinChannelValue;
}
if(target>mMaxChannelValue){
target=mMaxChannelValue;
}

command.resize(4);
command[0]=0x84;
command[1]=0x01;
command[2]=(char)(target & 0x7F);
command[3]=(char)((target >> 7) & 0x7F);
myCom-> QSerialPort::write(command);
}

/*****UEyeOpenCV.hpp*****/
#ifndef UEYEOPENCV_HPP
#define UEYEOPENCV_HPP

//#pragma once
#include <ueye.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

class UeyeOpencvCam {
public:
UeyeOpencvCam(int wdth, int heigh);
cv::Mat getFrame();

private:
HIDS hCam;
cv::Mat mattie;
int width;
int height;
};

#endif // UEYEOPENCV_HPP

/*****ueyeopencv.cpp*****/
#include <UEyeOpenCV.hpp>
#include <iostream>
#include <ueye.h>

UeyeOpencvCam::UeyeOpencvCam(int wdth, int heigh) {
width = wdth;
height = heigh;
mattie = cv::Mat(height, width, CV_8UC3);
hCam = 1;
char* ppcImgMem;
int pid;
INT nAOISupported = 0;
is_InitCamera(&hCam, NULL);
is_SetColorMode(hCam, IS_CM_BGR8_PACKED);
INT nGamma = 180;
is_Gamma(hCam, IS_GAMMA_CMD_SET, (void*) &nGamma, sizeof(nGamma));
is_ImageFormat(hCam, IMGFRMT_CMD_GET_ARBITRARY_AOI_SUPPORTED, (void*) &nAOISupported, sizeof(nAOISupported));
is_AllocImageMem(hCam, width, height, 24, &ppcImgMem, &pid);
is_SetImageMem(hCam, ppcImgMem, pid);
is_CaptureVideo(hCam, IS_WAIT);
}

cv::Mat UeyeOpencvCam::getFrame() {
VOID* pMem;
int retInt = is_GetImageMem(hCam, &pMem);
if (retInt != IS_SUCCESS) {
if (retInt == IS_INVALID_CAMERA_HANDLE){
std::cout<<"error CAMERA_HANDLE"<<std::endl;
}
else{
std::cout<<"error is_GetImageMem"<<std::endl;
}
}
else{
memcpy(mattie.ptr(), pMem, width * height * 3);
}
return mattie;
}
...全文
1066 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
星辰韦 2015-11-18
  • 打赏
  • 举报
回复
别沉了啊,求大神帮忙 把.pro文件补上

HEADERS += \
    UEyeOpenCV.hpp \
    servocontroller.h

SOURCES += \
    main.cpp \
    ueyeopencv.cpp \
    servocontroller.cpp

# important for QSerialport
QT       += serialport

# OpenCV库
INCLUDEPATH += /usr/local/include \
                /usr/local/include/opencv \
                /usr/local/include/opencv2
LIBS += /usr/local/lib/libopencv_core.so    \
        /usr/local/lib/libopencv_highgui.so    \
        /usr/local/lib/libopencv_imgproc.so

# UEye摄像头库
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../usr/lib/release/ -lueye_api
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../usr/lib/debug/ -lueye_api
else:unix: LIBS += -L$$PWD/../../../../usr/lib/ -lueye_api
INCLUDEPATH += $$PWD/../../../../usr/include
DEPENDPATH += $$PWD/../../../../usr/include

# ViSP库
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu/release/ -lvisp
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu/debug/ -lvisp
else:unix: LIBS += -L$$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu/ -lvisp
INCLUDEPATH += $$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu
DEPENDPATH += $$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu

16,216

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧