-
Notifications
You must be signed in to change notification settings - Fork 0
/
Physical.c
231 lines (206 loc) · 7.75 KB
/
Physical.c
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
/*------------------------------------------------------------------------------
-- SOURCE FILE: Physical.c - Contains all the OSI "physical layer"
-- functions for the RFID reader.
-- The definitions for the OSI layers
-- have been loosened somewhat, since the purpose
-- is to organize the functions intuitively,
-- rather than pedantically.
--
-- PROGRAM: RFID Reader - Enterprise Edition
--
-- FUNCTIONS:
-- DWORD WINAPI ReadThreadProc(HWND);
-- VOID RequestPacket(HWND hWnd);
-- VOID ProcessCommError(HANDLE);
--
--
-- DATE: Oct 13, 2010
--
-- REVISIONS: Nov 05, 2010
-- Modified ReadThreadProc to work more appropriately for the RFID
-- reader. Added RequestPacket()
--
-- DESIGNER: Dean Morin
--
-- PROGRAMMER: Dean Morin, Daniel Wright
--
-- NOTES:
-- Contains physical level functions for the RFID reader.
------------------------------------------------------------------------------*/
#include "Physical.h"
/*------------------------------------------------------------------------------
-- FUNCTION: ReadThreadProc
--
-- DATE: Oct 13, 2010
--
-- REVISIONS: Nov 05, 2010
-- Modified the function to also listen for a "disconnect" event,
-- and to break in that case.
-- ProcessRead() is now called once a complete packet is confirmed
-- (as opposed to sending the contents of the buffer to
-- ProcessRead() as soon as they arrive).
--
-- DESIGNER: Dean Morin
--
-- PROGRAMMER: Dean Morin, Daniel Wright
--
-- INTERFACE: DWORD WINAPI ReadThreadProc(HWND hWnd)
-- hWnd - the handle to the window
--
-- RETURNS: 0 because threads are required to return a DWORD.
--
-- NOTES:
-- While connected, this thread will loop and wait for characters
-- to arrive at the port. Once the EV_RXCHAR event is triggered,
-- ReadFile() is called to get however many characters have arrived
-- at the port by that time. This function uses overlapped I/O.
------------------------------------------------------------------------------*/
DWORD WINAPI ReadThreadProc(HWND hWnd) {
PWNDDATA pwd = NULL;
CHAR psReadBuf[READ_BUFSIZE] = {0};
OVERLAPPED overlap = {0};
DWORD dwBytesRead = 0;
DWORD dwEvent = 0;
DWORD dwError = 0;
COMSTAT cs = {0};
HANDLE hEvents[2] = {0};
BOOL requestPending = FALSE;
DWORD dwPacketLength = 0;
CHAR* pcPacket = NULL;
CHAR_LIST* pHead = NULL;
DWORD dwQueueSize = 0;
DWORD i = 0;
pwd = (PWNDDATA) GetWindowLongPtr(hWnd, 0);
if ((overlap.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL)) == NULL) {
DISPLAY_ERROR("Error creating event in read thread");
}
hEvents[0] = overlap.hEvent;
hEvents[1] = OpenEvent(DELETE | SYNCHRONIZE, FALSE, TEXT("disconnected"));
while (pwd->bConnected) {
if(!requestPending){
RequestPacket(hWnd);
requestPending = TRUE;
}
SetCommMask(pwd->hPort, EV_RXCHAR);
if (!WaitCommEvent(pwd->hPort, &dwEvent, &overlap)) {
ProcessCommError(pwd->hPort);
}
dwEvent = WaitForMultipleObjects(2, hEvents, FALSE, INFINITE);
if (dwEvent == WAIT_OBJECT_0 + 1) {
// the connection was severed
break;
}
ClearCommError(pwd->hPort, &dwError, &cs);
// ensures that there is a character at the port
if (cs.cbInQue) {
if (!ReadFile(pwd->hPort, psReadBuf, cs.cbInQue,
&dwBytesRead, &overlap)) {
// read is incomplete or had an error
ProcessCommError(pwd->hPort);
GetOverlappedResult(pwd->hThread, &overlap, &dwBytesRead, TRUE);
}
dwQueueSize = AddToBack(&pHead, psReadBuf, dwBytesRead);
if (dwQueueSize >= 2) {
dwPacketLength = GetFromList(pHead, 2);
if (dwQueueSize >= dwPacketLength) {
pcPacket = RemoveFromFront(&pHead, dwPacketLength);
ProcessPacket(hWnd, pcPacket, dwPacketLength);
memset(psReadBuf, 0, READ_BUFSIZE);
requestPending = FALSE;
free(pcPacket);
}
InvalidateRect(hWnd, NULL, FALSE);
}
}
ResetEvent(overlap.hEvent);
}
if (!PurgeComm(pwd->hPort, PURGE_RXCLEAR)) {
DISPLAY_ERROR("Error purging read buffer");
}
CloseHandle(overlap.hEvent);
return 0;
}
/*------------------------------------------------------------------------------
-- FUNCTION: RequestPacket
--
-- DATE: Nov 4, 2010
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Daniel Wright
--
-- PROGRAMMER: Daniel Wright
--
-- INTERFACE: BOOL RequestPacket(HWND hWnd)
-- hWnd - the handle to the window
--
-- RETURNS: True if the port write was successful.
--
-- NOTES:
-- Writes a string representing a packet request to the port.
------------------------------------------------------------------------------*/
BOOL RequestPacket(HWND hWnd) {
PWNDDATA pwd = {0};
CHAR psWriteBuf[10] = {0};
OVERLAPPED overlap = {0};
DWORD dwBytesRead = 0;
UINT bufLength = 9;
pwd = (PWNDDATA) GetWindowLongPtr(hWnd, 0);
psWriteBuf[0] = 0x01;
psWriteBuf[1] = 0x09;
psWriteBuf[2] = 0x00;
psWriteBuf[3] = 0x03;
psWriteBuf[4] = 0x01;
psWriteBuf[5] = 0x41;
psWriteBuf[6] = 0x00;
psWriteBuf[7] = 0x4B;
psWriteBuf[8] = 0xB4;
if (!WriteFile(pwd->hPort, psWriteBuf, bufLength, &dwBytesRead, &overlap)) {
if (GetLastError() != ERROR_IO_PENDING) {
return FALSE;
}
}
return TRUE;
}
/*------------------------------------------------------------------------------
-- FUNCTION: ProcessCommError
--
-- DATE: Oct 13, 2010
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Dean Morin
--
-- PROGRAMMER: Dean Morin
--
-- INTERFACE: VOID ProcessCommError(HANDLE hPort)
-- hPort - the handle to the open port
--
-- RETURNS: VOID.
--
-- NOTES:
-- Displays messages for various communication errors. Most of the
-- time, this function will process ERROR_IO_PENDING and return
-- early since this is expected in overlapped I/O.
------------------------------------------------------------------------------*/
VOID ProcessCommError(HANDLE hPort) {
DWORD dwError;
if (GetLastError() == ERROR_IO_PENDING) {
return;
}
ClearCommError(hPort, &dwError, NULL);
switch (dwError) {
case CE_BREAK:
DISPLAY_ERROR("The hardware detected a break condition");
case CE_FRAME:
DISPLAY_ERROR("The hardware detected a framing error");
case CE_OVERRUN:
DISPLAY_ERROR("A character-buffer overrun has occurred. The next character is lost.");
case CE_RXOVER:
DISPLAY_ERROR("An input buffer overflow has occurred. There is either no room in the input buffer, or a character was received after the end-of-file (EOF) character");
case CE_RXPARITY:
DISPLAY_ERROR("The hardware detected a parity error");
default:
DISPLAY_ERROR("A communication error occured");
}
}