 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
George Guest
|
Posted: Sun Jan 23, 2005 7:32 pm Post subject: WaitCommEvent |
|
|
I'm am trying to do Overlapped I/O to the Serial Port. I set the Event
Mask to respond to a charater (EV_RXCHAR) moved to the input buffer. I've
tried implemting using the mutilple examples provided by Borland and
Microsoft. When there is no receive character , all objects on the form
seem just fines. Once a character is sent, the form seems to freeze, and
can only terminate the app through the debug reset.
Is there something obvious that I am not doing setting up or handling the
event, or direct me to more information.
Thanks
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Sun Jan 23, 2005 11:53 pm Post subject: Re: WaitCommEvent |
|
|
"George" <George (AT) kallin (DOT) com> wrote
| Quote: | I'm am trying to do Overlapped I/O to the Serial Port.
|
Have you read the the following article yet? It contains a detailed
explanation of how to do what you are asking for:
Serial Communications in Win32
http://msdn.microsoft.com/library/en-us/dnfiles/html/msdn_serial.asp
| Quote: | I set the Event Mask to respond to a charater (EV_RXCHAR)
moved to the input buffer. I've tried implemting using the mutilple
examples provided by Borland and Microsoft. When there is no
receive character, all objects on the form seem just fines. Once a
character is sent, the form seems to freeze, and can only terminate
the app through the debug reset.
|
Then you are not managing the reading properly.
| Quote: | Is there something obvious that I am not doing setting up or handling
the event
|
You did not show your actual code yet, so that is hard to answer.
Gambit
|
|
| Back to top |
|
 |
George Guest
|
Posted: Mon Jan 24, 2005 12:33 am Post subject: Re: WaitCommEvent |
|
|
Thanks, more info the better.
I've attached the code...very simple.
void __fastcall TForm1::FormCreate(TObject *Sender)
{
HANDLE hCom;
OVERLAPPED o;
BOOL fSuccess;
DWORD dwEvtMask;
DCB dcb;
hCom = CreateFile("COM1",
GENERIC_READ | GENERIC_WRITE,
0, // Exclusive access
0, //default secuity attributes
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0
);
if (hCom == INVALID_HANDLE_VALUE)
{
//Handle the error
}
// Fill in the DCB: baud=9600, 8 data bits, no parity, 1 stop bit.
dcb.BaudRate = 9600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
}
// Set the evnt mask
fSuccess = SetCommMask(hCom, EV_RXCHAR);
if (!fSuccess)
{
// Handle the error
}
// Create an event object for use by WaitCommEvent.
o.hEvent = CreateEvent(
NULL, //default secutiry attribute
FALSE, // auto reset event
FALSE, // not signaled
NULL // no name
);
// Initialize the rest of the OVERLAPEPED truture to zero
o.Internal = 0;
o.InternalHigh = 0;
o.Offset = 0;
o.OffsetHigh = 0;
// o.hEvent = 0;
assert(o.hEvent);
char inBuffer[8] = "AB";
DWORD nBytesRead;
DWORD nBytesToRead = 0;
if (WaitCommEvent(hCom, &dwEvtMask, &o))
{
if (dwEvtMask & EV_RXCHAR)
{
nBytesToRead = 1;
fSuccess = ReadFile(hCom, &inBuffer, nBytesToRead,
&nBytesRead,&o) ;
Button1->Caption = "CHAR";
}
}
}
George
"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote
| Quote: |
"George" <George (AT) kallin (DOT) com> wrote in message
news:41f3fb42$1 (AT) newsgroups (DOT) borland.com...
I'm am trying to do Overlapped I/O to the Serial Port.
Have you read the the following article yet? It contains a detailed
explanation of how to do what you are asking for:
Serial Communications in Win32
http://msdn.microsoft.com/library/en-us/dnfiles/html/msdn_serial.asp
I set the Event Mask to respond to a charater (EV_RXCHAR)
moved to the input buffer. I've tried implemting using the mutilple
examples provided by Borland and Microsoft. When there is no
receive character, all objects on the form seem just fines. Once a
character is sent, the form seems to freeze, and can only terminate
the app through the debug reset.
Then you are not managing the reading properly.
Is there something obvious that I am not doing setting up or handling
the event
You did not show your actual code yet, so that is hard to answer.
Gambit
|
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Jan 24, 2005 9:37 am Post subject: Re: WaitCommEvent |
|
|
"George" <George (AT) kallin (DOT) com> wrote
| Quote: | void __fastcall TForm1::FormCreate(TObject *Sender)
|
Never never never use the OnCreate event in C++. It is a Delphi remnant
that is unstable in C++ as it can be triggered before the constructor, which
is illegal in C++. Use the actual constructor instead.
| Quote: | if (WaitCommEvent(hCom, &dwEvtMask, &o))
|
Why are you performing your serial port operations while the form is still
in the process of constructing itself? That is not a good place to do that.
Also, I see that you have not yet applied anything that the article
explained to you.
| Quote: | nBytesToRead = 1;
fSuccess = ReadFile(hCom, &inBuffer, nBytesToRead,
&nBytesRead,&o) ;
|
You are not checking the return value of ReadFile() or nBytesRead. You are
also not reading the serial port until it is completely empty. I am
assuming that you have not actually read the article yet at all. It has an
entire section halfway through the article on how to properly handle the
EV_RXCHAR event correctly. You are not using the event the way the article
says to use it.
Gambit
|
|
| Back to top |
|
 |
George Guest
|
Posted: Mon Jan 24, 2005 6:32 pm Post subject: Re: WaitCommEvent |
|
|
Remmy,
Using 'never' once is sufficient, thank you. I probably am in over my
head, but the attractiveness of Builder is that it so easy to use and see
immediate results. I've designed my micro to communicate over the Comm
port and understand the old ways (assembly) do not appy in the new Windows
enviornment.
I am controlling the receive characters, so I am not concerned with
reading multiple. When I can capture the event, then I can build the read
to handle any unsolicited characters. Now if you want to help, I would
appreciate your effort.
Yes, I am reading the article, and I have yet to see the errors of my
way.
Thank you,
George
"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote
| Quote: |
"George" <George (AT) kallin (DOT) com> wrote in message
news:41f441d3 (AT) newsgroups (DOT) borland.com...
void __fastcall TForm1::FormCreate(TObject *Sender)
Never never never use the OnCreate event in C++. It is a Delphi remnant
that is unstable in C++ as it can be triggered before the constructor,
which
is illegal in C++. Use the actual constructor instead.
if (WaitCommEvent(hCom, &dwEvtMask, &o))
Why are you performing your serial port operations while the form is still
in the process of constructing itself? That is not a good place to do
that.
Also, I see that you have not yet applied anything that the article
explained to you.
nBytesToRead = 1;
fSuccess = ReadFile(hCom, &inBuffer, nBytesToRead,
&nBytesRead,&o) ;
You are not checking the return value of ReadFile() or nBytesRead. You
are
also not reading the serial port until it is completely empty. I am
assuming that you have not actually read the article yet at all. It has
an
entire section halfway through the article on how to properly handle the
EV_RXCHAR event correctly. You are not using the event the way the
article
says to use it.
Gambit
|
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Jan 24, 2005 7:15 pm Post subject: Re: WaitCommEvent |
|
|
"George" <George (AT) kallin (DOT) com> wrote
| Quote: | Using 'never' once is sufficient, thank you.
|
I say it several times because I can't stress enough how dangerous the
OnCreate event can be in C++. Borland does not document its side effects in
the C++ environment, and too many users have had projects get corrupted
and/or crash altogether because of OnCreate. There are messages posted
frequently that contain OnCreate usage, and the advice is always the same -
don't use OnCreate at all.
| Quote: | if you want to help, I would appreciate your effort.
|
I already gave you everything you need. I use the same article myself that
I directed you to earlier, and it works fine for my own projects.
| Quote: | Yes, I am reading the article, and I have yet to see the errors
of my way.
|
Then you are not reading the article carefully enough. There is a whole
section called "Caveat" regarding the use of the EV_RXCHAR event
specifically. Also, you are setting up the serial port for overlapped I/O,
but then not actually making use of overlapped operations at all. Your code
is assuming non-overlapped I/O is being used, which is not the case. You
need to use the WaitForSingleObject() and GetOverlappedResult() functions to
see when an overlapped operation actually finishes its work and to see if
the operation actually succeeded or failed. You are not currently doing any
of that. The article clearly explains all of that, and even provides full
working code examples.
Gambit
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|