当前位置: 首页 > news >正文

地产网站建设公司青岛的建筑公司

地产网站建设公司,青岛的建筑公司,网站建设工作室 怎么样,游戏工作室网络组建方案目录 1.1.1 添加库#xff0c;添加类 1.1.2 定义串口 1.1.3 搜索串口 1.1.4 设置和打开串口 1.1.5 读取数据 1.1.6 发送数据 1.1.7 关闭串口 1.1.1 添加库#xff0c;添加类 首先#xff0c;QT5 是自带 QSerialPort(Qt5 封装的串口类)这个类的#xff0c;使用时…目录 1.1.1  添加库添加类 1.1.2 定义串口 1.1.3  搜索串口 1.1.4 设置和打开串口 1.1.5 读取数据 1.1.6 发送数据 1.1.7 关闭串口  1.1.1  添加库添加类 首先QT5 是自带 QSerialPort(Qt5 封装的串口类)这个类的使用时需要在 pro 文件里面添加一行 QT serialport 加入下面两个文件 #include QSerialPort #include QSerialPortInfo QT5 中串口通信是借助一个 QSerialPort 的对象来实现的在设置 QSerialPort 对象对串口的名称、波特率、 数据位、校验位、停止位等参数后方能对串口进行读写。下面我总结了一下借助 QSerailPort 对串口进行设 置、打开、读、写和关闭的过程。 1.1.2 定义串口 QSerialPort *uart; uart new QSerialPort(this);1.1.3  搜索串口 //availablePorts 是返回当前系统所有能用的串口,返回值是一个容器//QList 是容器类,在此的容器里边装的是 QSerialPortInfoQListQSerialPortInfo info QSerialPortInfo::availablePorts();for(int i0; iinfo.size(); i){ui-uartNameCombo-addItem(info[i].portName()); //addItem 加入数据到下拉 菜单里}1.1.4 设置和打开串口 void UiMain::on_openBtn_clicked() {QString uartName;QSerialPort::BaudRate baudRate;QSerialPort::DataBits dataBit;QSerialPort::StopBits stopBit;QSerialPort::Parity parityBit;//currentText 是获取当前选中的名字uartName ui-uartNameCombo-currentText();//波特率if(ui-baudrateCombo-currentText() 9600){baudRate QSerialPort::Baud9600; }else if(ui-baudrateCombo-currentText() 115200){baudRate QSerialPort::Baud115200;}//数据位if(ui-databitCombo-currentText() 5){dataBit QSerialPort::Data5;}else if(ui-databitCombo-currentText() 6){dataBit QSerialPort::Data6;}else if(ui-databitCombo-currentText() 7){dataBit QSerialPort::Data7;}else if(ui-databitCombo-currentText() 8){dataBit QSerialPort::Data8;}//停止位if(ui-stopbitCombo-currentText() 1){stopBit QSerialPort::OneStop;}else if(ui-stopbitCombo-currentText() 1.5){stopBit QSerialPort::OneAndHalfStop;}else if(ui-stopbitCombo-currentText() 2){stopBit QSerialPort::TwoStop;}//校验位if(ui-parityCombo-currentIndex() 0){parityBit QSerialPort::OddParity;}else if(ui-parityCombo-currentIndex() 1){parityBit QSerialPort::EvenParity;}else if(ui-parityCombo-currentIndex() 2){parityBit QSerialPort::NoParity;}//把参数设置到 uart 里uart-setPortName(uartName);uart-setBaudRate(baudRate);uart-setDataBits(dataBit);uart-setStopBits(stopBit);uart-setParity(parityBit);if(!uart-open(QIODevice::ReadWrite)){QMessageBox::critical(this, fbi warnning, 打开失败);}else{QMessageBox::information(this, 提示, 打开成功);}1.1.5 读取数据 串口在收到数据后会将数据存入接收缓冲区。此时我们可以通过 readAll()函数将接收缓冲区的数据读出 来。当串口的接收缓冲区有数据时QSerilaPort 对象会发出一个 readyRead()的信号。因此我们可以编写一个槽 函数来读数据。 connect(uart, SIGNAL(readyRead()), this, SLOT(slotUartReadyRead())); QString uartBuf; //假如没收完断截的数据,就会一直触发槽函数,定时器会被一直重置,不会超时 //当断截数据发完了,定时器就没人重启了,就会超时,这个时候收到的数据就是完整的数据 //也就是说收到一个字节后20ms 内没有再次收到数据默认为一帧数据接收完成 void UiMain::slotUartReadyRead() {timer-start(20); //重启定时器 20ms uartBuf.append(uart-readAll()); //追加写入到 uartBuf 里 }//超时了,说明数据完成一帧数据 void UiMain::slotTimeout() {timer-stop();QByteArray ba;ba uartBuf.toUtf8();//QString(nRecv)不是强制转换//QString(nRecv)是会触发 QString 的构造函数,当然 QString(int)这种构造函数是没有 的,会报错 ui-recvEdit-appendPlainText(ba); }1.1.6 发送数据 使用write函数便可以把字节数组中的字节发送出去。 uart-write(buf.toUtf8()); //QString 转 char *的方法: buf.toLocal8Bit().data() 1.1.7 关闭串口  串口不用时可通过 close()函数将其关闭。 uart-close();
http://www.dnsts.com.cn/news/196307.html

相关文章:

  • 学做美食交流网站平台小程序
  • 徐州网络优化招聘网连云港seo优化
  • 网站目录结构改变公众号做视频网站吗
  • 哈尔滨做网站费用企业推广系统
  • 广州建设网站怎么做收录提交
  • 光明楼网站建设百度关键词搜索排名帝搜软件
  • wap手机网站代码南通建设公司网站
  • 广西建设厅网站培训中心开发公司的盈利模式
  • 博客网站开发视频网站建设规划书300字
  • 宁波网站建设使用技巧分享广州网站建设 超凡科技
  • 六安商务网站建设电话中盛腾龙建设工程有限公司网站
  • 2017建站之星怎么样免备案域名
  • 网站空间150m76人vs猛龙
  • 中山做app网站公司哪家好wordpress修改首页模板文件
  • 网站建设与规划实训报告广州越秀区封控区域
  • wordpress两步验证seo获客
  • 第二章营销型网站建设测验建立个人网站主题
  • 成都网站优化维护如何做网站出单
  • 摄影网站免费源码0000网站建设
  • 运营推广的网站有哪些资阳做网站
  • 园林景观 网站建设绥化市新闻最新消息
  • 做海报的网站推荐开淘宝店要自己做网站吗
  • 做网站必须要文网文吗长沙网站建设 芙蓉区
  • 成都网站设计公司贵州网站建设公司推荐
  • 做deal网站二级域名网址查询
  • 驻马店住房和城乡建设局网站没人愿意干的68个暴利行业
  • 汕头设计网站建设好玩的页游
  • js进入网站时有指导怎么做公司接到网站中文域名到期
  • 你那个没封的网站怎么做啊乐清网站网站建设
  • wordpress 去掉左上角优化大师的使用方法