123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- #include "stdafx.h"
- #include "ComHandle.h"
- #include "global.h"
- #include <string>
- #include "ReadSensor.h"
- using namespace std;
- typedef union
- {
- float f;
- char c[4];
- } UNION_FLOAT_CONV_TypeDef;
- float __ltobf(BYTE* pc)
- {
- UNION_FLOAT_CONV_TypeDef d1, d2;
- d1.c[0] = *pc;
- d1.c[1] = *(pc + 1);
- d1.c[2] = *(pc + 2);
- d1.c[3] = *(pc + 3);
- //d1.c[0] = 0x41;
- //d1.c[1] = 0xcc;
- //d1.c[2] = 0x00;
- //d1.c[3] = 0x00;
- d2.c[0] = d1.c[3];
- d2.c[1] = d1.c[2];
- d2.c[2] = d1.c[1];
- d2.c[3] = d1.c[0];
- return d2.f;
- }
- extern float g_pressure;
- extern float g_tem;
- extern int g_readtype;
- ComHandle::ComHandle()
- {
- }
- int ComHandle::opencom(int portno)
- {
- closecom();
- if (portno > 0)
- {
- //新建串口通讯对象
- m_pSerial = new CMyCESeries();
- m_pSerial->m_OnSeriesRead = OnComRead;
- //m_nPortNo = portNo;
- int rtn = m_pSerial->OpenPort(this, portno,19200);
- //打开串口
- if (rtn)
- {
- addText("打开串口" + std::to_string(portno) + "成功");
- }
- else
- {
- addText("打开串口" + std::to_string(portno) + "失败");
- if (m_pSerial)
- {
- delete m_pSerial;
- m_pSerial = NULL;
- }
- }
- return rtn;
- }
- else
- return false;
- }
- int ComHandle::closecom()
- {
- if (m_pSerial)
- {
- m_pSerial->ClosePort();
- delete m_pSerial;
- m_pSerial = NULL;
- return true;
- }
- return false;
- }
- int ComHandle::senddata()
- {
- //int nRc = m_pSerial->WriteSyncPort(buff, len);
- //return nRc;
- return false;
- }
- void ComHandle::addText(string str)
- {
- if (m_pRichEdit)
- {
- m_pRichEdit->SetSel(-1, -1);
- m_pRichEdit->ReplaceSel((str+ string("\r\n")).c_str());
- m_pRichEdit->PostMessage(WM_VSCROLL, SB_BOTTOM, 0);
- }
- }
- bool ComHandle::ProcessComData(BYTE* buf, DWORD bufLen)
- {
- int datalen = 0;
- if (g_readtype == 0)
- {
- datalen = 9;
- }
- else if (g_readtype == 1)
- {
- datalen = 9;
- }
- else if (g_readtype == 2)
- {
- datalen = 13;
- }
- else if (g_readtype == 10)
- {
- datalen = 8;
- }
- if (bufLen > 13 || CommBufLen > 13)
- {
- addText("收到数据长度过长,弃用");
- memset(CommBuf, 0, 128);
- CommBufLen = 0;
- return false;
- }
- CString strS = "";
- for (int i = 0; i < bufLen; ++i)
- {
- CString strW;
- CStringA str;
- strW.Format(_T("%.2x"), buf[i]);
- strS += strW + " ";
- }
- CString str;
- str.Format("%s 收到数据:%s", getCurrentTime().c_str(), strS);
- addText(str.GetString());
- if (CommBufLen == 0)
- {
- memcpy(CommBuf, buf, bufLen);
- CommBufLen = bufLen;
- }
- else
- {
- memcpy(CommBuf + CommBufLen, buf, bufLen);
- CommBufLen += bufLen;
- }
- if (CommBufLen != datalen)
- {
- return false;
- }
- strS = "";
- for (int i = 0; i < CommBufLen; ++i)
- {
- CString strW;
- CStringA str;
- strW.Format(_T("%.2x"), CommBuf[i]);
- strS += strW + " ";
- }
-
- str.Format("%s 收到数据合并:%s", getCurrentTime().c_str(), strS);
- addText(str.GetString());
- if (g_readtype == 0)
- {
- g_pressure = __ltobf(&CommBuf[3]);;
- }
- else if (g_readtype == 1)
- {
- g_tem = __ltobf(&CommBuf[3]);;
- }
- else if (g_readtype == 2)
- {
- g_pressure = __ltobf(&CommBuf[3]);;
- g_tem = __ltobf(&CommBuf[3+4]);;
- }
- memset(CommBuf, 0, 128);
- CommBufLen = 0;
- //float a;
- //a = __ltobf();
- //printf("%0.4f \n", a);
- PostMessage(theApp.GetMainWnd()->GetSafeHwnd(), WM_DATARECEIVED, 0, 0);
- return true;
- }
- void ComHandle::linkRichEdit(CRichEditCtrl* pRichEdit)
- {
- m_pRichEdit = pRichEdit;
- }
- int ComHandle::senddata(const BYTE*buf, DWORD bufLen)
- {
- if (!m_pSerial)
- {
- addText("串口未成功打开");
- return false;
- }
- int nRc = m_pSerial->WriteSyncPort(buf, bufLen);
- if (nRc)
- {
- CString strS = "";
- for (int i = 0; i < bufLen; ++i)
- {
- CString strW;
- CStringA str;
- strW.Format(_T("%.2x"), buf[i]);
- strS += strW + " ";
- }
- addText(getCurrentTime() + string(" 发送:") + strS.GetString());
- }
- else
- {
- addText("发送数据失败");
- }
- return nRc;
- }
|