diff --git a/Cigarette/PLC/PLCDevice.cpp b/Cigarette/PLC/PLCDevice.cpp index 9de44b8..11512d1 100644 --- a/Cigarette/PLC/PLCDevice.cpp +++ b/Cigarette/PLC/PLCDevice.cpp @@ -37,21 +37,25 @@ bool PLCDevice::init_plc_tcp(PLCDevice* PLCptr) // modbus_connect() 建立连接,成功返回0,错误返回-1 // modbus_new_rtu 生成RTU的连接,建立成功则返回指向modbus_t结构的指针,否则将返回NULL/0 - PLCptr->g_modbus = modbus_new_tcp("192.168.1.1", 7002); - qDebug("new g_modbus:\t%p\n", PLCptr->g_modbus); - modbus_set_debug(PLCptr->g_modbus, 0); - modbus_set_response_timeout(PLCptr->g_modbus, 1, 0); // 设置响应超时 - modbus_connect(PLCptr->g_modbus); - modbus_set_slave(PLCptr->g_modbus, 1); // 设置从机地址 - int nRet = modbus_connect(PLCptr->g_modbus);// 连接设备 - // 连接失败 - if (-1 == nRet) - { + PLCptr->g_modbus = modbus_new_tcp("192.168.1.200", 2000); + modbus_set_debug(PLCptr->g_modbus, 0); // 用flag设置debug调试标志位,flag=1时显示modbus消息的字节 + modbus_set_response_timeout(PLCptr->g_modbus, 1, 0); // 设置响应超时 + modbus_connect(PLCptr->g_modbus); + modbus_set_slave(PLCptr->g_modbus, 1); // 设置从站id + + uint8_t data; + int ret = modbus_read_bits(PLCptr->g_modbus, 30000, 1, &data); + // 连接失败 + if (-1 == ret) + { PLCptr->g_plc_ok = false; - qDebug("connect failed:%s\n", modbus_strerror(errno)); - return false; - } - PLCptr->g_plc_ok = true; + qDebug("TCP connect failed:%s\n", modbus_strerror(errno)); + } + else + { + PLCptr->g_plc_ok = true; + } + qDebug("TCP connect successful "); return true; } diff --git a/Cigarette/alg_jd.cpp b/Cigarette/alg_jd.cpp index e3d6a44..1495636 100644 --- a/Cigarette/alg_jd.cpp +++ b/Cigarette/alg_jd.cpp @@ -24,14 +24,14 @@ bool AlgJd::init(QString model_path, QString model_name) modelWeights = model_path.toStdString() + "/" + model_name.toStdString(); modelConfiguration = model_path.toStdString() + "/jd.cfg"; classesFile = model_path.toStdString() + "/jd.names"; - image_path = model_path + "/" + "alg_jd.bmp"; + image_path = model_path + "/" + "alg_jd.jpg"; } else { modelWeights = "D:/model/jd.weights"; classesFile = "D:/model/jd.names"; // Give the configuration and weight files for the model modelConfiguration = "D:/model/jd.cfg"; - image_path = "D:/model/alg_jd.bmp"; + image_path = "D:/model/alg_jd.jpg"; } std::ifstream classNamesFile(classesFile.c_str()); diff --git a/Cigarette/basecamera.h b/Cigarette/basecamera.h index ce47ec6..8959e04 100644 --- a/Cigarette/basecamera.h +++ b/Cigarette/basecamera.h @@ -2,8 +2,9 @@ #include -#define NumberOfSupportedCameras 4 - +#define NumberOfSupportedCameras 2 +//˫ +#define NumberOfIdentityCameras 8 #define EdgeEvent 1 #define ImageCap 2 diff --git a/Cigarette/cigarette.cpp b/Cigarette/cigarette.cpp index 47c9cee..d8ae7e7 100644 --- a/Cigarette/cigarette.cpp +++ b/Cigarette/cigarette.cpp @@ -752,6 +752,10 @@ void Cigarette::Exit() m_PLCDevice->disconnect_plc(); delete m_PLCDevice; } + if (m_PLCTCPDevice) { + m_PLCTCPDevice->disconnect_plc(); + delete m_PLCTCPDevice; + } qApp->quit(); this->close(); @@ -2117,7 +2121,7 @@ void Cigarette::on_pushButton_clear_released()//换班 QString Cigarette::read_pswd() { std::fstream cfg_file; - cfg_file.open("pswd.txt"); + cfg_file.open("D:/Release/pswd.txt"); if (!cfg_file.is_open()) { std::cout << "Error: Open config file pswd.txt" << std::endl; @@ -2149,7 +2153,7 @@ QString Cigarette::read_pswd() QString Cigarette::read_op_pswd() { std::fstream cfg_file; - cfg_file.open("pswd_op.txt"); + cfg_file.open("D:/Release/pswd_op.txt"); if (!cfg_file.is_open()) { std::cout << "Error: Open config file pswd_op.txt" << std::endl; @@ -3395,7 +3399,7 @@ void Cigarette::EnumerateCameras(SingleCamInfoStruct* TempSingleCamInfoStruct, b Pylon::CInstantCameraArray cameras(filtered); Pylon::CTlFactory& tlFactory = Pylon::CTlFactory::GetInstance(); - for (int i = 0; i < min(filtered, NumberOfSupportedCameras); i++) + for (int i = 0; i < min(filtered, NumberOfIdentityCameras); i++) { SingleCamInfoStruct CamInfo; //cameras[i].Attach(tlFactory.CreateDevice(BaslerCamera::devices[i])); @@ -3426,7 +3430,7 @@ void Cigarette::EnumerateCameras(SingleCamInfoStruct* TempSingleCamInfoStruct, b int hik_cnt = 0; int unfiltered = 0; unfiltered = HIKCamera::Enumerate(); - for (int i = 0; i < min(unfiltered, NumberOfSupportedCameras); i++) + for (int i = 0; i < min(unfiltered, NumberOfIdentityCameras); i++) { MV_CC_DEVICE_INFO* pDeviceInfo = HIKCamera::stDeviceList.pDeviceInfo[i]; std::string manufacture; @@ -3489,7 +3493,7 @@ void Cigarette::EnumerateCameras(SingleCamInfoStruct* TempSingleCamInfoStruct, b int balluff_cnt = 0; unfiltered = BalluffCamera::Enumerate(); - for (int i = 0; i < min(unfiltered, NumberOfSupportedCameras); i++) + for (int i = 0; i < min(unfiltered, NumberOfIdentityCameras); i++) { Device* pDev = BalluffCamera::devMgr[i]; if (!pDev->isInUse()) { diff --git a/Cigarette/common.h b/Cigarette/common.h index eae8fb7..7731db7 100644 --- a/Cigarette/common.h +++ b/Cigarette/common.h @@ -5,10 +5,10 @@ #include "basecamera.h" #include "QtCore\qdatetime.h" -//#define __DEBUG //debug信息输出功能 +//#define __DEBUG //debug信息输出功能 //#define __UDPSend //网络发送功能 -//#define __TCPSend // TCP发送 -//#define __TCPServer // TCP服务器 +#define __TCPSend // TCP发送 +//#define __TCPServer // TCP服务器 #define __ExportData // FTP发送 #define USB_BASLER_NEW_FW //使用basler定制固件 //#define IMM_PROCESS //拍照后立马处理,不等校验信号 @@ -21,7 +21,7 @@ //#define CAP_FEED_BACK //拍照时也检测有没有测试结果,有的话就反馈 //#define DOUBLE_FEED_BACK //一次ng,两次反馈ng信号 #endif -#define identify_Hik_YSXID//识别海康相机YSXID +//#define identify_Hik_YSXID//识别海康相机YSXID //#define __ExportData // 输出检测数据到XML文档 //#define DRAW_RECT // 鼠标画框功能 #define SYNC_CAMERA //相机同步处理图片 @@ -40,7 +40,7 @@ #define DEFAULT_CONFPATH_PATH "D:/conf" #define DEFAULT_PIC_SAVE_PATH "D:/image" // 主界面基本参数配置文件 -#define CONFPATH "conf_path.txt" +#define CONFPATH "conf_path3.txt" // 相机旋转角度配置文件 #define ROTATE_FILE "rotate.txt" #define MODBUS_CONFIGURE_FILE "modbus.txt" diff --git a/Cigarette/dialogsetup.cpp b/Cigarette/dialogsetup.cpp index a952aef..628747a 100644 --- a/Cigarette/dialogsetup.cpp +++ b/Cigarette/dialogsetup.cpp @@ -472,7 +472,7 @@ void DialogSetup::on_toolButton_choose_path_jpg_released() { void DialogSetup::write_pswd() { std::fstream cfg_file; - cfg_file.open("pswd.txt", std::ios::out | std::ios::trunc); + cfg_file.open("D:/Release/pswd.txt", std::ios::out | std::ios::trunc); if (cfg_file.good()) { char buf[256]; @@ -485,7 +485,7 @@ void DialogSetup::write_pswd() void DialogSetup::write_pswd_op() { std::fstream cfg_file; - cfg_file.open("pswd_op.txt", std::ios::out | std::ios::trunc); + cfg_file.open("D:/Release/pswd_op.txt", std::ios::out | std::ios::trunc); if (cfg_file.good()) { char buf[256]; @@ -757,22 +757,23 @@ void DialogSetup::write_config() sprintf(buf, "MonitorIP=%s\n", g_sys_conf.MonitorIP.c_str()); cfg_file.write(buf, strlen(buf)); memset(buf, 0, 256); - cfg_file.write(buf, strlen(buf)); sprintf(buf, "MonitorPort=%d\n", g_sys_conf.MonitorPort); - + cfg_file.write(buf, strlen(buf)); + memset(buf, 0, 256); sprintf(buf, "TcpIP=%s\n", g_sys_conf.TcpIP.c_str()); cfg_file.write(buf, strlen(buf)); memset(buf, 0, 256); - cfg_file.write(buf, strlen(buf)); sprintf(buf, "TcpPort=%d\n", g_sys_conf.TcpPort); + cfg_file.write(buf, strlen(buf)); memset(buf, 0, 256); sprintf(buf, "FtpIP=%s\n", g_sys_conf.FtpIP.c_str()); cfg_file.write(buf, strlen(buf)); memset(buf, 0, 256); - cfg_file.write(buf, strlen(buf)); sprintf(buf, "FtpPort=%d\n", g_sys_conf.FtpPort); + cfg_file.write(buf, strlen(buf)); + memset(buf, 0, 256); sprintf(buf, "FtpUserName=%s\n", g_sys_conf.FtpUserName.c_str()); cfg_file.write(buf, strlen(buf)); diff --git a/Cigarette/main.cpp b/Cigarette/main.cpp index 941d4f6..f9bf5db 100644 --- a/Cigarette/main.cpp +++ b/Cigarette/main.cpp @@ -8,21 +8,10 @@ #pragma comment(lib,"CryptoToolLib.lib") #include "CryptoToolLib.h" #endif - +//ؿƴ +//#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) int main(int argc, char* argv[]) { - QApplication a(argc, argv); - HANDLE hMutex = NULL; - - hMutex = CreateMutex(nullptr, TRUE, L"CigratteShanghai"); - if ((GetLastError() == ERROR_ALREADY_EXISTS) || (hMutex == NULL)) { - QMessageBox::warning(nullptr, "Error", "An instance of the application is already running."); - CloseHandle(hMutex); - hMutex = NULL; - a.closeAllWindows(); - return 0; - } - #if defined LICENSE_VERIFY if (!VerifyLicense()) { @@ -30,20 +19,14 @@ int main(int argc, char* argv[]) } #endif qRegisterMetaType("cv::Mat"); - QPixmap pixmap("splash.jpg"); + QApplication a(argc, argv); + QPixmap pixmap("D:/Release/splash.jpg"); QSplashScreen splash(pixmap); splash.show(); a.processEvents(); Cigarette w; - w.show(); - //w.showFullScreen(); + //w.show(); + w.showFullScreen(); splash.finish(&w); - a.exec(); - - if (hMutex != NULL) { - CloseHandle (hMutex); - hMutex = NULL; - } - - return 0; + return a.exec(); } diff --git a/Cigarette/syncworkthread.cpp b/Cigarette/syncworkthread.cpp index c93c0e1..278dd34 100644 --- a/Cigarette/syncworkthread.cpp +++ b/Cigarette/syncworkthread.cpp @@ -72,7 +72,7 @@ void SyncWorkThread::run() local_g_image_sync_queue->take(element_vec); bool IsNGForAll = false; int merge_index; - cv::Mat merge_image = cv::Mat::zeros(512 * work_camera_nums, 640 * g_sys_conf.shoot[0], CV_8UC3); + cv::Mat merge_image = cv::Mat::zeros(512 * g_sys_conf.shoot[0], 640 * work_camera_nums, CV_8UC3); cv::Rect roi; int j = 0; // 实际工作的相机标识,element_vec中可能有相机没在工作 for (int i = 0; i < element_vec.size(); i++)//每个相机的图轮流遍历 @@ -175,13 +175,14 @@ void SyncWorkThread::run() #ifdef __UDPSend UDPSendInfo.JD = jd_no; #endif + IsALL = false;//4.10 bool IsNG = false; int ngReason = 0; QMap ng_reason_maps; - ng_reason_maps[0] = "OK"; - 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]) { @@ -277,15 +278,17 @@ void SyncWorkThread::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"; - - 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"; + + 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/" + @@ -296,6 +299,7 @@ void SyncWorkThread::run() ".jpg"; g_save_queue->put(std::make_pair(file_name.toLocal8Bit().constData(), m)); + } } } } @@ -320,16 +324,26 @@ void SyncWorkThread::run() cv::Mat m = vec_in[index].clone(); QString file_name; merge_index = j * unit_count + index + 1; - roi = cv::Rect(index * m.cols, j * m.rows, m.cols, m.rows); + 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) + { + ngReason = 1; + } + else + { + ngReason = 0; + } + file_name = g_conf_path.save_pics_path + "/" - + now_ts.toString("yyyy-MM-dd") + "/" - + now_ts.toString("yyyy-MM-dd_HH-mm-ss_zzz_") + "_" + ng_reason_maps[ngReason] + ".jpg"; + + now_ts.toString("yyyy-MM-dd") + "/"+"416_BJ_01_" + + now_ts.toString("yyyy-MM-dd_HH-mm-ss.zzz_") + ng_reason_maps[ngReason] + ".jpg"; g_save_queue->put(std::make_pair(file_name.toLocal8Bit().constData(), merge_image)); #ifdef __TCPSend - QString sendName = now_ts.toString("yyyy-MM-dd_HH-mm-ss_zzz_") + ng_reason_maps[ngReason] + ".jpg"; + QString sendName = "416_BJ_01_" + now_ts.toString("yyyy-MM-dd_HH-mm-ss.zzz_") + ng_reason_maps[ngReason]; TCPSendInfo.pics_name = sendName.toLocal8Bit().constData(); TCP_Info_queue->put(TCPSendInfo); #endif @@ -360,8 +374,15 @@ void SyncWorkThread::run() } if (IsNGForAll) { - /*to do something*/ + local_g_result_queue->put(false); + + } + else + { + local_g_result_queue->put(true); + } + // emit display_check_total(local_camera_number, ++(frame_total[local_camera_number])); } } catch (cv::Exception& e) diff --git a/Cigarette/threadSendTCP.cpp b/Cigarette/threadSendTCP.cpp index 59ca4c9..bed028c 100644 --- a/Cigarette/threadSendTCP.cpp +++ b/Cigarette/threadSendTCP.cpp @@ -44,17 +44,18 @@ void threadSendTCP::stop() #ifndef __TCPServer bool threadSendTCP::connectTCP() { - mySocket = new QTcpSocket(); - //mySocket = new QTcpServer(); - // 取消已有的连接 - mySocket->abort(); - // 连接服务器 - mySocket->connectToHost(ip, port); - if (!mySocket->waitForConnected(100)) { - qDebug() << "connect failed!"; - return false; - } - qDebug() << "connect successfully!"; + //mySocket = new QTcpSocket(); + ////mySocket = new QTcpServer(); + //// 取消已有的连接 + //mySocket->abort(); + //// 连接服务器 + //mySocket->connectToHost(ip, port); + //if (!mySocket->waitForConnected(100)) { + // qDebug() << "connect failed!"; + // return false; + //} + //qDebug() << "connect successfully!"; + // return true; } #endif @@ -83,17 +84,32 @@ void threadSendTCP::run() //mySocket->flush(); } } +//字符串转ASCII +std::vector stringToAscii(const std::string& str) +{ + std::vector asciiValues; + for (char c : str) { + asciiValues.push_back(static_cast(c)); + } + return asciiValues; +} void threadSendTCP::sendData(_TCPSendInfo* TCPSendInfo, int Num) { - //std::string fileName = TCPSendInfo->pics_name + ", " + QString::number(Num).toStdString(); + std::string fileName = TCPSendInfo->pics_name; + std::vector asciiVals = stringToAscii(fileName); //mySocket->write(fileName.c_str()); - char temp = num % 10; - - mySocket->write((char*)&temp,sizeof(char)); + //char temp = num % 10; + //mySocket->write((char*)&temp,sizeof(char)); //mySocket->write("\n"); - mySocket->flush(); - - m_PLCTCPDevice->write_short_2_plc(0, 1, (uint16_t*)& temp); + //mySocket->flush(); + // + //遍历发送 + int i = 0; + for (int val:asciiVals) + { + m_PLCTCPDevice->write_short_2_plc(40000 + i, 1, (uint16_t*)&val); + i++; + } } #ifdef __TCPServer