求教:用QSocketNotifier和信号槽接收串口数据,不能触发信号

jane_ybf 2016-02-20 09:49:16
串口只能发数据,不能接收数据,请大神指点

主函数:
#include<synserialcomm.h>

using namespace std;
int main(int argc, char*argv[])
{
SynSerialComm *myport = new SynSerialComm();
myport->start();

while(1);
delete myport;
return 0;
}
...全文
884 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jane_ybf 2017-11-03
  • 打赏
  • 举报
回复
引用 3 楼 qq_33454731 的回复:
我也是这样 楼主找到解决办法了么
好久远的事情了,用Qt的槽信号机制没搞定,后来改成在run里边查询接收了
caiwei_cs 2016-12-15
  • 打赏
  • 举报
回复
你run的while(1)里面没有释放线程,都占用线程死了,就算有数据来了,信号也没办法发出来。
  • 打赏
  • 举报
回复
run有问题吧
qq_33454731 2016-12-10
  • 打赏
  • 举报
回复
我也是这样 楼主找到解决办法了么
jane_ybf 2016-02-20
  • 打赏
  • 举报
回复
synserialcomm.cpp
#include<synserialcomm.h>

using namespace std;
static portinfo_t portinfo;
int serial_fd;
char databuffer[20];
int datalen;
SynSerialComm::SynSerialComm()
{
    portinfo = {
        '0', // print prompt after receiving
        38400, // baudrate: 115200
        '8', // databit 8
        '0', // debug: off
        '0', // echo off
        '2', // flow control: software
        '0', // default tt: SAC0
        '0', // parity: none
        '1', // stopbit: 1
        0 // reserved
    };
    this->moveToThread(this);
}

void SynSerialComm::run()
{
    open_serial();

    while(1)
    {
        string sendstr;
        cin>>sendstr;
        send_setial(sendstr);
    }
    close_serial();
}

void SynSerialComm::open_serial()
{
    serial_fd=PortOpen(&portinfo);
    if(serial_fd<0)
    {
        perror("Error:open serial port error");
        //exit(1);
    }
    PortSet(serial_fd,&portinfo);
    QSocketNotifier *notifier=new QSocketNotifier(serial_fd,QSocketNotifier::Read,this);
    connect(notifier,SIGNAL(activated(int)),this,SLOT(read_serial()));
}

void SynSerialComm::close_serial()
{
    PortClose(serial_fd);
}

void SynSerialComm::send_setial(string sendstr)
{
    char *psendchar = new char[sendstr.length()];
    strcpy(psendchar, sendstr.c_str());
    PortSend(serial_fd,psendchar,sendstr.length());
}

void SynSerialComm::read_serial()
{
    datalen=PortRecv(serial_fd,databuffer,10,38400);
    databuffer[datalen] = '\0';
    cout << databuffer << endl;
}
jane_ybf 2016-02-20
  • 打赏
  • 举报
回复
synserialcomm.h文件
#ifndef SYNSERIALCOMM_H
#define SYNSERIALCOMM_H
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <errno.h>
#include <string.h>
#include <termio.h>
#include<QSocketNotifier>
#include<qdebug.h>
#include "mqz_thread.h"
#include<iostream>
#include<serialport.h>

using namespace std;

class SynSerialComm : public QThread
{
    Q_OBJECT
public:

public:
    SynSerialComm();
    ~SynSerialComm(){}
    void open_serial();
    void close_serial();
    void send_setial(string sendstr);
    virtual void run();
private slots:
    void read_serial();
};

#endif // SYNSERIALCOMM_H

16,240

社区成员

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

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