嵌入式应用开发之基础设备控制

本文介绍了一个基于Qt开发的上位机应用与Linux环境下C语言开发的下位机之间的UDP通信实现。上位机通过GUI操作LED灯和滑动条来控制下位机的行为,而下位机则通过读写设备文件来响应这些控制指令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述

上位机(Qt开发)
weiget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QUdpSocket>
namespace Ui {
    class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

public slots:
    void LED_function(int);
    void slide_function(int);

private:
    Ui::Widget *ui;
     QUdpSocket* udpSocket;

};

#endif // WIDGET_H

weget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QCheckBox>
#include <QUdpSocket>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->udpSocket=new QUdpSocket;

    ui->PORT->setText("9413");//设定默认端口号
    ui->SLIDE->setRange(0,1500);

                      //对象间传参     //发送参数                   //接收参数
    connect(ui->LED1,SIGNAL(stateChanged(int)),this,SLOT(LED_function(int)));
    connect(ui->LED2,SIGNAL(stateChanged(int)),this,SLOT(LED_function(int)));
    connect(ui->LED3,SIGNAL(stateChanged(int)),this,SLOT(LED_function(int)));
    connect(ui->LED4,SIGNAL(stateChanged(int)),this,SLOT(LED_function(int)));

    connect(ui->SLIDE,SIGNAL(valueChanged(int)),this,SLOT(slide_function(int)));


}

Widget::~Widget()
{
    delete ui;
}

void Widget::LED_function(int state)//信号传状态信息
{
    QString ip=ui->IP->text();
    QString port=ui->PORT->text();

    char buf[100];
    QCheckBox* b=(QCheckBox*)sender();//区分发送对象,用sender函数
    if(b==ui->LED1)
    {               //设备//亮灭//第几个灯
        sprintf(buf,"LED#%d#0",(state==Qt::Checked)? 1:0);
        udpSocket->writeDatagram (buf,strlen(buf),QHostAddress(ip),port.toInt());
    }                                           //利用构造函数进行类型转换//利用函数类型转换
    if(b==ui->LED2)
    {
        sprintf(buf,"LED#%d#1",(state==Qt::Checked)? 1:0);
        udpSocket->writeDatagram (buf,strlen(buf),QHostAddress(ip),port.toInt());
    }
    if(b==ui->LED3)
    {
         sprintf(buf,"LED#%d#2",(state==Qt::Checked)? 1:0);
        udpSocket->writeDatagram (buf,strlen(buf),QHostAddress(ip),port.toInt());
    }
    if(b==ui->LED4)
    {
         sprintf(buf,"LED#%d#3",(state==Qt::Checked)? 1:0);
        udpSocket->writeDatagram (buf,strlen(buf),QHostAddress(ip),port.toInt());
    }
}
void Widget::slide_function(int value)
{
    QString ip=ui->IP->text();
    QString port=ui->PORT->text();
    char buf[100];


    sprintf(buf,"BUZZER#%d#%d",(value > 0)?1:0,value);
    udpSocket->writeDatagram(buf,strlen(buf), QHostAddress(ip),port.toInt());

}

下位机(linux C开发,arm-linux-gcc编译)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>         
#include <sys/socket.h>
#include <unistd.h>

int main()
{
    int fd_leds=open("/dev/leds",0);
    int fd_buzzer=open("/dev/pwm",0);

    int sock_connect=socket(AF_INET,SOCK_DGRAM,0);

    struct sockaddr_in myaddr,youraddr;
    myaddr.sin_family=AF_INET;
    myaddr.sin_addr.s_addr=INADDR_ANY;
    myaddr.sin_port=htons(8888);

    if(-1==bind(sock_connect,(struct sockaddr*)&myaddr,sizeof(myaddr)))
    {
        perror("bind");
        return -1;
    }


    char msg[1025];
    int ret;
    //name#instruct#arg
    char* dev_name=NULL,*cmd=NULL,*arg=NULL;//!!!

    while(1)
    {
        ret=recv(sock_connect,msg,sizeof(msg),0);
        msg[ret]='\0';

        dev_name=strtok(msg,"#");
        cmd=strtok(msg,"#");
        arg=strtok(msg,"#");

        if(strcmp(dev_name,"LED")==0)
        {
            ioctl(fd_leds,atoi(cmd),atoi(arg));
        }
        else if(strcmp(dev_name,"BUZZER")==0)
        {
            ioctl(fd_buzzer,atoi(cmd),atoi(arg));
        }
    }


    close(sock_connect);

    close(fd_leds);
    close(fd_buzzer);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值