序号 | 内容 | 语言 |
---|---|---|
1 | 《如何定制自己的HID调试助手》 | C# |
2 | 《C# 串口上位机开发》 | C# |
3 | 《Qt 串口上位机开发》 | QT |
4 | 《教你动手写UDP协议栈 - OTA上位机》 | python |
5 | 《基于RT-THREAD nano的平衡车--上位机软件》 | QT |
6 | 《R-Plan上位机》 | QT |
上位机开发不限于语言,找我之前开发中,初衷就是那种方便就使用那种语言开发,如:C#, QT, python, VB等。
本篇文章分享是采用QT开发的TCP上位机,功能:通过TCP上位机控制小熊派板载外设。
上位机采用QT开发,小熊派跑RT-Thread,如下图为总体框图。
代码链接:https://gitee.com/RiceChen0/bearpi_rt-thread.git
分支:tcp_demo
如果你们觉得不错,记得加个:Star。
QT += network
#include <QTcpSocket>
#include <QTcpServer>
#ifndef BEARPI_H
#define BEARPI_H
#include <QMainWindow>
#include <QTcpSocket>
#include <QTcpServer>
QT_BEGIN_NAMESPACE
namespace Ui { class bearpi; }
QT_END_NAMESPACE
class bearpi : public QMainWindow
{
Q_OBJECT
public:
bearpi(QWidget *parent = nullptr);
~bearpi();
private slots:
void on_mode_pushButton_clicked();
void on_start_pushButton_clicked();
void on_open_pushButton_clicked();
void on_pwm_horizontalSlider_valueChanged(int value);
private:
void switch_mode();
void new_client_connect();
private:
Ui::bearpi *ui;
QTcpServer *server;
QTcpSocket *socket;
};
#endif // BEARPI_H
bearpi::bearpi(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::bearpi)
{
ui->setupUi(this);
server = new QTcpServer();
socket = new QTcpSocket();
QRegExp ip_RegExp("^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$");
QRegExpValidator *ip_format = new QRegExpValidator(ip_RegExp, this);
ui->ip_lineEdit->setValidator(ip_format);
switch_mode();
}
void bearpi::switch_mode()
{
if(ui->mode_pushButton->text() == tr("SERVER"))
{
ui->ip_lineEdit->setEnabled(false);
}
else if(ui->mode_pushButton->text() == tr("CLIENT"))
{
ui->ip_lineEdit->setEnabled(true);
}
}
void bearpi::new_client_connect()
{
socket = server->nextPendingConnection();
qDebug() << "A Client connect!";
}
void bearpi::on_start_pushButton_clicked()
{
if(ui->start_pushButton->text() == tr("START"))
{
if(ui->mode_pushButton->text() == tr("SERVER"))
{
int port = 0;
connect(server,&QTcpServer::newConnection,this,&bearpi::new_client_connect);
port = ui->port_lineEdit->text().toInt();
if(!server->listen(QHostAddress::Any, port))
{
qDebug() << server->errorString();
return;
}
qDebug() << "Listen successfully";
}
else if(ui->mode_pushButton->text() == tr("CLIENT"))
{
QString ip;
int port = 8080;
ip = ui->ip_lineEdit->text();
port = ui->port_lineEdit->text().toInt();
socket->abort();
socket->connectToHost(ip, port);
if(!socket->waitForConnected(3000))
{
qDebug() << "Connect failed";
return;
}
qDebug() << "Connect successfully";
}
ui->start_pushButton->setText(tr("STOP"));
ui->mode_pushButton->setEnabled(false);
}
else if(ui->start_pushButton->text() == tr("STOP"))
{
if(ui->mode_pushButton->text() == tr("SERVER"))
{
socket->abort();
server->close();
}
else if(ui->mode_pushButton->text() == tr("CLIENT"))
{
socket->disconnectFromHost();
}
ui->start_pushButton->setText(tr("START"));
ui->mode_pushButton->setEnabled(true);
}
}
void bearpi::on_open_pushButton_clicked()
{
qDebug() << ui->open_pushButton->text();
if(ui->open_pushButton->text() == tr("OPEN LED"))
{
socket->write("led_open");
socket->flush();
ui->open_pushButton->setText(tr("CLOSE LED"));
}
else if(ui->open_pushButton->text() == tr("CLOSE LED"))
{
socket->write("led_close");
socket->flush();
ui->open_pushButton->setText(tr("OPEN LED"));
}
}
void bearpi::on_pwm_horizontalSlider_valueChanged(int value)
{
char pwm_str[1] = {0};
pwm_str[0] = (char)value;
socket->write(pwm_str);
socket->flush();
qDebug() << pwm_str[0];
}
void bearpi_client(int argc, char **argv)
{
int ret;
struct hostent *host;
struct sockaddr_in server_addr;
host = gethostbyname(IP_ADDR);
if ((sock_client_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
rt_kprintf("Socket error\n");
return;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
if (connect(sock_client_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{
rt_kprintf("Connect fail!\n");
closesocket(sock_client_fd);
return;
}
pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
rt_thread_init(&bearpi_thread,
"bearpi",
bearpi_entry,
RT_NULL,
&bearpi_stack[0],
sizeof(bearpi_stack),
10, 5);
rt_thread_startup(&bearpi_thread);
return;
}
MSH_CMD_EXPORT(bearpi_client, a tcp client sample);
void bearpi_entry(void* paramenter)
{
char recv_data[BUFSZ] = {0};
uint32_t recv_len = 0;
uint32_t is_open = 0;
while (1)
{
recv_len = recv(sock_client_fd, recv_data, BUFSZ - 1, 0);
if(recv_len > 0)
{
if(strncmp(recv_data, "led_open", 8) == 0)
{
rt_pin_write(LED0_PIN, PIN_HIGH);
is_open = RT_TRUE;
}
else if(strncmp(recv_data, "led_close", 1) == 0)
{
rt_pin_write(LED0_PIN, PIN_LOW);
is_open = RT_FALSE;
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, 0);
}
else
{
if(is_open == RT_TRUE)
{
pulse = period / 100 * recv_data[0];
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
}
}
}
}
}