Ik probeer op de nette manier een seriele poort uit te lezen onder windows. Dit lukt even
(+- 30 sec) , daarna krijg ik een error 1814 (NOT ENOUGH QUOTA). Ook trekt mijn progje 100% cpu, terwijl WaitCommEvent dit niet zou moeten doen. Ook gooit dit m'n hele netwerk overhoop, putty geeft bv. ook een melding dat er niet genoeg bufferruimte beschikbaar is.
Ik heb al heel wat zitten klooien met dingen als WaitForSingleObject maar ik kom er niet uit.
Ik heb al heel wat zitten klooien met dingen als WaitForSingleObject maar ik kom er niet uit.
code:
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
| //this code opens the comport
BOOL openComport(HWND hwnd,char *name,int baud, HANDLE *port)
{
closeComport(hwnd, port);
// Open the serial port.
*port = CreateFile (name, // Pointer to the name of the port
GENERIC_READ | GENERIC_WRITE,
// Access (read-write) mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_EXISTING, // How to open the serial port
FILE_FLAG_OVERLAPPED, // Port attributes
NULL); // Handle to port with attribute
// to copy
//if (port == INVALID_HANDLE_VALUE)
// MessageBox (hwnd, TEXT("Error creating port handle"), TEXT("Error"), MB_OK);
DCB PortDCB;
// Initialize the DCBlength member.
PortDCB.DCBlength = sizeof (DCB);
// Get the default port setting information.
GetCommState (*port, &PortDCB);
// Change the DCB structure settings.
PortDCB.BaudRate = baud; // Current baud
PortDCB.fBinary = TRUE; // Binary mode; no EOF check
PortDCB.fParity = TRUE; // Enable parity checking
PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control
PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
// DTR flow control type
PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
PortDCB.fOutX = FALSE; // No XON/XOFF out flow control
PortDCB.fInX = FALSE; // No XON/XOFF in flow control
PortDCB.fErrorChar = FALSE; // Disable error replacement
PortDCB.fNull = FALSE; // Disable null stripping
PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
// RTS flow control
PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on
PortDCB.EvtChar = '\r'; // event when \r is recieved
// error
PortDCB.ByteSize = 8; // Number of bits/byte, 4-8
PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
// Configure the port according to the specifications of the DCB
// structure.
if (!SetCommState (*port, &PortDCB))
{
// Could not configure the serial port.
//dwError = GetLastError ();
MessageBox (hwnd, TEXT("Unable to configure the serial port"),
TEXT("Error"), MB_OK);
//close the port
closeComport(hwnd,port);
return false;
}
// Retrieve the timeout parameters for all read and write operations
// on the port.
COMMTIMEOUTS CommTimeouts;
GetCommTimeouts (*port, &CommTimeouts);
// Change the COMMTIMEOUTS structure settings.
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 1;
CommTimeouts.ReadTotalTimeoutConstant = 1;
CommTimeouts.WriteTotalTimeoutMultiplier = 1;
CommTimeouts.WriteTotalTimeoutConstant = 1;
// Set the timeout parameters for all read and write operations
// on the port.
if (!SetCommTimeouts (*port, &CommTimeouts))
{
// Could not set the timeout parameters.
MessageBox (hwnd, TEXT("Unable to set the timeout parameters, closing Comport"),
TEXT("Error"), MB_OK);
//close the port
closeComport(hwnd,port);
return false;
}
if (!SetCommMask(*port,EV_RXCHAR) )
{
DWORD error = GetLastError();
MessageBox (hwnd, TEXT("Unable to set the commask parameters, closing Comport"),
TEXT("Error"), MB_OK);
//close the port
//closeComport(mainhwnd,&port);
return false;
}
//all is ok
DWORD dwThreadId;
DWORD frits;
LPDWORD henk = &frits;
//PMYDATA pData;
// pData = (PMYDATA) HeapAlloc(GetProcessHeap(),
// HEAP_ZERO_MEMORY, sizeof(MYDATA));
HANDLE hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadProc, // thread function
henk, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier
return true;
}
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
/********************************************************************/
/**THREAD CODE*******************************************************/
//CSerialCommHelper* apThis = (CSerialCommHelper*) pvParam ;
DWORD dwEventMask=0;
OVERLAPPED ov;
memset(&ov,0,sizeof(ov));
ov.hEvent = CreateEvent( 0,true,0,0);
//HANDLE arHandles[2];
//arHandles[0] = apThis->m_hThreadTerm;
DWORD dwWait;
//SetEvent(apThis->m_hThreadStarted);
while ( testerPort != NULL )
{
//Sleep(1);
BOOL abRet = WaitCommEvent(testerPort,&dwEventMask, &ovNULL) ;
//WaitForSingleObject(ov.hEvent,INFINITE);
DWORD error = GetLastError();
if ( !abRet && error != 997 )
{
char buf[33];
char errormsg[1024];
itoa (error,buf,10);
strcpy(errormsg,"Transmission Error: ");
strcat(errormsg,buf);
MessageBox (mainHwnd, errormsg,
TEXT("Error"), MB_OK);
closeComport(mainHwnd,&testerPort);
break;
}
{
OVERLAPPED rov;
memset(&rov,0,sizeof(ov));
rov.hEvent = CreateEvent( 0,true,0,0);
ResetEvent( rov.hEvent );
DWORD dwBytesRecieved=0;
DWORD dwBytesPeeked=0;
//set time
//repeat if no byte is read
//read the file
char text[1024];
char byte[2];
memset(text,0,1024);
int len =0;
do
{
ReadFile (testerPort, // Port handle
&byte, // Pointer to data to read
1, // Number of bytes to read
&dwBytesRecieved, // Pointer to number of bytesread
/*&rov */NULL // Must be NULL for Windows CE
);
if ( !abRet && error != 997 )
{
char buf[33];
char errormsg[1024];
itoa (error,buf,10);
strcpy(errormsg,"Transmission Error: ");
strcat(errormsg,buf);
MessageBox (mainHwnd, errormsg,
TEXT("Error 2"), MB_OK);
closeComport(mainHwnd,&testerPort);
}
ResetEvent ( ov.hEvent );
len++;
strcat(text,byte);
}
while (dwBytesRecieved > 0 && !(len >2 && text[len-2] == '\n' && text[len-1 == '\r']));
if (dwBytesRecieved != 0)
{
parseMessage(mainHwnd,text);
//SendDlgItemMessage(mainHwnd, IDC_RECIEVE, LB_INSERTSTRING, 0, (LPARAM)text);
PostMessage(mainHwnd,SERIAL_EVENT,dwEventMask,error);
}
}
ResetEvent ( ov.hEvent );
}
/********************************************************************/
// Free the memory allocated by the caller for the thread
// data structure.
return 0;
} |
elleP