Help for serial communication code

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2009
Posts: 10
Reputation: nicholasamh is an unknown quantity at this point 
Solved Threads: 0
nicholasamh nicholasamh is offline Offline
Newbie Poster

Help for serial communication code

 
0
  #1
Aug 25th, 2009
Hi, I am using Visual studio 2008, C++ MFC to do my programming for serial communication. I am having problem with my source code wehn i complied it. I have link the serial.cpp and serial.h file to my main program. In my main program, i just type Serial.Open(); to test my serial communication. I have initialise [CSerial Serial] in my .h(main program file).
When i run my main program, i get error C2512:"CSerial":no appropriate default constructor available. Hope someone can help me out. Thanks.

Serial.cpp
--------------
  1. // Serial.cpp: implementation of the CSerial class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include <string.h>
  6. #include "Serial.h"
  7. #include <sstream>
  8.  
  9. //////////////////////////////////////////////////////////////////////
  10. // Construction/Destruction
  11. //////////////////////////////////////////////////////////////////////
  12.  
  13. CSerial::CSerial(const char *pszPortName)
  14. : m_hSerialComm(INVALID_HANDLE_VALUE)
  15. {
  16. ASSERT(pszPortName);
  17.  
  18. m_pszPortName = new char[strlen(pszPortName)];
  19. strcpy(m_pszPortName, pszPortName);
  20. }
  21.  
  22. CSerial::~CSerial()
  23. {
  24. if(m_pszPortName)
  25. delete[] m_pszPortName;
  26.  
  27. Close();
  28. }
  29.  
  30. //////////////////////////////////////////////////////////////////////
  31. // Name: Open
  32. // Version: 1.0
  33. // Return: HRESULT
  34. // Comment: This function is used open a connection with a serial port.
  35. // Uses non-overlapped i/o, and allows for reading & writing to the
  36. // port.
  37. //////////////////////////////////////////////////////////////////////
  38.  
  39. HRESULT CSerial::Open()
  40. {
  41. HRESULT hResult;
  42.  
  43. m_hSerialComm = CreateFile((LPCTSTR)m_pszPortName, /* Port Name */
  44. GENERIC_READ | GENERIC_WRITE, /* Desired Access */
  45. 0, /* Shared Mode */
  46. NULL, /* Security */
  47. OPEN_EXISTING, /* Creation Disposition */
  48. 0,
  49. NULL); /* Non Overlapped */
  50.  
  51. if(m_hSerialComm == INVALID_HANDLE_VALUE)
  52. {
  53. unsigned long error = ::GetLastError();
  54. hResult = E_FAIL;
  55. }
  56.  
  57. else
  58. hResult = S_OK;
  59.  
  60. return hResult;
  61. }
  62.  
  63. //////////////////////////////////////////////////////////////////////
  64. // Name: Close
  65. // Version: 1.0
  66. // Return: HRESULT
  67. // Comment: This function is used to close the serial port connection
  68. // Note: This function is called with the destructor
  69. //////////////////////////////////////////////////////////////////////
  70.  
  71. HRESULT CSerial::Close()
  72. {
  73. if(m_hSerialComm != INVALID_HANDLE_VALUE)
  74. {
  75. CloseHandle(m_hSerialComm);
  76. m_hSerialComm = INVALID_HANDLE_VALUE;
  77. }
  78.  
  79. return S_OK;
  80. }
  81.  
  82. //////////////////////////////////////////////////////////////////////
  83. // Name: ConfigPort
  84. // Version: 1.0
  85. // Parameter: dwBaudRate - This must be set to the baud rate of the
  86. // serial port connection otherwise invalid reads occur.
  87. // dwTimeOutInSec - Specifies the timeout for read and write of the serial
  88. // port connection in seconds
  89. // Return: HRESULT
  90. // Comment: This function is used configure the serial port connection.
  91. //////////////////////////////////////////////////////////////////////
  92.  
  93. HRESULT CSerial::ConfigPort(DWORD dwBaudRate, DWORD dwTimeOutInSec)
  94. {
  95. if(!SetupComm(m_hSerialComm, 1024, 1024))
  96. return E_FAIL;
  97.  
  98. DCB dcbConfig;
  99.  
  100. if(GetCommState(m_hSerialComm, &dcbConfig)) /* Configuring Serial Port Settings */
  101. {
  102. dcbConfig.BaudRate = dwBaudRate;
  103. dcbConfig.ByteSize = 8;
  104. dcbConfig.Parity = NOPARITY;
  105. dcbConfig.StopBits = ONESTOPBIT;
  106. dcbConfig.fBinary = TRUE;
  107. dcbConfig.fParity = TRUE;
  108. }
  109.  
  110. else
  111. return E_FAIL;
  112.  
  113. if(!SetCommState(m_hSerialComm, &dcbConfig))
  114. return E_FAIL;
  115.  
  116. COMMTIMEOUTS commTimeout;
  117.  
  118. if(GetCommTimeouts(m_hSerialComm, &commTimeout)) /* Configuring Read & Write Time Outs */
  119. {
  120. commTimeout.ReadIntervalTimeout = 1000*dwTimeOutInSec;
  121. commTimeout.ReadTotalTimeoutConstant = 1000*dwTimeOutInSec;
  122. commTimeout.ReadTotalTimeoutMultiplier = 0;
  123. commTimeout.WriteTotalTimeoutConstant = 1000*dwTimeOutInSec;
  124. commTimeout.WriteTotalTimeoutMultiplier = 0;
  125. }
  126.  
  127. else
  128. return E_FAIL;
  129.  
  130. if(SetCommTimeouts(m_hSerialComm, &commTimeout))
  131. return S_OK;
  132.  
  133. else
  134. return E_FAIL;
  135. }
  136.  
  137. //////////////////////////////////////////////////////////////////////
  138. // Name: Read
  139. // Version: 1.0
  140. // Parameter: ppszBuf - The buffer that will have the value that was
  141. // read in from the serial port.
  142. // dwSize - The size of the buffer
  143. // Return: HRESULT
  144. // Comment: This function sets an event that will be signalled if the
  145. // any byte is buffered internally. Once this occurs, the function keeps
  146. // reading multiple a single byte at a time until there is no more furthur
  147. // byte to read from the input stream
  148. //////////////////////////////////////////////////////////////////////
  149.  
  150. HRESULT CSerial::Read(char **ppszBuf, DWORD &dwSize)
  151. {
  152. HRESULT hResult = S_OK;
  153. std::stringbuf sb;
  154. DWORD dwEventMask;
  155.  
  156. if(!SetCommMask(m_hSerialComm, EV_RXCHAR)) /* Setting Event Type */
  157. return E_FAIL;
  158.  
  159. if(WaitCommEvent(m_hSerialComm, &dwEventMask, NULL)) /* Waiting For Event to Occur */
  160. {
  161. char szBuf;
  162. DWORD dwIncommingReadSize;
  163.  
  164. do
  165. {
  166. if(ReadFile(m_hSerialComm, &szBuf, 1, &dwIncommingReadSize, NULL) != 0)
  167. {
  168. if(dwIncommingReadSize > 0)
  169. {
  170. dwSize += dwIncommingReadSize;
  171. sb.sputn(&szBuf, dwIncommingReadSize);
  172. }
  173. }
  174.  
  175. else
  176. {
  177. unsigned long error = ::GetLastError();
  178. hResult = E_FAIL;
  179. break;
  180. }
  181.  
  182. } while(dwIncommingReadSize > 0);
  183.  
  184. *ppszBuf = new char[dwSize];
  185. strcpy(*ppszBuf, (sb.str()).c_str());
  186.  
  187. return hResult;
  188. }
  189.  
  190. else
  191. return E_FAIL;
  192. }
  193.  
  194. //////////////////////////////////////////////////////////////////////
  195. // Name: Write
  196. // Version: 1.0
  197. // Parameter: szBuf - The buffer holding the bytes to write to the serial
  198. // port connection
  199. // dwSize - The size of the buffer
  200. // Return: HRESULT
  201. // Comment: This function writes one byte at a time until all the bytes
  202. // in the buffer is sent out
  203. //////////////////////////////////////////////////////////////////////
  204.  
  205. HRESULT CSerial::Write(const char *pszBuf, DWORD dwSize)
  206. {
  207. HRESULT hResult = S_OK;
  208.  
  209. ASSERT(pszBuf);
  210.  
  211. unsigned long dwNumberOfBytesSent = 0;
  212.  
  213. while(dwNumberOfBytesSent < dwSize)
  214. {
  215. unsigned long dwNumberOfBytesWritten;
  216.  
  217. if(WriteFile(m_hSerialComm, &pszBuf[dwNumberOfBytesSent], 1, &dwNumberOfBytesWritten, NULL) != 0)
  218. {
  219. if(dwNumberOfBytesWritten > 0)
  220. ++dwNumberOfBytesSent;
  221. else
  222. {
  223. unsigned long error = ::GetLastError();
  224. hResult = E_FAIL;
  225. break;
  226. }
  227. }
  228.  
  229. else
  230. {
  231. unsigned long error = ::GetLastError();
  232. hResult = E_FAIL;
  233. break;
  234. }
  235. }
  236.  
  237. return hResult;
  238. }
  239.  
  240. //////////////////////////////////////////////////////////////////////
  241. // Name: Flush
  242. // Version: 1.0
  243. // Parameter: dwFlag - The flag specifying if the input/output buffer
  244. // to be flushed
  245. // Return: HRESULT
  246. // Comment: This function is flushes the specfied buffer
  247. // Note: By default, both the input and output buffers are flushed
  248. //////////////////////////////////////////////////////////////////////
  249.  
  250. HRESULT CSerial::Flush(DWORD dwFlag)
  251. {
  252. if(PurgeComm(m_hSerialComm, dwFlag))
  253. return S_OK;
  254. else
  255. return E_FAIL;
  256. }
  257.  
  258.  
  259. Serial.h
  260. -----------
  261. // Serial.h: interface for the CSerial class.
  262. //
  263. //////////////////////////////////////////////////////////////////////
  264. #include <windows.h>
  265.  
  266. #if !defined(AFX_Serial_H__D1CAB621_DF4B_4729_82AB_31D5B9EFE8A9__INCLUDED_)
  267. #define AFX_Serial_H__D1CAB621_DF4B_4729_82AB_31D5B9EFE8A9__INCLUDED_
  268.  
  269. #if _MSC_VER > 1000
  270. #pragma once
  271. #endif // _MSC_VER > 1000
  272.  
  273. //////////////////////////////////////////////////////////////////////
  274. // Name: CSerial
  275. // Version: 1.0
  276. // Comment: This class is responsible for provide I/O operation with
  277. // a serial port. It is implemented with Synchronous I/O viz. both
  278. // input and output operations block. Both read and write operations
  279. // are supported.
  280. //////////////////////////////////////////////////////////////////////
  281.  
  282. class CSerial
  283. {
  284. public:
  285. HRESULT Flush(DWORD dwFlag = PURGE_TXCLEAR | PURGE_RXCLEAR);
  286. HRESULT Write(const char *pszBuf, DWORD dwSize);
  287. HRESULT Read(char **ppszBuf, DWORD &dwSize);
  288. HRESULT ConfigPort(DWORD dwBaudRate = CBR_19200, DWORD dwTimeOutInSec = 5);
  289. HRESULT Close();
  290. HRESULT Open();
  291.  
  292.  
  293. CSerial(const char *pszPortName);
  294. virtual ~CSerial();
  295.  
  296. private:
  297. char *m_pszPortName;
  298. HANDLE m_hSerialComm;
  299. };
  300.  
  301. #endif // !defined(AFX_Serial_H__D1CAB621_DF4B_4729_82AB_31D5B9EFE8A9__INCLUDED_)
Last edited by John A; Aug 26th, 2009 at 12:41 am. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 32
Reputation: vali82 is an unknown quantity at this point 
Solved Threads: 4
vali82 vali82 is offline Offline
Light Poster

Re: Help for serial communication code

 
0
  #2
Aug 25th, 2009
Hi,

You probably have something like:
  1. CSerial serial;
  2. serial.Open();

And you really do not have a default constructor(no param constructor); so you can't create objects like that.
You need to use the constructor you do have:
  1. CSerial serial("port name goes here");
  2. serial.Open();
Last edited by vali82; Aug 25th, 2009 at 4:59 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC