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