Qt监控Arduino开关状态(读取串口数据)
setup.ini配置文件内容
[General] #游戏所在主机IP
GameIp1=192.168.1.151
GameIp2=192.168.1.152
GameIp3=192.168.1.153
GameIp4=192.168.1.154
GameIp5=192.168.1.155
GameIp6=192.168.1.156
GameIp7=192.168.1.157
GameIp8=192.168.1.158 #游戏中监听的UDP端口
GamePort= #延迟发送指令时间(单位毫秒)
DelayTime1=
DelayTime2=
DelayTime3=
DelayTime4=
DelayTime5=
DelayTime6=
DelayTime7=
DelayTime8= #4D系统的IP
4DSystemIp=192.168.1.152 #4D系统监听的UDP端口
4DSystemPort= #向游戏发送的指令
UdpDataToGame=%Play% #向4D系统发送的指令
UdpDataTo4DSystem=AS%NPlayDirect%%
工程文件.pro中QT+=serialport
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow> #include <QSerialPort>
#include <QSerialPortInfo>
#include <QTimer> #include <QUdpSocket> namespace Ui {
class MainWindow;
} struct GamePCInfo
{
GamePCInfo(){}
GamePCInfo(QString s,quint16 p, int i){ GameIp=s;GamePort=p;DelayTime=i;}
QString GameIp;//游戏主机IP
quint16 GamePort;//游戏UDP绑定的端口
int DelayTime;//指令延迟发送时间
}; class MainWindow : public QMainWindow
{
Q_OBJECT public:
explicit MainWindow(QWidget *parent = );
~MainWindow(); void sendUdpData(); public slots:
void readyReadSlot(); void sendToGame1();
void sendToGame2();
void sendToGame3();
void sendToGame4();
void sendToGame5();
void sendToGame6();
void sendToGame7();
void sendToGame8();
private slots:
void on_pushButton_clicked(); private:
Ui::MainWindow *ui;
QSerialPort* m_reader;//COM串口
QTimer* queryTimer; QUdpSocket* udpSocket; GamePCInfo gamePC1,gamePC2,gamePC3,gamePC4,gamePC5,gamePC6,gamePC7,gamePC8;//8台电脑
QString FourDSystemIp;//4D系统IP
quint16 FourDSystemPort;//4D系统UDP绑定的端口
QString UdpDataToGame;//向游戏发送的UDP数据
QString UdpDataTo4DSystem;//向4D系统发送的UDP数据
}; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QSettings>
#include <QTextCodec> #pragma execution_character_set("utf-8") MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this); QString appDirPath = QCoreApplication::applicationDirPath();
QSettings* configIniRead = new QSettings(appDirPath+"/setup.ini", QSettings::IniFormat);
configIniRead->setIniCodec(QTextCodec::codecForName("GB2312"));
if( configIniRead->allKeys().size() > )
{
quint16 GamePort = configIniRead->value("GamePort").toUInt();
GamePCInfo game1(configIniRead->value("GameIp1").toString(),GamePort,configIniRead->value("DelayTime1").toInt());
GamePCInfo game2(configIniRead->value("GameIp2").toString(),GamePort,configIniRead->value("DelayTime2").toInt());
GamePCInfo game3(configIniRead->value("GameIp3").toString(),GamePort,configIniRead->value("DelayTime3").toInt());
GamePCInfo game4(configIniRead->value("GameIp4").toString(),GamePort,configIniRead->value("DelayTime4").toInt());
GamePCInfo game5(configIniRead->value("GameIp5").toString(),GamePort,configIniRead->value("DelayTime5").toInt());
GamePCInfo game6(configIniRead->value("GameIp6").toString(),GamePort,configIniRead->value("DelayTime6").toInt());
GamePCInfo game7(configIniRead->value("GameIp7").toString(),GamePort,configIniRead->value("DelayTime7").toInt());
GamePCInfo game8(configIniRead->value("GameIp8").toString(),GamePort,configIniRead->value("DelayTime8").toInt());
gamePC1=game1;
gamePC2=game2;
gamePC3=game3;
gamePC4=game4;
gamePC5=game5;
gamePC6=game6;
gamePC7=game7;
gamePC8=game8; qDebug()<<game3.DelayTime;
FourDSystemIp = configIniRead->value("4DSystemIp").toString();
FourDSystemPort = configIniRead->value("4DSystemPort").toUInt();
UdpDataToGame = configIniRead->value("UdpDataToGame").toString();
UdpDataTo4DSystem = configIniRead->value("UdpDataTo4DSystem").toString();
}
else
{
qDebug() << "have no setup.ini , use default setting ." ;
} udpSocket = new QUdpSocket(this);
//我们这里只需要发送数据,并不用接收数据
//connect( udpSocket, SIGNAL(readyRead()), SLOT(readUdpData()) ); //绑定Arduino端口
QSerialPortInfo com_info;
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
qDebug() << "Name : " << info.portName();
qDebug() << "Description : " << info.description();
qDebug() << "Manufacturer: " << info.manufacturer();
qDebug() << "Serial Number: " << info.serialNumber();
qDebug() << "System Location: " << info.systemLocation(); if( info.description().contains("Arduino") )//找到Arduino端口
{
com_info = info;
break;
}
}
if(com_info.description().contains("Arduino"))
{
ui->textEdit->append("已找到Arduino端口"+com_info.systemLocation());
m_reader = new QSerialPort(this);
m_reader->setPort(com_info);
if(m_reader->open(QIODevice::ReadOnly))
{
m_reader->setBaudRate(QSerialPort::Baud9600);
m_reader->setParity(QSerialPort::NoParity);
m_reader->setDataBits(QSerialPort::Data8);
m_reader->setStopBits(QSerialPort::OneStop);
m_reader->setFlowControl(QSerialPort::NoFlowControl);
m_reader->setDataTerminalReady(true);//这个很重要,否则的话拔插串口之后,刚开始的几个数据会读取不到!!!!!!!!!!!!!!!!!!! m_reader->clearError();
m_reader->clear();
connect(m_reader, SIGNAL(readyRead()), this, SLOT(readyReadSlot()));
ui->textEdit->append("Arduino端口"+com_info.systemLocation()+"++++++打开成功!+++++");
}
else
{
ui->textEdit->append("Arduino端口"+com_info.systemLocation()+"-----打开失败!------");
}
}
else
{
ui->textEdit->append("未找到Arduino端口");
}
} MainWindow::~MainWindow()
{
delete ui;
m_reader->clear();
m_reader->close();
} //读取端口数据
void MainWindow::readyReadSlot()
{
QByteArray arr = m_reader->readAll();
std::string data=arr.toStdString();
qDebug()<<QString::fromStdString(data);
if(data=="H")//高电平,开关接通
{
sendUdpData();
ui->textEdit->append("开关接通");
}
else if(data=="L")//低电平,开关断开
{
ui->textEdit->append("开关断开");
}
m_reader->clear();
} void MainWindow::sendToGame1()
{
udpSocket->writeDatagram(UdpDataToGame.toStdString().data(), QHostAddress(gamePC1.GameIp), gamePC1.GamePort);
qDebug()<<gamePC1.GameIp;
}
void MainWindow::sendToGame2()
{
udpSocket->writeDatagram(UdpDataToGame.toStdString().data(), QHostAddress(gamePC2.GameIp), gamePC2.GamePort);
qDebug()<<gamePC2.GameIp;
}
void MainWindow::sendToGame3()
{
udpSocket->writeDatagram(UdpDataToGame.toStdString().data(), QHostAddress(gamePC3.GameIp), gamePC3.GamePort);
qDebug()<<gamePC3.GameIp;
}
void MainWindow::sendToGame4()
{
udpSocket->writeDatagram(UdpDataToGame.toStdString().data(), QHostAddress(gamePC4.GameIp), gamePC4.GamePort);
qDebug()<<gamePC4.GameIp;
}
void MainWindow::sendToGame5()
{
udpSocket->writeDatagram(UdpDataToGame.toStdString().data(), QHostAddress(gamePC5.GameIp), gamePC5.GamePort);
qDebug()<<gamePC5.GameIp;
}
void MainWindow::sendToGame6()
{
udpSocket->writeDatagram(UdpDataToGame.toStdString().data(), QHostAddress(gamePC6.GameIp), gamePC6.GamePort);
qDebug()<<gamePC6.GameIp;
}
void MainWindow::sendToGame7()
{
udpSocket->writeDatagram(UdpDataToGame.toStdString().data(), QHostAddress(gamePC7.GameIp), gamePC7.GamePort);
qDebug()<<gamePC7.GameIp;
}
void MainWindow::sendToGame8()
{
udpSocket->writeDatagram(UdpDataToGame.toStdString().data(), QHostAddress(gamePC8.GameIp), gamePC8.GamePort);
qDebug()<<gamePC8.GameIp;
} void MainWindow::sendUdpData()
{
//向游戏发送Play指令
QTimer::singleShot( gamePC1.DelayTime, this, SLOT(sendToGame1()) );
QTimer::singleShot( gamePC2.DelayTime, this, SLOT(sendToGame2()) );
QTimer::singleShot( gamePC3.DelayTime, this, SLOT(sendToGame3()) );
QTimer::singleShot( gamePC4.DelayTime, this, SLOT(sendToGame4()) );
QTimer::singleShot( gamePC5.DelayTime, this, SLOT(sendToGame5()) );
QTimer::singleShot( gamePC6.DelayTime, this, SLOT(sendToGame6()) );
QTimer::singleShot( gamePC7.DelayTime, this, SLOT(sendToGame7()) );
QTimer::singleShot( gamePC8.DelayTime, this, SLOT(sendToGame8()) ); //向4D系统发送启动指令
udpSocket->writeDatagram(UdpDataTo4DSystem.toStdString().data(), QHostAddress(FourDSystemIp), FourDSystemPort);
} void MainWindow::on_pushButton_clicked()
{
sendUdpData();
}
Qt监控Arduino开关状态(读取串口数据)的更多相关文章
- python3 读取串口数据
python3 读取串口数据 demo import serial import time ser = serial.Serial("COM3",115200,timeout = ...
- C#SerialPort如何读取串口数据并显示在TextBox上
SerialPort中串口数据的读取与写入有较大的不同.由于串口不知道数据何时到达,因此有两种方法可以实现串口数据的读取.一.线程实时读串口:二.事件触发方式实现. 由于线程实时读串口的效率不是十分高 ...
- Arduino读取串口数据并进行字符串分割
String comdata = ""; int numdata[6] = {0}, PWMPin[6] = {3, 5, 6, 9, 10, 11}, mark = 0; voi ...
- (5)air202读取串口数据并上传到阿里云显示
一.首先进行云端设置 根据串口助手显示的信息,以及模块文档说明我们可以知道 其中red和ir是红光LED的原始数据, HR表示心率值, HRvalid是心率是否有效标识, SP02是血氧数值,,SPO ...
- Arduino从DHT11读取温湿度数据并显示在1602LCD
硬件清单 Arduino NANO1602LCD + PCF8574T模块YL-47 DHT11模块 连线 1. 连接LCD: PCF8574T模块4pin(Gnd, Vcc, SDA i2c数据, ...
- Matlab+Qt开发笔记(二):Qt打开mat文件显示读取的数据
前言 介绍了基础环境,最终是为了读取显示.mat文件,本篇读取mat文件并显示. 补充 测试的mat文件是double类型的. Matlab库数据类型 变量类型:matError,错误变量 ...
- android 读取串口数据的服务
2016-09-1813:10:03 继承Service,定义抽象方法onDataReceived,子类通过实现抽象方法获取接收到数据的回调. package com.zrsoft.liftad.se ...
- SerialPort如何读取串口数据并显示在TextBox上,多线程委托
namespace SerialPort { public partial class Form3 : Form { delegate void UpdateTextEventHandler(stri ...
- 一种非常巧妙的读取串口数据的方法--C#
读取不完就一直等待,读完了就立刻走,之前都是设置一个溢出时间,不管是不是早就读取完了都要在这等着,有一定的时间浪费. 注意,用之前要设置好SerialPort类的TimeOut属性:
随机推荐
- c#打印(转)
柴门闻狗吠,风雪夜归人.野旷天低树,江清月近人.香雾云鬟湿,清辉玉臂寒.莫愁前路无知己,天下谁人不识君.常将冷眼 看螃蟹,看你横行到几时.要在vs2005中实现最基本的打印文本基本上需要以下几个步骤 ...
- 常见Java工具——jps
简介 最常用的一个. 与Linux中的查看Java进程命令功能相同: ps -ef | grep java jps与这个命令的区别在于,jps仅仅过滤出Java本身的进程以及运行的引导类,就是引导ma ...
- pthread_self()究竟根据什么来得到线程的标识符????
#include<stdlib.h> #include<pthread.h> #include<stdio.h> #include<sched.h> # ...
- 预留端口避免占用ip_local_reserved_ports
问题描述: 业务遇到这个情况,在重启服务时,出现1986端口被占用而无法启动,非得等该端口释放后才启动成功. 问题分析: 1986端口被该服务器上的客户端随机选取源端口给占用掉了. 解决方案: 使 ...
- spring boot之访问静态页面
楼主前两天自学spring boot,然后在学习的过程中,出现一个疑问,就是如何去访问静态的html网页,这个问题,楼主上网上搜了下,找到的是在resource目录下建立一个templates文件夹, ...
- spring boot 拦截器之WebMvcConfigurerAdapter
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 上一篇我们讲到了拦截器,我们也简单的讲解到了WebMvcConfigurerAdapter这个拦截器.本篇我们来对Web ...
- SQL Server 查询分析器键盘快捷方式
下表列出 SQL Server 查询分析器提供的所有键盘快捷方式. 活动 快捷方式 书签:清除所有书签. CTRL-SHIFT-F2 书签:插入或删除书签(切换). CTRL+F2 书签:移动到下一个 ...
- 【转】【MySql】Update批量更新与批量更新多条记录的不同值实现方法
批量更新 mysql更新语句很简单,更新一条数据的某个字段,一般这样写: UPDATE mytable SET myfield = 'value' WHERE other_field = 'other ...
- 应当将指针变量用“==”或“!=”与 NULL 比较
应当将指针变量用“==”或“!=”与 NULL 比较. 指针变量的零值是“空”(记为 NULL). 尽管 NULL 的值与 0 相同,但是两者意义不 同. 假设指针变量的名字为 p,它与零值比较的标准 ...
- C#有关的vshost、exe、config格式说明
vshost.exe.config是程序运行时的配置文本 exe.config是程序运行后会复制到vshost.exe.config app.config是在vshost.exe.config和exe ...