Program controlling DC motor through LPT port

This is a discussion on Program controlling DC motor through LPT port within the C++ Programming forums, part of the General Programming Boards category; Hi, I've been following this guide http://multyremotes.com/DC-motor-control-in-VC.htm making a program that will control a DC 12v motor through the computers ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Program controlling DC motor through LPT port

    Hi,
    I've been following this guide http://multyremotes.com/DC-motor-control-in-VC.htm making a program that will control a DC 12v motor through the computers LPT port.
    But the problem is that when I try to build it I get 28 errors.
    I really need to finish this!

    If anyone got a spare minute to look through the code I would be really happy!

    Code:
    // DCMCDlg.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "DCMC.h"
    #include "DCMCDlg.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    int speedFactor;
    
    /////////////////////////////////////////////////////////////////////////////
    // CAboutDlg dialog used for App About
    
    class CAboutDlg : public CDialog
    {
    public:
    	CAboutDlg();
    
    // Dialog Data
    	//{{AFX_DATA(CAboutDlg)
    	enum { IDD = IDD_ABOUTBOX };
    	//}}AFX_DATA
    
    	// ClassWizard generated virtual function overrides
    	//{{AFX_VIRTUAL(CAboutDlg)
    	protected:
    	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    	//}}AFX_VIRTUAL
    
    // Implementation
    protected:
    	//{{AFX_MSG(CAboutDlg)
    	//}}AFX_MSG
    	DECLARE_MESSAGE_MAP()
    };
    
    CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
    {
    	//{{AFX_DATA_INIT(CAboutDlg)
    	//}}AFX_DATA_INIT
    }
    
    void CAboutDlg::DoDataExchange(CDataExchange* pDX)
    {
    	CDialog::DoDataExchange(pDX);
    	//{{AFX_DATA_MAP(CAboutDlg)
    	//}}AFX_DATA_MAP
    }
    
    BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    	//{{AFX_MSG_MAP(CAboutDlg)
    		// No message handlers
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    /////////////////////////////////////////////////////////////////////////////
    // CDCMCDlg dialog
    
    CDCMCDlg::CDCMCDlg(CWnd* pParent /*=NULL*/)
    	: CDialog(CDCMCDlg::IDD, pParent)
    {
    	//{{AFX_DATA_INIT(CDCMCDlg)
    	m_speedfact = _T("");
    	//}}AFX_DATA_INIT
    	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }
    
    void CDCMCDlg::DoDataExchange(CDataExchange* pDX)
    {
    	CDialog::DoDataExchange(pDX);
    	//{{AFX_DATA_MAP(CDCMCDlg)
    	DDX_Text(pDX, IDC_SF, m_speedfact);
    	//}}AFX_DATA_MAP
    }
    
    BEGIN_MESSAGE_MAP(CDCMCDlg, CDialog)
    	//{{AFX_MSG_MAP(CDCMCDlg)
    	ON_WM_SYSCOMMAND()
    	ON_WM_PAINT()
    	ON_WM_QUERYDRAGICON()
    	ON_BN_CLICKED(IDC_FWD, OnFwd)
    	ON_BN_CLICKED(IDC_FRW, OnFrw)
    	ON_BN_CLICKED(IDC_DWN, OnDwn)
    	ON_BN_CLICKED(IDC_REW, OnRew)
    	ON_BN_CLICKED(IDC_RWS, OnRws)
    	ON_BN_CLICKED(IDC_UP, OnUp)
    	ON_BN_CLICKED(IDC_XIT, OnXit)
    	ON_BN_CLICKED(IIDC_STP, OnStp)
    	ON_WM_LBUTTONDOWN()
    	ON_WM_RBUTTONDOWN()
    	ON_WM_MOUSEMOVE()
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    /////////////////////////////////////////////////////////////////////////////
    // CDCMCDlg message handlers
    
    BOOL CDCMCDlg::OnInitDialog()
    {
    	CDialog::OnInitDialog();
    
    	// Add "About..." menu item to system menu.
    
    	// IDM_ABOUTBOX must be in the system command range.
    	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    	ASSERT(IDM_ABOUTBOX < 0xF000);
    
    	CMenu* pSysMenu = GetSystemMenu(FALSE);
    	if (pSysMenu != NULL)
    	{
    		CString strAboutMenu;
    		strAboutMenu.LoadString(IDS_ABOUTBOX);
    		if (!strAboutMenu.IsEmpty())
    		{
    			pSysMenu->AppendMenu(MF_SEPARATOR);
    			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    		}
    	}
    
    	// Set the icon for this dialog.  The framework does this automatically
    	//  when the application's main window is not a dialog
    	SetIcon(m_hIcon, TRUE);			// Set big icon
    	SetIcon(m_hIcon, FALSE);		// Set small icon
    	
    	// TODO: Add extra initialization here
    	
    	return TRUE;  // return TRUE  unless you set the focus to a control
    }
    
    void CDCMCDlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
    	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    	{
    		CAboutDlg dlgAbout;
    		dlgAbout.DoModal();
    	}
    	else
    	{
    		CDialog::OnSysCommand(nID, lParam);
    	}
    }
    
    // If you add a minimize button to your dialog, you will need the code below
    //  to draw the icon.  For MFC applications using the document/view model,
    //  this is automatically done for you by the framework.
    
    void CDCMCDlg::OnPaint() 
    {
    	if (IsIconic())
    	{
    		CPaintDC dc(this); // device context for painting
    
    		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
    
    		// Center icon in client rectangle
    		int cxIcon = GetSystemMetrics(SM_CXICON);
    		int cyIcon = GetSystemMetrics(SM_CYICON);
    		CRect rect;
    		GetClientRect(&rect);
    		int x = (rect.Width() - cxIcon + 1) / 2;
    		int y = (rect.Height() - cyIcon + 1) / 2;
    
    		// Draw the icon
    		dc.DrawIcon(x, y, m_hIcon);
    	}
    	else
    	{
    		CDialog::OnPaint();
    	}
    }
    
    // The system calls this to obtain the cursor to display while the user drags
    //  the minimized window.
    HCURSOR CDCMCDlg::OnQueryDragIcon()
    {
    	return (HCURSOR) m_hIcon;
    }
    
    void CDCMCDlg::OnFwd() 
    {
    	// TODO: Add your control notification handler code here
    	int p;
    	int z = 5000/(2*m_sf);
    	for(int i=0;i<z;i++);
    		{
    			p = _outp(0x0378,0x03);
    			Sleep(m_sf + a);
    			p = _outp(0x0378,0x00);
    			Sleep(m_sf - a);
    		}
    
    }
    
    void CDCMCDlg::OnFrw() 
    {
    	// TODO: Add your control notification handler code here
                int p;
                int z = 5000/(2*m_sf);
                for(int i=0;i<z;i++)
                    {
                        p = _outp(0x0378,0x03);
                        Sleep(m_sf + a);
                        p = _outp(0x0378,0x00);
                        Sleep(m_sf - a);
                     }	
    }
    
    void CDCMCDlg::OnDwn() 
    {
    	// TODO: Add your control notification handler code here
    	        UpdateData(TRUE);
                if(a>-45) a-=5;
                else MessageBox("Minimum speed limit");
                m_speedfact.Format("%d", a);
                UpdateData(FALSE);
        }
    }
    
    void CDCMCDlg::OnRew() 
    {
    	// TODO: Add your control notification handler code here
                int p;
                int z = 5000/(2*m_sf);
                for(int i=0;i<z;i++)
                    {
                        p = _outp(0x0378,0x0C);
                        Sleep(m_sf + a);
                        p = _outp(0x0378,0x00);
                        Sleep(m_sf - a);
                    }	
    }
    
    void CDCMCDlg::OnRws() 
    {
    	// TODO: Add your control notification handler code here
    	            int r;
                r = _outp(0x0378,0x0C);
        }
    }
    
    void CDCMCDlg::OnUp() 
    {
    	// TODO: Add your control notification handler code here
    	        UpdateData(TRUE);
                if(a<45) a+=5;
                else MessageBox("Maximum speed limit");
                m_speedfact.Format("%d", a);
                UpdateData(FALSE);
        }
    }
    
    void CDCMCDlg::OnXit() 
    {
    	// TODO: Add your control notification handler code here
    	        int r;
                r = _outp(0x0378,0x00);
                OnOK();
        }
    }
    
    void CDCMCDlg::OnStp() 
    {
    	// TODO: Add your control notification handler code here
    	    int r;
            r = _outp(0x0378,0x00);
        }
    }
    
    void CDCMCDlg::OnLButtonDown(UINT nFlags, CPoint point) 
    {
    	// TODO: Add your message handler code here and/or call default
    	int p;
        if((nFlags & MK_LBUTTON) == MK_LBUTTON)
             {
                    p = _outp(0x0378,0x03);
                    Sleep(50);
                    p = _outp(0x0378,0x00);
    		}
    	CDialog::OnLButtonDown(nFlags, point);
    }
    
    void CDCMCDlg::OnRButtonDown(UINT nFlags, CPoint point) 
    {
    	// TODO: Add your message handler code here and/or call default
    	        int p;
            if((nFlags & MK_RBUTTON) == MK_RBUTTON)
                {
                    p = _outp(0x0378,0x0C);
                    Sleep(50);
                    p = _outp(0x0378,0x00);
                }
    	CDialog::OnRButtonDown(nFlags, point);
    }
    
    void CDCMCDlg::OnMouseMove(UINT nFlags, CPoint point) 
    {
    	// TODO: Add your message handler code here and/or call default
    	        int p;
            if((nFlags & MK_LBUTTON) == MK_LBUTTON)
                {
                    p = _outp(0x0378,0x03);
                    Sleep(m_sf + a);
                    p = _outp(0x0378,0x00);
                    Sleep(m_sf - a);
                }
            else if((nFlags & MK_RBUTTON) == MK_RBUTTON)
                {
                    p = _outp(0x0378,0x0C);
                    Sleep(m_sf + a);
                    p = _outp(0x0378,0x00);
                    Sleep(m_sf - a);
                }
    	CDialog::OnMouseMove(nFlags, point);
    }
    Last edited by asbus; 10-24-2009 at 05:21 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you have compile errors, presumably you could say what they are....

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    If you get errors, then show them. If you want us to help then you make it easy. Most of us don't make a habbit of compiling other's code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    I'm terrible sorry. I'm really new to programming at all, but I'm using Visual C++ so there should be a console showing what the errors are all about? Let me check!

    Code:
    --------------------Configuration: MCDC - Win32 Debug--------------------
    Compiling resources...
    Compiling...
    StdAfx.cpp
    Compiling...
    MCDC.cpp
    MCDCDlg.cpp
    C:\Users\Olof Anderberg\Desktop\MCDC\MCDCDlg.cpp(186) : error C2065: 'a' : undeclared identifier
    C:\Users\Olof Anderberg\Desktop\MCDC\MCDCDlg.cpp(191) : error C2143: syntax error : missing ';' before '}'
    C:\Users\Olof Anderberg\Desktop\MCDC\MCDCDlg.cpp(191) : error C2143: syntax error : missing ';' before '}'
    C:\Users\Olof Anderberg\Desktop\MCDC\MCDCDlg.cpp(191) : error C2143: syntax error : missing ';' before '}'
    C:\Users\Olof Anderberg\Desktop\MCDC\MCDCDlg.cpp(194) : error C2143: syntax error : missing ';' before '{'
    C:\Users\Olof Anderberg\Desktop\MCDC\MCDCDlg.cpp(194) : error C2447: missing function header (old-style formal list?)
    C:\Users\Olof Anderberg\Desktop\MCDC\MCDCDlg.cpp(199) : error C2143: syntax error : missing ';' before '}'
    C:\Users\Olof Anderberg\Desktop\MCDC\MCDCDlg.cpp(199) : error C2143: syntax error : missing ';' before '}'
    C:\Users\Olof Anderberg\Desktop\MCDC\MCDCDlg.cpp(199) : error C2143: syntax error : missing ';' before '}'
    C:\Users\Olof Anderberg\Desktop\MCDC\MCDCDlg.cpp(202) : error C2143: syntax error : missing ';' before '{'
    C:\Users\Olof Anderberg\Desktop\MCDC\MCDCDlg.cpp(202) : error C2447: missing function header (old-style formal list?)
    C:\Users\Olof Anderberg\Desktop\MCDC\MCDCDlg.cpp(222) : error C2065: '_outp' : undeclared identifier
    Generating Code...
    Error executing cl.exe.
    
    MCDC.exe - 12 error(s), 0 warning(s)
    Last edited by asbus; 10-25-2009 at 01:55 AM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you click on each of those errors it will take you to the line. In the case of undeclared identifiers, it will only mention it once, even if you use it a bunch of times.

    So: You do use the variable a a lot, but you never declare it. Perhaps this was supposed to be a global variable? The missing semicolon seems to come from you messing up your for loops.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. LPT Port Interfacing
    By ScriptBlue in forum Windows Programming
    Replies: 3
    Last Post: 06-10-2005, 03:13 PM
  4. Serial port read..can someone tell me..
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-27-2002, 08:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21