forked from xin/TowerOptoSifAndSpectral
添加了温度测试
This commit is contained in:
@ -0,0 +1,115 @@
|
||||
//
|
||||
// Created by xin on 24-12-12.
|
||||
//
|
||||
|
||||
#include "TemperaTureWoker.h"
|
||||
#include "fstream"
|
||||
#include <iostream>
|
||||
#include <qthread.h>
|
||||
TemperaTureWoker::TemperaTureWoker() {
|
||||
m_SerialPort = new QSerialPort();
|
||||
json jsonconfig;
|
||||
std::ifstream i("/home/data/Setting/tempreature.json");
|
||||
//<2F>ж<EFBFBD><D0B6>ļ<EFBFBD><C4BC>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
|
||||
if (!i.is_open())
|
||||
{
|
||||
std::cout << "Error open file" << std::endl;
|
||||
jsonconfig["portname"] = "ttyS2";
|
||||
jsonconfig["baudrate"] = 115200;
|
||||
|
||||
|
||||
}else {
|
||||
i >> jsonconfig;
|
||||
if(jsonconfig.contains("portname")&&jsonconfig["portname"].is_string())
|
||||
{
|
||||
|
||||
}else {
|
||||
jsonconfig["portname"] = "ttyS2";
|
||||
}
|
||||
if(jsonconfig.contains("baudrate")&&jsonconfig["baudrate"].is_number()) {
|
||||
}
|
||||
else {
|
||||
jsonconfig["baudrate"] = 115200;
|
||||
}
|
||||
}
|
||||
m_SerialPort->setPortName(jsonconfig["portname"].get<std::string>().c_str());
|
||||
m_SerialPort->setBaudRate(jsonconfig["baudrate"].get<int>());
|
||||
if (!m_SerialPort->open(QIODevice::ReadWrite))
|
||||
{
|
||||
std::cout << "Error open serial port" << std::endl;
|
||||
m_isInit = false;
|
||||
return;
|
||||
}
|
||||
else {
|
||||
std::cout << "Open serial port success" << std::endl;
|
||||
m_isInit = true;
|
||||
}
|
||||
tempera_group temp1;
|
||||
tempera_group temp2;
|
||||
tempera_group temp3;
|
||||
tempera_group temp4;
|
||||
m_Tempreaturegroups.append(temp1);
|
||||
m_Tempreaturegroups.append(temp2);
|
||||
m_Tempreaturegroups.append(temp3);
|
||||
m_Tempreaturegroups.append(temp4);
|
||||
|
||||
|
||||
ReadTempreature();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void TemperaTureWoker::ReadTempreature() {
|
||||
if (m_isInit) {
|
||||
m_SerialPort->write("{\"command\":\"get_temp\"}");
|
||||
m_SerialPort->waitForReadyRead(1000);
|
||||
//QThread::msleep(100);
|
||||
QByteArray data = m_SerialPort->read(1000);
|
||||
int lenth = data.size();
|
||||
//QThread::msleep(100);
|
||||
int lenthofnow=0;
|
||||
while (lenthofnow != lenth)
|
||||
{
|
||||
lenthofnow = lenth;
|
||||
m_SerialPort->waitForReadyRead(200);
|
||||
data.append(m_SerialPort->readAll());
|
||||
lenth = data.size();
|
||||
//QThread::msleep(100);
|
||||
}
|
||||
|
||||
if (lenth==0) {
|
||||
std::cout << "No data" << std::endl;
|
||||
return;
|
||||
}
|
||||
std::cout << data.toStdString() << std::endl;
|
||||
json jsondata = json::parse(data.toStdString());
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
if(jsondata.contains("Temperature"+std::to_string(j+1))) {
|
||||
int number = jsondata["Temperature"+std::to_string(j+1)]["number"].get<int>();
|
||||
for (int i = 1; i < number+1; ++i) {
|
||||
QString index = "A"+QString::number(i);
|
||||
float temp=jsondata["Temperature"+std::to_string(j+1)][index.toStdString()].get<float>();
|
||||
m_Tempreaturegroups[j].tempera[i-1]=temp;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//<2F><>ӡm_Tempreaturegroups
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
std::cout << "Group " << i << ":\t|";
|
||||
for (int j = 0; j < 8; ++j) {
|
||||
// <20><><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD>Ϊ 10<31><30>ʹ<EFBFBD>ÿո<C3BF><D5B8><EFBFBD><EFBFBD>䣬<EFBFBD><E4A3AC>ͨ<EFBFBD><CDA8> std::internal ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD><D6BE><EFBFBD>
|
||||
std::cout << std::setw(10) << std::setfill(' ') << std::internal
|
||||
<< m_Tempreaturegroups[i].tempera[j] << "\t|";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
} else {
|
||||
std::cout << "Serial port is not init" << std::endl;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user