- #include "widget.h"
- #include "ui_widget.h"
- #include <QHostAddress>
- #define WNDTITLE "智能家居溫濕度監測系統"
- Widget::Widget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Widget)
- {
- ui->setupUi(this);
- //分配一個套接字對象
- s = new QTcpSocket(this);
- //關聯信號槽
- connect(s, SIGNAL(connected()),
- this, SLOT(slotConnected()));
- connect(s, SIGNAL(disconnected()),
- this, SLOT(slotDisconnected()));
- connect(s, SIGNAL(error(QAbstractSocket::SocketError)),
- this, SLOT(slotError()));
- connect(s, SIGNAL(readyRead()),
- this, SLOT(slotReadyRead()));
- //設置窗口標題
- setWindowTitle(WNDTITLE);
- }
- Widget::~Widget()
- {
- delete ui;
- }
- void Widget::slotConnected()
- {
- //修改按鈕顯示的文本:設置按鈕標題
- ui->pushButton->setText("連接成功");
- //修改窗口標題
- setWindowTitle(QString(WNDTITLE)+"-連接成功");
- }
- void Widget::slotDisconnected()
- {
- //按鈕變亮:按鈕使能
- ui->pushButton->setEnabled(true);
- //修改按鈕顯示的文本:設置按鈕標題
- ui->pushButton->setText("連接");
- //修改窗口標題
- setWindowTitle(QString(WNDTITLE)+"-"+s->errorString());
- }
- void Widget::slotError()
- {
- //按鈕變亮:按鈕使能
- ui->pushButton->setEnabled(true);
- //修改按鈕顯示的文本:設置按鈕標題
- ui->pushButton->setText("連接");
- //修改窗口標題
- setWindowTitle(QString(WNDTITLE)+"-"+s->errorString());
- }
- void Widget::slotReadyRead()
- {
- //服務器每秒發4字節
- //溫度值: 1字節(整數位).0字節(小數位)
- //溫度值: 3字節(整數位).2字節(小數位)
- if(s->bytesAvailable() >= 4){ //判斷套接字緩存是否大于等于4bytes
- //定義4字節數組
- char buffer[4];
- //從套接字緩存讀出4字節
- s->read(buffer, 4);
- QString temp;
- //格式化字符串:顯示溫度值
- temp.sprintf("溫度值:%d.%d", buffer[1], buffer[0]);
- ui->tempLB->setText(temp);
- QString hum;
- //格式化字符串:顯示濕度值
- hum.sprintf("濕度值:%d.%d", buffer[3], buffer[2]);
- ui->humLB->setText(hum);
- }
- }
- void Widget::on_pushButton_clicked()
- {
- //定義一個字符串對象
- QString ip;
- //把行編輯器上的文本保存到ip
- ip = ui->lineEdit->text();
- s->connectToHost(QHostAddress(ip), 59999);
- //按鈕變灰:按鈕失效
- ui->pushButton->setEnabled(false);
- //修改按鈕顯示的文本:設置按鈕標題
- ui->pushButton->setText("連接中...");
- //修改窗口標題
- setWindowTitle(QString(WNDTITLE)+"-連接中...");
- }
- 透傳服務器main函數:
- #include <stdio.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <unistd.h>
- #include "serial.h"
- int init_server(ushort port, int num)
- {
- int s = socket(AF_INET, SOCK_STREAM, 0);
- if(0 > s){
- perror("socket");
- return -1;
- }
- struct sockaddr_in addr = {
- .sin_family = AF_INET,
- .sin_port = htons(port),
- .sin_addr = {
- .s_addr = INADDR_ANY,
- },
- };
- socklen_t len = 16;
- if(0 > bind(s, (struct sockaddr *)&addr, len)){
- perror("bind");
- goto ERR_STEP;
- }
- if(0 > listen(s, num)){
- perror("listen");
- goto ERR_STEP;
- }
- return s;
- ERR_STEP:
- close(s);
- return -1;
- }
- int main()
- {
- int fd = init_serial("/dev/ttyUSB0", B115200, 'n', 8, 1);
- if(0 > fd){
- return -1;
- }
- printf("init M0 device done.\n");
- int s = init_server(59999, 10);
- if(0 > s){
- return -1;
- }
- printf("Wait for a client ...\n");
- int rws = accept(s, NULL, NULL);
- if(0 > rws){
- perror("accept");
- return -1;
- while(1){
- char buf[36];
- int len = 36;
- char *p = buf;
- while(len){
- int size = recv_serial(fd, p, len, 300000);
- if(0 > size){
- return -1;
- }
- len -= size;
- p += size;
- }
- printf("temp: %d.%d\n", buf[5], buf[4]);
- printf(" hum: %d.%d\n", buf[7], buf[6]);
- if(4 != write(rws, buf+4, 4)){
- printf("snd temp & hum fail.\n");
- return -1;
- }
- }
- return 0;
- }
復制代碼
|