membervariabel übergeben



  • Hallo zusammen

    Ich habe folgendes Problem:

    Ich hab die Klasse CSerialport:(Hab alle funktionen ausser dem Konstruktor und Dekonstruktor weggelassen)

    In dieser Klasse habe ich die membervariabel "BOOL m_isDebugModeEnabled"

    class CSerialPort
    {
    public:
    
    	/*
    	 * Constructor
    	 */
    	CSerialPort ();
    
    	/*
    	 * Deconstructor
    	 */
    	~CSerialPort ();
    
    private:
    	HANDLE  hComm;
    
    	BOOL m_isDebugModeEnabled;
    };
    
    #endif
    

    Im main() würde ich nun dieser Variabel den wert "true" zuweisen. Nun weis ich leider nicht wie man dies am besten macht.

    Hoffe ihr könnt mir helfen.

    Danke und Grüsse TTS



  • class SerialPort {
    public:
        SerialPort( BOOL debugEnabled )
         : m_isDebugModeEnabled(debugEnabled)
        {}
    
        void setDebugEnabled(BOOL debugEnabled) {
            m_isDebugModeEnabled = debugEnabled;
        }
    };
    

    Oder was meinst du?
    Das an ungarische Notation erinnernde "C"-Präfix lass bitte weg, ist überflüssig wie ein Kropf. Und warum eigentlich BOOL? C++ hat doch den eingebauten Typ "bool" für sowas...



  • Danke l'abra d'or

    Ich weiss zwar was du mit deinem Code meinst, jedoch treten noch Fehler auf:

    Compiling...
    CFlukeReader.cpp
    CSerialPort.cpp
    c:\documents and settings\chtosch1\my documents\fluke45\fluke_45_reader\cserialport.cpp(4) : error C2511: 'CSerialPort::CSerialPort' : overloaded member function 'void (void)' not found in 'CSerialPort'
    c:\documents and settings\chtosch1\my documents\fluke45\fluke_45_reader\cserialport.h(27) : see declaration of 'CSerialPort'
    c:\documents and settings\chtosch1\my documents\fluke45\fluke_45_reader\cserialport.cpp(147) : fatal error C1004: unexpected end of file found
    DateTime.cpp
    main.cpp
    Error executing cl.exe.
    Creating browse info file...
    BSCMAKE: error BK1506 : cannot open file '.\Debug\CSerialPort.sbr': No such file or directory
    Error executing bscmake.exe.

    Fluke_45_Reader.exe - 3 error(s), 0 warning(s)

    Das mit der Ungarischen Notation und dem BOOL statt bool wurde mir von der Firma vorgegeben. Daran kann ich nichts ändern. Ausserdem finde ich diese Art zu schreiben noch recht leserlich und gut verständlich.



  • Deklaration und Definition der Funktionen (Konstruktor eingeschlossen) müssen natürlich zusammenpassen. Ich nehme stark an, du hast nur in der .cpp rumeditiert, aber nicht in der .h.



  • Ach ja natürlich, das habe ich total vergessen ^^

    jedoch habe ich nur im .h file editiert.

    Habs nun geändert, nun läuft gar nichts mehr.

    Hier die beiden Files komplett und die Fehlermeldungen. Ich bin echt am verzweifeln. Vor einigen Tagen hat noch alles funktioniert.

    --------------------Configuration: Fluke_45_Reader - Win32 Debug--------------------
    Compiling...
    CSerialPort.cpp
    C:\Documents and Settings\chtosch1\My Documents\Fluke45\Fluke_45_Reader\CSerialPort.cpp(4) : error C2084: function '__thiscall CSerialPort::CSerialPort(int)' already has a body
    Error executing cl.exe.
    Creating browse info file...

    Fluke_45_Reader.exe - 1 error(s), 0 warning(s)

    // CSerialPort.cpp : Defines the entry point for the console application.
    //
    #include <windows.h>
    #include <AFX.h>
    #include <stdio.h>
    #include <time.h>
    #include <iostream>
    using std::cin;
    #include <fstream>
    #include <assert.h>
    #include <string>
    #include <direct.h>
    
    using namespace std;
    
    //defs
    #define BUFFER_SIZE						255
    #define FLUKE45_START_CHAR					0x68
    #define FLUKE45_END_CHAR					0x16
    #define DEFAULT_BAUDRATE					9600
    
    #ifndef __FLUKE45_H__
    #define __FLUKE45_H__
    
    class CSerialPort
    {
    public:
    
    	/*
    	 * Constructor
    	 */
    	CSerialPort (BOOL debugEnabled):
    	  m_isDebugModeEnabled(debugEnabled)
    
    	{};
    
    	/*
    	 * Deconstructor
    	 */
    	~CSerialPort ();
    
    	/*
    	 * Open() opens the COM port and configures it with the specified parameters.
    	 *
    	 * @param [in] comPortNumber: number of COM port
    	 * @param [in] baudRate     : baud rate [bit/s]
    	 * @return     TRUE, if COM port has been successfully opened and configured
    	 *             FALSE, otherwise
    	 */
    	BOOL Open (int comPortNumber, int baudRate);
    
    	/*
    	 * Close() closes the COM port
    	 *
    	 * @return TRUE, if COM port has been closed successfully
    	 *         FALSE, otherwise
    	 */
    	BOOL Close (void);
    
        /*
    	 * ReceiveData() receives receives bytes from COM port and writes them to receiveBuffer.
    	 * The function returns if bytesToReceive bytes have been received or a timeout occured.
    	 *
    	 * @param [in]  bytesToReceive: number of bytes which are received before the function returns
    	 * @param [out] rxBuffer      : buffer where the received bytes are written to
    	 * @param [out] bytesReceived : number of bytes which have been received
    	 * @return      TRUE, if bytesToReceive bytes have been received
    	 *              FALSE, otherwise
    	 */
    	BOOL ReceiveData(int bytesToReceive, char* rxBuffer, unsigned long* bytesReceived, BOOL m_isDebugModeEnabled);
    
    	/*
    	 * SendData() sends bytes from the txBuffer to the COM port
    	 * The function returns if all bytes have been sent or an error occured
    	 *
    	 * @param [in]  bytesToSend: number of bytes which are sent before the function returns
    	 * @param [in]  txBuffer   : buffer, where the bytes to send are stored
    	 * @param [out] bytesSent  : number of bytes, wich have been sent
    	 * @return		TRUE,  if bytesToSend have been sent
    	 *				FALSE, otherwise
    	 */
    	BOOL SendData(int bytesToSend, char* txBuffer, unsigned long* bytesSent, BOOL m_isDebugModeEnabled);
    
    	/*
    	 * SetDebugMode() if IsEnabled is true the function activates the devug mode
    	 * 
    	 * @param [in]  IsEnabled	  : If the Debug Mode is enabled over the arguments IsEnabled = true
    	 * @param [in]  recDataArray  : Buffer, where the messages from the Fluke are stored.
    	 * @param [in]	MessageToFluke: Buffer, where the messages to the Fluke are stored.
    	 * @param [in]	timeStamp	  : Variable, where the timeStamp is stored.
    	 *				
    	 */
        void SetDebugMode(BOOL debugEnabled);
    
    private:
    	HANDLE  hComm;
    
    	BOOL m_isDebugModeEnabled;
    };
    
    #endif
    
    #include "CSerialPort.h"
    #include "windows.h"
    //-----------------------------------------------------------------------------
    CSerialPort::CSerialPort(BOOL debugEnabled):
    	m_isDebugModeEnabled(debugEnabled)
    {
    }
    
    //-----------------------------------------------------------------------------
    CSerialPort::~CSerialPort()
    {
    	Close ();
    }
    
    //-----------------------------------------------------------------------------
    BOOL CSerialPort::Open(int comPortNumber, int baudRate)
    {
    	char szPort[16];
    	DCB dcb;
    	COMMTIMEOUTS timeouts;
    
    	if (hComm != INVALID_HANDLE_VALUE)
    	{
    		return (TRUE);
    	}
    
        // open COM port
    	wsprintf (szPort, "\\\\.\\COM%d", comPortNumber);
    	hComm = CreateFile (szPort,
    						GENERIC_READ | GENERIC_WRITE,
    						FILE_SHARE_READ | FILE_SHARE_WRITE,
    						0,
    						OPEN_EXISTING,
    						FILE_ATTRIBUTE_NORMAL,
    						NULL);
    	if (hComm == INVALID_HANDLE_VALUE)
    		return (FALSE);
    
    	// set timeout
    	if (!GetCommTimeouts(hComm, &timeouts))
    	{
    		Close ();
    		return (FALSE);
    	}
    	timeouts.ReadIntervalTimeout         = 0 ;   // Specifies the maximum time, allowed to elapse between the arrival of two characters on the communications line. 
    	timeouts.ReadTotalTimeoutMultiplier  = 1 ;   // For each read operation, this value is multiplied by the requested number of bytes to be read. used to calculate the total time-out period for read operations.
    	timeouts.ReadTotalTimeoutConstant    = 3000; // For each read operation, this value is added to the product of the ReadTotalTimeoutMultiplier member and the requested number of bytes. 
    	timeouts.WriteTotalTimeoutMultiplier = 1000; // For each write operation, this value is multiplied by the number of bytes to be written. used to calculate the total time-out period for write operations.
    	timeouts.WriteTotalTimeoutConstant   = 1000; // For each write operation, this value is added to the product of the WriteTotalTimeoutMultiplier member and the number of bytes to be written. Used to calculate the total time-out period for write operations. 
    	if (!SetCommTimeouts(hComm, &timeouts))
    	{
    		Close ();
    		return (FALSE);
    	}
    
        // configure COM port	
    	ZeroMemory (&dcb, sizeof(dcb));
    	if (!GetCommState (hComm, &dcb))
    	{
    		Close ();
    		return (FALSE);
    	}
    
    	dcb.BaudRate     = baudRate;		// baud rate 
    	dcb.fBinary      = TRUE;			// must be true
    	dcb.fParity      = FALSE;			// no parity bit
    	dcb.fOutxCtsFlow = FALSE;			// monitor clear to send
    	dcb.ByteSize     = (byte)8;         // number of bits
    	dcb.StopBits = ONESTOPBIT;			// one stop bit
    
    	if (!SetCommState(hComm, &dcb))
    	{
    		Close ();
    		return (FALSE);
    	}
    
    	return(TRUE);
    }
    
    //-----------------------------------------------------------------------------
    BOOL CSerialPort::Close (void)
    {
    	BOOL bResult;
    
    	if(hComm == INVALID_HANDLE_VALUE)
    	{
    		return (TRUE);
    	}
    
    	// close COM port
    	bResult = CloseHandle(hComm);
    	hComm   = INVALID_HANDLE_VALUE;
    
    	return(bResult);
    }
    
    //-----------------------------------------------------------------------------
    BOOL CSerialPort::SendData (int bytesToSend, char* txBuffer, unsigned long* bytesSent, BOOL debugEnabled)
    { 
    	BOOL sendState;
    	m_isDebugModeEnabled = debugEnabled;
    	if (hComm == INVALID_HANDLE_VALUE)
    	{
    		return (FALSE);
    	}
    
    	sendState = WriteFile(hComm, txBuffer, bytesToSend, bytesSent, NULL);
    
    	if (m_isDebugModeEnabled = 1)
    	{
    		cout<<"   " << "Pc --> Fluke : " << txBuffer << endl;
    	}
    	 /*<<timeStamp */ 
    	return (sendState);
    }
    
    //-----------------------------------------------------------------------------
    BOOL CSerialPort::ReceiveData (int bytesToReceive, char *rxBuffer, unsigned long* bytesReceived, BOOL debugEnabled)
    {
    	BOOL readState;
    	m_isDebugModeEnabled = debugEnabled;
    	if (hComm == INVALID_HANDLE_VALUE)
    	{
    		return (FALSE);
    	}
    
    	readState = ReadFile(hComm, rxBuffer, bytesToReceive, bytesReceived, NULL);
    
    	if (m_isDebugModeEnabled = 1)
    	{
    		cout <<"   " << "Fluke --> PC : " << rxBuffer << endl;
    	}
    	/*<<timeStamp */
    	return (readState);
    }
    
    //-----------------------------------------------------------------------------
    void CSerialPort::SetDebugMode(BOOL debugEnabled)
    {
    	m_isDebugModeEnabled = debugEnabled;
    }
    


  • CSerialPort (BOOL debugEnabled): 
          m_isDebugModeEnabled(debugEnabled) 
    
        {};
    

    Funktion hat im header schon den Body und bekommt in cpp nochmals einen. Das kann nicht gehen.

    EDIT: Entweder Forwärtsdeklarieren oder in cpp entfernen.



  • Hmmm... Deine Firma bezahlt dich als C++-Entwickler? Interessant.
    Google mal nach "C++ odr" und schau, was du in Header und .cpp treibst.



  • l'abra d'or schrieb:

    Hmmm... Deine Firma bezahlt dich als C++-Entwickler? Interessant.

    Ich bin noch in der Ausbildung 😃


Anmelden zum Antworten