嵌入式实训总结

首先,简单地说一下本次实训的主要项目内容。项目使用PC机作为上位机,实现平台为VM下linux中ubuntu版本,下位机是使用华清远见研发中心 FS4412开发板。主体功能是:一、实现开发板外设摄像头拍照功能,二、开发板外接ZigBee协调器读取m0终端的环境数据,三,通过串口控制mo终端的外设。其中将得到的视频信息及环境数据显示在客户端,客户端使用qt开发,服务端运行在开发板上,二者通过TCP协议进行网络通信。可实现多线程通信,但服务器尚且不能并行访问。主体功能大致实现,但代码中存在一些尚未解决的问题可能影响实际效果,仅供参考。
客户端项目代码如下:
嵌入式实训总结_第1张图片

QT客户端部分代码

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
class Widget : public QWidget
{
    Q_OBJECT

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

    void send_pic_one(void)
    {
        sock1->connectToHost(le_hostip->text().toStdString().c_str(),le_port->text().toInt());
        connect(sock1, SIGNAL(connected()), this, SLOT(send_pic_two()));
        qDebug()<<" connect success! ";
    }
    void send_pic_two(void)
    {
        char buf[20]="start";
        sock1->write(buf,sizeof buf);
        qDebug()<<" request success! ";
    }
    void send_tempdata_one(void)
    {
        sock2->connectToHost(le_hostip->text().toStdString().c_str(),le_port->text().toInt());
        connect(sock2, SIGNAL(connected()), this, SLOT(send_tempdata_two()));
        qDebug()<<" connect success! ";
    }
    void send_tempdata_two(void)
    {
        char buf[20]="request";
        //sock->write(buf,sizeof buf);
        sock2->write(buf,sizeof(buf));
    }
    void led_connect(void)
    {
        sock_led->connectToHost(le_hostip->text().toStdString().c_str(),9990);
        connect(sock_led, SIGNAL(connected()), this, SLOT(send_leddata()));
    }
    void send_leddata(void)
    {
        static int led_flag=0;
        if(0==led_flag)
        {
            char CMD_LED_ON[36]={0xdd,4,0x24,0x00,0x00};
            sock_led->write(CMD_LED_ON,sizeof CMD_LED_ON);
            sock_led->disconnectFromHost();
            led_flag++;
        }
        else
        {
            char CMD_LED_OFF[36]={0xdd,4,0x24,0x00,0x01};
            sock_led->write(CMD_LED_OFF,sizeof CMD_LED_OFF);
            sock_led->disconnectFromHost();
            led_flag=0;
        }
    }
    void bee_connect(void)
    {
        sock_bee->connectToHost(le_hostip->text().toStdString().c_str(),9991);
        connect(sock_bee, SIGNAL(connected()), this, SLOT(send_beedata()));

    }
    void send_beedata(void)
    {
        qDebug()<<" good ";
        char CMD_BEE_ON[36]={0xdd,0x04,0x24,0x00,0x02};
        sock_bee->write(CMD_BEE_ON,sizeof CMD_BEE_ON);
    }
    void fan_connect(void)
    {
        sock_fan->connectToHost(le_hostip->text().toStdString().c_str(),9992);
        connect(sock_fan, SIGNAL(connected()), this, SLOT(send_fandata()));
    }
    void send_fandata(void)
    {
       #if 0
        static int fan_flag=0;
        if(0==fan_flag)
        {
            char CMD_FAN_ON[36]={0xdd,4,0x24,0x00,0x04};
            sock_fan->write(CMD_FAN_ON,sizeof CMD_FAN_ON);
            sock_fan->disconnectFromHost();
            fan_flag++;
        }
        else
        {
            char CMD_FAN_OFF[36]={0xdd,4,0x24,0x00,0x08};
            sock_fan->write(CMD_FAN_OFF,sizeof CMD_FAN_OFF);
            sock_fan->disconnectFromHost();
            fan_flag=0;
        }
        #endif
        char CMD_BEE_OFF[36]={0xdd,0x04,0x24,0x00,0x03};
        sock_fan->write(CMD_BEE_OFF,sizeof CMD_BEE_OFF);
        sock_fan->disconnectFromHost();
    }
    void show_data(void)
    {
        char buf[37] = {0};
        int size = sock2->read(buf, sizeof buf);
        qDebug()<<"recv: "<char temp_buf=buf[5];
        char hum_buf=buf[7];
        //qDebug()<
        QString temp_str = QString("%1").arg(temp_buf,0,10);
        QString hum_str_ = QString("%1").arg(hum_buf,0,10);
        le_temp->setText(temp_str);
        le_hum->setText(hum_str_);
        qDebug()<<"successed tooo! ";
    }
     void show_pic(void)
     {
         char buf[320*240*3] = {0};
         int size = sock1->read(buf, sizeof buf);
         qDebug()<<"recv: "<<sizeof(buf);
        QPixmap pix;
        QThread::msleep(100);
        pix.loadFromData((uchar *)buf, size, "jpeg");
        te_pic->setPixmap(pix);
        //sQThread::msleep(120);
     }

private:
    QLabel *te_temp,*te_hostip,*te_port,*te_pic,*te_hum;
    QLineEdit *le_temp,*le_hostip,*le_port,*le_hum;
    QPushButton *bt_led,*bt_start_pic,*bt_start_temp,*bt_start_bee,*bt_stop_bee;
    QTcpSocket *sock1,*sock2,*sock_led,*sock_bee,*sock_fan;

};


#endif // WIDGET_H

嵌入式实训总结_第2张图片
服务端部分代码:

#include "take_pic.h"
#include 
#include 
#include 
#include 
#include       
#include      
#include      
#include 
#include 
#include       
#include    
#include       
#include 
#include 
#include 
#include 
#include 
#include 

pthread_mutex_t mut ;   //the lock

//thread get pic
void thread_getpic()
{
    get_pic(mut) ;
}

//thread get data
void thread_getdata()
{
    //get data referrence
    get_data(mut) ;
}

void thread_getled()
{
    control_led(mut) ;
}

void thread_getbee()
{
    control_bee(mut) ;
}

void thread_getfan()
{
    control_fan(mut) ;
}

int main()
{   
    pthread_t thread_pic , thread_data ,thread_led , thread_bee , thread_fan;
    pthread_create(&thread_pic , NULL , (void *)thread_getpic , NULL) ;
    pthread_create(&thread_data , NULL , (void *)thread_getdata , NULL) ;
    pthread_create(&thread_led , NULL , (void *)thread_getled , NULL) ;
    pthread_create(&thread_bee , NULL , (void *)thread_getbee , NULL) ;
    pthread_create(&thread_fan , NULL , (void *)thread_getfan , NULL) ;
    pthread_join(thread_pic , NULL) ;
    pthread_join(thread_data , NULL) ;
    pthread_join(thread_led , NULL) ;
    pthread_join(thread_bee , NULL) ;
    pthread_join(thread_fan , NULL) ;

    return 0 ;
}

具体代码请看我的上传文件《嵌入式实训代码》。不足之处还望海涵。

你可能感兴趣的:(Linux)