完善ftp传文件功能

main
seiyu 1 year ago
parent 3f6c64d10c
commit 39f080cdca

@ -2,7 +2,7 @@
#include <opencv2/opencv.hpp>
#define NumberOfSupportedCameras 8
#define NumberOfSupportedCameras 4
#define EdgeEvent 1
#define ImageCap 2

@ -56,10 +56,15 @@ ASyncQueue<bool> *g_result_queue[NumberOfSupportedCameras]; //
bool g_debug_mode; //相机调试模式,工作模式必须暂停状态才能打开
SyncQueue<cv::Mat> *g_debug_queue[NumberOfSupportedCameras]; //相机调试模式图像队列
#ifdef __UDPSend
SyncQueue<_UDPSendInfo>* UDP_Info_queue;
//SyncQueue<_ExportDataInfo> *export_Data_Info_queue;
#endif
bool flag = false;
#ifdef __ExportData
SyncQueue<_XMLExportDataInfo>* export_XMLData_Info_queue;
ExportDataThread exportDataThread;
#endif
bool g_admin_mode; //是否管理员模式
@ -82,7 +87,6 @@ int quantity_stop_count; //
int g_seconds;
int CurOpLabel;
bool onrestart = false;
bool flag = false;
VOID BeforeWork(int shoot[])
{
@ -136,14 +140,14 @@ Cigarette::Cigarette(QWidget *parent)
g_save_queue = new SyncQueue<std::pair<std::string, cv::Mat> >(Queue_Size);
g_save_queue->name = "save queue";
#ifdef __UDPSend
UDP_Info_queue = new SyncQueue<_UDPSendInfo>(Queue_Size);
UDP_Info_queue->name = "UDP Info queue";
//export_Data_Info_queue = new SyncQueue<_ExportDataInfo>(Queue_Size);
//export_Data_Info_queue->name = "Export Data Info queue";
#endif
#ifdef __ExportData
export_XMLData_Info_queue = new SyncQueue<_XMLExportDataInfo>(Queue_Size);
export_XMLData_Info_queue->name = "Export Data Info queue";
#endif
last_shift = 256;
for(int i=0;i<NumberOfSupportedCameras;i++)
@ -454,7 +458,7 @@ Cigarette::Cigarette(QWidget *parent)
}
connect(toolButton_cam_signalMapper, SIGNAL(mapped(int)), this, SLOT(OnToolButtonCamReleasedHub(int)));
connect(pTimer_Cam_signalMapper, SIGNAL(mapped(int)), this, SLOT(OpenCamTimeoutHub(int)));
//connect(pTimer_Cam_signalMapper, SIGNAL(mapped(int)), this, SLOT(OpenCamTimeoutHub(int)));
connect(image_lable_DBsignalMapper0, SIGNAL(mapped(int)), this, SLOT(OnDBClickHub(int)));
connect(image_lable_DBsignalMapper1, SIGNAL(mapped(int)), this, SLOT(OnDBClickHub(int)));
#ifdef DRAW_RECT
@ -867,31 +871,27 @@ void Cigarette::enable_shift()
ui.pushButton_clear->setEnabled(true);
}
#ifdef __UDPSend
void Cigarette::sendLatestData() {
// 连接UDP发送最新的数据
int dataport = g_sys_conf.MonitorPort;
_UDPSendInfo UDPSendInfo;
for (int i = 0; i < NumberOfSupportedCameras; i++)
{
UDPSendInfo.index = i;
UDPSendInfo.FrameID = ok_frame;
UDPSendInfo.ok[i] = ok[i];
#ifdef __UDPSend
UDP_Info_queue->put(UDPSendInfo);
#endif
UDPSendInfo.FrameID = ng_frame;
UDPSendInfo.ng[i] = ng[i];
#ifdef __UDPSend
UDP_Info_queue->put(UDPSendInfo);
#endif
UDPSendInfo.FrameID = total_frame;
UDPSendInfo.Total = production_number[i];
#ifdef __UDPSend
UDP_Info_queue->put(UDPSendInfo);
#endif
}
}
#endif
void Cigarette::OnRotateReleasedHub(int Num)//旋转按钮
{
@ -1233,12 +1233,12 @@ void Cigarette::OnNotifyHub(int Num, int Cnt,cv::Mat m)
display_lable_mat[Num][Cnt]->setPixmap(QPixmap::fromImage(tempImage));
g_display_label_conf[Num].g_last_mat[Cnt] = m;
#ifdef __UDPSend
_UDPSendInfo UDPSendInfo;
UDPSendInfo.FrameID = image_frame;
UDPSendInfo.index = Num;
UDPSendInfo.cnt = Cnt;
UDPSendInfo.image = m.clone();
#ifdef __UDPSend
UDP_Info_queue->put(UDPSendInfo);
#endif
}
@ -1255,11 +1255,11 @@ void Cigarette::OnDisplayCheckNumberHub(int Num, long no)
{
production_number[Num]++;
lcdNumber_total_mat[Num]->display(production_number[Num]);
#ifdef __UDPSend
_UDPSendInfo UDPSendInfo;
UDPSendInfo.FrameID = total_frame;
UDPSendInfo.index = Num;
UDPSendInfo.Total = production_number[Num];
#ifdef __UDPSend
UDP_Info_queue->put(UDPSendInfo);
#endif
#ifdef __ExportData
@ -1279,11 +1279,11 @@ void Cigarette::OnDisplayJdNoHub(int Num, QString jd_no)
void Cigarette::OnOKHub(int Num)
{
ok[Num]++;
#ifdef __UDPSend
_UDPSendInfo UDPSendInfo;
UDPSendInfo.FrameID = ok_frame;
UDPSendInfo.index = Num;
UDPSendInfo.ok[Num] = ok[Num];
#ifdef __UDPSend
UDP_Info_queue->put(UDPSendInfo);
#endif
lcdNumber_ok_mat[Num]->display(ok[Num]);
@ -1297,11 +1297,11 @@ void Cigarette::OnOKHub(int Num)
void Cigarette::OnNGHub(int Num)
{
ng[Num]++;
#ifdef __UDPSend
_UDPSendInfo UDPSendInfo;
UDPSendInfo.FrameID = ng_frame;
UDPSendInfo.index = Num;
UDPSendInfo.ng[Num] = ng[Num];
#ifdef __UDPSend
UDP_Info_queue->put(UDPSendInfo);
#endif
lcdNumber_ng_mat[Num]->display(ng[Num]);
@ -1314,11 +1314,11 @@ void Cigarette::OnNGHub(int Num)
void Cigarette::updateStatisticsHub(const QString& statisticalData, int Num) {
label_cap_speed_mat[Num]->setText(statisticalData + QStringLiteral("个/秒"));
#ifdef __UDPSend
_UDPSendInfo UDPSendInfo;
UDPSendInfo.FrameID = speed_frame;
UDPSendInfo.index = Num;
UDPSendInfo.speed = statisticalData;
#ifdef __UDPSend
UDP_Info_queue->put(UDPSendInfo);
#endif
#ifdef __ExportData
@ -1952,14 +1952,14 @@ void Cigarette::on_pushButton_clear_released()//
ng_rate = 0;
exportXMLDataInfo.PassRate = 1.0 - ng_rate;
int flag = 0;
int f = 0;
for (int i = 1; i < NumberOfSupportedCameras; i++) {
if (exportData[i].kick != exportData[0].kick) {
flag = 1;
f = 1;
break;
}
}
if (flag == 1) {
if (f == 1) {
// Kick值不一致
for (int i = 0; i < NumberOfSupportedCameras; i++) {
exportXMLDataInfo.TotalKickNum += exportData[i].kick;
@ -1970,7 +1970,7 @@ void Cigarette::on_pushButton_clear_released()//
export_XMLData_Info_queue->put(exportXMLDataInfo);
}
flag = false;
//flag = false;
#endif
//发送换班消息给PLC
@ -1999,29 +1999,25 @@ void Cigarette::on_pushButton_clear_released()//
lcdNumber_ok_mat[i]->display(ok[i]);
ng[i] = 0;
lcdNumber_ng_mat[i]->display(ng[i]);
#ifdef __UDPSend
_UDPSendInfo UDPSendInfo;
UDPSendInfo.index = i;
UDPSendInfo.index = i;
UDPSendInfo.FrameID = ok_frame;
UDPSendInfo.ok[i] = ok[i];
#ifdef __UDPSend
UDP_Info_queue->put(UDPSendInfo);
#endif
UDPSendInfo.FrameID = ng_frame;
UDPSendInfo.ng[i] = ng[i];
#ifdef __UDPSend
UDP_Info_queue->put(UDPSendInfo);
#endif
UDPSendInfo.FrameID = info_frame;
UDPSendInfo.timecost = QString::number(0);
UDPSendInfo.JD = QString("0,0,0");
#ifdef __UDPSend
UDP_Info_queue->put(UDPSendInfo);
#endif
UDPSendInfo.FrameID = total_frame;
UDPSendInfo.Total = 0;
#ifdef __UDPSend
UDP_Info_queue->put(UDPSendInfo);
#endif
}
@ -2760,7 +2756,7 @@ void Cigarette::init_plc_value()
void Cigarette::CreatWorkThread(int classid,int Num,Cigarette* classptr)
{
work_thread[Num].init(g_image_queue[Num],g_result_queue[Num],classid,Num);
work_thread[Num].init(g_image_queue[Num], g_result_queue[Num], classid, Num);
connect(&work_thread[Num], SIGNAL(notify(int,int,cv::Mat)), classptr, SLOT(OnNotifyHub(int,int,cv::Mat)));
connect(&work_thread[Num], SIGNAL(display_timecost(int,int)), classptr, SLOT(OnDisplayTimeCostHub(int,int)));
connect(&work_thread[Num], SIGNAL(display_check_total(int,long)), classptr, SLOT(OnDisplayCheckNumberHub(int,long)));
@ -3580,10 +3576,10 @@ void Cigarette::record_output_statistic(qint64 quantity, int Kick[NumberOfSuppor
last_shift = shift;
}
#ifdef __UDPSend
void Cigarette::recMsgFromUdp(QString data)
{
//std::cout << data.toStdString() << std::endl;
#ifdef __UDPSend
if (data == "START")
{
if (!g_admin_mode)
@ -3999,5 +3995,5 @@ void Cigarette::recMsgFromUdp(QString data)
}
}
}
#endif
}
}
#endif

@ -91,7 +91,9 @@ private slots :
void on_toolButton_alarm_released();
void on_pushButton_clear_released();
void enable_shift();
#ifdef __UDPSend
void sendLatestData();
#endif
void OnNotifyHub(int Num, int Cnt, cv::Mat m);
void OnDisplayTimeCostHub(int Num, int ms);
void OnDisplayCheckNumberHub(int Num, long no);
@ -206,7 +208,9 @@ public:
#endif
public slots:
void CleanThreadStartAuto();
#ifdef __UDPSend
void recMsgFromUdp(QString data);
#endif
//void ClogThreadStart();
signals:

@ -7,7 +7,7 @@
#include "QtCore\qdatetime.h"
//#define __DEBUG //debug信息输出功能
//#define __UDPSend //网络发送功能
#define __UDPSend //网络发送功能
#define USB_BASLER_NEW_FW //使用basler定制固件
//#define IMM_PROCESS //拍照后立马处理,不等校验信号
//#define IMM_FEED_BACK //处理完后立马反馈,不等校验信号
@ -21,7 +21,7 @@
#endif
//#define identify_Hik_YSXID//识别海康相机YSXID
#define __ExportData // 输出检测数据到XML文档
//#define DRAW_RECT // 鼠标画框功能
#define DRAW_RECT // 鼠标画框功能
#define Queue_Size 15
#define Unit_Queue_Size Queue_Size*3

@ -35,27 +35,14 @@ ExportDataThread::ExportDataThread(QObject* parent) : QThread(parent)
if (hint == NULL) {
return;
}
// FTP地址
string ftpServer = "192.168.1.170";
/* 端口号一般为21 */
int port = 666;
/* 用户名 */
string userName = "FTP2";
/* 密码 */
string pwd = "123";
// 创建ftp连接
hftp = InternetConnectA(hint, ftpServer.c_str(), port, userName.c_str(), pwd.c_str(), INTERNET_SERVICE_FTP, 0, 0);
if (hftp == NULL) {
qDebug() << "ftp connect failed";
return;
}
if (!ConnectFtp())
qDebug() << "First connect FTP failed because " << GetLastError();
}
void ExportDataThread::init()
{
b_quit = false;
flag = 0;
flag = false;
}
void ExportDataThread::start_work()
@ -72,6 +59,31 @@ void ExportDataThread::stop()
InternetCloseHandle(hint);
}
bool ExportDataThread::ConnectFtp() {
// FTP地址
string ftpServer = "192.168.1.170";
/* 端口号一般为21 */
int port = 666;
/* 用户名 */
string userName = "FTP2";
/* 密码 */
string pwd = "123";
if (hftp != NULL) {
InternetCloseHandle(hftp);
hftp = NULL;
}
// 创建ftp连接
hftp = InternetConnectA(hint, ftpServer.c_str(), port, userName.c_str(), pwd.c_str(), INTERNET_SERVICE_FTP, 0, 0);
if (hftp == NULL) {
qDebug() << "ftp connect failed because " << GetLastError();
return false;
}
else
qDebug() << "ftp reconnect success";
return true;
}
bool _ExportDataInfo::getAverageData(map<string, float> &averageData, int index)
{
tinyxml2::XMLDocument doc;
@ -224,7 +236,7 @@ void ExportDataThread::run()
_XMLExportDataInfo element;
export_XMLData_Info_queue->take(element);
if (element.cameraId != -1 && flag == 1) {
if (element.cameraId != -1 && flag) {
char buf[256];
memset(buf, 0, 256);
QString xmlPath = QString(EXPORTDATA_FILE).arg(element.cameraId);
@ -235,6 +247,7 @@ void ExportDataThread::run()
ConnectServer(filePath, remotePath);
}
}
flag = false;
for (int index = 0; index < NumberOfSupportedCameras; index++) {
pDocument[index]->~XMLDocument();
@ -281,8 +294,6 @@ void ExportDataThread::ConnectServer(QString srcPath, QString destPath) {
GetFiles(filePath, files);
size = files.size();
}
DWORD dw = GetLastError();
// 上传文件源为一个文件
if (size == 0) {
@ -290,8 +301,17 @@ void ExportDataThread::ConnectServer(QString srcPath, QString destPath) {
string destFileName = remotePath.substr(pos + 1);
string tempPath = remotePath.substr(0, pos + 1);
check_save_dir(tempPath);
if (!FtpPutFileA(hftp, filePath.c_str(), remotePath.c_str(), FTP_TRANSFER_TYPE_BINARY, 0))
qDebug() << "ftp put file failed because" << dw;
if (!InternetGetConnectedState(NULL, 0)) {
qDebug() << "计算机未连接到互联网";
}
else {
while (!FtpPutFileA(hftp, filePath.c_str(), remotePath.c_str(), FTP_TRANSFER_TYPE_BINARY, 0)) {
qDebug() << "ftp put file "<< filePath.c_str() << "failed because" << GetLastError();
if (!ConnectFtp())
qDebug() << "connect FTP failed because " << GetLastError();
}
}
}
// 上传源为一个文件夹
else{
@ -302,7 +322,7 @@ void ExportDataThread::ConnectServer(QString srcPath, QString destPath) {
string destFileName = tempFilePath.substr(pos + 1);
check_save_dir(remotePath + tempFilePath.substr(0, pos + 1));
if(!FtpPutFileA(hftp, files[i].c_str(), destFileName.c_str(), FTP_TRANSFER_TYPE_BINARY, 0))
qDebug() << "ftp put files failed because " << dw;
qDebug() << "ftp put files failed because " << GetLastError();
}
}
}
@ -339,44 +359,4 @@ void ExportDataThread::GetFiles(string path, std::vector<string>& files) {
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
/*
BOOL ExportDataThread::FtpUploadPic(uchar *buffer, string remotePath) {
DWORD dwBytesReturn = 0;
DWORD UploadDataSize = 0;
BYTE* pUploadData = NULL;
DWORD dwRet, bRet = 0;
string tempFile = "temp.jpg";
HINTERNET hFtpFile = NULL;
hFtpFile = FtpOpenFileA(hftp, remotePath.c_str(), GENERIC_WRITE, FTP_TRANSFER_TYPE_BINARY | INTERNET_FLAG_RELOAD, NULL);
// 打开文件
HANDLE hFile = CreateFileA(tempFile.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, NULL);
if (INVALID_HANDLE_VALUE == hFile) {
qDebug() << "CreateFile error because " << GetLastError();
CloseHandle(hFile);
return FALSE;
}
/// -> 以下的代码还未改好
// 获取文件大小
UploadDataSize = GetFileSize(hFile, NULL);
pUploadData = new BYTE[UploadDataSize];
// 读取文件到缓冲区
ReadFile(hFile, pUploadData, UploadDataSize, &dwRet, NULL);
UploadDataSize = dwRet;
// 开始上传数据
bRet = InternetWriteFile(hFtpFile, buffer, UploadDataSize, &dwBytesReturn);
if (FALSE == bRet) {
qDebug() << "InternetWirteFile error because " << GetLastError();
delete[] buffer;
return FALSE;
}
delete[] buffer;
CloseHandle(hFile);
return TRUE;
}
*/
}

@ -119,6 +119,7 @@ public:
void stop();
//int insertXMLNode(const char* xmlPath, _ExportDataInfo& data);
int insertXMLNode(const char* xmlPath, _XMLExportDataInfo& data);
bool ConnectFtp();
void ConnectServer(QString srcPath, QString destPath);
void GetFiles(string path, std::vector<string>& files);
@ -128,10 +129,10 @@ protected:
public:
bool b_quit;
tinyxml2::XMLDocument *pDocument[NumberOfSupportedCameras];
int flag;
//int flag;
HINTERNET hint;
HINTERNET hftp;
HINTERNET hint = NULL;
HINTERNET hftp = NULL;
public slots:
void EDrecMsgFromCigarette();

@ -6,6 +6,7 @@
#pragma comment(lib,"CryptoToolLib.lib")
#include "CryptoToolLib.h"
#endif
int main(int argc, char *argv[])
{
#if defined LICENSE_VERIFY

@ -8,9 +8,7 @@
#include <iostream>
#include <string>
#include <vector>
#include <qdir.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/types_c.h>
@ -65,7 +63,8 @@ protected:
bool b_save = cv::imwrite(file_name, image);
if (b_save)
{
sendDataToExport(QString::fromStdString(file_name));
//sendDataToExport(QString::fromStdString(file_name));
sendDataToExport(QString::fromLocal8Bit(QByteArray::fromRawData(file_name.c_str(), file_name.size())));
//qDebug() << "Save pic successfully";
}
}

@ -12,14 +12,18 @@ extern AlgJd alg_jd[NumberOfSupportedCameras]; //
extern ConfPath g_conf_path;
extern SysConf g_sys_conf; //系统配置参数
extern DisplayLabelConf g_display_label_conf[NumberOfSupportedCameras];
extern ExportDataThread exportDataThread;
extern int rotationAngle[NumberOfSupportedCameras]; //图片旋转角度
extern bool isNeedRotate[NumberOfSupportedCameras];
extern SyncQueue<std::pair<std::string, cv::Mat> >* g_save_queue; //图片保存队列
extern SyncQueue<std::pair<int, cv::Mat> >* g_image_queue[NumberOfSupportedCameras]; //int±íʾһ¸öÄ¿±êÅÄÁ˼¸ÕÅ
extern SyncQueue<std::pair<int, cv::Mat>>* g_image_queue[NumberOfSupportedCameras]; //int表示一个目标拍了几张
#ifdef __UDPSend
extern SyncQueue<_UDPSendInfo>* UDP_Info_queue;
#endif
#ifdef __ExportData
extern ExportDataThread exportDataThread;
#endif
extern PLCDevice* m_PLCDevice;
extern bool g_debug_mode;
@ -36,7 +40,7 @@ void WorkThread::init(SyncQueue<std::pair<int, cv::Mat>>* image_ptr, ASyncQueue<
local_camera_number = Num;
local_classid = classid;
local_g_image_queue = image_ptr;
local_g_result_queue = result_ptr;
local_g_result_queue = result_ptr;
b_quit = false;
frame_total = 0;
}
@ -81,10 +85,11 @@ void WorkThread::run()
int unit_count = element.first;
cv::Mat image = element.second;
#ifdef __UDPSend
_UDPSendInfo UDPSendInfo;
UDPSendInfo.FrameID = info_frame;
UDPSendInfo.index = local_camera_number;
#endif
if (!image.data)
{
continue; //图像为空,跳过
@ -126,8 +131,9 @@ void WorkThread::run()
QDateTime ts_jd = QDateTime::currentDateTime();
int time_process = ts_start.msecsTo(ts_jd);
emit display_timecost(local_camera_number, time_process);
#ifdef __UDPSend
UDPSendInfo.timecost = QString::number(time_process);
#endif
cv::Mat image1;
cv::Mat image2;
@ -137,8 +143,9 @@ void WorkThread::run()
}
jd_no.chop(1);
emit display_jd_no(local_camera_number, jd_no);
#ifdef __UDPSend
UDPSendInfo.JD = jd_no;
#endif
bool IsNG = false;
int ngReason = 0;
QMap<int, QString> ng_reason_maps;
@ -169,17 +176,6 @@ void WorkThread::run()
"#" + "_" + QString::number(index + 1) + ".jpg";
std::string filename = file_name.toLocal8Bit().constData();
g_save_queue->put(std::make_pair(filename, m));
std::size_t pos = filename.find_first_of("/");
std::string tempPath = filename.substr(pos + 1);
pos = tempPath.find_first_of("/");
tempPath = tempPath.substr(pos + 1);
std::string remote_file_name = "/image/" + tempPath;
//if (m.data)
//{
// BOOL bRet = exportDataThread.FtpUploadPic(m.data, remote_file_name);
//}
}
}
@ -248,9 +244,6 @@ void WorkThread::run()
"#" + "_" + QString::number(index + 1) + "_" + ng_reason_maps[ngReason] +
".jpg";
g_save_queue->put(std::make_pair(file_name.toLocal8Bit().constData(), m));
//ftpManager.uploadSingleFile(file_name, remotePath);
//exportDataThread.ConnectServer(file_name, remotePath);
m = vec_out[index].clone();
file_name = g_conf_path.save_pics_path + "/ng_result/" +
@ -265,7 +258,6 @@ void WorkThread::run()
".jpg";
//g_save_queue->put(std::make_pair(file_name.toStdString(), m));
g_save_queue->put(std::make_pair(file_name.toLocal8Bit().constData(), m));
//exportDataThread.ConnectServer(file_name, remotePath);
}
}
}

Loading…
Cancel
Save