154 lines
4.6 KiB
C++
154 lines
4.6 KiB
C++
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <signal.h>
|
|
#include <termios.h>
|
|
#include <curl/curl.h>
|
|
#include <iostream>
|
|
#include <fstream>
|
|
#include <cstring>
|
|
#include <vector>
|
|
#include <iomanip>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <sstream>
|
|
#include <opencv2/opencv.hpp>
|
|
#include <mutex>
|
|
|
|
#define SERIAL_PORT "/dev/ttyS0"
|
|
#define BAUD_RATE B115200
|
|
#define ALARM_TEMPERATURE 45
|
|
static int serialPort;
|
|
float temperature_img[24][32];
|
|
unsigned char buffer[1544];
|
|
std::mutex mtx;
|
|
|
|
//输入进入ota的命令
|
|
void put_start_char(){
|
|
unsigned char start_data[] = {0xa5,0x35,0x01,0xdb};
|
|
write(serialPort, start_data, sizeof(start_data));
|
|
}
|
|
|
|
void createTemperatureImage(float temperature_img[24][32]) {
|
|
// 将浮点数数组转换为 OpenCV 的 Mat 对象
|
|
cv::Mat temperature_mat(24, 32, CV_32FC1, temperature_img);
|
|
|
|
// 将浮点数图像归一化到 0 到 255 范围内
|
|
cv::normalize(temperature_mat, temperature_mat, 0, 255, cv::NORM_MINMAX);
|
|
|
|
cv::Mat temperature_uint8;
|
|
temperature_mat.convertTo(temperature_uint8, CV_8U);
|
|
|
|
// 创建目标大小的 Mat 对象
|
|
cv::Mat img_1920(1080, 1440, CV_32FC1);
|
|
|
|
// 将原始图像拉伸到目标大小
|
|
cv::resize(temperature_uint8, img_1920, img_1920.size(), 0, 0, cv::INTER_LINEAR);
|
|
|
|
// 使用伪彩色映射创建图像
|
|
cv::Mat temperature_color;
|
|
// applyColorMap(temperature_uint8, temperature_color, cv::COLORMAP_JET);
|
|
applyColorMap(img_1920, temperature_color, cv::COLORMAP_JET);
|
|
|
|
// 将结果保存
|
|
cv::imwrite("temperature_image.jpg", temperature_color);
|
|
}
|
|
|
|
void *read_serial_thread(void *args)
|
|
{
|
|
ssize_t bytesRead;
|
|
serialPort = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NDELAY);
|
|
if (serialPort == -1) {
|
|
printf("Failed to open serial port: %s\n", SERIAL_PORT);
|
|
return 0;
|
|
}
|
|
|
|
struct termios tty;
|
|
memset(&tty, 0, sizeof(tty));
|
|
if (tcgetattr(serialPort, &tty) != 0) {
|
|
printf("Failed to get serial port attributes\n");
|
|
close(serialPort);
|
|
return 0;
|
|
}
|
|
cfsetospeed(&tty, BAUD_RATE);
|
|
cfsetispeed(&tty, BAUD_RATE);
|
|
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
|
|
tty.c_cflag &= ~(PARENB | PARODD);
|
|
tty.c_cflag &= ~CSTOPB;
|
|
tty.c_cflag |= CREAD | CLOCAL;
|
|
tty.c_iflag = IGNPAR;
|
|
tty.c_oflag = 0;
|
|
tty.c_lflag = 0;
|
|
if (tcsetattr(serialPort, TCSANOW, &tty) != 0) {
|
|
printf("Failed to set serial port attributes\n");
|
|
close(serialPort);
|
|
return 0;
|
|
}
|
|
|
|
while(1){
|
|
mtx.lock();
|
|
bytesRead = read(serialPort, buffer, sizeof(buffer));
|
|
mtx.unlock();
|
|
if (bytesRead>0) {
|
|
if((buffer[0]== 0x5a)&&(buffer[1]==0x5a)) {
|
|
printf("readed\n");
|
|
}else{
|
|
printf("read failed\n");
|
|
}
|
|
}else{
|
|
// printf("empty to read\n");
|
|
}
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
pthread_t read_serial;
|
|
pthread_create(&read_serial, NULL, read_serial_thread, NULL);
|
|
auto last_time = std::chrono::high_resolution_clock::now();
|
|
unsigned char copyBuffer[1544];
|
|
|
|
while(1){
|
|
int time2run = 0;
|
|
auto current_time = std::chrono::high_resolution_clock::now();
|
|
auto elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(current_time - last_time);// 计算距离开始时间的时间差
|
|
|
|
if (elapsed_time.count() - 3000 > 0) {
|
|
last_time = current_time;
|
|
time2run = 1;
|
|
}
|
|
if(time2run) {
|
|
mtx.lock();
|
|
memcpy(copyBuffer, buffer, sizeof(buffer));
|
|
mtx.unlock();
|
|
//计算温度
|
|
double temperature;
|
|
int Col,Row;
|
|
int j = 0;
|
|
for (int i = 4; i < 1540; i += 2) {
|
|
temperature = (buffer[i+1]*256+buffer[i])/100.0;
|
|
Col = 31-(j)%32;
|
|
Row = (j)/32;
|
|
// printf("[%d,%d] %.2f ", Row, Col, temperature);
|
|
// printf("[%d,%d]", Row, Col);
|
|
temperature_img[Row][Col] = temperature;
|
|
j++;
|
|
}
|
|
//按视觉顺序存入数组
|
|
for(int i=0; i<24; i++){
|
|
for(j=0; j<32; j++){
|
|
if(temperature_img[i][j]>ALARM_TEMPERATURE)
|
|
printf("[%.1f] ", temperature_img[i][j]);
|
|
else
|
|
printf("%.1f ", temperature_img[i][j]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
createTemperatureImage(temperature_img);
|
|
}
|
|
}
|
|
return 0;
|
|
} |