- #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;
- }
复制代码
|