Programación General > C/C++

 Problema con SerialPort.h

(1/2) > >>

Zakro:
Hola, estoy haciendo un pequeño programa para comunicarme a través del puerto serie en Ubuntu. El problema es que aunque hago un include de SerialPort.h parece que el compilador no lo encuentra.

Ejecuto: g++ PruebaPuerto.cpp y la salida del compilador es:

/tmp/cc8wej22.o: In function `main':
PruebaPuerto.cpp:(.text+0x7b): undefined reference to `SerialPort::SerialPort(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
PruebaPuerto.cpp:(.text+0xdf): undefined reference to `SerialPort::Open(SerialPort::BaudRate, SerialPort::CharacterSize, SerialPort::Parity, SerialPort::StopBits, SerialPort::FlowControl)'
PruebaPuerto.cpp:(.text+0xf3): undefined reference to `SerialPort::~SerialPort()'
PruebaPuerto.cpp:(.text+0x209): undefined reference to `SerialPort::Write(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
PruebaPuerto.cpp:(.text+0x241): undefined reference to `SerialPort::Read(std::vector<unsigned char, std::allocator<unsigned char> >&, unsigned int, unsigned int)'
PruebaPuerto.cpp:(.text+0x348): undefined reference to `SerialPort::Close()'
PruebaPuerto.cpp:(.text+0x395): undefined reference to `SerialPort::~SerialPort()'
PruebaPuerto.cpp:(.text+0x3ab): undefined reference to `SerialPort::~SerialPort()'
collect2: ld returned 1 exit status

El código del programa es:


--- Código: C++ ---#include <iostream>#include <SerialPort.h> using namespace std; string entrada = ""; int main(int argc, char **argv){    if (argc<2) {            cerr << "Falta el puerto serie a abrir" << endl;            return 1;    }    SerialPort puerto(argv[1]);    try {        puerto.Open(SerialPort::BAUD_115200,                SerialPort::CHAR_SIZE_8,                SerialPort::PARITY_NONE,                SerialPort::STOP_BITS_1,                SerialPort::FLOW_CONTROL_NONE);    }    catch (SerialPort::OpenFailed E) {        cerr << "Error abriendo el puerto" << endl;        return 1;    }    cout << "Escribe lo que quieres enviar" << endl;    getline(cin,entrada);    puerto.Write(entrada);    SerialPort::DataBuffer buf;    try {            puerto.Read(buf,entrada.size(),500);    }    catch (SerialPort::ReadTimeout E) {        cout << "TIMEOUT!";        return 1;    }    for(int i=0; i < buf.size(); i++){        cout << buf[i];    }    cout << endl;    puerto.Close();} 
¿Alguna idea de qué puede ser?

m0skit0:
¿Código de SerialPort.h y SerialPort.cpp? Y por favor usa las etiquetas de código, que no hay quien quiera leer el código así...  ^_^

Zakro:
Ya está  ^_^

SerialPort.h es una cabecera incluída en la biblioteca "libserial"; aquí está el código de la cabecera:


--- Código: C++ ---00001 /***************************************************************************00002  *   Copyright (C) 2004 by Manish Pagey                                    *00003  *   crayzeewulf@users.sourceforge.net00004  *                                                                         *00005  *   This program is free software; you can redistribute it and/or modify  *00006  *   it under the terms of the GNU General Public License as published by  *00007  *   the Free Software Foundation; either version 2 of the License, or     *00008  *   (at your option) any later version.                                   *00009  *                                                                         *00010  *   This program is distributed in the hope that it will be useful,       *00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *00013  *   GNU General Public License for more details.                          *00014  *                                                                         *00015  *   You should have received a copy of the GNU General Public License     *00016  *   along with this program; if not, write to the                         *00017  *   Free Software Foundation, Inc.,                                       *00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *00019  ***************************************************************************/00020 #ifndef _SerialPort_h_00021 #define _SerialPort_h_00022 00023 00024 #include <string>00025 #include <vector>00026 #include <stdexcept>00027 #include <termios.h>00028 00029 00043 class SerialPort00044 {00045 public:00049     enum BaudRate {00050         BAUD_50      = B50,00051         BAUD_75      = B75,00052         BAUD_110     = B110,00053         BAUD_134     = B134,00054         BAUD_150     = B150,00055         BAUD_200     = B200,00056         BAUD_300     = B300,00057         BAUD_600     = B600,00058         BAUD_1200    = B1200,00059         BAUD_1800    = B1800,00060         BAUD_2400    = B2400,00061         BAUD_4800    = B4800,00062         BAUD_9600    = B9600,00063         BAUD_19200   = B19200,00064         BAUD_38400   = B38400,00065         BAUD_57600   = B57600,00066         BAUD_115200  = B115200,00067         BAUD_230400  = B230400,00068         BAUD_460800  = B460800,00069         BAUD_DEFAULT = BAUD_5760000070     } ;00071 00072     enum CharacterSize {00073         CHAR_SIZE_5  = CS5, 00074         CHAR_SIZE_6  = CS6, 00075         CHAR_SIZE_7  = CS7, 00076         CHAR_SIZE_8  = CS8, 00077         CHAR_SIZE_DEFAULT = CHAR_SIZE_800078     } ;00079 00080     enum StopBits {00081         STOP_BITS_1,   00082         STOP_BITS_2,   00083         STOP_BITS_DEFAULT = STOP_BITS_100084     } ;00085 00086     enum Parity {00087         PARITY_EVEN,     00088         PARITY_ODD,      00089         PARITY_NONE,     00090         PARITY_DEFAULT = PARITY_NONE00091     } ;00092 00093     enum FlowControl {00094         FLOW_CONTROL_HARD,00095         // FLOW_CONTROL_SOFT,00096         FLOW_CONTROL_NONE,00097         FLOW_CONTROL_DEFAULT = FLOW_CONTROL_NONE00098     } ;00099 00100     class NotOpen : public std::logic_error00101     {00102     public:00103         NotOpen(const std::string& whatArg) :00104             logic_error(whatArg) { }00105     } ;00106 00107     class OpenFailed : public std::runtime_error00108     {00109     public:00110         OpenFailed(const std::string& whatArg) :00111             runtime_error(whatArg) { }00112     } ;00113 00114     class AlreadyOpen : public std::logic_error00115     {00116     public:00117         AlreadyOpen( const std::string& whatArg ) :00118             logic_error(whatArg) { }00119     } ;00120 00121     class UnsupportedBaudRate : public std::runtime_error00122     {00123     public:00124         UnsupportedBaudRate( const std::string& whatArg ) :00125             runtime_error(whatArg) { }00126     } ;00127 00128     class ReadTimeout : public std::runtime_error00129     {00130     public:00131         ReadTimeout() : runtime_error( "Read timeout" ) { }00132     } ;00133 00137     explicit SerialPort( const std::string& serialPortName ) ;00138 00142     ~SerialPort() throw() ;00143 00157     void00158     Open( const BaudRate      baudRate    = BAUD_DEFAULT,00159           const CharacterSize charSize    = CHAR_SIZE_DEFAULT,00160           const Parity        parityType  = PARITY_DEFAULT,00161           const StopBits      stopBits    = STOP_BITS_DEFAULT,00162           const FlowControl   flowControl = FLOW_CONTROL_DEFAULT )00163         throw( AlreadyOpen,00164                OpenFailed,00165                UnsupportedBaudRate,00166                std::invalid_argument ) ;00167 00171     bool00172     IsOpen() const ;00173 00182     void00183     Close()00184         throw(NotOpen) ;00185 00196     void00197     SetBaudRate( const BaudRate baudRate )00198         throw( UnsupportedBaudRate,00199                NotOpen,00200                std::invalid_argument ) ;00201 00208     BaudRate00209     GetBaudRate() const00210         throw( NotOpen,00211                std::runtime_error ) ;00212 00222     void00223     SetCharSize( const CharacterSize charSize )00224         throw( NotOpen,00225                std::invalid_argument ) ;00233     CharacterSize00234     GetCharSize() const00235         throw(NotOpen) ;00236 00246     void00247     SetParity( const Parity parityType )00248         throw( NotOpen,00249                std::invalid_argument ) ;00250 00258     Parity00259     GetParity() const00260         throw(NotOpen) ;00261 00271     void00272     SetNumOfStopBits( const StopBits numOfStopBits )00273         throw( NotOpen,00274                std::invalid_argument ) ;00275 00284     StopBits00285     GetNumOfStopBits() const00286         throw(NotOpen) ;00287 00297     void00298     SetFlowControl( const FlowControl   flowControl )00299         throw( NotOpen,00300                std::invalid_argument ) ;00301 00309     FlowControl00310     GetFlowControl() const00311         throw( NotOpen ) ;00312 00320     bool00321     IsDataAvailable() const00322         throw(NotOpen) ;00323 00330     unsigned char00331     ReadByte( const unsigned int msTimeout = 0 )00332         throw( NotOpen,00333                ReadTimeout,00334                std::runtime_error ) ;00335 00346     typedef std::vector<unsigned char> DataBuffer ;00347     void00348     Read( DataBuffer&        dataBuffer,00349           const unsigned int numOfBytes = 0,00350           const unsigned int msTimeout  = 0 )00351         throw( NotOpen,00352                ReadTimeout,00353                std::runtime_error ) ;00354 00355 00359     const std::string00360     ReadLine( const unsigned int msTimeout = 0,00361               const char         lineTerminator = 'n' )00362         throw( NotOpen,00363                ReadTimeout,00364                std::runtime_error ) ;00365 00372     void00373     WriteByte(const unsigned char dataByte)00374         throw( NotOpen,00375                std::runtime_error ) ;00376 00380     void00381     Write(const DataBuffer& dataBuffer)00382         throw( NotOpen,00383                std::runtime_error ) ;00384 00388     void00389     Write(const std::string& dataString)00390         throw( NotOpen,00391                std::runtime_error ) ;00392 private:00393     SerialPort( const SerialPort& otherSerialPort ) ;00394     SerialPort& operator=(const SerialPort& otherSerialPort ) ;00395     class SerialPortImpl ;00396     SerialPortImpl* mSerialPortImpl ;00397 } ;00398 00399 #endif // #ifndef _SerialPort_h_00400   

m0skit0:
Ah ok, perdona, había leído mal los errores. Te falta mandarle al compilador enlazar la librería:


--- Código: Text ---g++ -llibserial -o PruebaPuerto PruebaPuerto.cpp 
Ya me dices. Saludos.

PD: en entornos UNIX se suelen nombrar los ficheros todo en minusculas para no tener que andar escribiendo mayúsculas  :D

Zakro:
No era exactamente así pero estabas en lo cierto  ^_^

La sentencia correcta por si a alguien le pasa tambien sería:
--- Código: C++ ---g++ -lserial -o PruebaPuerto PruebaPuerto.cpp 
Lo de poner las mayúsculas en el nombre es porque sigo la notación que nos mandan en la universidad, y ya que esto es para un trabajo de allí... mejor no llevarles la contraria  ;)

Gracias de nuevo!

Navegación

[0] Índice de Mensajes

[#] Página Siguiente

Ir a la versión completa