diff --git a/Cigarette/CaptureThread.cpp b/Cigarette/CaptureThread.cpp index 709857c..3936f78 100644 --- a/Cigarette/CaptureThread.cpp +++ b/Cigarette/CaptureThread.cpp @@ -86,7 +86,7 @@ void CaptureThread::process(void) mvIMPACT::acquire::Statistics statistics(pDev_); mvIMPACT::acquire::Request* pRequest = 0; mvIMPACT::acquire::Request* pPreviousRequest = nullptr; - const unsigned int timeout_ms = 100; + const unsigned int timeout_ms = 10; Ready = true; while (!boTerminated_) { diff --git a/Cigarette/CaptureThreadBasler.cpp b/Cigarette/CaptureThreadBasler.cpp index 41febfc..1ec3f06 100644 --- a/Cigarette/CaptureThreadBasler.cpp +++ b/Cigarette/CaptureThreadBasler.cpp @@ -123,7 +123,7 @@ void CaptureThreadBasler::process(void) #endif #endif - const unsigned int timeout_ms = 100; + const unsigned int timeout_ms = 10; // This smart pointer will receive the grab result data. Pylon::CGrabResultPtr ptrGrabResult; diff --git a/Cigarette/CaptureThreadHIK.cpp b/Cigarette/CaptureThreadHIK.cpp index 0790557..3967ade 100644 --- a/Cigarette/CaptureThreadHIK.cpp +++ b/Cigarette/CaptureThreadHIK.cpp @@ -294,7 +294,7 @@ void CaptureThreadHIK::process(void) while (!boTerminated_)//循环取流,拍照取图 { //nRet = MV_CC_GetOneFrameTimeout(CamHandle, g_pImage_buf, nDataSize, &stFrameInfo, 200); - nRet = MV_CC_GetImageForBGR(CamHandle, g_pImage_buf, nDataSize, &stFrameInfo, 100); + nRet = MV_CC_GetImageForBGR(CamHandle, g_pImage_buf, nDataSize, &stFrameInfo, 10); if (MV_OK == nRet) { m_cntGrabbedImages++; diff --git a/Cigarette/Cleanthread.cpp b/Cigarette/Cleanthread.cpp index 9278578..dd75287 100644 --- a/Cigarette/Cleanthread.cpp +++ b/Cigarette/Cleanthread.cpp @@ -47,7 +47,10 @@ void CleanWorkThread::doWork() LPCWSTR strDriver = (LPCWSTR)iDriver.utf16(); ULARGE_INTEGER freeDiskSpaceAvailable, totalDiskSpace, totalFreeDiskSpace; qint64 gb = (1024 * 1024 * 1024); - QString dir(g_conf_path.save_pics_path + "/ALL"); + QString dir(g_conf_path.save_pics_path); + QString dirALL(g_conf_path.save_pics_path + "/ALL"); + QString dirNg(g_conf_path.save_pics_path + "/ng"); + QString dirNg_result(g_conf_path.save_pics_path + "/ng_result"); if (delSelection == 2) { @@ -58,6 +61,9 @@ void CleanWorkThread::doWork() qint64 delDays = g_sys_conf.save_days;/// qint64 freeSize = g_sys_conf.freesize; CleanImageFile(dir, delDays--); + CleanImageFile(dirALL, delDays--); + CleanImageFile(dirNg, delDays--); + CleanImageFile(dirNg_result, delDays--); //调用函数获取磁盘参数(单位为字节Byte),转化为GB,需要除以(1024*1024*1024) GetDiskFreeSpaceEx(strDriver, &freeDiskSpaceAvailable, &totalDiskSpace, &totalFreeDiskSpace); spaceSize = ((qint64)totalFreeDiskSpace.QuadPart * (100 / freeSize)) / (qint64)totalDiskSpace.QuadPart; @@ -65,6 +71,9 @@ void CleanWorkThread::doWork() //delDays = g_sys_conf.save_days; while ((spaceSize < 1) && (delDays > 1)) { //磁盘剩余空间小于百分之十 CleanImageFile(dir, delDays--); + CleanImageFile(dirALL, delDays--); + CleanImageFile(dirNg, delDays--); + CleanImageFile(dirNg_result, delDays--); GetDiskFreeSpaceEx(strDriver, &freeDiskSpaceAvailable, &totalDiskSpace, &totalFreeDiskSpace); spaceSize = ((qint64)totalFreeDiskSpace.QuadPart * (100 / freeSize)) / (qint64)totalDiskSpace.QuadPart; } diff --git a/Cigarette/PLC/PLCDevice.cpp b/Cigarette/PLC/PLCDevice.cpp index 859d32f..ab53fe3 100644 --- a/Cigarette/PLC/PLCDevice.cpp +++ b/Cigarette/PLC/PLCDevice.cpp @@ -56,7 +56,10 @@ bool PLCDevice::init_plc_tcp(PLCDevice* PLCptr) } #endif #ifdef __ModebusClient - PLCptr->g_modbus = modbus_new_tcp("192.168.1.176", 4444); + const char* ip = g_sys_conf.TcpIP.data(); + qDebug() << "Client ip:" << ip; + qDebug() << "Client port:" << g_sys_conf.TcpPort; + PLCptr->g_modbus = modbus_new_tcp(ip, g_sys_conf.TcpPort); modbus_set_debug(PLCptr->g_modbus, 0); // 用flag设置debug调试标志位,flag=1时显示modbus消息的字节 modbus_set_response_timeout(PLCptr->g_modbus, 1, 0); // 设置响应超时 // 侦听主站连接 diff --git a/Cigarette/cigarette.cpp b/Cigarette/cigarette.cpp index bd900ad..f263da7 100644 --- a/Cigarette/cigarette.cpp +++ b/Cigarette/cigarette.cpp @@ -273,6 +273,7 @@ Cigarette::Cigarette(QWidget* parent) g_modbus_conf.debug = 0; g_modbus_conf.reset = 0; g_modbus_conf.alarm = 0; + g_modbus_conf.heart = 0; read_modbus_config(g_modbus_conf); //初始化modbus地址 //保存图片线程 @@ -483,6 +484,11 @@ Cigarette::Cigarette(QWidget* parent) //报警标签双击消警 connect(ui.label_alarm, SIGNAL(QlabelDoubleClick()), this, SLOT(OnCancelAlarm()));// + //心跳检测 + heartbeat_pTimer = new QTimer(this); + connect(heartbeat_pTimer, SIGNAL(timeout()), this, SLOT(heartbeat())); + heartbeat_pTimer->start(5000); + //无参对有参需要利用信号转发器QSignalMapper(this) //信号转发器 image_lable_DBsignalMapper0 = new QSignalMapper(this); @@ -1585,6 +1591,12 @@ void Cigarette::admin_timeout() } } +//心跳检测 +void Cigarette::heartbeat() +{ + int ret = m_PLCDevice->write_bit_2_plc(g_modbus_conf.heart, 1); +} + void Cigarette::handleTimeout()//获取时间,读取PLC产量、剔除数;检测相机状态;读取PLC报警信息 { QDateTime ts_start = QDateTime::currentDateTime(); @@ -2699,6 +2711,10 @@ bool Cigarette::read_modbus_config(ModbusConf& conf) { conf.alarm = atoi(line.substr(pos + 1).c_str()); } + else if (tmp_key == "HEART") + { + conf.heart = atoi(line.substr(pos + 1).c_str()); + } } } cfg_file.close(); @@ -3703,39 +3719,70 @@ void Cigarette::record_output_statistic(qint64 quantity, int Kick[NumberOfSuppor QString file_name, file_path; for (int i = 0; i < NumberOfSupportedCameras; i++) { - if (SingleCamInfo[i].IsOpen && quantity > 0) { + if (SingleCamInfo[i].IsOpen) //&& quantity > 0 + { std::fstream cfg_file; file_name = QString(STATISTIC_FILE).arg(i); file_path = g_conf_path.config_path + "/" + file_name; - char buf[256]; - memset(buf, 0, 256); - sprintf(buf, "%s", file_path.toLocal8Bit().constData()); - - cfg_file.open(buf, std::ios::app); - if (cfg_file.good()) - { - if (last_shift == 2 || last_shift == 256) { - memset(buf, 0, 256); - sprintf(buf, "\n\n**************************************************************************************\n"); - cfg_file.write(buf, strlen(buf)); - //QDate date = QDate::currentDate(); - QDateTime date = QDateTime::currentDateTime(); - memset(buf, 0, 256); - sprintf(buf, "%s:\n", date.toString("yyyy-MM-dd_HH-mm-ss").toStdString().c_str()); - cfg_file.write(buf, strlen(buf)); - } - memset(buf, 0, 256); - double ng_rate; - if (ng[i] > 0) - ng_rate = double(ng[i]) / production_number[i]; - else - ng_rate = 0; + QDateTime date = QDateTime::currentDateTime(); + string nowtime = date.toString("yyyy-MM-dd_HH:mm:ss").toStdString().c_str(); + double ng_rate; + if (ng[i] > 0) + { + ng_rate = double(ng[i]) / production_number[i]; + } + else + { + ng_rate = 0; + } + if (quantity > 0) + { sprintf(buf, "%c 总产量:%010I64d 剔除率:%02.5f%% 合格率:%02.5f%% NG率:%02.5f%%\n", (shift == 0) ? 'A' : ((shift == 1) ? 'B' : 'C'), quantity, ((double)Kick[i] / quantity) * 100, (1.0 - ng_rate) * 100, ng_rate * 100); - cfg_file.write(buf, strlen(buf)); - memset(buf, 0, 256); } - cfg_file.close(); + else + { + sprintf(buf, "%c 总产量:%010I64d 剔除率:%02.5f%% 合格率:%02.5f%% NG率:%02.5f%%\n", (shift == 0) ? 'A' : ((shift == 1) ? 'B' : 'C'), quantity, 0.00, 0.00, 0.00); + } + std::ifstream file(file_path.toStdString()); + std::ifstream file_(file_path.toStdString()); + std::vector lines; //用于存储文件的所有行 + if (file_.good()) + { + file_.close(); + } + else + { + std::ofstream outFile(file_path.toStdString()); + std::string fristNews = "**************************************************************************************\n" + nowtime + "\n" + buf; + outFile << fristNews << endl; + outFile.close(); + } + if (file.is_open()) + { + std::string line; + while (std::getline(file, line)) + { + lines.push_back(line); + } + file.close(); + // 在第一行添加数据 + std::string newData = "**************************************************************************************\n"+nowtime +"\n" + buf; + if (!lines.empty()) + { + lines.insert(lines.begin(), newData); // 在vector的开始位置插入新数据 + } + // 将修改后的数据写回文件 + std::ofstream outFile(file_path.toStdString()); + if (outFile.is_open()) + { + for (const auto& l : lines) + { + outFile << l << std::endl; + } + outFile.close(); + } + } } } last_shift = shift; diff --git a/Cigarette/cigarette.h b/Cigarette/cigarette.h index 7f1f4da..45fa000 100644 --- a/Cigarette/cigarette.h +++ b/Cigarette/cigarette.h @@ -129,6 +129,8 @@ private slots: void OnExit(); void OnRestart(); void OnAdmin(); + + void heartbeat();//心跳检测 #ifdef __DEBUG void TestImg(); void TestImgs(); @@ -197,6 +199,7 @@ public: QTimer* m_op_delay; //操作员权限剩余时间 QTimer* m_admin_delay; //操作员权限剩余时间 QTimer* clean_pTimer; //定时清理任务 + QTimer* heartbeat_pTimer; //心跳检测 QSignalMapper* image_lable_DBsignalMapper0; QSignalMapper* image_lable_DBsignalMapper1; diff --git a/Cigarette/common.h b/Cigarette/common.h index 2c35598..4865b75 100644 --- a/Cigarette/common.h +++ b/Cigarette/common.h @@ -204,7 +204,7 @@ public: int debug; //调试模式,PLC产生模拟的line4信号 int reset; //复位 int alarm; //报警 - + int heart; //心跳 ModbusConf() { for (int i = 0; i < NumberOfSupportedCameras; i++) { @@ -219,6 +219,7 @@ public: debug = 0; //调试模式,PLC产生模拟的line4信号 reset = 0; //复位 alarm = 0; //报警 + heart = 0; //心跳 } }; diff --git a/Cigarette/syncworkthread.cpp b/Cigarette/syncworkthread.cpp index 402ec53..894acd7 100644 --- a/Cigarette/syncworkthread.cpp +++ b/Cigarette/syncworkthread.cpp @@ -337,8 +337,8 @@ void SyncWorkThread::run() roi = cv::Rect(j * m.cols, index * m.rows, m.cols, m.rows); m.copyTo(merge_image(roi)); if (merge_index == work_camera_nums * unit_count) { - - if (IsNGForAll) + + if (!IsNG) { ngReason = 1; } @@ -350,7 +350,7 @@ void SyncWorkThread::run() //条盒TJ 小盒BJ file_name = g_conf_path.save_pics_path + "/" + now_ts.toString("yyyy-MM-dd") + "/"+"416_BJ_01_" - + now_ts.toString("yyyy-MM-dd_HH-mm-ss.zzz_") + ng_reason_maps[ngReason] + ".jpg"; + + now_ts.toString("yyyy-MM-dd_HH-mm-ss.zzz") + ".jpg"; g_save_queue->put(std::make_pair(file_name.toLocal8Bit().constData(), merge_image)); #ifdef __TCPSend diff --git a/Cigarette/threadSendTCP.cpp b/Cigarette/threadSendTCP.cpp index 403d1fa..8fbd5de 100644 --- a/Cigarette/threadSendTCP.cpp +++ b/Cigarette/threadSendTCP.cpp @@ -158,9 +158,9 @@ void threadSendTCP::sendData(_TCPSendInfo* TCPSendInfo, int Num) { } #endif - QDateTime ts_end = QDateTime::currentDateTime(); - int time_process = ts_start.msecsTo(ts_end); - qDebug() << "end-now_ts=" << time_process; + //QDateTime ts_end = QDateTime::currentDateTime(); + //int time_process = ts_start.msecsTo(ts_end); + //qDebug() << "end-now_ts=" << time_process; } //字符串转ASCII std::vector threadSendTCP::stringToAscii(const std::string& str) diff --git a/Cigarette/workthread.cpp b/Cigarette/workthread.cpp index 72ff76b..cb6f166 100644 --- a/Cigarette/workthread.cpp +++ b/Cigarette/workthread.cpp @@ -161,10 +161,10 @@ void WorkThread::run() bool IsNG = false; int ngReason = 0; QMap ng_reason_maps; - ng_reason_maps[0] = "unknow"; - ng_reason_maps[1] = "less_than_setting"; - ng_reason_maps[2] = "too_diff_from_model"; - ng_reason_maps[3] = "out_of_setting_range"; + ng_reason_maps[0] = "1"; + ng_reason_maps[1] = "0"; + ng_reason_maps[2] = "0"; + ng_reason_maps[3] = "0"; for (int index = 0; index < unit_count; index++) { if (vec_results[index].size() < local_SysConf.no[local_camera_number][index]) @@ -256,32 +256,36 @@ void WorkThread::run() { for (int index = 0; index < unit_count; index++) { - cv::Mat m = vec_in[index].clone(); - QString file_name = g_conf_path.save_pics_path + "/ng/" + - now_ts.toString("yyyy-MM-dd") + "/" - + QString::number(local_camera_number + 1) + "/" + QString::number(index + 1) + "/" + - now_ts.toString("yyyy-MM-dd_HH-mm-ss_zzz_") + QString::number(local_camera_number + 1) + - "#" + "_" + QString::number(index + 1) + "_" + ng_reason_maps[ngReason] + - ".jpg"; - QString remotePath = "/image/ng/" + - now_ts.toString("yyyy-MM-dd_HH-mm-ss_zzz_") + QString::number(local_camera_number + 1) + - "#" + "_" + QString::number(index + 1) + "_" + ng_reason_maps[ngReason] + - ".jpg"; - g_save_queue->put(std::make_pair(file_name.toLocal8Bit().constData(), m)); + if (vec_results[index].size() < local_SysConf.no[local_camera_number][index]) + { + cv::Mat m = vec_in[index].clone(); + QString file_name = g_conf_path.save_pics_path + "/ng/" + + now_ts.toString("yyyy-MM-dd") + "/" + + QString::number(local_camera_number + 1) + "/" + QString::number(index + 1) + "/" + + now_ts.toString("yyyy-MM-dd_HH-mm-ss_zzz_") + QString::number(local_camera_number + 1) + + "#" + "_" + QString::number(index + 1) + "_" + ng_reason_maps[ngReason] + + ".jpg"; + QString remotePath = "/image/ng/" + + now_ts.toString("yyyy-MM-dd_HH-mm-ss_zzz_") + QString::number(local_camera_number + 1) + + "#" + "_" + QString::number(index + 1) + "_" + ng_reason_maps[ngReason] + + ".jpg"; + g_save_queue->put(std::make_pair(file_name.toLocal8Bit().constData(), m)); - m = vec_out[index].clone(); - file_name = g_conf_path.save_pics_path + "/ng_result/" + - now_ts.toString("yyyy-MM-dd") + "/" - + QString::number(local_camera_number + 1) + "/" + QString::number(index + 1) + "/" + - now_ts.toString("yyyy-MM-dd_HH-mm-ss_zzz_") + QString::number(local_camera_number + 1) + - "#" + "_" + QString::number(index + 1) + "_" + ng_reason_maps[ngReason] + - ".jpg"; - remotePath = "/image/ng_result/" + - now_ts.toString("yyyy-MM-dd_HH-mm-ss_zzz_") + QString::number(local_camera_number + 1) + - "#" + "_" + QString::number(index + 1) + "_" + ng_reason_maps[ngReason] + - ".jpg"; - //g_save_queue->put(std::make_pair(file_name.toStdString(), m)); - g_save_queue->put(std::make_pair(file_name.toLocal8Bit().constData(), m)); + m = vec_out[index].clone(); + file_name = g_conf_path.save_pics_path + "/ng_result/" + + now_ts.toString("yyyy-MM-dd") + "/" + + QString::number(local_camera_number + 1) + "/" + QString::number(index + 1) + "/" + + now_ts.toString("yyyy-MM-dd_HH-mm-ss_zzz_") + QString::number(local_camera_number + 1) + + "#" + "_" + QString::number(index + 1) + "_" + ng_reason_maps[ngReason] + + ".jpg"; + remotePath = "/image/ng_result/" + + now_ts.toString("yyyy-MM-dd_HH-mm-ss_zzz_") + QString::number(local_camera_number + 1) + + "#" + "_" + QString::number(index + 1) + "_" + ng_reason_maps[ngReason] + + ".jpg"; + //g_save_queue->put(std::make_pair(file_name.toStdString(), m)); + g_save_queue->put(std::make_pair(file_name.toLocal8Bit().constData(), m)); + } + } } }