Hello everybody,

For a project my dad has enlisted me in, I am to be able to communicate with a Vitamin Scanner from a computer. To illustrate, he opened 'Hyper Terminal' (should come with most Windows computers). He set up a communication using the program, and told me the baudrate, stop bits, parity, etc.

So I set about making this program. I decided to use C++. After researching for like a week or so, I 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 am using Visual C++ 2008 Express Edition. I use Windows XP. When I try to compile, it says it can't find the header file "stdafx.h". Does anybody know where I can download it, and its associated files? Or, if there is no way to download it, does anybody know of a better way for Serial Communication in windows?

// 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

#include "resource.h"		// main symbols

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

class CADRTestApp : public CWinApp

// Overrides
	// ClassWizard generated virtual function overrides
	virtual BOOL InitInstance();

// Implementation

		// NOTE - the ClassWizard will add and remove member functions here.
		//    DO NOT EDIT what you see in these blocks of generated code !


// 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
	CADRTestDlg(CWnd* pParent = NULL);	// standard constructor

// Dialog Data
	CString	m_sResults;

	// ClassWizard generated virtual function overrides
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support

// Implementation
	HICON m_hIcon;
	CString g_sComPort;

	// Generated message map functions
	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();

// 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, 
		0, // exclusive access
		NULL, // no security
		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);
8 Years
Discussion Span
Last Post by bobbuilder67

Start a new project in VC++ this time uncheck precompiled headers and leave empty project unchecked. Add your files to the project by right clicking on the solution/Add.. etc. and you can clear out all the other files (or just exclude them from the project to be safe). You've probably done everything correctly except for the precompiled headers part.


Ok , that fixed some probs, but I've now got another, in ADRTest.h, the file from above...
Here is the build log, the project is called ST2000, and the source file is ST.cpp.

Build Log Build started: Project: ST2000, Configuration: Debug|Win32
Command Lines Creating temporary file "c:\Documents and Settings\Bergeson\My Documents\Visual Studio 2008\Projects\ST2000\ST2000\Debug\RSP00000427523280.rsp" with contents
/Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /Gm /EHsc /RTC1 /MDd /Fo"Debug\\" /Fd"Debug\vc90.pdb" /W3 /c /ZI /TP ".\ST.cpp"
Creating command line "cl.exe @"c:\Documents and Settings\Bergeson\My Documents\Visual Studio 2008\Projects\ST2000\ST2000\Debug\RSP00000427523280.rsp" /nologo /errorReport:prompt"
Output Window Compiling...
c:\documents and settings\bergeson\my documents\visual studio 2008\projects\st2000\st2000\adrtest.h(12) : fatal error C1189: #error : include 'stdafx.h' before including this file for PCH
Results Build log was saved at "file://c:\Documents and Settings\Bergeson\My Documents\Visual Studio 2008\Projects\ST2000\ST2000\Debug\BuildLog.htm"
ST2000 - 1 error(s), 0 warning(s)


Turn off UNICODE -- goto Project --> Properties (the last menu item) -->Configuration Properties --> General. Now on the right side, the third item from the bottom is "Character Set". Change that setting to "Not Set"

You could have turned off precompiled headers in a similar way, except under Configuration Properties --> C/C++ --> Precompiled Headers.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.