Advertisement
olgierd

Untitled

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