Advertisement
andruhovski

Untitled

Apr 10th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "BalanceDevice.h"
  3.  
  4.  
  5. BalanceDevice::BalanceDevice()
  6. {
  7. }
  8.  
  9.  
  10. BalanceDevice::~BalanceDevice()
  11. {
  12. }
  13.  
  14. BalanceDevice::BalanceDevice(TCHAR* pszPortName) : m_hSerialComm(INVALID_HANDLE_VALUE)
  15. {
  16. //ASSERT(pszPortName);
  17. m_pszPortName = _tcsdup (pszPortName);
  18. }
  19.  
  20.  
  21. HRESULT BalanceDevice::Open()
  22. {
  23. HRESULT hResult;
  24. m_hSerialComm = CreateFile(m_pszPortName, /* Port Name */
  25. GENERIC_READ | GENERIC_WRITE, /* Desired Access */
  26. 0, /* Shared Mode */
  27. NULL, /* Security */
  28. OPEN_EXISTING, /* Creation Disposition */
  29. 0,
  30. NULL); /* Non Overlapped */
  31.  
  32. if (m_hSerialComm == INVALID_HANDLE_VALUE)
  33. {
  34. unsigned long error = ::GetLastError();
  35. hResult = E_FAIL;
  36. }
  37.  
  38. else
  39. hResult = S_OK;
  40.  
  41. return hResult;
  42. }
  43.  
  44. //////////////////////////////////////////////////////////////////////
  45. // Name: Close
  46. // Version: 1.0
  47. // Return: HRESULT
  48. // Comment: This function is used to close the serial port connection
  49. // Note: This function is called with the destructor
  50. //////////////////////////////////////////////////////////////////////
  51.  
  52. HRESULT BalanceDevice::Close()
  53. {
  54. if (m_hSerialComm != INVALID_HANDLE_VALUE)
  55. {
  56. CloseHandle(m_hSerialComm);
  57. m_hSerialComm = INVALID_HANDLE_VALUE;
  58. }
  59.  
  60. return S_OK;
  61. }
  62.  
  63. //////////////////////////////////////////////////////////////////////
  64. // Name: ConfigPort
  65. // Version: 1.0
  66. // Parameter: dwBaudRate - This must be set to the baud rate of the
  67. // serial port connection otherwise invalid reads occur.
  68. // dwTimeOutInSec - Specifies the timeout for read and write of the serial
  69. // port connection in seconds
  70. // Return: HRESULT
  71. // Comment: This function is used configure the serial port connection.
  72. //////////////////////////////////////////////////////////////////////
  73.  
  74. HRESULT BalanceDevice::ConfigPort(DWORD dwBaudRate, DWORD dwTimeOutInSec)
  75. {
  76. if (!SetupComm(m_hSerialComm, 1024, 1024))
  77. return E_FAIL;
  78.  
  79. DCB dcbConfig;
  80.  
  81. if (GetCommState(m_hSerialComm, &dcbConfig)) /* Configuring Serial Port Settings */
  82. {
  83. dcbConfig.BaudRate = dwBaudRate;
  84. dcbConfig.ByteSize = 8;
  85. dcbConfig.Parity = NOPARITY;
  86. dcbConfig.StopBits = ONESTOPBIT;
  87. dcbConfig.fBinary = TRUE;
  88. dcbConfig.fParity = TRUE;
  89. }
  90.  
  91. else
  92. return E_FAIL;
  93.  
  94. if (!SetCommState(m_hSerialComm, &dcbConfig))
  95. return E_FAIL;
  96.  
  97. COMMTIMEOUTS commTimeout;
  98.  
  99. if (GetCommTimeouts(m_hSerialComm, &commTimeout)) /* Configuring Read & Write Time Outs */
  100. {
  101. commTimeout.ReadIntervalTimeout = 1000 * dwTimeOutInSec;
  102. commTimeout.ReadTotalTimeoutConstant = 1000 * dwTimeOutInSec;
  103. commTimeout.ReadTotalTimeoutMultiplier = 0;
  104. commTimeout.WriteTotalTimeoutConstant = 1000 * dwTimeOutInSec;
  105. commTimeout.WriteTotalTimeoutMultiplier = 0;
  106. }
  107.  
  108. else
  109. return E_FAIL;
  110.  
  111. if (SetCommTimeouts(m_hSerialComm, &commTimeout))
  112. return S_OK;
  113.  
  114. else
  115. return E_FAIL;
  116. }
  117.  
  118. HRESULT BalanceDevice::Read(char **ppszBuf, DWORD &dwSize)
  119. {
  120. HRESULT hResult = S_OK;
  121. std::string sb;
  122.  
  123. DWORD dwEventMask;
  124.  
  125. if (!SetCommMask(m_hSerialComm, EV_RXCHAR)) /* Setting Event Type */
  126. return E_FAIL;
  127.  
  128. if (WaitCommEvent(m_hSerialComm, &dwEventMask, NULL)) /* Waiting For Event to Occur */
  129. {
  130. char szBuf;
  131. DWORD dwIncommingReadSize;
  132.  
  133. do
  134. {
  135. if (ReadFile(m_hSerialComm, &szBuf, 1, &dwIncommingReadSize, NULL) != 0)
  136. {
  137. if (dwIncommingReadSize > 0)
  138. {
  139. dwSize += dwIncommingReadSize;
  140. sb.append(&szBuf, dwIncommingReadSize);
  141. }
  142. }
  143.  
  144. else
  145. {
  146. unsigned long error = ::GetLastError();
  147. hResult = E_FAIL;
  148. break;
  149. }
  150.  
  151. } while (dwIncommingReadSize > 0);
  152.  
  153. *ppszBuf = new char[dwSize];
  154. strcpy(*ppszBuf, sb.c_str());
  155.  
  156. return hResult;
  157. }
  158.  
  159. else
  160. return E_FAIL;
  161. }
  162.  
  163.  
  164.  
  165. HRESULT BalanceDevice::Write(const char *pszBuf, DWORD dwSize)
  166. {
  167. HRESULT hResult = S_OK;
  168.  
  169. //assert(pszBuf);
  170.  
  171. unsigned long dwNumberOfBytesSent = 0;
  172.  
  173. while (dwNumberOfBytesSent < dwSize)
  174. {
  175. unsigned long dwNumberOfBytesWritten;
  176.  
  177. if (WriteFile(m_hSerialComm, &pszBuf[dwNumberOfBytesSent], 1, &dwNumberOfBytesWritten, NULL) != 0)
  178. {
  179. if (dwNumberOfBytesWritten > 0)
  180. ++dwNumberOfBytesSent;
  181. else
  182. {
  183. unsigned long error = ::GetLastError();
  184. hResult = E_FAIL;
  185. break;
  186. }
  187. }
  188.  
  189. else
  190. {
  191. unsigned long error = ::GetLastError();
  192. hResult = E_FAIL;
  193. break;
  194. }
  195. }
  196.  
  197. return hResult;
  198. }
  199.  
  200. //////////////////////////////////////////////////////////////////////
  201. // Name: Flush
  202. // Version: 1.0
  203. // Parameter: dwFlag - The flag specifying if the input/output buffer
  204. // to be flushed
  205. // Return: HRESULT
  206. // Comment: This function is flushes the specfied buffer
  207. // Note: By default, both the input and output buffers are flushed
  208. //////////////////////////////////////////////////////////////////////
  209.  
  210. HRESULT BalanceDevice::Flush(DWORD dwFlag)
  211. {
  212. if (PurgeComm(m_hSerialComm, dwFlag))
  213. return S_OK;
  214. else
  215. return E_FAIL;
  216. }
  217.  
  218.  
  219. http://www.codeguru.com/cpp/i-n/network/serialcommunications/article.php/c5425/Serial-Communication-in-Windows.htm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement