2010년 6월 3일 목요일

Serial Port MFC

// SerialDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Serial.h"
#include "SerialDlg.h"
#include "comm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
 CAboutDlg();

// Dialog Data
 enum { IDD = IDD_ABOUTBOX };

 protected:
 virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
 DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{

}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CSerialDlg dialog

 


CSerialDlg::CSerialDlg(CWnd* pParent /*=NULL*/)
 : CDialog(CSerialDlg::IDD, pParent)
{
 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSerialDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 DDX_Text(pDX, IDC_EDIT1, m_EditData);
 DDX_CBIndex(pDX, IDC_PORT, m_nSettingPort);
 DDX_CBIndex(pDX, IDC_BAUDRATE, m_nSettingBaud);
}

BEGIN_MESSAGE_MAP(CSerialDlg, CDialog)
 ON_WM_SYSCOMMAND()
 ON_WM_PAINT()
 ON_WM_QUERYDRAGICON()
 //}}AFX_MSG_MAP
 ON_BN_CLICKED(IDC_CHECK_COMM, &CSerialDlg::OnBnClickedCheckComm)
 ON_BN_CLICKED(IDOK, &CSerialDlg::OnBnClickedOk)
 ON_BN_CLICKED(IDCANCEL, &CSerialDlg::OnBnClickedCancel)
 ON_MESSAGE(WM_RECEIVEDATA,OnReceiveData)

END_MESSAGE_MAP()


// CSerialDlg message handlers

BOOL CSerialDlg::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
 //for(int i=0; i<2; i++){
  m_Comm.SetHwnd(this->m_hWnd);
  LoadSettings();

 //}
 UpdateData(FALSE);

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

void CSerialDlg::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 CSerialDlg::OnPaint()
{
 if (IsIconic())
 {
  CPaintDC dc(this); // device context for painting

  SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSerialDlg::OnQueryDragIcon()
{
 return static_cast<HCURSOR>(m_hIcon);
}


void CSerialDlg::OnBnClickedCheckComm()
{
 // TODO: Add your control notification handler code here
 UpdateData(TRUE);
 BOOL bCheck =((CButton*)GetDlgItem(IDC_CHECK_COMM))->GetCheck();
 if(bCheck)//통신시작
 {
 // for(int i=0; i<2; i++){

   if(m_Comm.OpenCommPort(&Int2TTY())!=TRUE)
   {
    CString str;
    str.Format("COM%d이 이미 사용중인지 확인하세요",m_nSettingPort);
    AfxMessageBox(str);

    ((CButton*)GetDlgItem(IDC_CHECK_COMM))->SetCheck(!bCheck);
    return;
   
   }
 

  GetDlgItem(IDC_CHECK_COMM)->SetWindowTextA("통신종료");
 
  GetDlgItem(IDC_PORT)->EnableWindow(FALSE);
  GetDlgItem(IDC_PORT2)->EnableWindow(FALSE);

  GetDlgItem(IDC_BAUDRATE)->EnableWindow(FALSE);
 }
 
 else{
  //for(int i=0; i<2; i++){
   m_Comm.CloseConnection();
  //}
  GetDlgItem(IDC_CHECK_COMM)->SetWindowTextA("통신시작");
  GetDlgItem(IDC_PORT)->EnableWindow(TRUE);
  GetDlgItem(IDC_PORT2)->EnableWindow(TRUE);
  GetDlgItem(IDC_BAUDRATE)->EnableWindow(TRUE);
 }
}
TTYSTRUCT CSerialDlg::Int2TTY()
{
 TTYSTRUCT tty;
 ZERO_MEMORY(tty);

 tty.byCommPort = (BYTE) m_nSettingPort;
 tty.byXonXoff = FALSE;
 tty.byByteSize = (BYTE)_nDataValues[m_nSettingData];
 tty.byFlowCtrl = (BYTE)_nFlow[m_nSettingFlow];
 tty.byParity = (BYTE)m_nSettingParity;
 tty.byStopBits = (BYTE)_nStopBits[m_nSettingStop];
 tty.dwBaudRate = (DWORD)_nBaudRates[m_nSettingBaud];
 return tty;
}
TTYSTRUCT CSerialDlg::LoadSettings()
{
 CWinApp *pApp = AfxGetApp();

 m_nSettingPort = pApp->GetProfileInt(CS_REGKEY_SETTINGS, CS_REGENTRY_PORT,1);
 m_nSettingParity = 0;
 m_nSettingBaud = pApp->GetProfileIntA(CS_REGKEY_SETTINGS, CS_REGENTRY_BAUD,1);
 m_nSettingData = 1;
 m_nSettingStop = 0;
 m_nSettingFlow = 0;

 return Int2TTY();

}
void CSerialDlg::SaveSettings()
{
 CWinApp *pApp = AfxGetApp();
 pApp->WriteProfileInt(CS_REGKEY_SETTINGS, CS_REGENTRY_PORT, m_nSettingPort);
 pApp->WriteProfileInt(CS_REGKEY_SETTINGS, CS_REGENTRY_PARITY,m_nSettingParity);
 pApp->WriteProfileInt(CS_REGKEY_SETTINGS, CS_REGENTRY_BAUD, m_nSettingBaud);
 pApp->WriteProfileInt(CS_REGKEY_SETTINGS, CS_REGENTRY_DATABITS, m_nSettingData);
 pApp->WriteProfileInt(CS_REGKEY_SETTINGS, CS_REGENTRY_STOPBITS, m_nSettingStop);
 pApp->WriteProfileInt(CS_REGKEY_SETTINGS, CS_REGENTRY_FLOW, m_nSettingFlow);
}
void CSerialDlg::OnBnClickedOk()
{
 // TODO: Add your control notification handler code here

 char start[] = {0x02, 0x00, 0xf0};
//for(int i=0; i<2; i++)
  m_Comm.WriteCommBlock(start,3);
// SaveSettings();
// CDialog::OnOK();
}

void CSerialDlg::OnBnClickedCancel()
{
// TODO: Add your control notification handler code here
// for(int i=0; i<2; i++)
// { 
  SaveSettings();
  m_Comm.CloseConnection();
 //}
  CDialog::OnCancel();
}
LONG CSerialDlg::OnReceiveData(UINT WParam, LONG a)
{
 int i;
 int nLen = WParam;
 CString str;

 UpdateData(TRUE);
 BYTE ch;
 int temp;

// for(int j=0; j<2; j++){
  for(i=0;i<nLen;i++)
  {
   ch =(int)m_Comm.abIn[i];
   str.Format("%x",ch);
   m_EditData += str;
  }
// }
 UpdateData(FALSE);
 return TRUE;
}

댓글 없음:

댓글 쓰기