-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_manualcontrol.cpp
172 lines (162 loc) · 5.71 KB
/
thread_manualcontrol.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "thread_manualcontrol.h"
#include "mainwindow.h"
#include "waypointspath_control.h"
#include <QMainWindow>
#include <QtGui>
#include "FlyCapture2.h"
#include<conio.h>
#include<stdio.h>
#include<iostream>
#include <qthread.h>
#include <qlayout.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/video/background_segm.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/flann/flann.hpp>
#include <opencv2/flann/flann_base.hpp>
#include <opencv2/calib3d/calib3d.hpp> // for homography
#include <opencv2/nonfree/nonfree.hpp>
#include <math.h>
#include <time.h>
#include<windows.h>
#include<string.h>
#include<atlstr.h>
#include "SiUSBXp.h"
Thread_manualcontrol::Thread_manualcontrol(QObject * parent = (QObject *)0, MainWindow * mw = NULL): QThread(parent){
qDebug() << "Running";
w = mw; //value = 0xaa; time_duration = 4000;
//QObject::connect(w->mapper,SIGNAL(mapped(QString)), this, SLOT(start()), Qt::QueuedConnection);
QObject::connect(this,SIGNAL(started()),this,SLOT(on_coilcontrol()), Qt::QueuedConnection);
QObject::connect(this,SIGNAL(manual_finished()),this,SLOT(quit()));
}
void Thread_manualcontrol::TimeDelay(UINT delay)
{
DWORD start, end;
start = end = GetTickCount();
// Wait for 'delay' milliseconds.
while ((end - start) < delay)
{
end = GetTickCount();
}
}
void Thread_manualcontrol::on_coilcontrol(){
//QThread::exec();
qDebug() << "Thread : "<<this->thread()->currentThreadId();
qDebug() <<"Command Value :" <<w->value <<" Coil : " << coil;
//time_duration = 2000;
//if(!w->stop_capture)
//w->on_Capturepb_pushed();
//emit manual_finished();
//return;
//MainWindow * w;
for(int i = 0; i < 7; i++)
{w->datasend[i] = 0; w->prevsent[i] = 0x00;}
bool success = false, check;
//serialHandle = NULL;
CString comport = "\\\\.\\COM4";
//ui->camwn_label->setText(coil);
//if(first_press)
//CloseHandle(serialHandle);
w->serialHandle = CreateFile(comport, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(w->serialHandle == INVALID_HANDLE_VALUE)
qDebug("Error: Invalid Handle Value for Device 1.");
// Do some basic settings
DCB serialParams = { 0 };
serialParams.DCBlength = sizeof(serialParams);
GetCommState(w->serialHandle, &serialParams);
serialParams.BaudRate = 9600;
serialParams.ByteSize = 8;
serialParams.StopBits = ONESTOPBIT;
serialParams.Parity = PARITY_NONE;
SetCommState(w->serialHandle, &serialParams);
// Set timeouts
COMMTIMEOUTS timeout = { 0 };
timeout.ReadIntervalTimeout = 50;
timeout.ReadTotalTimeoutConstant = 50;
timeout.ReadTotalTimeoutMultiplier = 50;
timeout.WriteTotalTimeoutConstant = 50;
timeout.WriteTotalTimeoutMultiplier = 10;
DWORD toBeWritten = 7, toBeRead = 7;
DWORD* written = (DWORD*)malloc(sizeof(DWORD));
DWORD* read = (DWORD*)malloc(sizeof(DWORD));
LPDWORD event = (DWORD *)malloc(sizeof(DWORD));int ev;
SetCommMask(w->serialHandle, EV_TXEMPTY);
//char coil_ch = coil.toStdString().c_str()[0];
switch(coil){
case 'U':
w->datasend[3] = w->value;
//qDebug() << " Command Value : " << w->value;
//w->camwn_label->setText(QString::number(datasend[0]));
//SetCommMask(serialHandle, EV_TXEMPTY);
//if(WaitCommEvent(serialHandle, event , NULL)){
//check = send_serialportcommand(datasend);
//if(WaitCommEvent(w->serialHandle,event,NULL))
check = WriteFile(w->serialHandle, (LPVOID)w->datasend, toBeWritten, written, NULL);
//GetCommMask(serialHandle,event);}
//send_serialportcommand(datasend);
TimeDelay(time_duration);
//if(check)
//check = send_serialportcommand(prevsent);
//datasend[0] = 0x00;
//if(WaitCommEvent(w->serialHandle,event,NULL))
check = WriteFile(w->serialHandle, (LPVOID)w->prevsent, toBeWritten, written, NULL);
//status = SI_Write(*hDev1, (LPVOID)dataSent, toBeWritten, written);
if(!check)
{
//w->ui->camwn_label->setText("Surprise Removal on Device 1 Write!");
}
break;
case 'D':
w->datasend[2] = w->value;
//send_serialportcommand(datasend);
check = WriteFile(w->serialHandle, (LPVOID)w->datasend, toBeWritten, written, NULL);
TimeDelay(time_duration);
w->datasend[2] = 0x00;
//send_serialportcommand(prevsent);
if(WaitCommEvent(w->serialHandle,event,NULL))
check = WriteFile(w->serialHandle, (LPVOID)w->datasend, toBeWritten, written, NULL);
//status = SI_Write(*hDev1, (LPVOID)dataSent, toBeWritten, written);
if(!check)
{
printf("Surprise Removal on Device 1 Write!");
}break;
case 'L':
w->datasend[5] = int(w->value);
check = WriteFile(w->serialHandle, (LPVOID)w->datasend, toBeWritten, written, NULL);
qDebug() << " Check status : "<< check;
TimeDelay(time_duration);
check = WriteFile(w->serialHandle, (LPVOID)w->prevsent, toBeWritten, written, NULL);
qDebug() << " Check status : "<< check;
//status = SI_Write(*hDev1, (LPVOID)dataSent, toBeWritten, written);
if(!check)
{
printf("Surprise Removal on Device 1 Write!");
}break;
case 'R':
w->datasend[6] = w->value;
check = WriteFile(w->serialHandle, (LPVOID)w->datasend, toBeWritten, written, NULL);
TimeDelay(time_duration);
check = WriteFile(w->serialHandle, (LPVOID)w->prevsent, toBeWritten, written, NULL);
//status = SI_Write(*hDev1, (LPVOID)dataSent, toBeWritten, written);
if(!check)
{
printf("Surprise Removal on Device 1 Write!");
}
break;
case 'C':
//w->camwn_label->setText("Checking terminate");
w->datasend[0] = 0x00;
check = WriteFile(w->serialHandle,(LPVOID)w->datasend,toBeWritten,written,NULL);
break;
}
//first_press++;
//if(first_press)
TimeDelay(100);
CloseHandle(w->serialHandle);
emit manual_finished();
//WriteFile(serialHandle,(LPVOID){0x00,0x00,0x00,0x00,0x00,0x00,0x00},toBeWritten,written, NULL);
}