Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. // PCComDlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "PCCom.h"
  6. #include "PCComDlg.h"
  7.  
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #endif
  11.  
  12.  
  13. // CAboutDlg dialog used for App About
  14.  
  15. class CAboutDlg : public CDialog
  16. {
  17. public:
  18. CAboutDlg();
  19.  
  20. // Dialog Data
  21. enum { IDD = IDD_ABOUTBOX };
  22.  
  23. protected:
  24. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  25.  
  26. // Implementation
  27. protected:
  28. DECLARE_MESSAGE_MAP()
  29. };
  30.  
  31. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  32. {
  33. }
  34.  
  35. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  36. {
  37. CDialog::DoDataExchange(pDX);
  38. }
  39.  
  40. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  41. END_MESSAGE_MAP()
  42.  
  43.  
  44. // CPCComDlg dialog
  45.  
  46.  
  47.  
  48.  
  49. CPCComDlg::CPCComDlg(CWnd* pParent /*=NULL*/)
  50. : CDialog(CPCComDlg::IDD, pParent)
  51. {
  52. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  53. }
  54.  
  55. void CPCComDlg::DoDataExchange(CDataExchange* pDX)
  56. {
  57. CDialog::DoDataExchange(pDX);
  58. }
  59.  
  60. BEGIN_MESSAGE_MAP(CPCComDlg, CDialog)
  61. ON_WM_SYSCOMMAND()
  62. ON_WM_PAINT()
  63. ON_WM_QUERYDRAGICON()
  64. //}}AFX_MSG_MAP
  65. ON_BN_CLICKED(IDC_BUTOPEN, &CPCComDlg::OnBnClickedButopen)
  66. ON_BN_CLICKED(IDC_BUTCLOSE, &CPCComDlg::OnBnClickedButclose)
  67. ON_BN_CLICKED(IDC_BUTSEND, &CPCComDlg::OnBnClickedButsend)
  68. ON_BN_CLICKED(IIDC_BUTBUFCNT, &CPCComDlg::OnBnClickedButbufcnt)
  69. ON_BN_CLICKED(IDC_BUTREC, &CPCComDlg::OnBnClickedButrec)
  70. END_MESSAGE_MAP()
  71.  
  72.  
  73. // CPCComDlg message handlers
  74.  
  75. BOOL CPCComDlg::OnInitDialog()
  76. {
  77. CDialog::OnInitDialog();
  78.  
  79. // Add "About..." menu item to system menu.
  80.  
  81. // IDM_ABOUTBOX must be in the system command range.
  82. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  83. ASSERT(IDM_ABOUTBOX < 0xF000);
  84.  
  85. CMenu* pSysMenu = GetSystemMenu(FALSE);
  86. if (pSysMenu != NULL)
  87. {
  88. CString strAboutMenu;
  89. strAboutMenu.LoadString(IDS_ABOUTBOX);
  90. if (!strAboutMenu.IsEmpty())
  91. {
  92. pSysMenu->AppendMenu(MF_SEPARATOR);
  93. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  94. }
  95. }
  96.  
  97. // Set the icon for this dialog. The framework does this automatically
  98. // when the application's main window is not a dialog
  99. SetIcon(m_hIcon, TRUE); // Set big icon
  100. SetIcon(m_hIcon, FALSE); // Set small icon
  101.  
  102. // TODO: Add extra initialization here
  103.  
  104. return TRUE; // return TRUE unless you set the focus to a control
  105. }
  106.  
  107. void CPCComDlg::OnSysCommand(UINT nID, LPARAM lParam)
  108. {
  109. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  110. {
  111. CAboutDlg dlgAbout;
  112. dlgAbout.DoModal();
  113. }
  114. else
  115. {
  116. CDialog::OnSysCommand(nID, lParam);
  117. }
  118. }
  119.  
  120. // If you add a minimize button to your dialog, you will need the code below
  121. // to draw the icon. For MFC applications using the document/view model,
  122. // this is automatically done for you by the framework.
  123.  
  124. void CPCComDlg::OnPaint()
  125. {
  126. if (IsIconic())
  127. {
  128. CPaintDC dc(this); // device context for painting
  129.  
  130. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  131.  
  132. // Center icon in client rectangle
  133. int cxIcon = GetSystemMetrics(SM_CXICON);
  134. int cyIcon = GetSystemMetrics(SM_CYICON);
  135. CRect rect;
  136. GetClientRect(&rect);
  137. int x = (rect.Width() - cxIcon + 1) / 2;
  138. int y = (rect.Height() - cyIcon + 1) / 2;
  139.  
  140. // Draw the icon
  141. dc.DrawIcon(x, y, m_hIcon);
  142. }
  143. else
  144. {
  145. CPaintDC dc(this); // device context for painting
  146.  
  147. dc.TextOut(10,200,m_strRecText);
  148. CDialog::OnPaint();
  149. }
  150. }
  151.  
  152. // The system calls this function to obtain the cursor to display while the user drags
  153. // the minimized window.
  154. HCURSOR CPCComDlg::OnQueryDragIcon()
  155. {
  156. return static_cast<HCURSOR>(m_hIcon);
  157. }
  158.  
  159.  
  160. void CPCComDlg::OnBnClickedButopen()
  161. {
  162. // TODO: Add your control notification handler code here
  163. m_hCom = CreateFile( L"COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  164. if ( m_hCom == INVALID_HANDLE_VALUE )
  165. {
  166. WCHAR text[100];
  167. Beep(1000, 50);
  168.  
  169. long errorID = GetLastError();
  170. FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), text, 100, NULL );
  171. MessageBox(text);
  172. return;
  173. };
  174.  
  175. SetupComm(m_hCom, 256, 256);
  176.  
  177. DCB dcb;
  178. dcb.DCBlength=sizeof(dcb);
  179. GetCommState(m_hCom, &dcb);
  180. dcb.BaudRate=CBR_9600;
  181. dcb.fParity=TRUE;
  182. dcb.Parity=NOPARITY;
  183. dcb.StopBits=ONESTOPBIT;
  184. dcb.ByteSize=8;
  185. bool result = SetCommState(m_hCom, &dcb);
  186.  
  187. if (!result){
  188. WCHAR text[100];
  189. Beep (1000, 50);
  190.  
  191. long errorID=GetLastError();
  192. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), text, 100, NULL);
  193. MessageBox(text);
  194. CloseHandle(m_hCom);
  195. return;
  196. };
  197. }
  198.  
  199. void CPCComDlg::OnBnClickedButclose()
  200. {
  201. // TODO: Add your control notification handler code here
  202. CloseHandle(m_hCom);
  203. }
  204.  
  205. /*void CPCComDlg::OnBnClickedButton3()
  206. {
  207. // TODO: Add your control notification handler code here
  208. }*/
  209.  
  210. void CPCComDlg::OnBnClickedButsend()
  211. {
  212. // TODO: Add your control notification handler code here
  213. m_aBuf[0]='i';
  214. m_aBuf[1]='m';
  215. m_aBuf[2]='i';
  216. m_aBuf[3]='e';
  217. m_aBuf[4]=13;
  218. m_aBuf[5]=10;
  219.  
  220. DWORD nbrToWrite=6;
  221. DWORD nbrWritten=0;
  222.  
  223. BOOL result=WriteFile(m_hCom, m_aBuf, nbrToWrite, &nbrWritten, NULL);
  224. }
  225.  
  226.  
  227. void CPCComDlg::OnBnClickedButbufcnt()
  228. {
  229. // TODO: Add your control notification handler code here
  230. COMSTAT status;
  231. DWORD errors;
  232. DWORD nbrBytesToRead;
  233.  
  234. ClearCommError(m_hCom, &errors, &status);
  235.  
  236. CString str;
  237. str.Format(L"Liczba danych w buforze: %d", status.cbInQue);
  238.  
  239. MessageBox(str);
  240.  
  241. }
  242.  
  243. void CPCComDlg::OnBnClickedButrec()
  244. {
  245. // TODO: Add your control notification handler code here
  246. COMSTAT status;
  247. DWORD errors;
  248. DWORD nbrBytesToRead;
  249. ClearCommError(m_hCom, &errors, &status);
  250.  
  251. /*
  252. if(status.cbInQue > 0)
  253. {
  254. DWORD nbrRead;
  255. char buf;
  256. BOOL result = ReadFile(m_hCom, &buf, 1, &nbrRead, NULL);
  257.  
  258. m_strRecText += buf;
  259. this->RedrawWindow();
  260. };
  261. */
  262. int draw = 0;
  263. while(status.cbInQue > 0)
  264. {
  265. draw = 1;
  266. ClearCommError(m_hCom, &errors, &status);
  267. DWORD nbrRead;
  268. char buf;
  269. BOOL result = ReadFile(m_hCom, &buf, 1, &nbrRead, NULL);
  270.  
  271. m_strRecText += buf;
  272. this->RedrawWindow();
  273. }
  274. if(draw)
  275. {
  276. this->RedrawWindow();
  277. }
  278.  
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement