欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-06-09 08:29:01
...

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

上位机(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);
}