Here is a simple serial class I wrote to talk to a motor controller. I’ve never used it to talk to Arduino, but it should work fine. I use vs2005 so it should work for you.
Serial.h
#ifndef __SERIAL_H__
#define __SERIAL_H__
#include <Windows.h>
#include "RbTypes.h"
// Copied from winbase.h
#define NOPARITY 0
#define ODDPARITY 1
#define EVENPARITY 2
#define MARKPARITY 3
#define SPACEPARITY 4
#define ONESTOPBIT 0
#define ONE5STOPBITS 1
#define TWOSTOPBITS 2
class CSerial
{
public:
CSerial();
~CSerial();
RBbool Open( char *portName, int baud, int bufSize, int portByteSize, RBubyte portParity, RBubyte portStopBits );
RBbool Close();
int ReadData( void *buffer, int limit );
int SendData( const char * buffer, int size );
protected:
HANDLE m_hCom;
BOOL m_portOpen;
int m_serialBufSize;
};
#endif
Serial.cpp
#include "Serial.h"
#include <stdio.h>
CSerial::CSerial()
{
m_hCom = NULL;
m_serialBufSize = 0;
m_portOpen = FALSE;
}
CSerial::~CSerial()
{
Close();
}
RBbool CSerial::Open( char *portName, int baud, int bufSize, int portByteSize, RBubyte portParity, RBubyte portStopBits )
{
DCB dcb;
BOOL portReady;
DWORD error;
COMMTIMEOUTS commTimeouts;
if( m_portOpen )
{
return RB_TRUE;
}
m_hCom = CreateFile( portName,
GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security
OPEN_EXISTING,
0, // no overlapped I/O
NULL ); // null template
if( (m_hCom == NULL) || (m_hCom == INVALID_HANDLE_VALUE) )
{
error = GetLastError();
printf( "COM open failed: Port=%s Error=%d\n", portName, error );
return RB_FALSE;
}
portReady = SetupComm( m_hCom, bufSize, bufSize ); // set buffer sizes
if ( !portReady )
{
error = GetLastError();
printf( "SetupComm failed: Port=%s Error=%d\n", portName, error );
CloseHandle( m_hCom );
return RB_FALSE;
}
dcb.DCBlength = sizeof( DCB );
portReady = GetCommState( m_hCom, &dcb );
if ( !portReady)
{
error = GetLastError();
printf( "GetCommState failed: Port=%s Error=%d\n", portName, error );
CloseHandle( m_hCom );
return RB_FALSE;
}
dcb.BaudRate = baud;
dcb.ByteSize = portByteSize;
dcb.Parity = portParity;
dcb.StopBits = portStopBits;
dcb.fAbortOnError = TRUE;
portReady = SetCommState( m_hCom, &dcb );
if ( !portReady )
{
error = GetLastError();
printf( "SetCommState failed: Port=%s Error = %d\n", portName, error );
CloseHandle( m_hCom );
return RB_FALSE;
}
portReady = GetCommTimeouts ( m_hCom, &commTimeouts );
if ( !portReady )
{
error = GetLastError();
printf( "GetCommTimeouts failed: Port=%s Error = %d\n", portName, error );
CloseHandle( m_hCom );
return RB_FALSE;
}
commTimeouts.ReadIntervalTimeout = 50;
commTimeouts.ReadTotalTimeoutConstant = 50;
commTimeouts.ReadTotalTimeoutMultiplier = 10;
commTimeouts.WriteTotalTimeoutConstant = 50;
commTimeouts.WriteTotalTimeoutMultiplier = 10;
portReady = SetCommTimeouts ( m_hCom, &commTimeouts );
if ( !portReady )
{
error = GetLastError();
printf( "SetCommTimeouts failed: Port=%s Error = %d\n", portName, error );
CloseHandle( m_hCom );
return RB_FALSE;
}
m_serialBufSize = bufSize;
m_portOpen = RB_TRUE;
return( m_portOpen );
}
RBbool CSerial::Close()
{
BOOL pass;
if( !m_portOpen || m_hCom == NULL )
{
return RB_TRUE;
}
pass = CloseHandle( m_hCom );
if ( !pass )
{
printf("Failed to close the serial port\n");
return RB_FALSE;
}
m_portOpen = FALSE;
m_hCom = NULL;
m_serialBufSize = 0;
return RB_TRUE;
}
int CSerial::SendData( const char *buffer, int size )
{
BOOL pass;
DWORD bytesWritten;
if( !m_portOpen || m_hCom == NULL )
{
return 0;
}
bytesWritten = 0;
pass = WriteFile( m_hCom, buffer, size, (LPDWORD)&bytesWritten, NULL );
if ( !pass || (bytesWritten != size) )
{
DWORD error = GetLastError();
printf( "Write of length query failed: Bytes Written=%d, Error=%d\n", bytesWritten, error);
return 0;
}
return bytesWritten;
}
int CSerial::ReadData( void *buffer, int maxSize )
{
BOOL readStatus;
DWORD bytesRead;
//DWORD errorFlags;
//COMSTAT comStat;
if( !m_portOpen || m_hCom == NULL )
{
return 0;
}
//ClearCommError( m_hCom, &errorFlags, &comStat );
//if( !comStat.cbInQue )
//{
// return 0;
//}
readStatus = ReadFile( m_hCom, buffer, maxSize, &bytesRead, NULL );
if ( !readStatus )
{
DWORD error = GetLastError();
printf( "Read length failed: Bytes read=%d, Error=%d\n", bytesRead, error );
return 0;
}
return bytesRead;
}
RbTypes.h
#ifndef __RBTYPES_H__
#define __RBTYPES_H__
#include <assert.h>
#define RB_TRUE 1
#define RB_FALSE 0
typedef char RBbyte;
typedef unsigned char RBubyte;
typedef int RBint;
typedef unsigned int RBuint;
typedef int RBbool;
#define RB_MIN( a, b ) (( a < b ) ? a : b )
#define RB_MAX( a, b ) (( a > b ) ? a : b )
#define RB_ASSERT assert
#endif