// µ¥ÀÌÅÍ ¼Û½Å ÇÔ¼ö
bool CSerialComm::Send(LPCTSTR WrittenBufData, int len)
{
DWORD BytesWritten;
//////////////////////////////////////////////////////////////////////////////////////////
// WriteFile() ÇÔ¼ö
//
// HANDLE WriteFile(
//HANDLE hFile, : ÆÄÀÏÀ̳ª I/O µð¹ÙÀ̽ºÀÇ ÇÚµé
// LPCVOID lpBuffer, : ÆÄÀÏÀ̳ª µð¹ÙÀ̽º¿¡ ¾µ µ¥ÀÌÅ͸¦ Æ÷ÇÔÇÏ´Â ¹öÆÛÀÇ Æ÷ÀÎÅÍ
// DWORD nNumberOfBytesToWrite, : ÆÄÀÏÀ̳ª µð¹ÙÀ̽º¿¡ ¾µ ¹ÙÀÌÆ®ÀÇ ¼ö
// LPDWORD lpNumberOfBytesWritten, : hFile ÆĶó¹ÌÅÍ°¡ »ç¿ëÇÑ ¹ÙÀÌÆ®ÀÇ ¼ö¸¦ ¹ÞÀ» º¯¼öÀÇ Æ÷ÀÎÅÍ
// LPOVERLAPPED lpOverlapped : ºñµ¿±âÀÔÃâ·Â »ç¿ë½Ã OVERLAPPED ±¸Á¶Ã¼ÀÇ Æ÷ÀÎÅÍ, »ç¿ëÇÏÁö¾ÊÀ»½Ã NULL
////////////////////////////////////////////////////////////////////////////////////////
if (WriteFile(m_hComm, WrittenBufData, len, &BytesWritten, NULL) !=0)
return true;
else
return false;
}
// µ¥ÀÌÅÍ ¼ö½Å ÇÔ¼ö
bool CSerialComm::Receive(LPSTR ReadBufData, int len)
{
DWORD BytesRead;
LPVOID lpData;
CSerialComm* Comm = (CSerialComm*)lpData;
LPARAM temp = (LPARAM)Comm;
//////////////////////////////////////////////////////////////////////////////////////////
// ReadFile() ÇÔ¼ö
//
// HANDLE WriteFile(
//HANDLE hFile, : ÆÄÀÏÀ̳ª I/O µð¹ÙÀ̽ºÀÇ ÇÚµé
// LPCVOID lpBuffer, : ÆÄÀÏÀ̳ª µð¹ÙÀ̽º·ÎºÎÅÍ ÀÐÀº µ¥ÀÌÅ͸¦ ¹ÞÀ» ¹öÆÛÀÇ Æ÷ÀÎÅÍ
// DWORD nNumberOfBytesToRead, : ÀÐÀ» µ¥ÀÌÅÍÀÇ ÃÖ´ë ¹ÙÀÌÆ®ÀÇ ¼ö
// LPDWORD lpNumberOfBytesRead, : hFile ÆĶó¹ÌÅÍ°¡ ÀÐÀº ¹ÙÀÌÆ®ÀÇ ¼ö¸¦ ¹ÞÀ» º¯¼öÀÇ Æ÷ÀÎÅÍ
// LPOVERLAPPED lpOverlapped : ºñµ¿±âÀÔÃâ·Â »ç¿ë½Ã OVERLAPPED ±¸Á¶Ã¼ÀÇ Æ÷ÀÎÅÍ, »ç¿ëÇÏÁö¾ÊÀ»½Ã NULL
////////////////////////////////////////////////////////////////////////////////////////
if (m_sPortOpenned==true)
{
SendMessage(m_hWnd,WM_MYRECEIVE,BytesRead,temp);
if (ReadFile(m_hComm, &ReadBufData, len, &BytesRead, NULL) != 0)
return true;
else
return false;
}
else
return false;
}
´ëÈ»óÀÚ
BEGIN_MESSAGE_MAP(CSerialDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_CBN_SELCHANGE(IDC_SETCOMPORT, &CSerialDlg::OnCbnSelchangeSetcomport)
ON_EN_CHANGE(IDC_EDITVIEWDATA, &CSerialDlg::OnEnChangeEditviewdata)
ON_BN_CLICKED(IDC_OPENPORT, &CSerialDlg::OnBnClickedOpenport)
ON_BN_CLICKED(IDC_CLEAR, &CSerialDlg::OnBnClickedClear)
ON_BN_CLICKED(IDC_SENDDATA, &CSerialDlg::OnBnClickedSenddata)
ON_CBN_SELCHANGE(IDC_SETBAUDRATE, &CSerialDlg::OnCbnSelchangeSetbaudrate)
ON_EN_CHANGE(IDC_EDIT_SENDDATA, &CSerialDlg::OnEnChangeEditSenddata)
ON_MESSAGE(WM_MYRECEIVE, &CSerialDlg::OnReceive)
END_MESSAGE_MAP()
// Æ÷Æ® ¿ÀÇÂ
void CSerialDlg::OnBnClickedOpenport()
{
// TODO: ¿©±â¿¡ ÄÁÆ®·Ñ ¾Ë¸² 󸮱â Äڵ带 Ãß°¡ÇÕ´Ï´Ù.
if(Comport1_State)
{
if(m_Comm)
{
m_Comm ->Close();
m_Comm = NULL;
AfxMessageBox(_T("COM Æ÷Æ® ´ÝÈû"));
Comport1_State = false;
GetDlgItem(IDC_OPENPORT)->SetWindowText(_T("OPEN"));
//GetDlgItem(IDC_OPENPORT)->EnableWindow(true);
GetDlgItem(IDC_SENDDATA)->EnableWindow(false);
}
}
else
{
m_Comm = new CSerialComm(_T("\\\\.\\")+m_strSetComPort, m_strSetBaudRate, _T("NONE"), _T("8 Bit"), _T("1 Bit"));
if (m_Comm->OpenPort(GetSafeHwnd()) !=0)
{
AfxMessageBox(_T("COM Æ÷Æ® ¿¸²"));
Comport1_State=true;
GetDlgItem(IDC_OPENPORT)->SetWindowText(_T("CLOSE"));
//GetDlgItem(IDC_OPENPORT)->EnableWindow(false);
GetDlgItem(IDC_SENDDATA)->EnableWindow(true);
}
else
{
AfxMessageBox(_T("ERROR"));
}
}
}
//¼Û½Å
void CSerialDlg::OnBnClickedSenddata()
{
// TODO: ¿©±â¿¡ ÄÁÆ®·Ñ ¾Ë¸² 󸮱â Äڵ带 Ãß°¡ÇÕ´Ï´Ù.
CString str;
if(Comport1_State == true)
{
GetDlgItem(IDC_EDIT_SENDDATA) -> GetWindowText(str);
str += "\r\n";
m_Comm->Send(str,str.GetLength());
AfxMessageBox(_T("¼º°ø"));
}
else
{
AfxMessageBox(_T("ERROR"));
}
}
// ¼ö½Å
LRESULT CSerialDlg::OnReceive(WPARAM length, LPARAM lpara)
{
CString str;
char data[20000];
if(m_Comm)
{
m_Comm->Receive(data,length);
data[length]=_T('\0');
str+=_T("\r\n");
AfxMessageBox(_T("123"));
for(int i=0; i<(int)length; i++)
{
str+=data[i];
}
m_EditViewData.ReplaceSel(str);
str="";
}
return 0;
}
¼ö½Å ÇÔ¼öÂÊ¿¡ ºê·¹ÀÌÅ©¸¦ °É¾î³õ°í µð¹ö±ë Çغ¸¸é °É¸®Áö°¡ ¾Ê³×¿ä...
¾î¶²°Ô ¹®Á¦Àϱî¿ä...
ÇÏ´Ù°¡ ¸¸ Äڵ尰³×¿ä.
Âü°í·Î ÀÌ ¼Ò½º¸¦ »ó¿ëÀ¸·Î ½è´Ù°¡´Â Å«ÀÏ ³³´Ï´Ù.¤¾
read ´ë±âŸ´Â °÷À̶ó´Â°Ô µ¥ÀÌÅÍ°¡ µé¾î¿À´Â°É °¨½ÃÇÏ´Â ºÎºÐ ¸»¾¸ÇÏ½Ã´Â°Ô ¸Â³ª¿ä?
¸¸µé¾îº¸°í´Â Àִµ¥ ÀÚ²Ù ¹«ÇÑ·çÇÁ°¡ µµ´Â°Å°°³×¿ä...