Success!

rs232 communications between VC++ and a PIC microcontroller have been
established!

What a relief!

I *must* be very thankfull to the news groups for this ! You guys were
great, and I would of never of made it without your help!

Although all was constructive, I ran into one last but simple question!

As I have been learning VC++ and some of all of its vast world of
programming, Windows data types have creeped back, and as I looked at posts
on this from 6 months ago, I feel I would still need some guidence regarding
this particular issue.

So here is my question:

As VC++ is the software that I am using on the PC end, as you may know, one
of the methods of the serial class is :

MySerial.Write("x");

Therefore as you know, the first parameter of the "Write" function takes a
LPCSTR.

You see.... at the microcontroller end, the C code reads this byte by the
following line: c=getc(); in the code fragment below:

=====================
void get_string(char* s)

{
int len;
char c;

len = 0;

do {
c=getc(); //getc is a ready made function by the compiler which waits
// for a character to be received. So until VC++ doesn't
// send something, the controller stays at this line!

if ((c>=' ')&&(c<='~')) //if 'c' is within range, then append to array
s[len++]=c;

} while(c!=13); //Wait for enter key!

...other code...
======================

In the above code, I have only included the pertaining lines to my question,
but its pretty much representative of what the function consists of.

So, in the above code, it waits for a character and once recieved, it
appends it to "s". So, if VC++ sends a "B", then a "B" will be appended in
the string array "s".
This continues until VC++ sends a ascii code for "enter key", which is ascii
code 13.

My question is, given the code shown above for the *PC end*, how would VC++
, by using code, send an "enter" or a "13" in this case?

I tried,

MySerial.Write("13"); //The controller only reads: "1" instead of 13

The next line doesn't work either:

MySerial.Write(13);

And before I try all kinds of type castings, I would prefer to get some
understanding of what needs to be done. I mean, if MySetrial.Write() takes a
LPCSTR and the C language at the controller end needs to see a 13 to
understand that VC++ sent the 'enter key' code, then I need to convert 13 to
"13"... right?

What's happens now, is that the controller doesn't recognize 13, no matter
which of the following commands I do:

MySerial.Write(13);
or
MySerial.Write("13");

or

int x;
x=13;
MySerial.Write((WCHAR)x); //Which makes no sence, since it expects a LPCSTR

If I may be excused on this one, its been a while since I have been set
straight on Windows data types, even though given the fact that the news
groups have done more than great job at explaining me this stuff months ago!

All help much appreciated!

And again, thankyou all fellow newsgroups!

--
Kind regards
Roberto

Re: rs232-[B] by William

William
Fri Jun 23 13:02:18 CDT 2006


"Robby" <Robby@discussions.microsoft.com> wrote in message
news:6B4DB6A5-AD45-4F43-B508-64B41A92C645@microsoft.com...
> Success!

Congratulations.

> My question is, given the code shown above for the *PC end*, how would
> VC++
> , by using code, send an "enter" or a "13" in this case?

A pretty thorough explanation is here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang98/html/_pluslang_c.2b2b_.string_literals.asp

I might write something like this

char szControl[] = {13, 0};

MySerial.Write(szControl);

Regards,
Will




Re: rs232-[B] by Robby

Robby
Fri Jun 23 14:17:01 CDT 2006

Thankyou William,

I will try this as soon as I can!

Quick question:

While I was trying stuff to solve the question in this post, I realized that
the WM_KEYDOWN handler did not pick up my keystrokes on my second window.

I have a WM_KEYDOWN handler in my main window, and when I press say F1 key,
I can trap it with a break point, but when I am in my second window, which is
a 2nd WindProc, I cannot trap the WM_KEYDOWN messages.

here is the basic code:

===================================WndProc.CPP
LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
...other code...

switch(message)
{
case WM_KEYDOWN:

switch(wParam) //**********Break point recognized at debug time
{
case VK_F2:
ShowWindow(hdCW1,SW_HIDE);
break;
}
return 0;
case WM_COMMAND:

if (LOWORD(wParam) == WindowButtons[1].MW_btC_ID) //If Button #1
{
hdCW1 = CreateWindow( //Create Child window #1
szCW1_ClassName,szCW1_Name,
WS_CHILD | WS_CAPTION | WS_SYSMENU,
150,50,400,300,hwnd,(HMENU)CW1_ID,
(HINSTANCE) GetWindowLong(hwnd,GWL_HINSTANCE),NULL);
ShowWindow(hdCW1,SW_SHOW); //Show child window #1
}
return 0;

...other code....
===============================================

And here is the code pertaining to the child window, pressing F1 key does
not get trapped by break point in this window?
===================================WndProc_CW1.CPP
LRESULT CALLBACK WndProc_CW1 (HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{

...other code...

case WM_KEYDOWN:
switch (wParam) //Break point here! Break point NOT recognized at
debug time
{
case 0: //Dummy case
break;
}
return 0;

...other code....
=================================================

Its like as if when I am in my child window and I press say F1 key, the
WM_KEYDOWN message is not sent, and even if I have a break point on the
following line:

switch (wParam)

debug doesn't stop on that line, it goes right through as if I had no break
point ????

If you have any ideas, please get back!

All help appreciated!

Thanks

--
Best regards
Robert


"William DePalo [MVP VC++]" wrote:

>
> "Robby" <Robby@discussions.microsoft.com> wrote in message
> news:6B4DB6A5-AD45-4F43-B508-64B41A92C645@microsoft.com...
> > Success!
>
> Congratulations.
>
> > My question is, given the code shown above for the *PC end*, how would
> > VC++
> > , by using code, send an "enter" or a "13" in this case?
>
> A pretty thorough explanation is here:
>
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang98/html/_pluslang_c.2b2b_.string_literals.asp
>
> I might write something like this
>
> char szControl[] = {13, 0};
>
> MySerial.Write(szControl);
>
> Regards,
> Will
>
>
>
>

Re: rs232-[B] by Heinz

Heinz
Fri Jun 23 17:04:58 CDT 2006

"Robby" <Robby@discussions.microsoft.com> schrieb im Newsbeitrag
news:6B4DB6A5-AD45-4F43-B508-64B41A92C645@microsoft.com...
...
> MySerial.Write("x");
>
> Therefore as you know, the first parameter of the "Write" function takes a
> LPCSTR.
...
> My question is, given the code shown above for the *PC end*, how would
> VC++
> , by using code, send an "enter" or a "13" in this case?
>
> I tried,
>
> MySerial.Write("13"); //The controller only reads: "1" instead of 13

Since Write expects a string, you should pass a string containing the
carriage return character.

MySerial.Write("\r");
MySerial.Write("\015");
MySerial.Write("\x0D");

should all send a single CR character. You can also send longer strings with
embedded CRs, like

MySerial.Write("Hello World\r");

HTH
Heinz


Re: rs232-[B] by Robby

Robby
Fri Jun 23 19:41:01 CDT 2006

Hello Heinz,

Yes that's what I saw when I ran the program in debug and put my cursor over
the 'c' variable and it showed >>> "\r".

I couldn't figure out why, and as you pointed it out, I made the *link*as
for its the same as the printf backslash character constant "\r" for
carriage return!!!!

Thankyou Heinz
Very appreciated!

--
Best regards
Robert


"Heinz Ozwirk" wrote:

> "Robby" <Robby@discussions.microsoft.com> schrieb im Newsbeitrag
> news:6B4DB6A5-AD45-4F43-B508-64B41A92C645@microsoft.com...
> ....
> > MySerial.Write("x");
> >
> > Therefore as you know, the first parameter of the "Write" function takes a
> > LPCSTR.
> ....
> > My question is, given the code shown above for the *PC end*, how would
> > VC++
> > , by using code, send an "enter" or a "13" in this case?
> >
> > I tried,
> >
> > MySerial.Write("13"); //The controller only reads: "1" instead of 13
>
> Since Write expects a string, you should pass a string containing the
> carriage return character.
>
> MySerial.Write("\r");
> MySerial.Write("\015");
> MySerial.Write("\x0D");
>
> should all send a single CR character. You can also send longer strings with
> embedded CRs, like
>
> MySerial.Write("Hello World\r");
>
> HTH
> Heinz
>
>