A simple windows app to open and write to COM1

Hi experts,

I was hoping someone could just provide a simple Visual Studio C++ app that just opens and writes a series of hex values out the COM1 port.  I am linux programmer and have never touch VS.  I am trying to wirte a test app that simulates a device to test an embedded device that I have programed.  This app needs to be run on a windows machine that is why I am using Visual c++.

Thanks
DanRaposoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwbgen/html/msdn_serial.asp ("Serial Communications in Win32"), this article comes with full source code that demonstrates (among other things) what you want.
0
jkrCommented:
BTW, see also

http://www.codeproject.com/system/chaiyasit_t.asp ("Creating a Serial communication on Win32")
http://www.codeproject.com/system/SerialPortComm.asp ("Non Overlapped Serial Port Communication using Win32")

and finally

http://www.codeproject.com/system/serial.asp ("Serial library for C++")
0
DanRaposoAuthor Commented:
ok, so I checked that stuff out and this is what I came up with

// IGT_Sim.cpp : Defines the entry point for the console application.
//

#include "StdAfx.h"
#include <iostream.h>
#include <stdio.h>
#include <windows.h>


int main()
{
      HANDLE HdComm;
      DWORD DwError;
      DCB Dcb;
      char Msg[128];
      DWORD RealNum;

      printf("Starting the IGT Sim\n");

      // Open the COMM port.
      HdComm = CreateFile( "//.//COM1", GENERIC_READ | GENERIC_WRITE, 0,
                                          NULL, OPEN_EXISTING, 0, NULL );
      if ( HdComm == INVALID_HANDLE_VALUE ) // If COMM port can't be opened, then ...
      {
            DwError = GetLastError();  // Display error code, and abort.
            FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, 0, DwError, 0, (char *)Msg, 124, 0 );
            printf( "Cannot Open Port COM1 !\nError Code: %d, - %s.\n", DwError, Msg );

            return 1;
      }
      else
      {
            printf("COM port opened successfully");
      }

      // Initialize COMM port.
      SetupComm( HdComm, 1024, 1024 );
      GetCommState( HdComm, &Dcb );
      Dcb.BaudRate = BAUD_9600;
      Dcb.Parity = NOPARITY;
      Dcb.ByteSize = 8;
      //Dcb.StopBits = ONESTOPBIT;
      SetCommState( HdComm, &Dcb );

      char message[5] = {0x32, 0x00, 0x01, 0x61, 0x52};

      while (1) {
            WriteFile( HdComm, message, 5, &RealNum, NULL );
            printf("%d\n", RealNum);
            Sleep(300);
      }
      CloseHandle( HdComm );
      return 0;
}

It tells me that it is writing 5 bytes, but I am not seeing it go over the serial connection.  Could someone let me know what is wrong with this?

Thanks
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

jkrCommented:
Try to 'FlushFileBuffers(HdComm);' to ensure they're actually sent over the port.
0
DanRaposoAuthor Commented:
sorry to be stupid, but where should i put that.  I put it in my while but didn't see anything.
0
jkrCommented:
Yes, that would be the place. But, I think I spotted the problem. By using

     SetupComm( HdComm, 1024, 1024 );

you make the UART buffer up to 1024 bytes - use

     SetupComm( HdComm, 0, 0);

insted to eliminate the buffering.
0
DanRaposoAuthor Commented:
hmmm still didn't work
0
DanRaposoAuthor Commented:
here is my code now....

// IGT_Sim.cpp : Defines the entry point for the console application.
//

#include "StdAfx.h"
#include <iostream.h>
#include <stdio.h>
#include <windows.h>


int main()
{
      HANDLE HdComm;
      DWORD DwError;
      DCB Dcb;
      char Msg[128];
      DWORD RealNum;

      printf("Starting the IGT Sim\n");

      // Open the COMM port.
      HdComm = CreateFile( "COM1", GENERIC_READ | GENERIC_WRITE, 0,
                                     NULL, OPEN_EXISTING, 0, NULL );
      if ( HdComm == INVALID_HANDLE_VALUE ) // If COMM port can't be opened, then ...
      {
            DwError = GetLastError();  // Display error code, and abort.
            FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, 0, DwError, 0, (char *)Msg, 124, 0 );
            printf( "Cannot Open Port COM1 !\nError Code: %d, - %s.\n", DwError, Msg );

            return 1;
      }
      else
      {
            printf("COM port opened successfully");
      }

      // Initialize COMM port.
      SetupComm( HdComm, 0, 0 );
      GetCommState( HdComm, &Dcb );
      Dcb.BaudRate = BAUD_9600;
      Dcb.Parity = NOPARITY;
      Dcb.ByteSize = 8;
      Dcb.StopBits = ONESTOPBIT;
      SetCommState( HdComm, &Dcb );

      char message[5] = {0x32, 0x00, 0x01, 0x61, 0x52};

      while (1) {
            WriteFile( HdComm, (char*)message, 5, &RealNum, NULL );
            printf("%d\n", RealNum);
            Sleep(300);
            FlushFileBuffers(HdComm);
      }
      CloseHandle( HdComm );
      return 0;
}
0
jkrCommented:
Are you sure the settings are correct? To simplify that, use

BuildCommDCB("baud=9600 parity=N data=8 stop=1",&Dcb);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DanRaposoAuthor Commented:
i believe so...here are the settings on the other end....in the embedded linux board

this->fd = ACE_OS::open( this->serialPort, O_RDWR | O_NDELAY );
   if (fd != -1) {
      tcgetattr( fd, &stTermios );
      memset( &stTermios, 0, sizeof(stTermios));
      cfsetispeed( &stTermios, B9600 );
      cfsetospeed( &stTermios, B9600 );

      stTermios.c_cc[VMIN] = 1;
      stTermios.c_cc[VTIME] = 0;
      stTermios.c_cflag &= ~(PARENB|CSTOPB|CSIZE|CRTSCTS); // 8 bits, no parity, Disable hardware flow control
      stTermios.c_cflag |= (CLOCAL | CREAD |CS8);          // Select 8 data bits
      stTermios.c_lflag &= ~(ICANON | ECHO | ISIG);        // set raw mode for input
      stTermios.c_iflag &= ~(IXON | IXOFF | IXANY|INLCR|ICRNL|IUCLC);   //no software flow control
      stTermios.c_oflag &= ~OPOST;                         //raw output

      if ( tcsetattr( this->fd, TCSAFLUSH, &stTermios ) != 0 ) {
         fbError=errno;
      }
   } else {
      fbError = errno;
   }
0
DanRaposoAuthor Commented:
SWEET!!!

BuildCommDCB("baud=9600 parity=N data=8 stop=1",&Dcb);

fixed the problem...working now!

Thanks!
0
jkrCommented:
Great! ;o)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.