ReadSensorDlg.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // ReadSensorDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ReadSensor.h"
  5. #include "ReadSensorDlg.h"
  6. #include "afxdialogex.h"
  7. #include "global.h"
  8. #include "ComHandle.h"
  9. #include <string>
  10. using namespace std;
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #endif
  14. // CReadSensorDlg dialog
  15. ComHandle g_com;
  16. float g_pressure;
  17. float g_tem;
  18. int g_readtype;
  19. CReadSensorDlg::CReadSensorDlg(CWnd* pParent /*=NULL*/)
  20. : CDialogEx(CReadSensorDlg::IDD, pParent)
  21. {
  22. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  23. }
  24. void CReadSensorDlg::DoDataExchange(CDataExchange* pDX)
  25. {
  26. CDialogEx::DoDataExchange(pDX);
  27. DDX_Control(pDX, IDC_RICHEDIT21, m_richedit);
  28. DDX_Control(pDX, IDC_COMBO1, CB_Port);
  29. DDX_Control(pDX, IDC_EDIT1, ED_PRESSURE);
  30. DDX_Control(pDX, IDC_EDIT2, ED_TEM);
  31. DDX_Control(pDX, IDC_CHECK1, BTN_Loop);
  32. DDX_Control(pDX, IDC_EDIT3, ED_LoopTime);
  33. }
  34. BEGIN_MESSAGE_MAP(CReadSensorDlg, CDialogEx)
  35. ON_WM_PAINT()
  36. ON_WM_QUERYDRAGICON()
  37. ON_BN_CLICKED(IDC_OPENCOMM, &CReadSensorDlg::OnBnClickedOpencomm)
  38. ON_BN_CLICKED(IDC_READ_PRESSURE, &CReadSensorDlg::OnBnClickedReadPressure)
  39. ON_BN_CLICKED(IDC_READ_TEM, &CReadSensorDlg::OnBnClickedReadTem)
  40. ON_BN_CLICKED(IDC_READ_ALL, &CReadSensorDlg::OnBnClickedReadAll)
  41. ON_BN_CLICKED(IDC_CHECK1, &CReadSensorDlg::OnBnClickedCheck1)
  42. ON_MESSAGE(WM_DATARECEIVED, OnMyMessage)
  43. ON_WM_TIMER()
  44. ON_EN_CHANGE(IDC_EDIT3, &CReadSensorDlg::OnEnChangeEdit3)
  45. ON_BN_CLICKED(IDC_CLEAR, &CReadSensorDlg::OnBnClickedClear)
  46. ON_BN_CLICKED(IDC_Reflash, &CReadSensorDlg::OnBnClickedReflash)
  47. END_MESSAGE_MAP()
  48. // CReadSensorDlg message handlers
  49. BOOL CReadSensorDlg::OnInitDialog()
  50. {
  51. CDialogEx::OnInitDialog();
  52. // Set the icon for this dialog. The framework does this automatically
  53. // when the application's main window is not a dialog
  54. SetIcon(m_hIcon, TRUE); // Set big icon
  55. SetIcon(m_hIcon, FALSE); // Set small icon
  56. // TODO: Add extra initialization here
  57. g_com.linkRichEdit(&m_richedit);
  58. std::string strport = getConfig("port");
  59. CString strCom[255];
  60. CString strTemp;
  61. CString strPort;
  62. HANDLE hCom;
  63. int index = 0;
  64. for (int i = 0; i<255; i++)
  65. {
  66. strTemp.Format("\\\\.\\COM%d", i + 1);
  67. strPort.Format("%d", i + 1);
  68. hCom = CreateFile(strTemp, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
  69. OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
  70. if (INVALID_HANDLE_VALUE == hCom)
  71. continue;
  72. strCom[i] = strTemp;
  73. CB_Port.AddString(strPort);
  74. //if (string(strPort.GetString()) == strport)
  75. //{
  76. // CB_Port.SetCurSel(index);
  77. //}
  78. index++;
  79. CloseHandle(hCom);
  80. }
  81. for (int i = 0; i < CB_Port.GetCount(); i++)
  82. {
  83. CString str;
  84. CB_Port.GetLBText(i, str);
  85. if (string(str.GetString()) == strport)
  86. {
  87. CB_Port.SetCurSel(i);
  88. }
  89. }
  90. BTN_Loop.SetCheck(getConfig_INT("Loop"));
  91. ED_LoopTime.SetWindowText(getConfig("LoopTime").c_str());
  92. if (BTN_Loop.GetCheck())
  93. {
  94. CString str;
  95. ED_LoopTime.GetWindowText(str);
  96. int time = atoi(str);
  97. if (time > 0)
  98. {
  99. SetTimer(1, time, NULL);
  100. }
  101. }
  102. g_readtype = getConfig_INT("ReadType");
  103. OnBnClickedOpencomm();
  104. return TRUE; // return TRUE unless you set the focus to a control
  105. }
  106. // If you add a minimize button to your dialog, you will need the code below
  107. // to draw the icon. For MFC applications using the document/view model,
  108. // this is automatically done for you by the framework.
  109. void CReadSensorDlg::OnPaint()
  110. {
  111. if (IsIconic())
  112. {
  113. CPaintDC dc(this); // device context for painting
  114. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  115. // Center icon in client rectangle
  116. int cxIcon = GetSystemMetrics(SM_CXICON);
  117. int cyIcon = GetSystemMetrics(SM_CYICON);
  118. CRect rect;
  119. GetClientRect(&rect);
  120. int x = (rect.Width() - cxIcon + 1) / 2;
  121. int y = (rect.Height() - cyIcon + 1) / 2;
  122. // Draw the icon
  123. dc.DrawIcon(x, y, m_hIcon);
  124. }
  125. else
  126. {
  127. CDialogEx::OnPaint();
  128. }
  129. }
  130. // The system calls this function to obtain the cursor to display while the user drags
  131. // the minimized window.
  132. HCURSOR CReadSensorDlg::OnQueryDragIcon()
  133. {
  134. return static_cast<HCURSOR>(m_hIcon);
  135. }
  136. void CReadSensorDlg::OnBnClickedOpencomm()
  137. {
  138. int portno = 0;
  139. CString strport;
  140. CB_Port.GetWindowText(strport);
  141. portno = atoi(strport.GetString());
  142. g_com.opencom(portno);
  143. if (!strport.IsEmpty())
  144. {
  145. saveConfig("port", strport.GetString());
  146. }
  147. }
  148. void CReadSensorDlg::OnBnClickedReadPressure()
  149. {
  150. ED_PRESSURE.SetWindowText("");
  151. ED_TEM.SetWindowText("");
  152. g_readtype = 0;
  153. saveConfig("ReadType", g_readtype);
  154. BYTE buf[] = { 0x02, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x38};
  155. g_com.senddata(buf, _countof(buf));
  156. }
  157. void CReadSensorDlg::OnBnClickedReadTem()
  158. {
  159. ED_PRESSURE.SetWindowText("");
  160. ED_TEM.SetWindowText("");
  161. g_readtype = 1;
  162. saveConfig("ReadType", g_readtype);
  163. BYTE buf[] = { 0x02, 0x03, 0x00, 0x04, 0x00, 0x02, 0x85, 0xF9 };
  164. g_com.senddata(buf, _countof(buf));
  165. }
  166. void CReadSensorDlg::OnBnClickedReadAll()
  167. {
  168. ED_PRESSURE.SetWindowText("");
  169. ED_TEM.SetWindowText("");
  170. g_readtype = 2;
  171. saveConfig("ReadType", g_readtype);
  172. BYTE buf[] = { 0x02, 0x03, 0x00, 0x00, 0x00, 0x04, 0x44, 0x3A };
  173. g_com.senddata(buf, _countof(buf));
  174. }
  175. void CReadSensorDlg::OnBnClickedCheck1()
  176. {
  177. saveConfig("Loop", BTN_Loop.GetCheck());
  178. if (BTN_Loop.GetCheck())
  179. {
  180. ED_LoopTime.EnableWindow(FALSE);
  181. CString str;
  182. ED_LoopTime.GetWindowText(str);
  183. int time = atoi(str);
  184. if (time > 0)
  185. {
  186. SetTimer(1, time, NULL);
  187. }
  188. }
  189. else
  190. {
  191. ED_LoopTime.EnableWindow(TRUE);
  192. KillTimer(1);
  193. }
  194. }
  195. LRESULT CReadSensorDlg::OnMyMessage(WPARAM wParam, LPARAM lParam)
  196. {
  197. char pre[10] = { 0 };
  198. char tem[10] = { 0 };
  199. if (g_readtype == 0)
  200. {
  201. sprintf_s(pre, "%.4f", g_pressure);
  202. ED_PRESSURE.SetWindowText(pre);
  203. }
  204. else if (g_readtype == 1)
  205. {
  206. ED_TEM.SetWindowText(to_string(g_tem).c_str());
  207. }
  208. else if (g_readtype == 2)
  209. {
  210. ED_PRESSURE.SetWindowText(to_string(g_pressure).c_str());
  211. ED_TEM.SetWindowText(to_string(g_tem).c_str());
  212. }
  213. return 1;
  214. }
  215. void CReadSensorDlg::OnTimer(UINT_PTR nIDEvent)
  216. {
  217. // TODO: Add your message handler code here and/or call default
  218. if (g_readtype == 0)
  219. {
  220. OnBnClickedReadPressure();
  221. }
  222. else if (g_readtype == 1)
  223. {
  224. OnBnClickedReadTem();
  225. }
  226. else if (g_readtype == 2)
  227. {
  228. OnBnClickedReadAll();
  229. }
  230. CDialogEx::OnTimer(nIDEvent);
  231. }
  232. void CReadSensorDlg::OnEnChangeEdit3()
  233. {
  234. CString str;
  235. ED_LoopTime.GetWindowText(str);
  236. int time = atoi(str);
  237. if (time > 0)
  238. {
  239. saveConfig("LoopTime", time);
  240. }
  241. if (BTN_Loop.GetCheck())
  242. {
  243. if (time > 0)
  244. {
  245. SetTimer(1, time, NULL);
  246. }
  247. }
  248. }
  249. void CReadSensorDlg::OnBnClickedClear()
  250. {
  251. ED_PRESSURE.SetWindowText("");
  252. ED_TEM.SetWindowText("");
  253. g_readtype = 10;
  254. //saveConfig("ReadType", g_readtype);
  255. BYTE buf[] = { 0x02, 0x06, 0x00, 0x00, 0x00, 0x00, 0x89, 0xF9 };
  256. g_com.senddata(buf, _countof(buf));
  257. }
  258. void CReadSensorDlg::OnBnClickedReflash()
  259. {
  260. CB_Port.ResetContent();
  261. g_com.closecom();
  262. std::string strport = getConfig("port");
  263. CString strCom[255];
  264. CString strTemp;
  265. CString strPort;
  266. HANDLE hCom;
  267. int index = 0;
  268. for (int i = 0; i<255; i++)
  269. {
  270. strTemp.Format("\\\\.\\COM%d", i + 1);
  271. strPort.Format("%d", i + 1);
  272. hCom = CreateFile(strTemp, GENERIC_READ | GENERIC_WRITE, 0, NULL,
  273. OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
  274. if (INVALID_HANDLE_VALUE == hCom)
  275. continue;
  276. strCom[i] = strTemp;
  277. CB_Port.AddString(strPort);
  278. //if (string(strPort.GetString()) == strport)
  279. //{
  280. // CB_Port.SetCurSel(index);
  281. //
  282. //}
  283. index++;
  284. CloseHandle(hCom);
  285. }
  286. for (int i = 0; i < CB_Port.GetCount(); i++)
  287. {
  288. CString str;
  289. CB_Port.GetLBText(i, str);
  290. if (string(str.GetString()) == strport)
  291. {
  292. CB_Port.SetCurSel(i);
  293. }
  294. }
  295. OnBnClickedOpencomm();
  296. }