Compare commits
2 Commits
5.26last
...
3.11latest
Author | SHA1 | Date | |
---|---|---|---|
0a54756274 | |||
9226b7b6e2 |
@ -1,5 +1,3 @@
|
||||
# 本串口协议必备要求:先发送在接收
|
||||
|
||||
## 注:本项目已经通过串口测试(传感器为115200,摄像头为9500),每次报警前会识别设备名称序号,然后反馈到串口输出序列中
|
||||
|
||||
# rknn_yolo_EAI_pic
|
||||
|
@ -1,81 +0,0 @@
|
||||
/***********************************************************************
|
||||
* @file jlinux_uart.h
|
||||
JLINUX_UART
|
||||
* @brief header file
|
||||
* @history
|
||||
* Date Version Author description
|
||||
* ========== ======= ========= =======================================
|
||||
* 2022-07-27 V1.0 Lucky,lukai@jovision.com Create
|
||||
*
|
||||
* @Copyright (C) 2022 Jovision Technology Co., Ltd.
|
||||
***********************************************************************/
|
||||
#ifndef __JLINUX_UART_H__
|
||||
#define __JLINUX_UART_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef struct _uart_ctx *juart_hdl_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int baudrate; //波特率:1200/2400/4800/9600/19200/38400/57600/115200/230400/380400/460800/921600
|
||||
int datawidth; //数据位宽度:5/6/7/8
|
||||
int stopbit; //停止位宽度:1/2
|
||||
int parity; //奇偶校验:0无校验,1奇校验,2偶校验
|
||||
}JUartAttr_t;
|
||||
|
||||
/**
|
||||
*@brief 打开串口设备设备,如果是485设备请配合jctrl_rs485相关接口使用
|
||||
*@param name 设备名称,如:/dev/ttyS0
|
||||
*@return 句柄
|
||||
*/
|
||||
juart_hdl_t juart_open(const char *name);
|
||||
|
||||
/**
|
||||
*@brief 关闭串口
|
||||
*@param handle 句柄
|
||||
*/
|
||||
int juart_close(juart_hdl_t handle);
|
||||
|
||||
/**
|
||||
*@brief 配置串口属性
|
||||
*@param handle 句柄
|
||||
*@param attr 属性
|
||||
*/
|
||||
int juart_set_attr(juart_hdl_t handle, JUartAttr_t *attr);
|
||||
|
||||
int juart_get_fd(juart_hdl_t uart);
|
||||
/**
|
||||
*@brief 通过串口发送数据
|
||||
*@param handle 句柄
|
||||
*@param data 数据buffer
|
||||
*@param len 数据长度
|
||||
*@return 0成功
|
||||
*/
|
||||
int juart_send(juart_hdl_t handle, char *data, int len);
|
||||
|
||||
/**
|
||||
*@brief 通过串口接受数据
|
||||
*@param handle 句柄
|
||||
*@param data 数据buffer
|
||||
*@param len buffer的长度
|
||||
*@param timeout 超时毫秒
|
||||
*@return 收到数据的长度
|
||||
*/
|
||||
int juart_recv(juart_hdl_t handle, char *data, int len, int timeout);
|
||||
|
||||
/**
|
||||
*@brief 设置rs485模式
|
||||
*@param handle 句柄
|
||||
*@param mode 0:发送后立刻置为接收状态;非0:发送后保持发送状态
|
||||
*@return 0 成功
|
||||
*/
|
||||
int juart_set_rs485(juart_hdl_t handle, int mode);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif // __JLINUX_UART_H__
|
1826
src/6.10main .cpp
1826
src/6.10main .cpp
File diff suppressed because it is too large
Load Diff
@ -1,313 +0,0 @@
|
||||
/***********************************************************************
|
||||
* @file jctrl_uart.cpp
|
||||
JCTRL_UART
|
||||
* @brief header file
|
||||
* @history
|
||||
* Date Version Author description
|
||||
* ========== ======= ========= =======================================
|
||||
* 2022-07-21 V1.0 Lucky,lukai@jovision.com Create
|
||||
*
|
||||
* @Copyright (C) 2022 Jovision Technology Co., Ltd.
|
||||
***********************************************************************/
|
||||
#include <termios.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <linux/stat.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/serial.h>
|
||||
#include "jlinux_uart.h"
|
||||
|
||||
struct _uart_ctx{
|
||||
int fd;
|
||||
};
|
||||
|
||||
int _get_baudrate(int nBaud)
|
||||
{
|
||||
switch(nBaud)
|
||||
{
|
||||
case 1200:
|
||||
return B1200; //注:B1200为系统定义
|
||||
case 2400:
|
||||
return B2400;
|
||||
case 4800:
|
||||
return B4800;
|
||||
case 9600:
|
||||
return B9600;
|
||||
case 19200 :
|
||||
return B19200;
|
||||
case 38400:
|
||||
return B38400;
|
||||
case 57600:
|
||||
return B57600;
|
||||
case 115200:
|
||||
return B115200;
|
||||
default:
|
||||
return B2400;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
juart_hdl_t juart_open(const char *name){
|
||||
juart_hdl_t uart = new _uart_ctx;
|
||||
uart->fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY | O_EXCL|O_SYNC);
|
||||
return uart;
|
||||
}
|
||||
int juart_close(juart_hdl_t uart){
|
||||
if(uart->fd>0)
|
||||
close(uart->fd);
|
||||
uart->fd = 0;
|
||||
return 0;
|
||||
}
|
||||
int juart_get_fd(juart_hdl_t uart){
|
||||
return uart->fd;
|
||||
}
|
||||
int juart_set_attr(juart_hdl_t uart, JUartAttr_t *attr){
|
||||
if (uart->fd <= 0)
|
||||
{
|
||||
printf("jv_uart_recv_ex fd error\n");
|
||||
return -1;
|
||||
}
|
||||
struct termios newtio, oldtio;
|
||||
memset(&oldtio, 0, sizeof(oldtio));
|
||||
/* save the old serial port configuration */
|
||||
if (tcgetattr(uart->fd, &oldtio) != 0) {
|
||||
perror("set_port/tcgetattr");
|
||||
return -1;
|
||||
}
|
||||
memset(&newtio, 0, sizeof(newtio));
|
||||
//设置波特率
|
||||
int nBaud = _get_baudrate(attr->baudrate);
|
||||
switch (nBaud)
|
||||
{
|
||||
case B300:
|
||||
case B1200:
|
||||
case B2400:
|
||||
case B4800:
|
||||
case B9600:
|
||||
case B19200:
|
||||
case B38400:
|
||||
case B57600:
|
||||
case B115200:
|
||||
cfsetospeed(&newtio, nBaud);
|
||||
cfsetispeed(&newtio, nBaud);
|
||||
break;
|
||||
default:
|
||||
printf("jv_uart_set_attr:Unsupported baudrate!\n");
|
||||
return -1;
|
||||
}
|
||||
/* ignore modem control lines and enable receiver */
|
||||
newtio.c_cflag |= CLOCAL | CREAD;
|
||||
newtio.c_cflag &= ~CSIZE;
|
||||
/* set character size */
|
||||
switch (attr->datawidth) {
|
||||
case 5:
|
||||
newtio.c_cflag |= CS5;
|
||||
break;
|
||||
case 6:
|
||||
newtio.c_cflag |= CS6;
|
||||
break;
|
||||
case 7:
|
||||
newtio.c_cflag |= CS7;
|
||||
break;
|
||||
case 8:
|
||||
default:
|
||||
newtio.c_cflag |= CS8;
|
||||
break;
|
||||
}
|
||||
/* set the stop bits */
|
||||
switch (attr->stopbit) {
|
||||
default:
|
||||
case 1:
|
||||
newtio.c_cflag &= ~CSTOPB;
|
||||
break;
|
||||
case 2:
|
||||
newtio.c_cflag |= CSTOPB;
|
||||
break;
|
||||
}
|
||||
/* set the parity */
|
||||
switch (attr->parity) {
|
||||
case 'o':
|
||||
case 'O':
|
||||
case 1:
|
||||
newtio.c_cflag |= PARENB;
|
||||
newtio.c_cflag |= PARODD;
|
||||
newtio.c_iflag |= INPCK;
|
||||
break;
|
||||
case 'e':
|
||||
case 'E':
|
||||
case 2:
|
||||
newtio.c_cflag |= PARENB;
|
||||
newtio.c_cflag &= ~PARODD;
|
||||
newtio.c_iflag |= INPCK;
|
||||
break;
|
||||
case 'n':
|
||||
case 'N':
|
||||
case 0:
|
||||
default:
|
||||
newtio.c_cflag &= ~PARENB;
|
||||
newtio.c_iflag &= ~INPCK;
|
||||
break;
|
||||
}
|
||||
/* Raw input */
|
||||
newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
|
||||
/* Software flow control is disabled */
|
||||
newtio.c_iflag &= ~(IXON | IXOFF | IXANY);
|
||||
/* Raw ouput */
|
||||
newtio.c_oflag &=~ OPOST;
|
||||
/* set timeout in deciseconds for non-canonical read */
|
||||
newtio.c_cc[VTIME] = 0;
|
||||
/* set minimum number of characters for non-canonical read */
|
||||
newtio.c_cc[VMIN] = 0;
|
||||
/* flushes data received but not read */
|
||||
tcflush(uart->fd, TCIFLUSH);
|
||||
/* set the parameters associated with the terminal from
|
||||
the termios structure and the change occurs immediately */
|
||||
if ((tcsetattr(uart->fd, TCSANOW, &newtio)) != 0) {
|
||||
perror("set_port/tcsetattr");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int juart_send(juart_hdl_t uart, char *data, int len){
|
||||
if (uart->fd > 0)
|
||||
{
|
||||
int ret = write(uart->fd, data, len);
|
||||
if (ret == len)
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
int _modbus_rtu_select(juart_hdl_t uart, struct timeval *tv)
|
||||
{
|
||||
fd_set rfds;
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(uart->fd, &rfds);
|
||||
int s_rc;
|
||||
while ((s_rc = select(uart->fd+1, &rfds, NULL, NULL, tv)) == -1) {
|
||||
if (errno == EINTR) {
|
||||
fprintf(stderr, "A non blocked signal was caught\n");
|
||||
/* Necessary after an error */
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(uart->fd, &rfds);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (s_rc == 0) {
|
||||
/* Timeout */
|
||||
errno = ETIMEDOUT;
|
||||
return -1;
|
||||
}
|
||||
return s_rc;
|
||||
}
|
||||
int juart_recv(juart_hdl_t uart, char *data, int len, int timeout){
|
||||
if (uart->fd > 0)
|
||||
{
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = timeout*1000;
|
||||
if(_modbus_rtu_select(uart, &tv) > 0){
|
||||
return read(uart->fd, data, len);
|
||||
}
|
||||
return -1;
|
||||
}else{
|
||||
usleep(timeout*1000);
|
||||
}
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*@brief 接收串口数据扩展接口,从收到start开始接收,到收到stop返回
|
||||
*@param handle 句柄
|
||||
*@param data 接收数据缓存
|
||||
*@param len 缓存大小
|
||||
*@param start 数据头标志:前nstart个字节
|
||||
*@param nstart 数据头字节数
|
||||
*@param stop 数据截止标志:后nstop个字节
|
||||
*@param nstop 数据截止标志的字节数
|
||||
*/
|
||||
extern "C" int juart_recv_ex(juart_hdl_t handle, char *data, int len, char *start, int nstart, char *stop, int nstop, int timeout);
|
||||
int juart_recv_ex(juart_hdl_t uart, char *data, int len, char *start, int nstart, char *stop, int nstop, int timeout){
|
||||
if (uart->fd <= 0)
|
||||
{
|
||||
printf("jv_uart_recv_ex fd error\n");
|
||||
return -1;
|
||||
}
|
||||
int offset = 0;
|
||||
int offset_end = 0;
|
||||
int bytes_read = 0;
|
||||
while (1)
|
||||
{
|
||||
bytes_read = read(uart->fd, &data[offset], 1);
|
||||
if (bytes_read == 1 && offset < nstart && data[offset] == start[offset])
|
||||
{
|
||||
offset++;
|
||||
}
|
||||
if (offset == nstart)
|
||||
break;
|
||||
if (bytes_read < 1)
|
||||
usleep(0);
|
||||
}
|
||||
while (offset < len)
|
||||
{
|
||||
if (data[offset] == stop[offset_end])
|
||||
{
|
||||
offset_end++;
|
||||
}
|
||||
if (offset_end == nstop)
|
||||
break;
|
||||
bytes_read = read(uart->fd, &data[offset], 1);
|
||||
if (bytes_read == 1)
|
||||
{
|
||||
offset++;
|
||||
}
|
||||
else
|
||||
{
|
||||
usleep(0);
|
||||
}
|
||||
}
|
||||
return offset;
|
||||
}
|
||||
|
||||
/**
|
||||
*@brief 设置rs485模式
|
||||
*@param handle 句柄
|
||||
*@param mode 0:发送后立刻置为接收状态;非0:发送后保持发送状态
|
||||
*@return 0 成功
|
||||
*/
|
||||
int juart_set_rs485(juart_hdl_t handle, int mode)
|
||||
{
|
||||
struct serial_rs485 rs485;
|
||||
if (ioctl(handle->fd, TIOCGRS485, &rs485) == -1)
|
||||
{
|
||||
printf("TIOCGRS485 ioctl error.\n");
|
||||
return -1;
|
||||
}
|
||||
rs485.flags |= SER_RS485_ENABLED;
|
||||
if (mode == 0)
|
||||
{
|
||||
rs485.flags &= ~SER_RS485_RTS_ON_SEND;
|
||||
rs485.flags |= SER_RS485_RTS_AFTER_SEND;
|
||||
}
|
||||
else
|
||||
{
|
||||
rs485.flags |= SER_RS485_RTS_ON_SEND;
|
||||
rs485.flags &= ~SER_RS485_RTS_AFTER_SEND;
|
||||
}
|
||||
rs485.delay_rts_before_send = 0;
|
||||
rs485.delay_rts_after_send = 0;
|
||||
if (ioctl(handle->fd, TIOCSRS485, &rs485) == -1)
|
||||
{
|
||||
printf("TIOCSRS485 ioctrl error.\n");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
357
src/main.cpp
357
src/main.cpp
@ -41,7 +41,7 @@
|
||||
#include <jes/jes_isp.h>
|
||||
#include <jes/jes_spk.h>
|
||||
#include <jes/jes_bas.h>
|
||||
#include "jlinux_uart.h"
|
||||
#include <jlinux/jlinux_mem.h>
|
||||
|
||||
#include <termios.h>
|
||||
#include <cmath>
|
||||
@ -73,10 +73,7 @@ static int serialPortInfraredSensor;
|
||||
static int serialPortSolenoid;
|
||||
float temperature_img[24][32];
|
||||
unsigned char buffer[1544];
|
||||
|
||||
std::mutex mtx;
|
||||
std::vector<uint8_t> ControlInstructions(16, 0x00);
|
||||
|
||||
|
||||
#define BAS_TEST_BUS_NAME "bastest"
|
||||
jbas_hdl_t g_jbas;
|
||||
@ -91,20 +88,6 @@ using json = nlohmann::json;
|
||||
// 定义GPIO控制引脚(假设GPIO2_B0已经设置好)
|
||||
#define RELAY_GPIO_PIN 116 // 使用GPIO 72控制继电器
|
||||
|
||||
|
||||
|
||||
|
||||
void hexdump(const char *buf, int len)
|
||||
{
|
||||
//printf("hexdump: len:%d \n", len);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
printf("\033[0m\033[1;33m%02X \033[0m", buf[i], buf[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
// 控制继电器复位的函数
|
||||
void reset_relay() {
|
||||
// 导出GPIO引脚
|
||||
@ -161,8 +144,7 @@ public:
|
||||
}
|
||||
throw runtime_error("activation_url not found in config");
|
||||
|
||||
}
|
||||
catch (const exception& e) {
|
||||
} catch (const exception& e) {
|
||||
cerr << "[WARN] " << e.what() << " - Using default URL" << endl;
|
||||
return "http://183.238.1.242:8889/api/label/security";
|
||||
}
|
||||
@ -191,8 +173,7 @@ public:
|
||||
for (char c : value) {
|
||||
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
|
||||
escaped << c;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
escaped << '%' << setw(2) << int((unsigned char)c);
|
||||
}
|
||||
}
|
||||
@ -226,8 +207,7 @@ public:
|
||||
|
||||
result = base64Encode(encrypted.data(), len);
|
||||
|
||||
}
|
||||
catch (const exception& e) {
|
||||
} catch (const exception& e) {
|
||||
cerr << "[ERROR] RSA encryption failed: " << e.what() << endl;
|
||||
ERR_print_errors_fp(stderr);
|
||||
result.clear();
|
||||
@ -412,8 +392,7 @@ private:
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
catch (const json::exception& e) {
|
||||
} catch (const json::exception& e) {
|
||||
cerr << "[ERROR] JSON parse error: " << e.what() << endl;
|
||||
}
|
||||
}
|
||||
@ -533,8 +512,7 @@ string Get_Time(int input) {
|
||||
<< std::setw(2) << hour << ":"
|
||||
<< std::setw(2) << minute << ":"
|
||||
<< std::setw(2) << second;
|
||||
}
|
||||
else {
|
||||
}else{
|
||||
oss_alarmTime << std::setfill('0')
|
||||
<< std::setw(2) << month << "_"
|
||||
<< std::setw(2) << day << "_"
|
||||
@ -616,8 +594,7 @@ void readFileAndStoreInGlobal(const std::string& filename) {
|
||||
if (file.is_open()) {
|
||||
std::getline(file, DeviceID); // 读取一行并存储在全局字符串中
|
||||
file.close();
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
std::cerr << "无法打开文件: " << filename << std::endl;
|
||||
}
|
||||
}
|
||||
@ -750,8 +727,7 @@ bool directoryExists(const std::string& path) {
|
||||
struct stat info;
|
||||
if (stat(path.c_str(), &info) != 0) {
|
||||
return false; // 文件夹不存在
|
||||
}
|
||||
else if (info.st_mode & S_IFDIR) {
|
||||
} else if (info.st_mode & S_IFDIR) {
|
||||
return true; // 文件夹存在
|
||||
}
|
||||
return false; // 存在但不是文件夹
|
||||
@ -763,13 +739,11 @@ bool createDirectory(const std::string& path) {
|
||||
if (mkdir(path.c_str(), 0755) == 0) {
|
||||
std::cout << "文件夹创建成功: " << path << std::endl;
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
std::cerr << "文件夹创建失败: " << strerror(errno) << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
std::cout << "文件夹已存在: " << path << std::endl;
|
||||
return true;
|
||||
}
|
||||
@ -802,8 +776,6 @@ int main(int argc, char** argv)
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 获取设备信息
|
||||
jbus_hdl_t jbus = jbus_init("devinfo_test_app");
|
||||
if (!jbus) {
|
||||
@ -818,35 +790,7 @@ int main(int argc, char** argv)
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
JBASDevInfo_t devinfo;
|
||||
int results = JES_BAS_GetDevInfo(jbas_handle, &devinfo, JFALSE);
|
||||
|
||||
if (results == 0) {
|
||||
printf("Successfully retrieved device information:\n");
|
||||
PRSTR(devinfo.devname);
|
||||
string CA_ID = devinfo.devname;
|
||||
PRSTR(devinfo.language);
|
||||
|
||||
//获取设备
|
||||
// 提取设备名称中的摄像头编号
|
||||
if (!CA_ID.empty()) {
|
||||
char lastChar = CA_ID.back(); // 获取最后一个字符
|
||||
if (isdigit(lastChar) && lastChar >= '1' && lastChar <= '4') {
|
||||
g_camera_number = lastChar - '0'; // 转换为整数,例如 '2' -> 2
|
||||
}
|
||||
else {
|
||||
g_camera_number = 1; // 默认值
|
||||
fprintf(stderr, "Warning: Invalid device name format: %s\n", CA_ID.c_str());
|
||||
}
|
||||
}
|
||||
else {
|
||||
g_camera_number = 1; // 默认值
|
||||
fprintf(stderr, "Warning: Device name is empty\n");
|
||||
}
|
||||
|
||||
// 打印验证
|
||||
printf("Camera number: %d\n", g_camera_number);
|
||||
}
|
||||
|
||||
|
||||
// 打开 JSON 文件
|
||||
@ -945,10 +889,6 @@ int main(int argc, char** argv)
|
||||
pthread_t fire_rknn_tidp;
|
||||
pthread_t smog_rknn_tidp;
|
||||
pthread_t heart_beat_tidp;
|
||||
|
||||
pthread_t storage_serial;
|
||||
pthread_create(&storage_serial, NULL, storage_serial_thread, NULL);
|
||||
|
||||
pthread_create(&fire_rknn_tidp, NULL, rkmedia_rknn_thread, model_fire);
|
||||
// pthread_create(&heart_beat_tidp, NULL, heart_beat, NULL);
|
||||
//串口读数据线程
|
||||
@ -956,7 +896,6 @@ int main(int argc, char** argv)
|
||||
pthread_create(&read_serial, NULL, read_serial_thread, NULL);
|
||||
printf("%s initial finish\n", __func__);
|
||||
|
||||
|
||||
while (!quit)
|
||||
{
|
||||
usleep(500000);
|
||||
@ -966,8 +905,7 @@ int main(int argc, char** argv)
|
||||
JES_ISP_Deinit(g_jisp);
|
||||
jbus_cleanup(g_jbus);
|
||||
|
||||
}
|
||||
catch (const exception& e) {
|
||||
} catch (const exception& e) {
|
||||
cerr << "[FATAL] " << e.what() << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
@ -977,12 +915,41 @@ int main(int argc, char** argv)
|
||||
void *rkmedia_rknn_thread(void *args)
|
||||
{
|
||||
pthread_detach(pthread_self());
|
||||
//打开协议串口
|
||||
serialPortSolenoid = open(SERIAL_PORT_SOLENOID, O_RDWR | O_NOCTTY | O_NDELAY);
|
||||
cout << "02" << endl;
|
||||
|
||||
if (serialPortSolenoid == -1) {
|
||||
printf("Failed to open serial port: %s\n", SERIAL_PORT_SOLENOID);
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct termios tty;
|
||||
memset(&tty, 0, sizeof(tty));
|
||||
if (tcgetattr(serialPortSolenoid, &tty) != 0) {
|
||||
printf("Failed to get serial port attributes\n");
|
||||
close(serialPortSolenoid);
|
||||
return 0;
|
||||
}
|
||||
cfsetospeed(&tty, B9600);
|
||||
cfsetispeed(&tty, B9600);
|
||||
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(serialPortSolenoid, TCSANOW, &tty) != 0) {
|
||||
printf("Failed to set serial port attributes\n");
|
||||
close(serialPortSolenoid);
|
||||
return 0;
|
||||
}
|
||||
//初始化协议信息
|
||||
// ControlInstructions[0] = 0xCA; //协议头
|
||||
// ControlInstructions[1] = 0x14; //总长度
|
||||
// ControlInstructions[2] = 0x01; //操作指令
|
||||
std::vector<uint8_t> ControlInstructions(20,0x00);
|
||||
ControlInstructions[0] = 0xCA; //协议头
|
||||
ControlInstructions[1] = 0x14; //总长度
|
||||
ControlInstructions[2] = 0x01; //操作指令
|
||||
|
||||
//打开JES的通道
|
||||
jmss_raw_t *rawchn = JES_MSS_RawOpen(g_jmss, 0, width, height, type);
|
||||
@ -1028,8 +995,6 @@ void* rkmedia_rknn_thread(void* args)
|
||||
compression_params.push_back(30); // 设置压缩质量,范围为 0-100
|
||||
int overtem_cnt = 0;
|
||||
std::vector<int> over_tmp_deque(10, 0);
|
||||
|
||||
|
||||
/* 算法运行 */
|
||||
while(!quit){
|
||||
int time2run = 1;
|
||||
@ -1068,22 +1033,22 @@ void* rkmedia_rknn_thread(void* args)
|
||||
char result_warn[4]={'0','0','0','0'};
|
||||
char result_fire_rknn[4]={'0','0','0','0'};
|
||||
//初始化ControlInstructions
|
||||
// ControlInstructions[3]=0x00;
|
||||
// ControlInstructions[4]=0x00;
|
||||
// ControlInstructions[5]=0x00;
|
||||
// ControlInstructions[6]=0x00;
|
||||
// ControlInstructions[7]=0x00;
|
||||
// ControlInstructions[8]=0x00;
|
||||
// ControlInstructions[9]=0x00;
|
||||
// ControlInstructions[10]=0x00;
|
||||
// ControlInstructions[11]=0x00;
|
||||
// ControlInstructions[12]=0x00;
|
||||
// ControlInstructions[13]=0x00;
|
||||
// ControlInstructions[14]=0x00;
|
||||
// ControlInstructions[15]=0x00;
|
||||
// ControlInstructions[16]=0x00;
|
||||
// ControlInstructions[17]=0x00;
|
||||
// ControlInstructions[18]=0x00;
|
||||
ControlInstructions[3]=0x00;
|
||||
ControlInstructions[4]=0x00;
|
||||
ControlInstructions[5]=0x00;
|
||||
ControlInstructions[6]=0x00;
|
||||
ControlInstructions[7]=0x00;
|
||||
ControlInstructions[8]=0x00;
|
||||
ControlInstructions[9]=0x00;
|
||||
ControlInstructions[10]=0x00;
|
||||
ControlInstructions[11]=0x00;
|
||||
ControlInstructions[12]=0x00;
|
||||
ControlInstructions[13]=0x00;
|
||||
ControlInstructions[14]=0x00;
|
||||
ControlInstructions[15]=0x00;
|
||||
ControlInstructions[16]=0x00;
|
||||
ControlInstructions[17]=0x00;
|
||||
ControlInstructions[18]=0x00;
|
||||
|
||||
if(time2run){
|
||||
cout << "run ." << endl;
|
||||
@ -1159,12 +1124,10 @@ void* rkmedia_rknn_thread(void* args)
|
||||
Alarm.ifalarm = 1;
|
||||
over_tmp_deque.assign(9, 0);
|
||||
over_tmp_deque.push_back(1);
|
||||
}
|
||||
else {
|
||||
}else{
|
||||
memset(result, '0', 4*sizeof(char));
|
||||
}
|
||||
}
|
||||
else {
|
||||
}else{
|
||||
over_tmp_deque.push_back(0);
|
||||
// 保持队列长度为10,如果超过长度则移除最前面的元素
|
||||
if (over_tmp_deque.size() > 10) {
|
||||
@ -1200,8 +1163,7 @@ void* rkmedia_rknn_thread(void* args)
|
||||
std::string resultString_warn(result_warn, 4);
|
||||
std::cout << WARN_TEMPERATURE << "度结果: " << resultString_warn << std::endl;
|
||||
temperature_log << WARN_TEMPERATURE <<"度结果: " << resultString_warn << std::endl;
|
||||
}
|
||||
else if (Alarm.ifwarn) {
|
||||
}else if(Alarm.ifwarn){
|
||||
printf("temprature > %d°C !\n", WARN_TEMPERATURE);
|
||||
std::string resultString_warn(result_warn, 4);
|
||||
std::cout << WARN_TEMPERATURE << "结果: " << resultString_warn << std::endl;
|
||||
@ -1280,7 +1242,10 @@ void* rkmedia_rknn_thread(void* args)
|
||||
plot_one_box(src, x1, x2, y1, y2, label_text, i%10);
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(Alarm.ifwarn){
|
||||
|
||||
// if(!now_result.empty()){
|
||||
// last_result.clear();
|
||||
// last_result = now_result;
|
||||
@ -1289,21 +1254,55 @@ void* rkmedia_rknn_thread(void* args)
|
||||
//若上帧结果为空,依然清理结果队列 --2024.11.4
|
||||
last_result.clear();
|
||||
last_result = now_result;
|
||||
|
||||
|
||||
|
||||
last_result_time = std::chrono::high_resolution_clock::now();// 时间
|
||||
std::string resultString_fire(result_fire_rknn, 4);
|
||||
std::cout << "rknn检测结果: " << resultString_fire << std::endl;
|
||||
temperature_log << "rknn检测结果: " << resultString_fire << std::endl;
|
||||
//对45度区域和火焰检测区域做出 与操作
|
||||
and_result(result_warn,result_fire_rknn);
|
||||
|
||||
|
||||
|
||||
//对火与45度结果 与 60 度结果 做或操作
|
||||
or_result(result,result_warn);
|
||||
temperature_log << "报警输入:" << string(result, 4) <<endl;
|
||||
//处理ControlInstructions数据
|
||||
|
||||
if (result[0] == '1') ControlInstructions[(g_camera_number - 1) * 4] = 0x01;
|
||||
if (result[1] == '1') ControlInstructions[(g_camera_number - 1) * 4] = 0x01;
|
||||
if (result[2] == '1') ControlInstructions[(g_camera_number - 1) * 4] = 0x01;
|
||||
if (result[3] == '1') ControlInstructions[(g_camera_number - 1) * 4] = 0x01;
|
||||
JBASDevInfo_t devinfo;
|
||||
int results = JES_BAS_GetDevInfo(jbas_handle, &devinfo, JFALSE);
|
||||
|
||||
if (results == 0) {
|
||||
printf("Successfully retrieved device information:\n");
|
||||
PRSTR(devinfo.devname);
|
||||
string CA_ID = devinfo.devname;
|
||||
PRSTR(devinfo.language);
|
||||
|
||||
//获取设备
|
||||
// 提取设备名称中的摄像头编号
|
||||
if (!CA_ID.empty()) {
|
||||
char lastChar = CA_ID.back(); // 获取最后一个字符
|
||||
if (isdigit(lastChar) && lastChar >= '1' && lastChar <= '4') {
|
||||
g_camera_number = lastChar - '0'; // 转换为整数,例如 '2' -> 2
|
||||
} else {
|
||||
g_camera_number = 1; // 默认值
|
||||
fprintf(stderr, "Warning: Invalid device name format: %s\n", CA_ID.c_str());
|
||||
}
|
||||
} else {
|
||||
g_camera_number = 1; // 默认值
|
||||
fprintf(stderr, "Warning: Device name is empty\n");
|
||||
}
|
||||
|
||||
// 打印验证
|
||||
printf("Camera number: %d\n", g_camera_number);
|
||||
}
|
||||
|
||||
if(result[0] == '1') ControlInstructions[3+(g_camera_number-1)*4]=0x01;
|
||||
if(result[1] == '1') ControlInstructions[4+(g_camera_number-1)*4]=0x01;
|
||||
if(result[2] == '1') ControlInstructions[5+(g_camera_number-1)*4]=0x01;
|
||||
if(result[3] == '1') ControlInstructions[6+(g_camera_number-1)*4]=0x01;
|
||||
std::string indices = generateIndices(result,&Alarm);
|
||||
std::cout << "结果: " << indices << std::endl;
|
||||
Alarm.alarmCoverage = indices;
|
||||
@ -1346,6 +1345,13 @@ void* rkmedia_rknn_thread(void* args)
|
||||
}
|
||||
|
||||
|
||||
//计算校验位并发送
|
||||
ControlInstructions[19] = calculateChecksum(ControlInstructions);
|
||||
write(serialPortSolenoid, ControlInstructions.data(), ControlInstructions.size());
|
||||
//打印串口协议
|
||||
cout << "串口发送信息:";
|
||||
printVector(ControlInstructions);
|
||||
|
||||
|
||||
cv::Mat rgb_img;
|
||||
cv::cvtColor(src, rgb_img, COLOR_BGR2RGB);
|
||||
@ -1367,8 +1373,7 @@ void* rkmedia_rknn_thread(void* args)
|
||||
int ret =pthread_create(&upload_message_controller_tidp, NULL, upload_message_controller, static_cast<void*>(&Alarm));
|
||||
if (ret != 0) {
|
||||
std::cerr << "Error creating controller thread: " << strerror(ret) << std::endl;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
std::cerr << "success creating controller thread" << std::endl;
|
||||
}
|
||||
}
|
||||
@ -1468,8 +1473,7 @@ void* upload_message(void* args)
|
||||
// fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
|
||||
if (res != CURLE_OK) {
|
||||
std::cerr << "Failed to perform cURL request: " << curl_easy_strerror(res) << std::endl;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
std::cout << "Request successful!" << std::endl;
|
||||
std::cout << "Response: " << response << std::endl;
|
||||
}
|
||||
@ -1549,8 +1553,7 @@ void* upload_message_controller(void* args)
|
||||
// fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
|
||||
if (res != CURLE_OK) {
|
||||
std::cerr << "controller: Failed to perform cURL request: " << curl_easy_strerror(res) << std::endl;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
std::cout << "controller: Request successful!" << std::endl;
|
||||
// std::cout << "controller: Response: " << response << std::endl;
|
||||
}
|
||||
@ -1615,8 +1618,7 @@ void* heart_beat(void* args) {
|
||||
// fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
|
||||
if (res != CURLE_OK) {
|
||||
std::cerr << "heart beat: Failed to perform cURL request: " << curl_easy_strerror(res) << std::endl;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
std::cout << "heart beat: Request successful!" << std::endl;
|
||||
std::cout << "heart beat: Response: " << response << std::endl;
|
||||
// 解析 JSON 字符串
|
||||
@ -1627,8 +1629,7 @@ void* heart_beat(void* args) {
|
||||
response_controllerId = jsonData["controllerId"];
|
||||
|
||||
std::cout << "Controller ID: " << response_controllerId << std::endl;
|
||||
}
|
||||
catch (const json::parse_error& e) {
|
||||
} catch (const json::parse_error& e) {
|
||||
std::cerr << "JSON parsing error: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
@ -1675,6 +1676,20 @@ void* read_serial_thread(void* args)
|
||||
return 0;
|
||||
}
|
||||
|
||||
// while(1){
|
||||
// mtx.lock();
|
||||
// bytesRead = read(serialPortInfraredSensor, buffer, sizeof(buffer));
|
||||
// mtx.unlock();
|
||||
// if (bytesRead>0) {
|
||||
// if((buffer[0]== 0x5a)&&(buffer[1]==0x5a)) {
|
||||
// // printf("readed serialPortInfraredSensor date\n");
|
||||
// }else{
|
||||
// // printf("read failed\n");
|
||||
// }
|
||||
// }else{
|
||||
// // printf("empty to read\n");
|
||||
// }
|
||||
// }
|
||||
fd_set readfds;
|
||||
struct timeval timeout;
|
||||
int selectResult;
|
||||
@ -1697,126 +1712,18 @@ void* read_serial_thread(void* args)
|
||||
if (bytesRead > 0) {
|
||||
if (buffer[0] == 0x5a && buffer[1] == 0x5a) {
|
||||
// printf("readed serialPortInfraredSensor date\n");
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// printf("read failed\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (selectResult == 0) {
|
||||
} else if (selectResult == 0) {
|
||||
// 超时,没有数据可读
|
||||
// printf("Timeout, no data available\n");
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// select出错
|
||||
printf("select() failed\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// pthread_t storage_serial;
|
||||
// pthread_create(&storage_serial, NULL, storage_serial_thread, NULL);
|
||||
|
||||
// 首先定义协议相关的常量
|
||||
#define PROTOCOL_HEAD 0x7E
|
||||
#define PROTOCOL_VERSION 1
|
||||
#define PROTOCOL_LENGTH 22
|
||||
#define SOLENOID_VALVE_COUNT 16
|
||||
#define COMMAND_QUERY 1
|
||||
#define COMMAND_RESPONSE 2
|
||||
|
||||
// 定义协议结构体
|
||||
typedef struct tagCamera485Protocol {
|
||||
uint8_t m_head; // 协议头 0x7e
|
||||
uint8_t m_version; // 版本1~255
|
||||
uint8_t m_length; // 协议总长度,包含校验位
|
||||
uint8_t m_address; // 地址0~255
|
||||
uint8_t m_command; // 操作指令
|
||||
uint8_t m_solenoidValve[SOLENOID_VALVE_COUNT]; // 电磁阀状态
|
||||
uint8_t m_checkSum; // 校验和
|
||||
} CCamera485Protocol;
|
||||
|
||||
// 优化后的storage_serial_thread函数
|
||||
void* storage_serial_thread(void* args) {
|
||||
pthread_detach(pthread_self());
|
||||
|
||||
// {
|
||||
// test *ptest1 = NULL;
|
||||
// test *ptest2[2] = {};
|
||||
// if (ptest1 == ptest2[0])
|
||||
// {
|
||||
// printf("ptest1 == ptest2");
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// printf("ptest1 != ptest2");
|
||||
// }
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
juart_hdl_t hdl = juart_open("/dev/ttyS5");
|
||||
JUartAttr_t attr = {.baudrate = 9600,
|
||||
.datawidth = 8,
|
||||
.stopbit = 1,
|
||||
.parity = 0};
|
||||
|
||||
juart_set_attr(hdl, &attr);
|
||||
juart_set_rs485(hdl, 0);
|
||||
|
||||
unsigned char hexData[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x0B};
|
||||
juart_send(hdl, (char*)hexData, sizeof(hexData));
|
||||
printf("send: ");
|
||||
hexdump((char*)hexData, sizeof(hexData));
|
||||
|
||||
while (1)
|
||||
{
|
||||
CCamera485Protocol protocol;
|
||||
int len = juart_recv(hdl, (char *)&protocol, sizeof(CCamera485Protocol), 6000);
|
||||
if (len <= 0)
|
||||
{
|
||||
printf("==============>>>: %s,%d: recv timeout!\n", strrchr(__FILE__,'/'),__LINE__);
|
||||
}
|
||||
|
||||
if (len == sizeof(CCamera485Protocol))
|
||||
{
|
||||
hexdump((char*)&protocol,len);
|
||||
// 处理协议
|
||||
if (protocol.m_head == PROTOCOL_HEAD &&
|
||||
protocol.m_command == COMMAND_QUERY &&
|
||||
protocol.m_address == (g_camera_number - 1))
|
||||
{
|
||||
// 构造响应
|
||||
uint8_t response_array[22] = {0};
|
||||
// 1. 填充头部
|
||||
response_array[0] = PROTOCOL_HEAD; // 协议头
|
||||
response_array[1] = PROTOCOL_VERSION; // 版本
|
||||
response_array[2] = PROTOCOL_LENGTH; // 长度
|
||||
response_array[3] = g_camera_number - 1; // 地址
|
||||
response_array[4] = COMMAND_RESPONSE; // 命令
|
||||
|
||||
// 2. 填充电磁阀状态
|
||||
mtx.lock();
|
||||
for (int i = 0; i < SOLENOID_VALVE_COUNT; i++) {
|
||||
response_array[5 + i] = ControlInstructions[i];
|
||||
}
|
||||
mtx.unlock();
|
||||
|
||||
// 3. 计算校验和(前21字节累加和)
|
||||
uint8_t sum = 0;
|
||||
for (int i = 0; i < 21; i++) {
|
||||
sum += response_array[i];
|
||||
}
|
||||
response_array[21] = sum; // 校验和
|
||||
juart_send(hdl, (char*)response_array, sizeof(response_array));
|
||||
hexdump((char*)&response_array,sizeof(response_array));
|
||||
std::fill(ControlInstructions.begin(), ControlInstructions.end(), 0x00);
|
||||
sleep(2);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
juart_close(hdl);
|
||||
return 0;
|
||||
}
|
@ -62,8 +62,6 @@ void *upload_message_controller(void *args);
|
||||
void *heart_beat(void *args); //上传心跳检测
|
||||
void *distortion(void *args); //矫正
|
||||
void *read_serial_thread(void *args); //读取串口传来的红外温度数据
|
||||
void *storage_serial_thread(void *args);
|
||||
|
||||
|
||||
struct Alarm {
|
||||
int ifalarm;
|
||||
|
Loading…
x
Reference in New Issue
Block a user