Bare Bones Windows Serial Program

Discussion in 'Programmer's Corner' started by Brownout, Jan 13, 2014.

  1. Brownout

    Thread Starter Well-Known Member

    Jan 10, 2012
    I want an absolute bare bones windows serial program to use as a base to build my application. I found this code (shamelessly copied from here: It looks like what I need to get started. I plan to compile it using VCC 2005 and run it on a XP machine. Does anyone see any potential issues?:

    NOTE: I;m working to get this code to display better

    Code ( (Unknown Language)):
    3. [SIZE=2]#include <windows.h>[/SIZE]
    4. [SIZE=2]#include <stdio.h>[/SIZE]
    5. [SIZE=2]int main(int argc, char *argv[])[/SIZE]
    6. [SIZE=2]{[/SIZE]
    7. [SIZE=2]DCB dcb;[/SIZE]
    8. [SIZE=2]HANDLE hCom;[/SIZE]
    9. [SIZE=2]BOOL fSuccess;[/SIZE]
    10. [SIZE=2]char *pcCommPort = "COM2";[/SIZE]
    12. [SIZE=2]hCom = CreateFile( pcCommPort,[/SIZE]
    14. [SIZE=2]  0, // must be opened with exclusive-access[/SIZE]
    15. [SIZE=2]  NULL, // no security attributes[/SIZE]
    16. [SIZE=2]  OPEN_EXISTING, // must use OPEN_EXISTING[/SIZE]
    17. [SIZE=2]  0, // not overlapped I/O[/SIZE]
    18. [SIZE=2]  NULL // hTemplate must be NULL for comm devices[/SIZE]
    19. [SIZE=2]);[/SIZE]
    20. [SIZE=2]if (hCom == INVALID_HANDLE_VALUE) [/SIZE]
    21. [SIZE=2]  {[/SIZE]
    22. [SIZE=2]  // Handle the error.[/SIZE]
    23. [SIZE=2]  printf ("CreateFile failed with error %d.\n", GetLastError());[/SIZE]
    24. [SIZE=2]  return (1);[/SIZE]
    25. [SIZE=2]}[/SIZE]
    26. [SIZE=2] // Build on the current configuration, and skip setting the size[/SIZE]
    27. [SIZE=2] // of the input and output buffers with SetupComm.[/SIZE]
    28. [SIZE=2] fSuccess = GetCommState(hCom, &dcb);[/SIZE]
    29. [SIZE=2] if (!fSuccess) [/SIZE]
    30. [SIZE=2]   {[/SIZE]
    31. [SIZE=2]   // Handle the error.[/SIZE]
    32. [SIZE=2]   printf ("GetCommState failed with error %d.\n", GetLastError());[/SIZE]
    33. [SIZE=2]   return (2);[/SIZE]
    34. [SIZE=2] }[/SIZE]
    35. [SIZE=2] // Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.[/SIZE]
    36. [SIZE=2] dcb.BaudRate = CBR_57600; // set the baud rate[/SIZE]
    37. [SIZE=2] dcb.ByteSize = 8; // data size, xmit, and rcv[/SIZE]
    38. [SIZE=2] dcb.Parity = NOPARITY; // no parity bit[/SIZE]
    39. [SIZE=2] dcb.StopBits = ONESTOPBIT; // one stop bit[/SIZE]
    40. [SIZE=2] fSuccess = SetCommState(hCom, &dcb);[/SIZE]
    41. [SIZE=2] if (!fSuccess) [/SIZE]
    42. [SIZE=2] {[/SIZE]
    43. [SIZE=2]   // Handle the error.[/SIZE]
    44. [SIZE=2]   printf ("SetCommState failed with error %d.\n", GetLastError());[/SIZE]
    45. [SIZE=2]   return (3);[/SIZE]
    46. [SIZE=2] }[/SIZE]
    47. [SIZE=2] printf ("Serial port %s successfully reconfigured.\n", pcCommPort);[/SIZE]
    48. [SIZE=2] return (0);[/SIZE]
    49. [SIZE=2]}[/SIZE]
    Last edited: Jan 13, 2014
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    It looks suspiciously similar to the "Configuring a Communications Resource" article from MSDN, which means it has a little more credibility than most internet-generated code. I haven't used this method, but it seems okay.

    The only thing I would point out, and this may, or may not, seem obvious, make sure you set the correct COM port when you compile.
    Brownout likes this.
  3. Brownout

    Thread Starter Well-Known Member

    Jan 10, 2012
    I got this code to work, but only after dynking around with it all day. I had to make a change to my VC++ configuration to use Multi Byte Character Set for the CreateFile function to work. After exhausting google looking for the answer, I posted on Jan Alexson's Lake View Project forum, and she provided links that got me going :)
    tshuck likes this.