Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Serial Communication Using MFC in VC++6 under XP

Posted on 2005-05-02
15
Medium Priority
?
537 Views
Last Modified: 2013-11-25
Hi,
Can any expert tell me "exactly" how MFC matches 50 Baud on serial comport

I had a look at standard API's and it supports the foll [110...19200...Baud]

Can I replace 110 by 50 .If not, how can I write a class to do such thing.

and reflect the changes in the property in the system areas

"Only 50 Baud" !!!

Thanks in adv
0
Comment
Question by:pcm211
  • 6
  • 5
  • 3
  • +1
15 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 13911792
In short: If you need the baud rate locked to 50 bps, specify the decimal value '50' as 'BaudRate' in the DCB for the COM port you're using, e.g.


#include <windows.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
   DCB dcb;
   HANDLE hCom;
   BOOL fSuccess;
   char *pcCommPort = "COM2";

   hCom = CreateFile( pcCommPort,
                    GENERIC_READ | GENERIC_WRITE,
                    0,    // must be opened with exclusive-access
                    NULL, // no security attributes
                    OPEN_EXISTING, // must use OPEN_EXISTING
                    0,    // not overlapped I/O
                    NULL  // hTemplate must be NULL for comm devices
                    );

   if (hCom == INVALID_HANDLE_VALUE)
   {
       // Handle the error.
       printf ("CreateFile failed with error %d.\n", GetLastError());
       return (1);
   }

   // Build on the current configuration, and skip setting the size
   // of the input and output buffers with SetupComm.

   fSuccess = GetCommState(hCom, &dcb);

   if (!fSuccess)
   {
      // Handle the error.
      printf ("GetCommState failed with error %d.\n", GetLastError());
      return (2);
   }

   // Fill in DCB: 50 bps, 8 data bits, no parity, and 1 stop bit.

   dcb.BaudRate = 50;     // set the baud rate
   dcb.ByteSize = 8;             // data size, xmit, and rcv
   dcb.Parity = NOPARITY;        // no parity bit
   dcb.StopBits = ONESTOPBIT;    // one stop bit

   fSuccess = SetCommState(hCom, &dcb);

   if (!fSuccess)
   {
      // Handle the error.
      printf ("SetCommState failed with error %d.\n", GetLastError());
      return (3);
   }

   printf ("Serial port %s successfully reconfigured.\n", pcCommPort);
   return (0);
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 13911811
FYI - the above code works here at least

C:\tmp\baudtest>cl baud.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

baud.cpp
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/out:baud.exe
baud.obj

C:\tmp\baudtest>baud
Serial port COM2 successfully reconfigured.



The constants like 'CBR_110' etc. are just predefined constants for the coder's convenience - the docs explicitly state

BaudRate
Specifies the baud rate at which the communications device operates. This member can be an actual baud rate value, or one of the following baud rate indexes [...]

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13912026
I remember your question:
http://www.experts-exchange.com/Programming/Programming_Languages/MFC/Q_20998512.html

I still think it is not possible with current Windows versions, maybe with DOS. There is another alternative:
you can connect your RX line to some different pin like CTS or DSR, then you will have to implement your own serial protocol with proper timing (everybody sais it is not possible in Windows, but since 50 bauds is a very low speed, I think you can ensure proper timing by giving high priority to reading process)

It is not a trivial work, but it is usual in microcontroller's world,  but you don't have much alternatives:

Some related question:
http://www.experts-exchange.com/Programming/Programming_Languages/MFC/Q_10004120.html
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 13916480
>>>> I remember your question:

Jaime, it seems to me that 'e-learning' and 'pcm211' are different members.

Do you know better?

Regards, Alex
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13919552
Oh, sorry, get confused while reviewing my bookmarks (I remembered a similar question), but still are uselful links...
0
 

Author Comment

by:pcm211
ID: 13920483
well.... thank u very much for ur interest ...in this ..... but ... the problem still lies ... in achieving 50 bauds ....... the API does not allow it .... .

how could i use the code which u have given "jkr" in my program ....  could u plz help me out .....  

i also know that it is possible .....  and since i am not getting a break thru ... i am moving on to the instruction set of x86 ... and writing inline assembler code ... to increase the bit time ... and achieve the 50 baud mark .....

wat do u suggest

cheers ..!!
0
 
LVL 86

Accepted Solution

by:
jkr earned 750 total points
ID: 13920520
>>how could i use the code which u have given "jkr" in my program

Well, just copy the part that sets teh DCB and paste that into your own code:

   fSuccess = GetCommState(hCom, &dcb);

  if (!fSuccess)
   {
     // Handle the error.
     printf ("GetCommState failed with error %d.\n", GetLastError());
     return (2);
  }

  // Fill in DCB: 50 bps, 8 data bits, no parity, and 1 stop bit.

  dcb.BaudRate = 50;     // set the baud rate
  dcb.ByteSize = 8;             // data size, xmit, and rcv
  dcb.Parity = NOPARITY;        // no parity bit
  dcb.StopBits = ONESTOPBIT;    // one stop bit

  fSuccess = SetCommState(hCom, &dcb);

  if (!fSuccess)
   {
     // Handle the error.
     printf ("SetCommState failed with error %d.\n", GetLastError());
     return (3);
  }
0
 

Author Comment

by:pcm211
ID: 13920650


>>for  " jkr "

but .... does it work .... with the MFC..... it ....gives 100's of errors .....sir .....

and the dcb ...part is very confusing to ....me ...... atti_boy@yahoo.com
0
 

Author Comment

by:pcm211
ID: 13920680
it is clearly said in the MFC that ... it cannot go below 110 baud ....
and .... changes are not reflected in the machine also ........

tx ....
cheers

pl add me ....
0
 
LVL 86

Expert Comment

by:jkr
ID: 13920681
That should just work fine - use e.g.

HANDLE OpenAndConfigurePort ( LPCTSTR pszPort)
{
  DCB dcb;
  HANDLE hCom;
  BOOL fSuccess;

  hCom = CreateFile( pszPort,
                   GENERIC_READ | GENERIC_WRITE,
                   0,    // must be opened with exclusive-access
                   NULL, // no security attributes
                   OPEN_EXISTING, // must use OPEN_EXISTING
                   0,    // not overlapped I/O
                   NULL  // hTemplate must be NULL for comm devices
                   );

  if (hCom == INVALID_HANDLE_VALUE)
   {
      // Handle the error.
      printf ("CreateFile failed with error %d.\n", GetLastError());
      return (1);
  }

  // Build on the current configuration, and skip setting the size
  // of the input and output buffers with SetupComm.

  fSuccess = GetCommState(hCom, &dcb);

  if (!fSuccess)
   {
     // Handle the error.
     printf ("GetCommState failed with error %d.\n", GetLastError());
     return (2);
  }

  // Fill in DCB: 50 bps, 8 data bits, no parity, and 1 stop bit.

  dcb.BaudRate = 50;     // set the baud rate
  dcb.ByteSize = 8;             // data size, xmit, and rcv
  dcb.Parity = NOPARITY;        // no parity bit
  dcb.StopBits = ONESTOPBIT;    // one stop bit

  fSuccess = SetCommState(hCom, &dcb);

  if (!fSuccess)
   {
     // Handle the error.
     printf ("SetCommState failed with error %d.\n", GetLastError());
     return (3);
  }

  printf ("Serial port %s successfully reconfigured.\n", pcCommPort);
  return (hCom);
}
0
 

Author Comment

by:pcm211
ID: 13920771
>> " jkr " .... plz could ..... u give me a working code ..... in totality ..... i am not able to understand where .... am i making a mistake in understanding ur code ... and applying it .....

thank you very very much ....in adv ....

cheers
atti_boy@yahoo.com
0
 
LVL 86

Expert Comment

by:jkr
ID: 13920903
The above almost is 'working code', just the 'printf()' statements should be removed:

HANDLE OpenAndConfigurePort ( LPCTSTR pszPort)
{
 DCB dcb;
 HANDLE hCom;
 BOOL fSuccess;

 hCom = CreateFile( pszPort,
                  GENERIC_READ | GENERIC_WRITE,
                  0,    // must be opened with exclusive-access
                  NULL, // no security attributes
                  OPEN_EXISTING, // must use OPEN_EXISTING
                  0,    // not overlapped I/O
                  NULL  // hTemplate must be NULL for comm devices
                  );

 if (hCom == INVALID_HANDLE_VALUE)
  {
     // Handle the error.
     printf ("CreateFile failed with error %d.\n", GetLastError());
     return (1);
 }

 // Build on the current configuration, and skip setting the size
 // of the input and output buffers with SetupComm.

 fSuccess = GetCommState(hCom, &dcb);

 if (!fSuccess)
  {
    // Handle the error.

    return NULL;
 }

 // Fill in DCB: 50 bps, 8 data bits, no parity, and 1 stop bit.

 dcb.BaudRate = 50;     // set the baud rate
 dcb.ByteSize = 8;             // data size, xmit, and rcv
 dcb.Parity = NOPARITY;        // no parity bit
 dcb.StopBits = ONESTOPBIT;    // one stop bit

 fSuccess = SetCommState(hCom, &dcb);

 if (!fSuccess)
  {
    // Handle the error.

    return NULL;
 }

 return (hCom);
}
0
 

Author Comment

by:pcm211
ID: 13920968
>> "jkr" ...thank u very much .. i will .......work on it .......... and make it work rite now ...........
u were ..very kind and helpful.............
i shud have ratd u " A" ...........  

plz mail . me the full code if u have ...........  it with u at : atti_boy@yahoo.com

cheers ..!!
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13921418
So, does it works? really want to know.
I see you have put another similar question....
0
 

Author Comment

by:pcm211
ID: 13928972
well... it does work ...nad it works fine ... u can see the changes if forom the command propmpt u type "mode" for the status of com ports available.....  " jamie " u bet this person has some brains ....

my similar question is a step ahead of it where i want to make this code available for other applications .... makin the code a perfect device driver instead of an applicaton alone .....

cheers ...!!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question