完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
linux 3.1.15 +qt 4.8 lincom demo中的serialport_串口类不能接受00数据和包含00的数据?
下面是serialport.h 代码 #ifn SER_COM_PORT_TX_H #define SER_COMPORT_TX_H #include #include #include #include #include #include< sys/ioctl.h> #include #include #include #include #include #include #include #include enum BaudRateType { BAUD50, //POSIX ONLY BAUD75,// POSIX BAUD110, BAUD134,// 仅 POSIX BAUD150,// 仅POSIX BAUD200,// 仅POSIX BAUD300, BAUD600, BAUD1100, BAUD1800,// POSIX仅限 BAUD2400, BAUD4800, BAUD9600, BAUD14400,// Windows仅 BAUD19200, BAUD38400, BAUD56000,// Windows仅 BAUD57600, BAUD76800,// POSIX仅限 BAUD115200, BAUD115200,BAUD128000,// Windows 仅为BAUD256000 / / WINDOWS ONLY }; 枚举数据位类型 { DATA_5, DATA_6, 数据_7,数据 _8 }; 枚举ParityType { PAR_NONE, PAR_ODD, PAR_EVEN, PAR_MARK,//仅限WINDOWS PAR_SPACE }; enum StopBitsType { STOP_1, STOP_1_5, //WINDOWS ONLY STOP_2 }; 枚举 FlowType { FLOW_OFF, FLOW_HARDWARE, FLOW_XONXOFF }; 类 SerialPort:public QObject { Q_OBJECT public: 显式 SerialPort(QWidget *parent = 0);个人 : int m_fd; termios new_serialArry; QSocketNotifier *m_notifier; QByteArray *rev_buf; QMutex mutex_r; public : //open bool openPort(QString portName,BaudRateType baundRate,DataBitsType dataBits,ParityType parity, StopBitsType stopBits, FlowType flow ,int time_out); //关闭 bool close(); //write int write(QByteArray buf); //读取 QByteArray read(); 信号: 无效hasdata(); 私人插槽: void remoteDateInComing(); }; #endif // SER_COM_PORT_TX_H 下面的serialport.cpp #include "serialport.h" #include SerialPort::SerialPort(QWidget *parent): QObject(父) { m_fd=-1; rev_buf=新 QByteArray(); } //open bool SerialPort::openPort(QString portName, BaudRateType baundRate, DataBitsType dataBits, ParityType parity, StopBitsType stopBits, FlowType flow, int time_out) { if(m_fd!=-1) { qDebug("is areay opens!") ; 返回假; } rev_buf->clear(); memset(&new_serialArry,0,sizeof new_serialArry); m_fd=::open(portName.toLatin1(),O_RDWR|O_NONBLOCK); if(m_fd==-1) { qDebug("串口打开错误!"); 返回假; } switch(baundRate) { case BAUD50: new_serialArry.c_cflag |= B50; 替换案例 ; BAUD75: new_serialArry.c_cflag |= B75; 休息; 案例BAUD110: new_serialArry.c_cflag | = B110; 休息; 案例BAUD300: new_serialArry.c_cflag | = B300; 休息; 案例BAUD600: new_serialArry.c_cflag | = B600; 休息; 案例BAUD1200: new_serialArry.c_cflag | = B1200; 休息; 案例BAUD2400 : new_serialArry.c_cflag |= B2400;; 休息 案例BAUD4800: new_serialArry.c_cflag |= B4800; 休息; 案例BAUD9600: new_serialArry.c_cflag |= B9600;; 休息 案例BAUD19200: new_serialArry.c_cflag |= B19200 ;; 案例BAUD38400: new_serialArry.c_cflag |= B38400; 休息; 案例BAUD57600: new_serialArry.c_cflag |= B57600; 休息; 案例BAUD115200: new_serialArry.c_cflag |= B115200; 休息; 默认值: new_serialArry.c_cflag |= B9600 ; } 开关(数据位) { 案例DATA_5: new_serialArry.c_cflag|=CS5; 休息; 案例DATA_6: new_serialArry.c_cflag|=CS6; 休息; 案例DATA_7: new_serialArry.c_cflag|=CS7; 休息; 案例DATA_8: new_serialArry.c_cflag|=CS8; 休息; 默认值: new_serialArry.c_cflag|=CS8; 休息; } switch (parity) { case PAR_NONE: new_serialArry.c_cflag &= (~PARENB); 休息; case PAR_EVEN: new_serialArry.c_cflag &= (~PARODD); new_serialArry.c_cflag |= PARENB; 休息; case PAR_ODD: new_serialArry.c_cflag |= (PARODD|PARENB); 休息; 默认值: 中断; } switch(stopBits) { case STOP_1: new_serialArry.c_cflag &=(~CSTOPB); 休息; 案例 STOP_1_5: 中断; STOP_2: new_serialArry.c_cflag |=CSTOPB; 休息; 值: new_serialArry.c_cflag &=(~CSTOPB); 休息; } switch(flow) { case FLOW_OFF: new_serialArry.c_cflag &=(~CRTSCTS); new_serialArry.c_iflag &=(~(IXON|IXOFF|IXANY)); 休息; case FLOW_XONXOFF: new_serialArry.c_cflag &=(~CRTSCTS); new_serialArry.c_iflag |=(IXON|IXOFF|IXANY); 休息; case FLOW_HARDWARE: new_serialArry.c_cflag |=~CRTSCTS; new_serialArry.c_iflag &=(~(IXON|IXOFF|IXANY)); 休息 默认值: 中断; } new_serialArry.c_oflag=0; new_serialArry.c_cc[VTIME]=time_out/100; tcflush(m_fd,TCIFLUSH); tcsetattr(m_fd,TCSANOW,&new_serialArry); m_notifier=new QSocketNotifier(m_fd,QSocketNotifier::Read,0); 连接(m_notifier,SIGNAL(激活(int)),这个,SLOT(remoteDateInComing())); 返回真; } //关闭 bool SerialPort::close( ) { mutex_r.lock(); if(m_fd!=-1) { m_AL_AL_连接(m_notifier,SIGN(激活(int)),this,SLOT(remoteDateInComing())); 破坏连接; m_fd=-1; mutex_r.unlock(); 返回真; } mutex_r.unlock(); 返回假; } //接收数据 无效 SerialPort::remoteDateInComing() { unsigned char c[1024]; int n= ::read(m_fd,&c,sizeof c); mutex_r.lock(); for(int i=0;i { rev_buf->append(c ); } mutex_r.unlock(); 发出 hasdata(); } //写入数据 int SerialPort::write(QByteArray buf) { mutex_r.lock(); 诠释; ret=0; 如果(m_fd!=-1) { ret= :: write(m_fd,buf.data(),buf.length()); } mutex_r.unlock(); 返回 ret; } QByteArray SerialPort::read() { mutex_r.lock(); QByteArray retByteArray; if(rev_buf->isEmpty()) { retByteArray.append("aaa"); retByteArray.clear(); }else { retByteArray.append(rev_buf->data()); rev_buf->clear(); } mutex_r.unlock(); 返回 retByteArray; } |
|
相关推荐
1个回答
|
|
我们建议您提出一个方法,可以试一下,将字体修改为部分
解决方法:工程内部 serialport.cpp QByteArray SerialPort::read() { mutex_r.lock(); QByteArray retByteArray; if(rev_buf->isEmpty()) { retByteArray.append("aaa"); retByteArray.clear(); }else { retByteArray.append(rev_buf->data()); rev_buf->clear(); } mutex_r.unlock(); 返回 retByteArray; } 改为: QByteArray SerialPort::read() { mutex_r.lock(); QByteArray retByteArray; if(rev_buf->isEmpty()) { retByteArray.append("aaa"); retByteArray.clear(); }else { retByteArray.append(*rev_buf); rev_buf->clear(); } mutex_r.unlock(); 返回 retByteArray; } |
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
431 浏览 0 评论
524 浏览 1 评论
1789 浏览 1 评论
3043 浏览 1 评论
synopsys 的design ware:DW_fpv_div,浮点数除法器,默认32位下,想提升覆盖率(TMAX),如果用功能case去提升覆盖率呢?
3771 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-19 06:34 , Processed in 0.570351 second(s), Total 70, Slave 54 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号