| | |
Help for serial communication code
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2009
Posts: 10
Reputation:
Solved Threads: 0
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
--------------
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
--------------
C++ Syntax (Toggle Plain Text)
// Serial.cpp: implementation of the CSerial class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include <string.h> #include "Serial.h" #include <sstream> ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CSerial::CSerial(const char *pszPortName) : m_hSerialComm(INVALID_HANDLE_VALUE) { ASSERT(pszPortName); m_pszPortName = new char[strlen(pszPortName)]; strcpy(m_pszPortName, pszPortName); } CSerial::~CSerial() { if(m_pszPortName) delete[] m_pszPortName; Close(); } ////////////////////////////////////////////////////////////////////// // Name: Open // Version: 1.0 // Return: HRESULT // Comment: This function is used open a connection with a serial port. // Uses non-overlapped i/o, and allows for reading & writing to the // port. ////////////////////////////////////////////////////////////////////// HRESULT CSerial::Open() { HRESULT hResult; m_hSerialComm = CreateFile((LPCTSTR)m_pszPortName, /* Port Name */ GENERIC_READ | GENERIC_WRITE, /* Desired Access */ 0, /* Shared Mode */ NULL, /* Security */ OPEN_EXISTING, /* Creation Disposition */ 0, NULL); /* Non Overlapped */ if(m_hSerialComm == INVALID_HANDLE_VALUE) { unsigned long error = ::GetLastError(); hResult = E_FAIL; } else hResult = S_OK; return hResult; } ////////////////////////////////////////////////////////////////////// // Name: Close // Version: 1.0 // Return: HRESULT // Comment: This function is used to close the serial port connection // Note: This function is called with the destructor ////////////////////////////////////////////////////////////////////// HRESULT CSerial::Close() { if(m_hSerialComm != INVALID_HANDLE_VALUE) { CloseHandle(m_hSerialComm); m_hSerialComm = INVALID_HANDLE_VALUE; } return S_OK; } ////////////////////////////////////////////////////////////////////// // Name: ConfigPort // Version: 1.0 // Parameter: dwBaudRate - This must be set to the baud rate of the // serial port connection otherwise invalid reads occur. // dwTimeOutInSec - Specifies the timeout for read and write of the serial // port connection in seconds // Return: HRESULT // Comment: This function is used configure the serial port connection. ////////////////////////////////////////////////////////////////////// HRESULT CSerial::ConfigPort(DWORD dwBaudRate, DWORD dwTimeOutInSec) { if(!SetupComm(m_hSerialComm, 1024, 1024)) return E_FAIL; DCB dcbConfig; if(GetCommState(m_hSerialComm, &dcbConfig)) /* Configuring Serial Port Settings */ { dcbConfig.BaudRate = dwBaudRate; dcbConfig.ByteSize = 8; dcbConfig.Parity = NOPARITY; dcbConfig.StopBits = ONESTOPBIT; dcbConfig.fBinary = TRUE; dcbConfig.fParity = TRUE; } else return E_FAIL; if(!SetCommState(m_hSerialComm, &dcbConfig)) return E_FAIL; COMMTIMEOUTS commTimeout; if(GetCommTimeouts(m_hSerialComm, &commTimeout)) /* Configuring Read & Write Time Outs */ { commTimeout.ReadIntervalTimeout = 1000*dwTimeOutInSec; commTimeout.ReadTotalTimeoutConstant = 1000*dwTimeOutInSec; commTimeout.ReadTotalTimeoutMultiplier = 0; commTimeout.WriteTotalTimeoutConstant = 1000*dwTimeOutInSec; commTimeout.WriteTotalTimeoutMultiplier = 0; } else return E_FAIL; if(SetCommTimeouts(m_hSerialComm, &commTimeout)) return S_OK; else return E_FAIL; } ////////////////////////////////////////////////////////////////////// // Name: Read // Version: 1.0 // Parameter: ppszBuf - The buffer that will have the value that was // read in from the serial port. // dwSize - The size of the buffer // Return: HRESULT // Comment: This function sets an event that will be signalled if the // any byte is buffered internally. Once this occurs, the function keeps // reading multiple a single byte at a time until there is no more furthur // byte to read from the input stream ////////////////////////////////////////////////////////////////////// HRESULT CSerial::Read(char **ppszBuf, DWORD &dwSize) { HRESULT hResult = S_OK; std::stringbuf sb; DWORD dwEventMask; if(!SetCommMask(m_hSerialComm, EV_RXCHAR)) /* Setting Event Type */ return E_FAIL; if(WaitCommEvent(m_hSerialComm, &dwEventMask, NULL)) /* Waiting For Event to Occur */ { char szBuf; DWORD dwIncommingReadSize; do { if(ReadFile(m_hSerialComm, &szBuf, 1, &dwIncommingReadSize, NULL) != 0) { if(dwIncommingReadSize > 0) { dwSize += dwIncommingReadSize; sb.sputn(&szBuf, dwIncommingReadSize); } } else { unsigned long error = ::GetLastError(); hResult = E_FAIL; break; } } while(dwIncommingReadSize > 0); *ppszBuf = new char[dwSize]; strcpy(*ppszBuf, (sb.str()).c_str()); return hResult; } else return E_FAIL; } ////////////////////////////////////////////////////////////////////// // Name: Write // Version: 1.0 // Parameter: szBuf - The buffer holding the bytes to write to the serial // port connection // dwSize - The size of the buffer // Return: HRESULT // Comment: This function writes one byte at a time until all the bytes // in the buffer is sent out ////////////////////////////////////////////////////////////////////// HRESULT CSerial::Write(const char *pszBuf, DWORD dwSize) { HRESULT hResult = S_OK; ASSERT(pszBuf); unsigned long dwNumberOfBytesSent = 0; while(dwNumberOfBytesSent < dwSize) { unsigned long dwNumberOfBytesWritten; if(WriteFile(m_hSerialComm, &pszBuf[dwNumberOfBytesSent], 1, &dwNumberOfBytesWritten, NULL) != 0) { if(dwNumberOfBytesWritten > 0) ++dwNumberOfBytesSent; else { unsigned long error = ::GetLastError(); hResult = E_FAIL; break; } } else { unsigned long error = ::GetLastError(); hResult = E_FAIL; break; } } return hResult; } ////////////////////////////////////////////////////////////////////// // Name: Flush // Version: 1.0 // Parameter: dwFlag - The flag specifying if the input/output buffer // to be flushed // Return: HRESULT // Comment: This function is flushes the specfied buffer // Note: By default, both the input and output buffers are flushed ////////////////////////////////////////////////////////////////////// HRESULT CSerial::Flush(DWORD dwFlag) { if(PurgeComm(m_hSerialComm, dwFlag)) return S_OK; else return E_FAIL; } Serial.h ----------- // Serial.h: interface for the CSerial class. // ////////////////////////////////////////////////////////////////////// #include <windows.h> #if !defined(AFX_Serial_H__D1CAB621_DF4B_4729_82AB_31D5B9EFE8A9__INCLUDED_) #define AFX_Serial_H__D1CAB621_DF4B_4729_82AB_31D5B9EFE8A9__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 ////////////////////////////////////////////////////////////////////// // Name: CSerial // Version: 1.0 // Comment: This class is responsible for provide I/O operation with // a serial port. It is implemented with Synchronous I/O viz. both // input and output operations block. Both read and write operations // are supported. ////////////////////////////////////////////////////////////////////// class CSerial { public: HRESULT Flush(DWORD dwFlag = PURGE_TXCLEAR | PURGE_RXCLEAR); HRESULT Write(const char *pszBuf, DWORD dwSize); HRESULT Read(char **ppszBuf, DWORD &dwSize); HRESULT ConfigPort(DWORD dwBaudRate = CBR_19200, DWORD dwTimeOutInSec = 5); HRESULT Close(); HRESULT Open(); CSerial(const char *pszPortName); virtual ~CSerial(); private: char *m_pszPortName; HANDLE m_hSerialComm; }; #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
•
•
Join Date: Aug 2009
Posts: 32
Reputation:
Solved Threads: 4
Hi,
You probably have something like:
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:
You probably have something like:
C++ Syntax (Toggle Plain Text)
CSerial serial; 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:
C++ Syntax (Toggle Plain Text)
CSerial serial("port name goes here"); serial.Open();
Last edited by vali82; Aug 25th, 2009 at 4:59 am.
![]() |
Similar Threads
- serial communication RS 232 (C)
- serial communication in C (C)
- Need code for serial port communication using VB6.0 (Visual Basic 4 / 5 / 6)
- Serial communication using DEV CPP IDE 4.9.9.2 (C)
- Serial Communication in .net (VB.NET)
- WIN32 serial communication 'C' code (C)
- Serial communication (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Parsing a string
- Next Thread: Need to know how to configure serial communication to update baudrate, databits, etc
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





