构建代码时出错
Error when I building my code
我在构建代码时在 Raspberry 上的 C++ 代码遇到了一些问题。代码包括从传感器获取和发送来自 Arduino 的数据。
因此我认为问题不可能来自 Arduino,因为 Arduino 的代码与程序一起工作 Python 但由于某些原因我需要使用 C++ 我想知道如何解决我的问题因为是大学项目,时间有点紧
所以这是我的代码:
#include "SerialPort.h" //My library
SerialPort::SerialPort() //Constructor
{
this->numCon = open("/dev/ttyACM0", O_RDWR| O_NOCTTY ); //ttyACM0 arduino port
this->connected = false;
struct termios tty;
struct termios tty_old;
memset (&tty, 0, sizeof tty);
/* Error Handling */
if ( tcgetattr ( this->numCon, &tty ) != 0 ) {
std::cout << "Error " << errno << " from tcgetattr: " << strerror(errno) << std::endl;
return;
}
/* Save old tty parameters */
tty_old = tty;
/* Set Baud Rate */
cfsetospeed (&tty, (speed_t)B9600);
cfsetispeed (&tty, (speed_t)B9600);
/* Setting other Port Stuff */
tty.c_cflag &= ~PARENB; // Make 8n1
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_cflag &= ~CRTSCTS; // no flow control
tty.c_cc[VMIN] = 1; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
/* Make raw */
cfmakeraw(&tty);
/* Flush Port, then applies attributes */
tcflush(this->numCon, TCIFLUSH );
if ( tcsetattr (this->numCon, TCSANOW, &tty ) != 0) {
std::cout << "Error " << errno << " from tcsetattr" << std::endl;
return;
}
this->connected = true;
}
SerialPort::~SerialPort() //Destructor
{
if (this->connected) {
this->connected = false;
}
}
char* SerialPort::readSerialPort()
{
int n = 0, spot = 0;
char buf = '[=10=]';
/* Whole response*/
char response[1024];
memset(response, '[=10=]', sizeof response);
do {
n = read(this->numCon, &buf, 1 );
sprintf( &response[spot], "%c", buf );
spot += n;
} while( buf != '\r' && n > 0);
if (n < 0) {
std::cout << "Error reading: " << strerror(errno) << std::endl;
}
else if (n == 0) {
std::cout << "Read nothing!" << std::endl;
}
else {
std::cout << "Response: " << response << std::endl;
}
return response;
}
void SerialPort::writeSerialPort(unsigned char cmd[])
{
int n_written = 0, spot = 0;
do {
n_written = write(this->numCon, &cmd[spot], 1 );
spot += n_written;
} while (cmd[spot-1] != '\r' && n_written > 0);
}
bool SerialPort::isConnected()
{
return this->connected;
}
这是图书馆 (SerialPort.h):
#ifndef SERIALPORT_H
#define SERIALPORT_H
#include <stdio.h> // standard input / output functions
#include <stdlib.h>
#include <string.h> // string function definitions
#include <unistd.h> // UNIX standard function definitions
#include <fcntl.h> // File control definitions
#include <errno.h> // Error number definitions
#include <termios.h> // POSIX terminal control definitions
#include <iostream>
class SerialPort
{
private:
int numCon;
bool connected;
public:
SerialPort();
~SerialPort();
char* readSerialPort();
void writeSerialPort(unsigned char cmd[]);
bool isConnected();
};
#endif // SERIALPORT_H
错误:
pi@Lux:~/PROJET $ g++ SerialPort.cpp -o SerialPort.out
SerialPort.cpp: In member function ‘char* SerialPort::readSerialPort()’:
SerialPort.cpp:63:7: warning: address of local variable ‘response’ returned
[-Wreturn-local-addr]
char response[1024];
^~~~~~~~
/usr/lib/gcc/arm-linux-gnueabihf/6/../../../arm-linux-gnueabihf/crt1.o : Dans
la fonction « _start » :
(.text+0x34) : référence indéfinie vers « main »
collect2: error: ld returned 1 exit status
pi@Lux:~/PROJET $ ./SerialPort.cpp
./SerialPort.cpp: ligne 2: $'\r' : commande introuvable
./SerialPort.cpp: ligne 3: erreur de syntaxe près du symbole inattendu « $'\r' »
'/SerialPort.cpp: ligne 3: `SerialPort::SerialPort()
让我们来看第一个错误,它并不是真正的错误,而是 "just" 警告您做错了什么:
SerialPort.cpp: In member function ‘char* SerialPort::readSerialPort()’:
SerialPort.cpp:63:7: warning: address of local variable ‘response’ returned
在SerialPort::readSerialPort
函数中变量response
是局部变量。它的生命周期和范围是函数调用的生命周期和范围。当函数结束时,变量的生命周期也会结束。在某种程度上,它将不复存在。返回指向此变量的指针将立即使指针无效。
如果你想把读到的数据当成字符串,那就用字符串data-type。就像 Arduino 中的 std::string
in standard C++, and String
。
第二个错误是因为您的代码没有 main
函数。全局 main
函数是所有标准 C++ 程序的 entry-point。
我在构建代码时在 Raspberry 上的 C++ 代码遇到了一些问题。代码包括从传感器获取和发送来自 Arduino 的数据。
因此我认为问题不可能来自 Arduino,因为 Arduino 的代码与程序一起工作 Python 但由于某些原因我需要使用 C++ 我想知道如何解决我的问题因为是大学项目,时间有点紧
所以这是我的代码:
#include "SerialPort.h" //My library
SerialPort::SerialPort() //Constructor
{
this->numCon = open("/dev/ttyACM0", O_RDWR| O_NOCTTY ); //ttyACM0 arduino port
this->connected = false;
struct termios tty;
struct termios tty_old;
memset (&tty, 0, sizeof tty);
/* Error Handling */
if ( tcgetattr ( this->numCon, &tty ) != 0 ) {
std::cout << "Error " << errno << " from tcgetattr: " << strerror(errno) << std::endl;
return;
}
/* Save old tty parameters */
tty_old = tty;
/* Set Baud Rate */
cfsetospeed (&tty, (speed_t)B9600);
cfsetispeed (&tty, (speed_t)B9600);
/* Setting other Port Stuff */
tty.c_cflag &= ~PARENB; // Make 8n1
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_cflag &= ~CRTSCTS; // no flow control
tty.c_cc[VMIN] = 1; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
/* Make raw */
cfmakeraw(&tty);
/* Flush Port, then applies attributes */
tcflush(this->numCon, TCIFLUSH );
if ( tcsetattr (this->numCon, TCSANOW, &tty ) != 0) {
std::cout << "Error " << errno << " from tcsetattr" << std::endl;
return;
}
this->connected = true;
}
SerialPort::~SerialPort() //Destructor
{
if (this->connected) {
this->connected = false;
}
}
char* SerialPort::readSerialPort()
{
int n = 0, spot = 0;
char buf = '[=10=]';
/* Whole response*/
char response[1024];
memset(response, '[=10=]', sizeof response);
do {
n = read(this->numCon, &buf, 1 );
sprintf( &response[spot], "%c", buf );
spot += n;
} while( buf != '\r' && n > 0);
if (n < 0) {
std::cout << "Error reading: " << strerror(errno) << std::endl;
}
else if (n == 0) {
std::cout << "Read nothing!" << std::endl;
}
else {
std::cout << "Response: " << response << std::endl;
}
return response;
}
void SerialPort::writeSerialPort(unsigned char cmd[])
{
int n_written = 0, spot = 0;
do {
n_written = write(this->numCon, &cmd[spot], 1 );
spot += n_written;
} while (cmd[spot-1] != '\r' && n_written > 0);
}
bool SerialPort::isConnected()
{
return this->connected;
}
这是图书馆 (SerialPort.h):
#ifndef SERIALPORT_H
#define SERIALPORT_H
#include <stdio.h> // standard input / output functions
#include <stdlib.h>
#include <string.h> // string function definitions
#include <unistd.h> // UNIX standard function definitions
#include <fcntl.h> // File control definitions
#include <errno.h> // Error number definitions
#include <termios.h> // POSIX terminal control definitions
#include <iostream>
class SerialPort
{
private:
int numCon;
bool connected;
public:
SerialPort();
~SerialPort();
char* readSerialPort();
void writeSerialPort(unsigned char cmd[]);
bool isConnected();
};
#endif // SERIALPORT_H
错误:
pi@Lux:~/PROJET $ g++ SerialPort.cpp -o SerialPort.out
SerialPort.cpp: In member function ‘char* SerialPort::readSerialPort()’:
SerialPort.cpp:63:7: warning: address of local variable ‘response’ returned
[-Wreturn-local-addr]
char response[1024];
^~~~~~~~
/usr/lib/gcc/arm-linux-gnueabihf/6/../../../arm-linux-gnueabihf/crt1.o : Dans
la fonction « _start » :
(.text+0x34) : référence indéfinie vers « main »
collect2: error: ld returned 1 exit status
pi@Lux:~/PROJET $ ./SerialPort.cpp
./SerialPort.cpp: ligne 2: $'\r' : commande introuvable
./SerialPort.cpp: ligne 3: erreur de syntaxe près du symbole inattendu « $'\r' »
'/SerialPort.cpp: ligne 3: `SerialPort::SerialPort()
让我们来看第一个错误,它并不是真正的错误,而是 "just" 警告您做错了什么:
SerialPort.cpp: In member function ‘char* SerialPort::readSerialPort()’:
SerialPort.cpp:63:7: warning: address of local variable ‘response’ returned
在SerialPort::readSerialPort
函数中变量response
是局部变量。它的生命周期和范围是函数调用的生命周期和范围。当函数结束时,变量的生命周期也会结束。在某种程度上,它将不复存在。返回指向此变量的指针将立即使指针无效。
如果你想把读到的数据当成字符串,那就用字符串data-type。就像 Arduino 中的 std::string
in standard C++, and String
。
第二个错误是因为您的代码没有 main
函数。全局 main
函数是所有标准 C++ 程序的 entry-point。