CncPDlg.cpp

 // CncPDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CncP.h"
#include "CncPDlg.h"
#include "fstream.h"
#include "iostream.h"
#include "stdio.h"
#include "conio.h"
#include "process.h"
#include "dos.h"
#include "stdlib.h"
#include "time.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define DATA 0x378
#define STATUS 0x379
#define CONTROL 0x37a

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

/////////////////////////////////////////////////////////////////////////////
// CCncPDlg dialog

CCncPDlg::CCncPDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCncPDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCncPDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCncPDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCncPDlg)
DDX_Control(pDX, IDC_PIN16, m_Pin16);
DDX_Control(pDX, IDC_PIN17, m_Pin17);
DDX_Control(pDX, IDC_PIN14, m_Pin14);
DDX_Control(pDX, IDC_PIN9, m_Pin9);
DDX_Control(pDX, IDC_PIN8, m_Pin8);
DDX_Control(pDX, IDC_PIN7, m_Pin7);
DDX_Control(pDX, IDC_PIN6, m_Pin6);
DDX_Control(pDX, IDC_PIN5, m_Pin5);
DDX_Control(pDX, IDC_PIN4, m_Pin4);
DDX_Control(pDX, IDC_PIN2, m_Pin2);
DDX_Control(pDX, IDC_PIN3, m_Pin3);
DDX_Control(pDX, IDC_PIN1, m_Pin1);
DDX_Control(pDX, IDC_PIN15, m_Pin15);
DDX_Control(pDX, IDC_PIN13, m_Pin13);
DDX_Control(pDX, IDC_PIN12, m_Pin12);
DDX_Control(pDX, IDC_PIN11, m_Pin11);
DDX_Control(pDX, IDC_PIN10, m_Pin10);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCncPDlg, CDialog)
//{{AFX_MSG_MAP(CCncPDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTONRUNMOTORCW, OnButtonRunMotorCW)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTONRUNMOTORCCW, OnButtonRunMotorCCW)
ON_BN_CLICKED(IDC_BUTTONSPEEDUP, OnButtonspeedup)

ON_BN_CLICKED(IDC_BUTTONSLOWDOWN, OnButtonslowdown)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCncPDlg message handlers

BOOL CCncPDlg::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
SetTimer(1,2,NULL);
speed=5;
//_outp(CONTROL, _inp(CONTROL) & 0xDF);

return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCncPDlg::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 CCncPDlg::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 CCncPDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CCncPDlg::OnButtonRunMotorCW()
{
// TODO: Add your control notification handler code here

          _outp(CONTROL,2); //Run motor steps CW.
      for(int run=0; run<400; run++) //Run the motor
  {
         _outp(DATA,0x00);//Set Pin 2 X axis low //Pin 6 step A axis

Sleep(speed);

_outp(DATA,0x01); //Set pin 2 high
      } 
}

void CCncPDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
/*in379=_inp(0x379);
    nPin10=in379 & 64;
    nPin11=in379 & 128;
    nPin12=in379 & 32;
    nPin13=in379 & 16;
    nPin15=in379 & 8;

    if(nPin10==0)
        m_Pin10.SetCheck(0);
    else if(nPin10==64)
        m_Pin10.SetCheck(1);

    if(nPin11==0)
        m_Pin11.SetCheck(0);
    else if(nPin11==128)
        m_Pin11.SetCheck(1);

    if(nPin12==0)
        m_Pin12.SetCheck(0);
    else if(nPin12==32)
        m_Pin12.SetCheck(1);

    if(nPin13==0)
        m_Pin13.SetCheck(0);
    else if(nPin13==16)
        m_Pin13.SetCheck(1);

    if(nPin15==0)
        m_Pin15.SetCheck(0);
    else if(nPin15==8)
        m_Pin15.SetCheck(1);*/
   
    int reg;
     /*reg=_inp(STATUS);

     if((reg & 0x40)==0) m_pin10=0;     else m_pin10=1;
     if((reg & 0x80)==0) m_pin11=0;     else m_pin11=1;
     if((reg & 0x20)==0) m_pin12=0;     else m_pin12=1;   
     if((reg & 0x10)==0) m_pin13=0;     else m_pin13=1;
     if((reg & 0x08)==0) m_pin15=0;     else m_pin15=1;*/
     //////////
     reg=_inp(DATA);
   
     if((reg & 0x01)==0)
//m_Pin2=0;    
     m_Pin2.SetCheck(0);
else //m_Pin2=1;
    m_Pin2.SetCheck(1);

     if((reg & 0x02)==0)
//m_Pin3=0;
         m_Pin3.SetCheck(0);
else //m_Pin3=1;  
      m_Pin3.SetCheck(1);

     if((reg & 0x04)==0)
//m_Pin4=0;
     m_Pin4.SetCheck(0);
else //m_Pin4=1;
     m_Pin4.SetCheck(1);

     if((reg & 0x08)==0)
//m_Pin5=0;
     m_Pin5.SetCheck(0);
else //m_Pin5=1;
          m_Pin5.SetCheck(1);

     if((reg & 0x10)==0)
//m_Pin6=0; 
     m_Pin6.SetCheck(0);
else //m_Pin6=1;
      m_Pin6.SetCheck(1);

     if((reg & 0x20)==0)
  //m_Pin7=0;
      m_Pin7.SetCheck(0);
else //m_Pin7=2;
          m_Pin7.SetCheck(1);

     if((reg & 0x40)==0)
//m_Pin8=0; 
     m_Pin8.SetCheck(0);
else //m_Pin8=1;
         m_Pin8.SetCheck(1); 

     if((reg & 0x80)==0)
//m_Pin9=0;
     m_Pin9.SetCheck(0);
else //m_Pin9=1;
      m_Pin9.SetCheck(1);
     //////
     reg = _inp(CONTROL);

     if((reg & 0x01)==0)
    //m_pin1=0;     
m_Pin1.SetCheck(1);
     else //m_pin1=1;
    m_Pin1.SetCheck(0);
     if((reg & 0x02)==0)
//m_pin14=0;    
m_Pin14.SetCheck(0);
else //m_pin14=1;
         m_Pin14.SetCheck(1);
     if((reg & 0x04)==0) //m_pin16=0;
         m_Pin16.SetCheck(0);
else //m_pin16=1;
         m_Pin16.SetCheck(1);
     if((reg & 0x08)==0)
//m_pin17=0;    
m_Pin17.SetCheck(0);
else //m_pin17=1;
m_Pin17.SetCheck(1);

    int status_reg;
    status_reg=_inp(STATUS);

    if((status_reg & 0x40)==0)
//m_Pin10=0;   
m_Pin10.SetCheck(0);
else //m_Pin10=2;
        m_Pin10.SetCheck(1);

    if((status_reg & 0x80)==0)
//m_Pin11=0;
m_Pin11.SetCheck(0);
else //m_Pin11=1;
m_Pin11.SetCheck(1);
       
    if((status_reg & 0x20)==0)
//m_Pin12=0;   
m_Pin12.SetCheck(0);
else //m_Pin12=1;
m_Pin12.SetCheck(1);

    if((status_reg & 0x10)==0)
//m_Pin13=0;  
  m_Pin13.SetCheck(0);
else //m_Pin13=1;
m_Pin13.SetCheck(1);

    if((status_reg & 0x08)==0)
//m_Pin15=0;
  m_Pin15.SetCheck(0);
else //m_Pin15=1;
           m_Pin15.SetCheck(1);

   // UpdateData(FALSE);

CDialog::OnTimer(nIDEvent);
}

  


void CCncPDlg::OnButtonRunMotorCCW()
{
// TODO: Add your control notification handler code here
   
           _outp(CONTROL,0); //Run motor steps CCW
      for(int run=0; run<400; run++) //Run the motor
  {
         _outp(DATA,0x00);//Set Pin 2 X axis low

Sleep(speed);  //Sleep mill a seconds between steps.

_outp(DATA,0x01); //Set pin 2 high
      } 
//_outp(DATA,0xFF);//0xFF Turns all data pins on
  //_outp(DATA,0);
}

void CCncPDlg::OnButtonspeedup()
{
// TODO: Add your control notification handler code here
speed--;
if(speed<=1)
{
speed=1;
}
}

void CCncPDlg::OnButtonslowdown()
{
// TODO: Add your control notification handler code here
speed++;
if(speed>=5)
{
speed=5;
}
}

BOOL CCncPDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
KillTimer(1);

return CDialog::DestroyWindow();
}

Project Homepage: