Hello,

It *was* a success... however not for long! :-)

Please consider the following code:
============================================

#include "Serial.h"
#include "SerialEx.h"
#include "SerialWnd.h"

CSerialWnd MySerial;

LRESULT CALLBACK WndProc_CW1 (HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
static DWORD dwBytesRead = 0;
static BYTE abBuffer[40]; //Bytes
static TCHAR szBuffer[80]; //Wide characters

...other code...

MySerial.Open(TEXT("COM1"),hwnd,WM_NULL,lParam,0,0);

if (message == CSerialWnd::mg_nDefaultComMsg)
{
// A serial message occurred
const CSerialWnd::EEvent eEvent = CSerialWnd::EEvent(LOWORD(wParam));
const CSerialWnd::EError eError = CSerialWnd::EError(HIWORD(wParam));

switch (eEvent)
{
case CSerialWnd::EEventRecv:
MySerial.Read(abBuffer,sizeof(abBuffer),&dwBytesRead); //Break point
break;
default:
break;
}

MultiByteToWideChar(CP_ACP,0,(LPCSTR)abBuffer,40,szBuffer,80);

MySerial.Close();

return 0;
}

switch(message)
{
case WM_SIZE:

...other code...

=================================================

The micro controller is sending the following message to VC++,

printf("Enter the first number: ");

And all I am able to read is the following as I put my cursor over abBuffer
(in which has a breakpoint on it) is:

"Enter thUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU"

Except that there is two dots on top of al the U.

A little discouraged!

I mean why would it read the first 8 characters and then all Us?

And furthermore, sometimes I get the right result, being:

"Enter the first number: UUUUUUUUUUUUUUUU"

Its like rs232 is inconsistant?

Can someone help!

Thanking you in advance!

--
Best regards
Robert

RE: rs232- [C] by Robby

Robby
Sat Jun 24 20:57:06 CDT 2006

Hi,

This is an update of the comm problem on rs232!

Did anyone experience severe inconsistencies in rs232 communications! Because
I am!

Its been 32 hours of consistencies to be more precise!

I have narrowed it down to the fact that it can't be the communications
hardware settings, because when I connect the Micro controller end to the
manufacturer's Serial monitor program which is on the PC end, the
communications are fine!!! And the Serial monitor program is set to 9600, No
parity, 8 data bits, 1 Stop bit, and I tried also to set it up in VC++ by
doing:

MySerial.Setup(CSerial::EBaud9600,CSerial::EData8,
CSerial::EParNone,CSerial::EStop1);

And I get the same inconsistencies.... Always with the read though! I have
no problems with the write!

So basically I have problems only with the read.... is it because of the way
I set up the read code...(See innitial question in this post).
I usually like to understand every bit of code I do, however I feel
vulnerable because I don't quite know everything about the serial classes I
added to my project, there is some coding in there that I have not even
learnt yet! so I am sort of taking it for granted that these serial classes
work and that I am using them right!

...is it because I should create the Serial object in a class somewhere?
...is it because my cable is approximately 25 feet long between my PC and
the micro controller chip?

All I know is that I am using two wire communications (Recv and transm).
ANd if we refer to a DB9 connector pin outs. On the PC end, the receive pin
#2 is connected to the DCD pin#1. I don't know exactly why it is wired this
way, I mean a DCD is used to indicate that the carrier of a remote modem has
been senced by the local modem....or simpler yet, it informs the PC that
somdething has been detected... so be ready!

Well! I am washed out on this one! It is approximately a week that I have
kept on asking questions and pushing so to try to get this rs232 up and
kicking, but I have to admit, I am out of ideas, and I hope to hear from you
guys!

At this point, I would appreciate some guidence on the structure of the code
that I did for the Serial object done in my first question of this post!

All sincere help appreciated!

--
Best regards
Roberto
:(

"Robby" wrote:

> Hello,
>
> It *was* a success... however not for long! :-)
>
> Please consider the following code:
> ============================================
>
> #include "Serial.h"
> #include "SerialEx.h"
> #include "SerialWnd.h"
>
> CSerialWnd MySerial;
>
> LRESULT CALLBACK WndProc_CW1 (HWND hwnd, UINT message,
> WPARAM wParam, LPARAM lParam)
> {
> static DWORD dwBytesRead = 0;
> static BYTE abBuffer[40]; //Bytes
> static TCHAR szBuffer[80]; //Wide characters
>
> ...other code...
>
> MySerial.Open(TEXT("COM1"),hwnd,WM_NULL,lParam,0,0);
>
> if (message == CSerialWnd::mg_nDefaultComMsg)
> {
> // A serial message occurred
> const CSerialWnd::EEvent eEvent = CSerialWnd::EEvent(LOWORD(wParam));
> const CSerialWnd::EError eError = CSerialWnd::EError(HIWORD(wParam));
>
> switch (eEvent)
> {
> case CSerialWnd::EEventRecv:
> MySerial.Read(abBuffer,sizeof(abBuffer),&dwBytesRead); //Break point
> break;
> default:
> break;
> }
>
> MultiByteToWideChar(CP_ACP,0,(LPCSTR)abBuffer,40,szBuffer,80);
>
> MySerial.Close();
>
> return 0;
> }
>
> switch(message)
> {
> case WM_SIZE:
>
> ...other code...
>
> =================================================
>
> The micro controller is sending the following message to VC++,
>
> printf("Enter the first number: ");
>
> And all I am able to read is the following as I put my cursor over abBuffer
> (in which has a breakpoint on it) is:
>
> "Enter thUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU"
>
> Except that there is two dots on top of al the U.
>
> A little discouraged!
>
> I mean why would it read the first 8 characters and then all Us?
>
> And furthermore, sometimes I get the right result, being:
>
> "Enter the first number: UUUUUUUUUUUUUUUU"
>
> Its like rs232 is inconsistant?
>
> Can someone help!
>
> Thanking you in advance!
>
> --
> Best regards
> Robert

Re: rs232- [C] by Scott

Scott
Sat Jun 24 21:52:09 CDT 2006

Robby wrote:
> The micro controller is sending the following message to VC++,
>
> printf("Enter the first number: ");
>
> And all I am able to read is the following as I put my cursor over
abBuffer
> (in which has a breakpoint on it) is:
>
> "Enter thUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU"
>
> Except that there is two dots on top of al the U.
>
> A little discouraged!
>
> I mean why would it read the first 8 characters and then all Us?
>
> And furthermore, sometimes I get the right result, being:
>
> "Enter the first number: UUUUUUUUUUUUUUUU"
>
> Its like rs232 is inconsistant?

The UUU characters are probably random junk in the buffer beyond the
characters that were received. You're displaying the junk. A character
string must be terminated by a nul character (a zero). RS-232 read does
not put a nul after the characters, and you don't put a nul after the
characters, so displaying the buffer is actually an invalid and
dangerous thing to do. You can fix that problem with

MySerial.Read(abBuffer,sizeof(abBuffer),&dwBytesRead);
abBuffer[dwBytesRead] = 0;

The serial port does not know what a "message" is. It just receives
some bytes and returns them to you. When does it decide to return them
to you, you should ask? It decides to return them to you if it fills
the buffer, or if a period of time goes by with no characters received.
That period of time is set by your call to SetCommTimeouts. Do you
have one?

In general, you never know how many bytes you're going to get because
you don't control the timing of the bytes. So what you think of as a
"message" may come in in two or more pieces from calling read.

--
Scott McPhillips [VC++ MVP]


Re: rs232- [C] by Robby

Robby
Sun Jun 25 11:42:01 CDT 2006

Hi Scott,

Thankyou for your informative feedback, it is much appreciated.

So I did incorporate the following line in my code:

MySerial.Read(abBuffer,sizeof(abBuffer),&dwBytesRead);
abBuffer[dwBytesRead] = 0;

However, I tried also to not include your line:
abBuffer[dwBytesRead] = 0;

and replaced the printf statement at the proccessor end and terminated it
with a null character as so:

printf("Enter a number:\0");

So when I read the bytes comming in from VC++, the read statement already
contained the termination character 0.

So this also worked, I now can read the whole string without ant junk.

However, what is going on, is that once I read the string the first time,
immediately after, it does another read, as you stated, I guess Windows stops
reading once it read the whole serial rs232 buffer!

So, basically I find myself with two successive reads and then VC++ hangs. I
don't know if it is because I put a break point on the read statement??????

Anyhow, scott, do you have any ideas on what can be causing this!

Please get back....if you can!

Hope to hear from you!

--
Best regards
Robert


"Scott McPhillips [MVP]" wrote:

> Robby wrote:
> > The micro controller is sending the following message to VC++,
> >
> > printf("Enter the first number: ");
> >
> > And all I am able to read is the following as I put my cursor over
> abBuffer
> > (in which has a breakpoint on it) is:
> >
> > "Enter thUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU"
> >
> > Except that there is two dots on top of al the U.
> >
> > A little discouraged!
> >
> > I mean why would it read the first 8 characters and then all Us?
> >
> > And furthermore, sometimes I get the right result, being:
> >
> > "Enter the first number: UUUUUUUUUUUUUUUU"
> >
> > Its like rs232 is inconsistant?
>
> The UUU characters are probably random junk in the buffer beyond the
> characters that were received. You're displaying the junk. A character
> string must be terminated by a nul character (a zero). RS-232 read does
> not put a nul after the characters, and you don't put a nul after the
> characters, so displaying the buffer is actually an invalid and
> dangerous thing to do. You can fix that problem with
>
> MySerial.Read(abBuffer,sizeof(abBuffer),&dwBytesRead);
> abBuffer[dwBytesRead] = 0;
>
> The serial port does not know what a "message" is. It just receives
> some bytes and returns them to you. When does it decide to return them
> to you, you should ask? It decides to return them to you if it fills
> the buffer, or if a period of time goes by with no characters received.
> That period of time is set by your call to SetCommTimeouts. Do you
> have one?
>
> In general, you never know how many bytes you're going to get because
> you don't control the timing of the bytes. So what you think of as a
> "message" may come in in two or more pieces from calling read.
>
> --
> Scott McPhillips [VC++ MVP]
>
>

Re: rs232- [C] by Scott

Scott
Sun Jun 25 12:48:47 CDT 2006

Robby wrote:
> However, what is going on, is that once I read the string the first time,
> immediately after, it does another read, as you stated, I guess Windows stops
> reading once it read the whole serial rs232 buffer!
>
> So, basically I find myself with two successive reads and then VC++ hangs. I
> don't know if it is because I put a break point on the read statement??????
>
> Anyhow, scott, do you have any ideas on what can be causing this!
>
> Please get back....if you can!
>
> Hope to hear from you!

I told you in the previous answer about how the serial read decides when
to return. If there is no data coming in it returns after the timeout
you specify in SetCommTimeouts. If you don't specify this then it will
never return. It sounds like that's where you are at.

Since serial data comes in at its own pace, and you can never be sure
you will get all the data you expect in one read call, and the serial
read likes to block for the timeout duration, it is usually necessary to
do serial reading in a separate thread from your GUI. For now you can
probably get by using one read call with a generous timeout, but
consider moving to a serial thread later to make things more responsive
and robust.

--
Scott McPhillips [VC++ MVP]


Re: rs232- [C] by Robby

Robby
Mon Jun 26 10:00:01 CDT 2006

Hi Scott,

Yes, yes.... I was meaning to tell you about the SetCommTimeouts, and as I
was typing my question, I was thinking of other stuff and unfortunately it
slipped my mind! And apperantly, this was very important.... silly me!

In your post, you exclusively asked me if I have a SetCommTimeouts. The
answer is yes and since I don't have much documentaion on the library, I will
have to assume its one of the following:

MySerial.SetupReadTimeouts(CSerial::EReadTimeout eReadTimeOut);

or

MySerial.EReadTimeoutBlocking();

However in the latter, I get no intellisence for the parameters, so I don't
know if it is a preset function or not pertaining at all to what I need. So,
I experimented with the first one. This is what I tried, however it gives me
error:

MySerial.SetupReadTimeouts(CSerial::EReadTimeout = 100);

here is the error:

c:\_DTS_PROGRAMMING\C_PROGRAMMING\vc++\MY_APPS_LAB\XPPLC\WndProc_CW1.cpp(82):
error C2275: 'CSerial::EReadTimeout' : illegal use of this type as an
expression.

I also tried:

MySerial.SetupReadTimeouts(CSerial::EReadTimeout eReadTimeOut = 100);

and nada!

As I read the definition of what the method does pretty well describes what
you have been telling me. I beleive the 100 is bytes!

Hope you can get back!

--
Best regards
Robert


"Scott McPhillips [MVP]" wrote:

> Robby wrote:
> > However, what is going on, is that once I read the string the first time,
> > immediately after, it does another read, as you stated, I guess Windows stops
> > reading once it read the whole serial rs232 buffer!
> >
> > So, basically I find myself with two successive reads and then VC++ hangs. I
> > don't know if it is because I put a break point on the read statement??????
> >
> > Anyhow, scott, do you have any ideas on what can be causing this!
> >
> > Please get back....if you can!
> >
> > Hope to hear from you!
>
> I told you in the previous answer about how the serial read decides when
> to return. If there is no data coming in it returns after the timeout
> you specify in SetCommTimeouts. If you don't specify this then it will
> never return. It sounds like that's where you are at.
>
> Since serial data comes in at its own pace, and you can never be sure
> you will get all the data you expect in one read call, and the serial
> read likes to block for the timeout duration, it is usually necessary to
> do serial reading in a separate thread from your GUI. For now you can
> probably get by using one read call with a generous timeout, but
> consider moving to a serial thread later to make things more responsive
> and robust.
>
> --
> Scott McPhillips [VC++ MVP]
>
>

Re: rs232- [C] by John

John
Mon Jun 26 19:22:27 CDT 2006

"Robby" <Robby@discussions.microsoft.com> wrote in message
news:26973EB6-0E4E-4684-B619-43F1D134C288@microsoft.com
>
> The answer is yes and since I don't have much documentaion on the
> library, I will have to assume its one of the following:
>
> MySerial.SetupReadTimeouts(CSerial::EReadTimeout eReadTimeOut);
>
> or
>
> MySerial.EReadTimeoutBlocking();
>
> However in the latter, I get no intellisence for the parameters, so I
> don't know if it is a preset function or not pertaining at all to
> what I need. So, I experimented with the first one. This is what I
> tried, however it gives me error:
>
> MySerial.SetupReadTimeouts(CSerial::EReadTimeout = 100);
>
> here is the error:
>
> c:\_DTS_PROGRAMMING\C_PROGRAMMING\vc++\MY_APPS_LAB\XPPLC\WndProc_CW1.cpp(82):
> error C2275: 'CSerial::EReadTimeout' : illegal use of this type as an
> expression.
>
> I also tried:
>
> MySerial.SetupReadTimeouts(CSerial::EReadTimeout eReadTimeOut = 100);
>
> and nada!


I don't know anything about the library you are using, but you never *call*
functions in that way. The syntax you are using for the function argument is
what you use when *declaring* functions, not when calling them. Try

MySerial.SetupReadTimeouts(100);


--
John Carson



Re: rs232- [C] by Robby

Robby
Mon Jun 26 23:07:02 CDT 2006

Hello John,

Thanks for getting back to me!

Yes you are right, I didn't khow how else to call the function, because I
tried many ways including:

MySerial.SetupReadTimeouts(100);

and at build time it gave me the following error:

c:\_DTS_PROGRAMMING\C_PROGRAMMING\vc++\MY_APPS_LAB\XPPLC\WndProc_CW1.cpp(75):
error C2664: 'CSerial::SetupReadTimeouts' : cannot convert parameter 1 from
'int' to 'CSerial::EReadTimeout'

So I then tried:

MySerial.SetupReadTimeouts((CSerial::EReadTimeout)100);


And surprisingly enough it compile without errors, but at runtime it gave an
error ... and oh yeah its my favorite error, check this out!

"Unhandled exception at 0x7c901230 in MT_3.exe: User breakpoint."

And program stopped in the dbgrpt.c file!!! at some weird code?!?!?!?!

You know, John, sometimes the things that we believe will be simple turn out
to be the most complicated and most time consuming. I have this question
running in 2 different posts in this newsgroup and also in the code project
newsgroups and I politely sent an e-mail to the author of the library at
M.Ramon.de.Klein@ict.nl. requesting some advice.

Right now, unfortunately for reasons out of my control I cannot afford to
charge into the source code of this library and disect it the I would
passionately would want to! Unfortunaltely, I must concentrate on rs232 and
get it up and running. Its just one of those situations where the people
around me have no patients and just want everything done yesterday, you know
the type, which we all dread.

M. Scott really helped me out alot, and I thank him very much if I may add
he was very nice. Aslo Hienz too was good help.... You were too.

Its just one of those things that I must face to *try* to figure out....
Since I don't have much experience, I don't think I will be able to do it
alone. Oh well, so that's that! :-)

If you have any other I deas, I would surely like to hear them, as you can
see rs232 has backed me up against a wall!

To all! Any suggestions are welcome and would be taken as a token of great
apprciation and a kind gesture in your part!

Have a nice day John and see ya around! :)

--
Best regards
Roberto


"John Carson" wrote:

> "Robby" <Robby@discussions.microsoft.com> wrote in message
> news:26973EB6-0E4E-4684-B619-43F1D134C288@microsoft.com
> >
> > The answer is yes and since I don't have much documentaion on the
> > library, I will have to assume its one of the following:
> >
> > MySerial.SetupReadTimeouts(CSerial::EReadTimeout eReadTimeOut);
> >
> > or
> >
> > MySerial.EReadTimeoutBlocking();
> >
> > However in the latter, I get no intellisence for the parameters, so I
> > don't know if it is a preset function or not pertaining at all to
> > what I need. So, I experimented with the first one. This is what I
> > tried, however it gives me error:
> >
> > MySerial.SetupReadTimeouts(CSerial::EReadTimeout = 100);
> >
> > here is the error:
> >
> > c:\_DTS_PROGRAMMING\C_PROGRAMMING\vc++\MY_APPS_LAB\XPPLC\WndProc_CW1.cpp(82):
> > error C2275: 'CSerial::EReadTimeout' : illegal use of this type as an
> > expression.
> >
> > I also tried:
> >
> > MySerial.SetupReadTimeouts(CSerial::EReadTimeout eReadTimeOut = 100);
> >
> > and nada!
>
>
> I don't know anything about the library you are using, but you never *call*
> functions in that way. The syntax you are using for the function argument is
> what you use when *declaring* functions, not when calling them. Try
>
> MySerial.SetupReadTimeouts(100);
>
>
> --
> John Carson
>
>
>

Re: rs232- [C] by John

John
Tue Jun 27 00:48:55 CDT 2006

"Robby" <Robby@discussions.microsoft.com> wrote in message
news:131D5B83-EF3A-43CB-AFF2-2967F3134659@microsoft.com
> Hello John,
>
> Thanks for getting back to me!
>
> Yes you are right, I didn't khow how else to call the function,
> because I tried many ways including:
>
> MySerial.SetupReadTimeouts(100);
>
> and at build time it gave me the following error:
>
> c:\_DTS_PROGRAMMING\C_PROGRAMMING\vc++\MY_APPS_LAB\XPPLC\WndProc_CW1.cpp(75):
> error C2664: 'CSerial::SetupReadTimeouts' : cannot convert parameter
> 1 from 'int' to 'CSerial::EReadTimeout'
>
> So I then tried:
>
> MySerial.SetupReadTimeouts((CSerial::EReadTimeout)100);


A quick look through the source code shows that SetupReadTimeouts does not
set a time; it sets a mode of operation. The argument it takes is an enum
value. You have a choice of three:

typedef enum
{
EReadTimeoutUnknown = -1, // Unknown
EReadTimeoutNonblocking = 0, // Always return immediately
EReadTimeoutBlocking = 1 // Block until everything is
retrieved
}
EReadTimeout;

> And surprisingly enough it compile without errors

Not that surprising. All sorts of stuff will compile if you use a cast.

> but at runtime it
> gave an error ... and oh yeah its my favorite error, check this out!
>
> "Unhandled exception at 0x7c901230 in MT_3.exe: User breakpoint."
>
> And program stopped in the dbgrpt.c file!!! at some weird code?!?!?!?!
>
> You know, John, sometimes the things that we believe will be simple
> turn out to be the most complicated and most time consuming. I have
> this question running in 2 different posts in this newsgroup and also
> in the code project newsgroups and I politely sent an e-mail to the
> author of the library at M.Ramon.de.Klein@ict.nl. requesting some
> advice.

I wouldn't have expected communication with a serial port to be simple. Some
things just do take a lot of time to get on top of.


--
John Carson



Re: rs232- [C] by Robby

Robby
Wed Jun 28 12:47:02 CDT 2006

Hi john!

So if this sets a mode of operation, then the question I should be asking,
is where do we set the time for the timout of the blocking mode?

If anyone knows this please get back!

--
Best regards
Robert


"John Carson" wrote:

> "Robby" <Robby@discussions.microsoft.com> wrote in message
> news:131D5B83-EF3A-43CB-AFF2-2967F3134659@microsoft.com
> > Hello John,
> >
> > Thanks for getting back to me!
> >
> > Yes you are right, I didn't khow how else to call the function,
> > because I tried many ways including:
> >
> > MySerial.SetupReadTimeouts(100);
> >
> > and at build time it gave me the following error:
> >
> > c:\_DTS_PROGRAMMING\C_PROGRAMMING\vc++\MY_APPS_LAB\XPPLC\WndProc_CW1.cpp(75):
> > error C2664: 'CSerial::SetupReadTimeouts' : cannot convert parameter
> > 1 from 'int' to 'CSerial::EReadTimeout'
> >
> > So I then tried:
> >
> > MySerial.SetupReadTimeouts((CSerial::EReadTimeout)100);
>
>
> A quick look through the source code shows that SetupReadTimeouts does not
> set a time; it sets a mode of operation. The argument it takes is an enum
> value. You have a choice of three:
>
> typedef enum
> {
> EReadTimeoutUnknown = -1, // Unknown
> EReadTimeoutNonblocking = 0, // Always return immediately
> EReadTimeoutBlocking = 1 // Block until everything is
> retrieved
> }
> EReadTimeout;
>
> > And surprisingly enough it compile without errors
>
> Not that surprising. All sorts of stuff will compile if you use a cast.
>
> > but at runtime it
> > gave an error ... and oh yeah its my favorite error, check this out!
> >
> > "Unhandled exception at 0x7c901230 in MT_3.exe: User breakpoint."
> >
> > And program stopped in the dbgrpt.c file!!! at some weird code?!?!?!?!
> >
> > You know, John, sometimes the things that we believe will be simple
> > turn out to be the most complicated and most time consuming. I have
> > this question running in 2 different posts in this newsgroup and also
> > in the code project newsgroups and I politely sent an e-mail to the
> > author of the library at M.Ramon.de.Klein@ict.nl. requesting some
> > advice.
>
> I wouldn't have expected communication with a serial port to be simple. Some
> things just do take a lot of time to get on top of.
>
>
> --
> John Carson
>
>
>