Link to home
Start Free TrialLog in
Avatar of pcm211
pcm211

asked on

Serial Communication Using MFC in VC++6 under XP

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
Avatar of jkr
jkr
Flag of Germany image

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);
}
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 [...]

I remember your question:
https://www.experts-exchange.com/questions/20998512/Can-not-receive-right-charactors-sent-in-50-Baud-5-databits-no-parity-1-5-stopbits-Overlapped.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:
https://www.experts-exchange.com/questions/10004120/Using-COM-port-as-a-trigger-input.html
>>>> I remember your question:

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

Do you know better?

Regards, Alex
Oh, sorry, get confused while reviewing my bookmarks (I remembered a similar question), but still are uselful links...
Avatar of pcm211
pcm211

ASKER

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 ..!!
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pcm211

ASKER



>>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
Avatar of pcm211

ASKER

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 ....
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);
}
Avatar of pcm211

ASKER

>> " 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
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);
}
Avatar of pcm211

ASKER

>> "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 ..!!
So, does it works? really want to know.
I see you have put another similar question....
Avatar of pcm211

ASKER

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 ...!!