ComHandle.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #include "stdafx.h"
  2. #include "ComHandle.h"
  3. #include "global.h"
  4. #include <string>
  5. #include "ReadSensor.h"
  6. using namespace std;
  7. typedef union
  8. {
  9. float f;
  10. char c[4];
  11. } UNION_FLOAT_CONV_TypeDef;
  12. float __ltobf(BYTE* pc)
  13. {
  14. UNION_FLOAT_CONV_TypeDef d1, d2;
  15. d1.c[0] = *pc;
  16. d1.c[1] = *(pc + 1);
  17. d1.c[2] = *(pc + 2);
  18. d1.c[3] = *(pc + 3);
  19. //d1.c[0] = 0x41;
  20. //d1.c[1] = 0xcc;
  21. //d1.c[2] = 0x00;
  22. //d1.c[3] = 0x00;
  23. d2.c[0] = d1.c[3];
  24. d2.c[1] = d1.c[2];
  25. d2.c[2] = d1.c[1];
  26. d2.c[3] = d1.c[0];
  27. return d2.f;
  28. }
  29. extern float g_pressure;
  30. extern float g_tem;
  31. extern int g_readtype;
  32. ComHandle::ComHandle()
  33. {
  34. }
  35. int ComHandle::opencom(int portno)
  36. {
  37. closecom();
  38. if (portno > 0)
  39. {
  40. //新建串口通讯对象
  41. m_pSerial = new CMyCESeries();
  42. m_pSerial->m_OnSeriesRead = OnComRead;
  43. //m_nPortNo = portNo;
  44. int rtn = m_pSerial->OpenPort(this, portno,19200);
  45. //打开串口
  46. if (rtn)
  47. {
  48. addText("打开串口" + std::to_string(portno) + "成功");
  49. }
  50. else
  51. {
  52. addText("打开串口" + std::to_string(portno) + "失败");
  53. if (m_pSerial)
  54. {
  55. delete m_pSerial;
  56. m_pSerial = NULL;
  57. }
  58. }
  59. return rtn;
  60. }
  61. else
  62. return false;
  63. }
  64. int ComHandle::closecom()
  65. {
  66. if (m_pSerial)
  67. {
  68. m_pSerial->ClosePort();
  69. delete m_pSerial;
  70. m_pSerial = NULL;
  71. return true;
  72. }
  73. return false;
  74. }
  75. int ComHandle::senddata()
  76. {
  77. //int nRc = m_pSerial->WriteSyncPort(buff, len);
  78. //return nRc;
  79. return false;
  80. }
  81. void ComHandle::addText(string str)
  82. {
  83. if (m_pRichEdit)
  84. {
  85. m_pRichEdit->SetSel(-1, -1);
  86. m_pRichEdit->ReplaceSel((str+ string("\r\n")).c_str());
  87. m_pRichEdit->PostMessage(WM_VSCROLL, SB_BOTTOM, 0);
  88. }
  89. }
  90. bool ComHandle::ProcessComData(BYTE* buf, DWORD bufLen)
  91. {
  92. int datalen = 0;
  93. if (g_readtype == 0)
  94. {
  95. datalen = 9;
  96. }
  97. else if (g_readtype == 1)
  98. {
  99. datalen = 9;
  100. }
  101. else if (g_readtype == 2)
  102. {
  103. datalen = 13;
  104. }
  105. else if (g_readtype == 10)
  106. {
  107. datalen = 8;
  108. }
  109. if (bufLen > 13 || CommBufLen > 13)
  110. {
  111. addText("收到数据长度过长,弃用");
  112. memset(CommBuf, 0, 128);
  113. CommBufLen = 0;
  114. return false;
  115. }
  116. CString strS = "";
  117. for (int i = 0; i < bufLen; ++i)
  118. {
  119. CString strW;
  120. CStringA str;
  121. strW.Format(_T("%.2x"), buf[i]);
  122. strS += strW + " ";
  123. }
  124. CString str;
  125. str.Format("%s 收到数据:%s", getCurrentTime().c_str(), strS);
  126. addText(str.GetString());
  127. if (CommBufLen == 0)
  128. {
  129. memcpy(CommBuf, buf, bufLen);
  130. CommBufLen = bufLen;
  131. }
  132. else
  133. {
  134. memcpy(CommBuf + CommBufLen, buf, bufLen);
  135. CommBufLen += bufLen;
  136. }
  137. if (CommBufLen != datalen)
  138. {
  139. return false;
  140. }
  141. strS = "";
  142. for (int i = 0; i < CommBufLen; ++i)
  143. {
  144. CString strW;
  145. CStringA str;
  146. strW.Format(_T("%.2x"), CommBuf[i]);
  147. strS += strW + " ";
  148. }
  149. str.Format("%s 收到数据合并:%s", getCurrentTime().c_str(), strS);
  150. addText(str.GetString());
  151. if (g_readtype == 0)
  152. {
  153. g_pressure = __ltobf(&CommBuf[3]);;
  154. }
  155. else if (g_readtype == 1)
  156. {
  157. g_tem = __ltobf(&CommBuf[3]);;
  158. }
  159. else if (g_readtype == 2)
  160. {
  161. g_pressure = __ltobf(&CommBuf[3]);;
  162. g_tem = __ltobf(&CommBuf[3+4]);;
  163. }
  164. memset(CommBuf, 0, 128);
  165. CommBufLen = 0;
  166. //float a;
  167. //a = __ltobf();
  168. //printf("%0.4f \n", a);
  169. PostMessage(theApp.GetMainWnd()->GetSafeHwnd(), WM_DATARECEIVED, 0, 0);
  170. return true;
  171. }
  172. void ComHandle::linkRichEdit(CRichEditCtrl* pRichEdit)
  173. {
  174. m_pRichEdit = pRichEdit;
  175. }
  176. int ComHandle::senddata(const BYTE*buf, DWORD bufLen)
  177. {
  178. if (!m_pSerial)
  179. {
  180. addText("串口未成功打开");
  181. return false;
  182. }
  183. int nRc = m_pSerial->WriteSyncPort(buf, bufLen);
  184. if (nRc)
  185. {
  186. CString strS = "";
  187. for (int i = 0; i < bufLen; ++i)
  188. {
  189. CString strW;
  190. CStringA str;
  191. strW.Format(_T("%.2x"), buf[i]);
  192. strS += strW + " ";
  193. }
  194. addText(getCurrentTime() + string(" 发送:") + strS.GetString());
  195. }
  196. else
  197. {
  198. addText("发送数据失败");
  199. }
  200. return nRc;
  201. }