-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialDevice_pc.c
More file actions
273 lines (233 loc) · 7.05 KB
/
SerialDevice_pc.c
File metadata and controls
273 lines (233 loc) · 7.05 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//------------------------------------------------------------------------------
//
// File: SerialDevice.cpp
// Abstract: Serial Device Abstraction
// Version: 0.1
// Date: 18.05.2016
// Disclaimer: This example code is provided by IMST GmbH on an "AS IS" basis
// without any warranties.
//
// This file was modified to allow as possible a no platform dependant implementation
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Include Files
//------------------------------------------------------------------------------
#include "genericSerial.h"
#ifdef Q_OS_WIN
#include <windows.h>
#endif // Q_OS_WIN
#ifdef Q_OS_UX
//#include <stdio.h> // Standard input/output definitions
#include <string.h> // String function definitions
#include <unistd.h> // UNIX standard function definitions
#include <fcntl.h> // File control definitions
//#include <errno.h> // Error number definitions
#include <termios.h> // POSIX terminal control definitions
#endif // Q_OS_UX
//------------------------------------------------------------------------------
// Section RAM
//------------------------------------------------------------------------------
#ifdef Q_OS_WIN
// File Handle
static HANDLE ComHandle = INVALID_HANDLE_VALUE;
#endif
#ifdef Q_OS_UX
static int fd = -1; // File descriptor for the port
#endif // Q_OS_UX
//------------------------------------------------------------------------------
// Section Code
//------------------------------------------------------------------------------
// open serial device
bool
SerialDevice_Open(const char *comPort,
UINT32 baudRate,
int dataBits,
UINT8 parity)
{
#ifdef Q_OS_WIN
// handle valid ?
if (ComHandle != INVALID_HANDLE_VALUE)
SerialDevice_Close();
char devName[80];
// windows workaround for COM Ports higher than COM9
strcpy(devName, "\\\\.\\");
strcat(devName, comPort);
ComHandle = CreateFileA(devName,
GENERIC_WRITE | GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
0, //FILE_FLAG_WRITE_THROUGH, //0
NULL);
// handle valid ?
if (ComHandle != INVALID_HANDLE_VALUE) {
DCB dcb;
if (GetCommState(ComHandle, &dcb)) {
dcb.DCBlength = sizeof(DCB);
dcb.BaudRate = baudRate;
dcb.ByteSize = dataBits;
dcb.Parity = parity; //EVENPARITY;// NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fDsrSensitivity = FALSE;
dcb.fTXContinueOnXoff = FALSE;
dcb.fOutX = FALSE; // no XON/XOFF
dcb.fInX = FALSE; // no XON/XOFF
dcb.fErrorChar = FALSE;
dcb.fNull = FALSE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fAbortOnError = FALSE;
if (SetCommState(ComHandle, &dcb)) {
COMMTIMEOUTS commTimeouts;
commTimeouts.ReadIntervalTimeout = MAXDWORD;
commTimeouts.ReadTotalTimeoutMultiplier = 0;
commTimeouts.ReadTotalTimeoutConstant = 0;
commTimeouts.WriteTotalTimeoutMultiplier = 10;
commTimeouts.WriteTotalTimeoutConstant = 1;
SetCommTimeouts(ComHandle, &commTimeouts);
// ok
return true;
}
}
// close device
SerialDevice_Close();
}
#endif // Q_OS_WIN
#ifdef Q_OS_UX
char devName[80];
struct termios options;
strcpy(devName, comPort);
fd = open(devName, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
//Could not open the port.
return false;
}
else {
fcntl(fd, F_SETFL, 0);
//Get the current options for the port
tcgetattr(fd, &options);
//Set baud rates
cfsetispeed(&options, baudRate);
cfsetospeed(&options, baudRate);
options.c_cflag &= ~CSIZE; /* Mask the character size bits */
options.c_cflag |= dataBits; /* Select 8 data bits */
//Enable the receiver and set local mode
options.c_cflag |= (CLOCAL | CREAD);
//Set the new options for the port
tcsetattr(fd, TCSANOW, &options);
}
return (fd);
#endif // Q_OS_UX
// error
return false;
}
// close serial device
bool
SerialDevice_Close()
{
#ifdef Q_OS_WIN
// handle valid ?
if (ComHandle != INVALID_HANDLE_VALUE) {
// cancel last operation
CancelIo(ComHandle);
// wait 100us
Sleep(100);
// close device
CloseHandle(ComHandle);
// invalidate handle
ComHandle = INVALID_HANDLE_VALUE;
// ok
return true;
}
#else
// Todo : add your own platform specific code here
#endif
// error
return false;
}
// send data
int
SerialDevice_SendData(UINT8 *txBuffer, UINT8 txLength)
{
#ifdef Q_OS_WIN
// handle valid ?
if (ComHandle == INVALID_HANDLE_VALUE)
return -1;
UINT32 numTxBytes;
// write chunk of data
if (!WriteFile(ComHandle, txBuffer, txLength, (DWORD*)&numTxBytes, 0))
{
// error
return -1;
}
// all bytes written ?
if (numTxBytes == (UINT32)txLength)
{
// ok
return numTxBytes;
}
#endif // Q_OS_WIN
#ifdef Q_OS_UX
unsigned int n = write(fd, txBuffer, txLength);
if (n < 0)
return -1;
else
return n;
#endif // Q_OS_UX
// error
return -1;
}
// send single byte
int
SerialDevice_SendByte(UINT8 txByte)
{
#ifdef Q_OS_WIN
// handle valid ?
if (ComHandle == INVALID_HANDLE_VALUE)
return -1;
UINT32 numTxBytes;
// write chunk of data
if (!WriteFile(ComHandle, &txByte, 1, (DWORD*)&numTxBytes, 0))
{
// error
return -1;
}
// all bytes written ?
if (numTxBytes == 1)
{
// ok
return numTxBytes;
}
#else
// Todo : add your own platform specific code here
#endif
// error
return -1;
}
// read data
int
SerialDevice_ReadData(UINT8 *rxBuffer, int rxBufferSize)
{
#ifdef Q_OS_WIN
// handle ok ?
if (ComHandle == INVALID_HANDLE_VALUE)
return -1;
DWORD numRxBytes = 0;
// read chunk of data
if (ReadFile(ComHandle, rxBuffer, rxBufferSize, &numRxBytes, 0))
{
// return number of bytes read
return (int)numRxBytes;
}
#else
// Todo : add your own platform specific code here
#endif
// error
return -1;
}
//------------------------------------------------------------------------------
// end of file
//------------------------------------------------------------------------------