-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdriver.cpp
254 lines (222 loc) · 5.67 KB
/
driver.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <QDebug>
#include <QFile>
#include <QtGlobal>
#include "driver.h"
#include "unistd.h"
#include "errno.h"
#include "fcntl.h"
#include <iostream>
#ifdef Q_WS_X11
#include <sys/ioctl.h>
#include "termios.h"
#endif
#ifdef Q_WS_WIN
#include "windows.h"
#endif
Driver::Driver(QObject *parent) :
QObject(parent)
{
file = new QFile;
}
Driver::~Driver()
{
file->close();
delete file;
}
unsigned short Driver::PortInit(QString name, unsigned long int Baud) //otwieranie pliku odpowiedzialnego za port szeregowy
{
#ifdef Q_WS_X11
port_file = open(name, O_RDWR | O_NOCTTY | O_NDELAY);
#endif
#ifdef Q_WS_WIN
const wchar_t *portNameArray = (const wchar_t*)name.utf16();
hCommDev = CreateFile(portNameArray,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
#endif
#ifdef Q_WS_X11
if(port_file < 0) // b³¹d otwarcia portu
{
qDebug() << "Nie mozna otworzyc portu, port_file = " << port_file;
return 0;
}
else //konfiguracja portu
{
file->open(port_file,QIODevice::ReadWrite|QIODevice::Unbuffered);
struct termios options; //utworzenie struktury konfiguracyjnej portu com
tcgetattr(port_file,&options);
//ustawienie predkosci wysy³ania
//tutaj zeby odczytywac predkosc z Baud trzeba by zrobic jakiegos cesa...
cfsetispeed(&options,B115200);
cfsetospeed(&options,B115200);
//ustawienie ramki danych
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CRTSCTS;
options.c_iflag &= ~(IXON|IXOFF|IXANY);
options.c_cc[VTIME] = 1; //w milisekundach
options.c_cflag|=CREAD|CLOCAL;
options.c_lflag&=(~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|ISIG));
options.c_iflag&=(~(INPCK|IGNPAR|PARMRK|ISTRIP|ICRNL|IXANY));
options.c_oflag&=(~OPOST);
options.c_cc[VMIN]=1;
options.c_cc[VINTR] = _POSIX_VDISABLE;
options.c_cc[VQUIT] = _POSIX_VDISABLE;
options.c_cc[VSTART] = _POSIX_VDISABLE;
options.c_cc[VSTOP] = _POSIX_VDISABLE;
options.c_cc[VSUSP] = _POSIX_VDISABLE;
fcntl(port_file,F_SETFL,FNDELAY);
tcsetattr(port_file,TCSANOW,&options); //natychmiastowe zaakceptowanie ustawien
}
#endif
#ifdef Q_WS_WIN
DCB dcb = {0}; //struktura z konfiguracja
GetCommState(hCommDev,&dcb); //przekopiowanie wartoœci do tej struktury
if(hCommDev != INVALID_HANDLE_VALUE)
{
dcb.DCBlength = sizeof(dcb);
if(!GetCommState(hCommDev,&dcb))
return false;
dcb.BaudRate = Baud;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.ByteSize = 8;
dcb.fParity = FALSE;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDsrSensitivity = FALSE;
dcb.fAbortOnError = FALSE;
dcb.fAbortOnError = FALSE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fErrorChar = FALSE;
dcb.fNull = FALSE;
if(!SetCommState(hCommDev,&dcb))
return false;
//if(!SetupComm())
}
SetCommState(hCommDev,&dcb);
#endif
return 1;
}
unsigned short Driver::Check() //sprawdza czy port szeregowy jest otwarty
{
#ifdef Q_WS_X11
if(port_file <= 0 )
{
qDebug()<< "Port szeregowy zamkniety" << port_file;
return 0;
}
else
{
qDebug() << "Port szeregowy otwarty" << port_file;
return 1;
}
#endif
#ifdef Q_WS_WIN
if(hCommDev == INVALID_HANDLE_VALUE)
{
return 0;
}
else
{
return 1;
}
#endif
}
void Driver::PortClose() //zamykanie portu szeregowego
{
#ifdef Q_WS_X11
file->close();
close(port_file);
//qDebug() << "Port zamkniety";
#endif
#ifdef Q_WS_WIN
CloseHandle(hCommDev);
#endif
}
unsigned long Driver::WriteData(const char *data,long int maxSize) //wysylanie danych
{
#ifdef Q_WS_X11
long bytes;
bytes = file->write(data,maxSize);
file->flush();
return bytes;
#endif
#ifdef Q_WS_WIN
DWORD bytesWritten = 0;
bool state;
state = WriteFile(hCommDev,(void *)data,(DWORD) maxSize,&bytesWritten,NULL);
if(state == true)
{
return bytesWritten;
}
else
{
return -1;
}
#endif
}
unsigned long Driver::ReadData(char *data,long maxSize) //odczyt danych
{
#ifdef Q_WS_X11
int bytes;
bytes = file->read(data,maxSize);
return bytes;
#endif
#ifdef Q_WS_WIN
DWORD bytesRead =0;
bool state;
state = ReadFile(hCommDev,(void *)data,(DWORD)maxSize,&bytesRead,NULL);
return bytesRead;
#endif
}
void Driver::Flush() //czyszczenie bufora
{
#ifdef Q_WS_X11
file->flush();
#endif
#ifdef Q_WS_WIN
FlushFileBuffers(hCommDev);
#endif
}
long Driver::BytesAvailable() //ilosc bajtow do odebrania z bufora
{
#ifdef Q_WS_X11
int nbytes;
if(ioctl(file->handle(),FIONREAD,&nbytes)<0)
nbytes = 0;
return (long int)nbytes;
#endif
#ifdef Q_WS_WIN
DWORD erros;
COMSTAT status;
if(ClearCommError(hCommDev,&erros,&status))
{
return status.cbInQue;
}
else
return -1;
#endif
}
unsigned int Driver::SendVal(int *val_to_send)
{
#ifdef Q_WS_X11
QByteArray *array = new QByteArray;
array->setNum(*val_to_send);
file->write(*array);
delete array;
return 0;
#endif
#ifdef Q_WS_WIN
QByteArray *array = new QByteArray;
array->setNum(*val_to_send);
int maxSize = sizeof(array);
DWORD bytesWritten = 0;
bool state;
state = WriteFile(hCommDev,(void *)array,(DWORD) maxSize,&bytesWritten,NULL);
return 0;
#endif
}