Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Interface (Serial) Chung-Buk HRD Institute of KCCI Dept. of Information & Communication PhD. Kang, Won-Chan.

Similar presentations


Presentation on theme: "Computer Interface (Serial) Chung-Buk HRD Institute of KCCI Dept. of Information & Communication PhD. Kang, Won-Chan."— Presentation transcript:

1 Computer Interface (Serial) Chung-Buk HRD Institute of KCCI Dept. of Information & Communication PhD. Kang, Won-Chan

2 Max232c

3 Computer Interface Board Tx Board Rx PC Tx PC Rx

4 Computer Interface Hyper Terminal

5 Computer Interface Example 1 Make a project MFC Wizard Dialog based Name: Serial Input Library: none Input Function: timer() Select hardware none Using Command OpenPort() ClosePort()

6 Computer Interface Add File: Comthread.h GrapWnd.h Add File: Comthread.cpp GrapWnd.cpp

7 Computer Interface

8 IDC_CON IDC_DISCON ID_OK IDC_EDIT1 IDC_LISTEN

9 Computer Interface IDC_EDIT1

10 Computer Interface

11 #include "CommThread.h"// Added by ClassView : class CSerialDlg : public CDialog { // Construction public: BOOL LS_FLAG; BOOL SI_FLAG; CString global_buf; int icount; char SerialRead(); int SeialSend(char sdata); void Monitor(CString str); CSerialDlg(CWnd* pParent = NULL);// standard constructor CCommThread m_ComuPort; : protected: BYTE SerialNumbers[256]; long PortNO; HICON m_hIcon; SerialDlg.h : header file

12 Computer Interface #include "stdafx.h" #include "Serial.h" #include "SerialDlg.h" HWND hCommWnd; : CSerialDlg::CSerialDlg(CWnd* pParent /*=NULL*/) : CDialog(CSerialDlg::IDD, pParent) { //{{AFX_DATA_INIT(CSerialDlg) m_strEdit = _T(""); icount=0; LS_FLAG=FALSE; //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } SerialDlg.cpp : implementation file

13 Computer Interface void CSerialDlg::OnConnect() { if(m_ComuPort.m_bConnected == FALSE) { if(m_ComuPort.OpenPort("COM1", CBR_9600, 8, ONESTOPBIT, NOPARITY) ==TRUE) { if(SeialSend('k')==1) { Monitor(" COM1 Connect Succes!!."); GetDlgItem(IDC_CON)->EnableWindow(FALSE); GetDlgItem(IDC_DISCON)->EnableWindow(TRUE); SI_FLAG=TRUE; } else AfxMessageBox("Transmit error"); } else { AfxMessageBox("Already Port open"); }

14 Computer Interface void CSerialDlg::OnDisconnect() { GetDlgItem(IDC_CON)->EnableWindow(TRUE); GetDlgItem(IDC_DISCON)->EnableWindow(FALSE); OnConnect(); if(m_ComuPort.m_bConnected == TRUE) {m_ComuPort.ClosePort(); SI_FLAG=FALSE; } else {AfxMessageBox("COM1 not open!!."); Monitor("COM1 not open!."); } Monitor("COM1 Disconnect success!!."); } void CSerialDlg::OnOK() { CDialog::OnOK(); }

15 Computer Interface void CSerialDlg::Monitor(CString str) { CString temp,k; temp.Format("[%d] step = ",icount); CTime time = CTime::GetCurrentTime(); k.Format("%d.%02d.%02d.:%02d/%02d/%02d:", time.GetYear(), time.GetMonth(), time.GetDay(), time.GetHour(), time.GetMinute(), time.GetSecond()); UpdateData(TRUE); m_strEdit+="\r\n"; m_strEdit+=temp; m_strEdit+=k; m_strEdit+=str; temp+=m_strEdit; UpdateData(FALSE); //sndPlaySound("sound.wav",SND_ASYNC|SND_NODEFAULT); icount++; }

16 Computer Interface int CSerialDlg::SeialSend(char sdata) { int nSize = 1; //2byte transmit int bufPos = 0; BYTE *Send_buff; Send_buff = new BYTE[nSize]; for(int i = 0; i < nSize; i++) {Send_buff[bufPos++] = sdata; } int etc = bufPos % 8; for(int j =0; j < bufPos-etc; j+= 8) {m_ComuPort.WriteComm(&Send_buff[j], 8) ; } if(etc != 0) {m_ComuPort.WriteComm(&Send_buff[bufPos -etc], etc) ; } delete [] Send_buff; return nSize; } Add function

17 Computer Interface Add function char CSerialDlg::SerialRead() { CString str = ""; CString result; char rdata; BYTE aByte; // 데이터를 저장할 변수 int iSize = (m_ComuPort.m_QueueRead).GetSize(); if(iSize!=1) AfxMessageBox("Seial data type is not char"); for(int i = 0 ; i < iSize; i++) { (m_ComuPort.m_QueueRead).GetByte(&aByte); str.Format("%c", aByte); rdata=aByte; } return rdata; }

18 Computer Interface void CSerialDlg::OnListen() { char temp; CString tt; if(LS_FLAG==TRUE) LS_FLAG=FALSE; else LS_FLAG=TRUE; m_strEdit+="\r\n Receive data:"; if((LS_FLAG==TRUE) && (SI_FLAG==TRUE)) {temp=SerialRead(); tt.Format("%c",temp); m_strEdit+=tt; UpdateData(FALSE); } else {AfxMessageBox("Termination of receive!!!"); }

19 Computer Interface

20 END


Download ppt "Computer Interface (Serial) Chung-Buk HRD Institute of KCCI Dept. of Information & Communication PhD. Kang, Won-Chan."

Similar presentations


Ads by Google