Zeichen ersetzten gibt Errors zurück
-
Hallo Zusammen ich hab folgende Funktion: (ersetzt mir \x0a und \x0d durch Leerzeichen).
void replace(char** buffer, char search, char replacement) { if(buffer == NULL || *buffer == NULL) return; char* str = *buffer; while(*str++ != '\0') { if(*str == search) *str = replacement; } }Wenn ich die Funktion in einem Programm so aufrufe funktioniert es Perfekt:
int main(int argc, const char** argv) { char* test = (char*) malloc(sizeof(char) * 13); strncpy(test, "Servus\x0a\x0dtest\0", 13); // 53 65 72 76 75 73 0a 0d 74 65 73 74 00 replace(&test, '\x0a', ' '); replace(&test, '\x0d', ' '); puts(test); // 53 65 72 76 75 73 20 20 74 65 73 74 00 return 0; }Jedoch wenn ich die Funktion in Mein Programm einbinden will geht gar niichts mehr:
char *rxBuffer // Wert wurde schon früher zugewiesen replace(&rxBuffer, '\x0a', ' '); replace(&rxBuffer, '\x0d', ' '); puts(rxBuffer); // 53 65 72 76 75 73 20 20 74 65 73 74 00Folgende Fehlermeldungen erscheinen:
--------------------Configuration: Fluke_45_Reader - Win32 Debug--------------------
Compiling...
CSerialPort.cpp
Linking...
CSerialPort.obj : error LNK2001: unresolved external symbol "public: void __thiscall CSerialPort::replace(char * *,char,char)" (?replace@CSerialPort@@QAEXPAPADDD@Z)
Debug/Fluke_45_Reader.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Creating browse info file...Fluke_45_Reader.exe - 2 error(s), 0 warning(s)
Ich hoffe ihr könnt mir helfen, bin schon den ganzen Morgen am gleichen Problem. Weis echt nicht mehr weiter.
Danke
-
[Murks]
Hast du die .cpp in´s Projekt aufgenommen?
Ist sie jetzt vielleicht member der Klasse CSerialPort?
-
DocShoe schrieb:
[Murks]
Hast du die .cpp in´s Projekt aufgenommen?
Ist sie jetzt vielleicht member der Klasse CSerialPort?Ja die .cpp habe ich ins Projekt aufgenommen, dieses File existiert schon länger
Ja, die Funktion replace ist member der Klasse CSerialPort.
-
Dann poste doch mal bitte die .h und .cpp Datei.
-
CSerialPort.h
// 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): hComm(INVALID_HANDLE_VALUE) {}; /* * 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); /* * 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); /* * SetDebugMode() if IsEnabled is true the function activates the debug 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); void replace(char** buffer, char search, char replacement); private: HANDLE hComm; BOOL m_isDebugModeEnabled; }; #endifCSerialPort.cpp
#include "CSerialPort.h" #include "DateTime.h" #include "windows.h" #include <afx.h> #include <vector> #include <algorithm> #include <string.h> //----------------------------------------------------------------------------- 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 sendState; 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 readState; if (hComm == INVALID_HANDLE_VALUE) { return (FALSE); } readState = ReadFile(hComm,rxBuffer , bytesToReceive, bytesReceived, NULL); if (m_isDebugModeEnabled == 1) { replace(&rxBuffer, '\x0a', ' '); replace(&rxBuffer, '\x0d', ' '); puts(rxBuffer); // 53 65 72 76 75 73 20 20 74 65 73 74 00 } cout <<" " << "Fluke --> PC : " << endl; /*<<timeStamp */ return (readState); } //----------------------------------------------------------------------------- void CSerialPort::SetDebugMode(BOOL debugEnabled) { m_isDebugModeEnabled = debugEnabled; } //----------------------------------------------------------------------------- void replace(char** buffer, char search, char replacement) { if(buffer == NULL || *buffer == NULL) return; char* str = *buffer; while(*str++ != '\0') { if(*str == search) *str = replacement; } }
-
Danke für die Hilfe, hat sich erledigt.
im CPP File, wo die Funktion implementiert wurde habe ich vergessen zu schreiben "CSerialPort::replace"