Article View: borland.public.cppbuilder.nativeapi
Article #6124Re: select() on serial port
From: Patrick Rotsaert
Date: Fri, 10 Jun 2005 13:35
Date: Fri, 10 Jun 2005 13:35
171 lines
6124 bytes
6124 bytes
I did some test, but not very succesful so far.
It seems that WSAWaitForMultipleEvents cannot handle non-WSA-events.
To start with I tried this method on the serial port only, hoping to
extend it to sockets later on.
Here's what I did:
int main(int argc, char* argv[])
{
WSADATA wsaData;
COMMTIMEOUTS CTimeout;
DCB dcb;
HANDLE hCom;
DWORD EventTotal = 0;
WSAEVENT EventArray[WSA_MAXIMUM_WAIT_EVENTS];
OVERLAPPED PortOverlapped;
char buff[1024];
DWORD BytesRead;
DWORD Index;
DWORD Flags = 0;
WSAStartup(MAKEWORD(2,2), &wsaData);
hCom = CreateFile("COM2",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
if (hCom == INVALID_HANDLE_VALUE) {
printf("CreateFile: %s\n", win_strerror(GetLastError()));
return -1;
}
// configure port
if (GetCommState(hCom, &dcb) == FALSE) {
CloseHandle(hCom);
printf("GetCommState: %s\n", win_strerror(GetLastError()));
return -1;
}
dcb.BaudRate = 19200;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fBinary = TRUE;
if (SetCommState(hCom, &dcb) == FALSE) {
CloseHandle(hCom);
printf("SetCommState: %s\n", win_strerror(GetLastError()));
return -1;
}
if (GetCommTimeouts(hCom, &CTimeout) == FALSE) {
CloseHandle(hCom);
printf("GetCommTimeouts: %s\n", win_strerror(GetLastError()));
return -1;
}
CTimeout.ReadIntervalTimeout = MAXDWORD;
CTimeout.ReadTotalTimeoutMultiplier = 0;
CTimeout.ReadTotalTimeoutConstant = 0;
if (SetCommTimeouts(hCom, &CTimeout) == FALSE) {
CloseHandle(hCom);
printf("SetCommTimeouts: %s\n", win_strerror(GetLastError()));
return -1;
}
if(SetCommMask(hCom, EV_RXCHAR) == FALSE) {
CloseHandle(hCom);
printf("SetCommMask: %s\n", win_strerror(GetLastError()));
return -1;
}
EventArray[EventTotal] =
(WSAEVENT)CreateEvent(NULL, // pointer to security attributes
TRUE, // flag for manual-reset event
FALSE,// flag for initial state
NULL);// pointer to event-object name
ZeroMemory(&PortOverlapped, sizeof(PortOverlapped));
PortOverlapped.hEvent = (HANDLE)EventArray[EventTotal];
EventTotal++;
printf("Port init ok\n");
BytesRead = 0;
if (ReadFile(hCom, buff, 1, &BytesRead, &PortOverlapped) ==
FALSE) {
printf("ReadFile: %s\n", win_strerror(GetLastError()));
} else {
printf("BytesRead: %d\n", BytesRead);
}
for (;;) {
printf("WSAWaitForMultipleEvents...\n");
Index = WSAWaitForMultipleEvents(EventTotal,
EventArray,
FALSE,
5000,
FALSE);
printf("Index = %d\n", Index);
if (Index == WSA_WAIT_FAILED) {
printf("WSA_WAIT_FAILED\n");
break;
}
WSAResetEvent(EventArray[Index - WSA_WAIT_EVENT_0]);
WSAGetOverlappedResult(hCom, &PortOverlapped, &BytesRead, FALSE, &Flags);
printf("- BytesRead: %d\n", BytesRead);
}
CloseHandle(hCom);
return 0;
}
...
...
The program output is:
Port init ok
BytesRead: 0
WSAWaitForMultipleEvents...
Index = 0
- BytesRead: 0
WSAWaitForMultipleEvents...
Index = 0
- BytesRead: 0
WSAWaitForMultipleEvents...
Index = 0
- BytesRead: 0
...
The timeout, given to WSAWaitForMultipleEvents does not seem to have any
effect, because there is no delay in the loop.
This event-overlapped-stuff is still very dark to me, so I probably did
a lot of things wrong here... But what?
Thanks,
Patrick
Eelke wrote:
> Patrick Rotsaert wrote:
>
>> Sounds promising!
>> Do you know of any good examples on overlapped IO, events and such.
>> Reading the microsoft help does not make it any easier ;-)
>>
> Most of my knowledge comes from the book Advanced Win32 Programming from
> Microsoft Press and I also have had a course on concurrent programming
> (thought we used NeXT for that course) so I can't give a link. I have
> never used overlapped IO myself but I can give you a quick idea of how
> to use Event objects.
>
> An event object is an object that can have two states signaled or
> non-signaled. A thread can use wait functions on an Event object to wait
> until the object goes into the signaled state. In case of the use of
> events with overlapped IO and sockets windows will put the event in the
> signalled state when certain conditions are met like the overlapped IO
> transfer is completed or data is available for reading on a socket.
>
> Eelke
>
Technical Details
Message-ID:
<42a99737$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<42a6eb15$3@newsgroups.borland.com> <42a6f1f5$1@newsgroups.borland.com> <42a6fc95$1@newsgroups.borland.com> <42a7e509$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,be0dfdb43d2d029e X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Fri, 10 Jun 2005 15:35:47 +0200 From: Patrick Rotsaert <patrick.rotsaert@tts-online.be> User-Agent: Mozilla Thunderbird 1.0.2 (Windows/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: select() on serial port References: <42a6eb15$3@newsgroups.borland.com> <42a6f1f5$1@newsgroups.borland.com> <42a6fc95$1@newsgroups.borland.com> <42a7e509$1@newsgroups.borland.com> In-Reply-To: <42a7e509$1@newsgroups.borland.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 194.7.45.88 Message-ID: <42a99737$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1118410551 194.7.45.88 (10 Jun 2005 06:35:51 -0700) Lines: 172 Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail