Hello everybody,

I am trying to communicate with a Vitamin Scanner from a computer, which connects via Comm port. It should do the equivalent thing of opening up 'Hyper Terminal', typing in 'a', hitting enter, then typing in '?I', enter, 'S', enter, and a long list of integers shows up on the GUI.

So far, I have stumbled upon some code that by all means, should work. I have attached the code in a file attachment, along with a few header files that are important. I have also attached my Build Log in a screen shot, BuildLog.jpg.

I am using Visual C++ 2008 Express Edition. I use Windows XP. I try to compile, it says:

fatal error C1189: #error : include 'stdafx.h' before including this file for PCH

Please help me, I am stuck.

Attached Files:
http://lbpages.heliohost.org/BuildLog.jpg
See the others on the bottom.

Attachments
// ADRTest.h : main header file for the ADRTEST application
//

#if !defined(AFX_ADRTEST_H__AD0D66E6_D7CC_11D2_8E68_006008A8250F__INCLUDED_)
#define AFX_ADRTEST_H__AD0D66E6_D7CC_11D2_8E68_006008A8250F__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

#ifndef __AFXWIN_H__
	#error include 'stdafx.h' before including this file for PCH
#endif

#include "resource.h"		// main symbols

/////////////////////////////////////////////////////////////////////////////
// CADRTestApp:
// See ADRTest.cpp for the implementation of this class
//

class CADRTestApp : public CWinApp
{
public:
	CADRTestApp();

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CADRTestApp)
	public:
	virtual BOOL InitInstance();
	//}}AFX_VIRTUAL

// Implementation

	//{{AFX_MSG(CADRTestApp)
		// NOTE - the ClassWizard will add and remove member functions here.
		//    DO NOT EDIT what you see in these blocks of generated code !
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};


/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_ADRTEST_H__AD0D66E6_D7CC_11D2_8E68_006008A8250F__INCLUDED_)
// ADRTestDlg.h : header file
//

#if !defined(AFX_ADRTESTDLG_H__AD0D66E8_D7CC_11D2_8E68_006008A8250F__INCLUDED_)
#define AFX_ADRTESTDLG_H__AD0D66E8_D7CC_11D2_8E68_006008A8250F__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

/////////////////////////////////////////////////////////////////////////////
// CADRTestDlg dialog

class CADRTestDlg : public CDialog
{
// Construction
public:
	CADRTestDlg(CWnd* pParent = NULL);	// standard constructor

// Dialog Data
	//{{AFX_DATA(CADRTestDlg)
	enum { IDD = IDD_ADRTEST_DIALOG };
	CString	m_sResults;
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CADRTestDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	HICON m_hIcon;
	CString g_sComPort;

	// Generated message map functions
	//{{AFX_MSG(CADRTestDlg)
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	afx_msg void OnCom1();
	afx_msg void OnCom2();
	afx_msg void OnCom3();
	afx_msg void OnCom4();
	afx_msg void OnRead();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_ADRTESTDLG_H__AD0D66E8_D7CC_11D2_8E68_006008A8250F__INCLUDED_)
#include <iostream>
#include <windows.h>
#include "stdafx.h"
#include "ADRTest.h"
#include "ADRTestDlg.h"
int main() {
// variables used with the com port
BOOL     m_bPortReady;
HANDLE   m_hCom;
CString  m_sComPort;
DCB      m_dcb;
COMMTIMEOUTS m_CommTimeouts;
BOOL     bWriteRC;
BOOL     bReadRC;
DWORD iBytesWritten;
DWORD iBytesRead;
char       sBuffer[128];

m_sComPort = "Com1";
m_hCom = CreateFile(m_sComPort, 
		GENERIC_READ | GENERIC_WRITE,
		0, // exclusive access
		NULL, // no security
		OPEN_EXISTING,
		0, // no overlapped I/O
		NULL); // null template 
m_bPortReady = SetupComm(m_hCom, 128, 128); // set buffer sizes
m_bPortReady = GetCommState(m_hCom, &m_dcb);
m_dcb.BaudRate = 9600;
m_dcb.ByteSize = 8;
m_dcb.Parity = NOPARITY;
m_dcb.StopBits = ONESTOPBIT;
m_dcb.fAbortOnError = TRUE;

m_bPortReady = SetCommState(m_hCom, &m_dcb);

m_bPortReady = GetCommTimeouts (m_hCom, &m_CommTimeouts);

m_CommTimeouts.ReadIntervalTimeout = 50;
m_CommTimeouts.ReadTotalTimeoutConstant = 50;
m_CommTimeouts.ReadTotalTimeoutMultiplier = 10;
m_CommTimeouts.WriteTotalTimeoutConstant = 50;
m_CommTimeouts.WriteTotalTimeoutMultiplier = 10;

bWriteRC = WriteFile(m_hCom, "a\r",3,&iBytesWritten,NULL);
bReadRC = ReadFile(m_hCom, &sBuffer, 100, &iBytesRead, NULL);
cout << bReadRc << endl;
bWriteRC = WriteFile(m_hCom, "S\r",3,&iBytesWritten,NULL);
bReadRC = ReadFile(m_hCom, &sBuffer, 1000, &iBytesRead,NULL);
CloseHandl(m_hCom);
}

There are a couple of things that are standing in the way of this compiling.
When you posted last I assumed incorrectly that this would be a console application. Upon a closer look it appears to be an MFC (MS Foundation Classes) application which is not directly supported by the express edition of VC++ only the standard.
In addition to that, the project seems to be missing some of the components, in particular the dialog resources (to what extent they can be regenerated I'm not sure).
That error message you were getting was coded into one of the header files to make sure that stdafx.h was included before that particular header file. In the stdafx.h that I generated with a Win32 project in VC++ express that value was not defined, which again may be a result of needing MFC, but I am not 100% on that.

Others more steeped in MFC might be able to advise you on how you can finagle this code back into Win32 or might be able to suggest a means through which you can access the serial port with the API.

So does anyone know of how I should edit stdafx.h to get his program to work?

Unfortunately editing stdafx.h will do no good. It is a MFC program you have there, and the VC Express won't be able to compile it like jonsca ^said^.
If you know someone, who's familiar with the MFC, the program might be quickly converted to a Win 32 GUI application, after all it appears to be a small'ish dialog-based program.

This article has been dead for over six months. Start a new discussion instead.